1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/process/process_metrics.h"
11 #include <sys/types.h>
14 #include "base/files/file_util.h"
15 #include "base/logging.h"
16 #include "base/process/internal_linux.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h"
19 #include "base/strings/string_tokenizer.h"
20 #include "base/strings/string_util.h"
21 #include "base/sys_info.h"
22 #include "base/threading/thread_restrictions.h"
28 void TrimKeyValuePairs(StringPairs
* pairs
) {
30 StringPairs
& p_ref
= *pairs
;
31 for (size_t i
= 0; i
< p_ref
.size(); ++i
) {
32 TrimWhitespaceASCII(p_ref
[i
].first
, TRIM_ALL
, &p_ref
[i
].first
);
33 TrimWhitespaceASCII(p_ref
[i
].second
, TRIM_ALL
, &p_ref
[i
].second
);
37 #if defined(OS_CHROMEOS)
38 // Read a file with a single number string and return the number as a uint64.
39 static uint64
ReadFileToUint64(const FilePath file
) {
40 std::string file_as_string
;
41 if (!ReadFileToString(file
, &file_as_string
))
43 TrimWhitespaceASCII(file_as_string
, TRIM_ALL
, &file_as_string
);
44 uint64 file_as_uint64
= 0;
45 if (!StringToUint64(file_as_string
, &file_as_uint64
))
47 return file_as_uint64
;
51 // Read /proc/<pid>/status and return the value for |field|, or 0 on failure.
52 // Only works for fields in the form of "Field: value kB".
53 size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid
, const std::string
& field
) {
56 // Synchronously reading files in /proc does not hit the disk.
57 ThreadRestrictions::ScopedAllowIO allow_io
;
58 FilePath stat_file
= internal::GetProcPidDir(pid
).Append("status");
59 if (!ReadFileToString(stat_file
, &status
))
64 SplitStringIntoKeyValuePairs(status
, ':', '\n', &pairs
);
65 TrimKeyValuePairs(&pairs
);
66 for (size_t i
= 0; i
< pairs
.size(); ++i
) {
67 const std::string
& key
= pairs
[i
].first
;
68 const std::string
& value_str
= pairs
[i
].second
;
70 std::vector
<std::string
> split_value_str
;
71 SplitString(value_str
, ' ', &split_value_str
);
72 if (split_value_str
.size() != 2 || split_value_str
[1] != "kB") {
77 if (!StringToSizeT(split_value_str
[0], &value
)) {
89 // Read /proc/<pid>/sched and look for |field|. On succes, return true and
90 // write the value for |field| into |result|.
91 // Only works for fields in the form of "field : uint_value"
92 bool ReadProcSchedAndGetFieldAsUint64(pid_t pid
,
93 const std::string
& field
,
95 std::string sched_data
;
97 // Synchronously reading files in /proc does not hit the disk.
98 ThreadRestrictions::ScopedAllowIO allow_io
;
99 FilePath sched_file
= internal::GetProcPidDir(pid
).Append("sched");
100 if (!ReadFileToString(sched_file
, &sched_data
))
105 SplitStringIntoKeyValuePairs(sched_data
, ':', '\n', &pairs
);
106 TrimKeyValuePairs(&pairs
);
107 for (size_t i
= 0; i
< pairs
.size(); ++i
) {
108 const std::string
& key
= pairs
[i
].first
;
109 const std::string
& value_str
= pairs
[i
].second
;
112 if (!StringToUint64(value_str
, &value
))
120 #endif // defined(OS_LINUX)
122 // Get the total CPU of a single process. Return value is number of jiffies
123 // on success or -1 on error.
124 int GetProcessCPU(pid_t pid
) {
125 // Use /proc/<pid>/task to find all threads and parse their /stat file.
126 FilePath task_path
= internal::GetProcPidDir(pid
).Append("task");
128 DIR* dir
= opendir(task_path
.value().c_str());
130 DPLOG(ERROR
) << "opendir(" << task_path
.value() << ")";
135 while (struct dirent
* ent
= readdir(dir
)) {
136 pid_t tid
= internal::ProcDirSlotToPid(ent
->d_name
);
140 // Synchronously reading files in /proc does not hit the disk.
141 ThreadRestrictions::ScopedAllowIO allow_io
;
145 task_path
.Append(ent
->d_name
).Append(internal::kStatFile
);
146 if (ReadFileToString(stat_path
, &stat
)) {
147 int cpu
= ParseProcStatCPU(stat
);
160 ProcessMetrics
* ProcessMetrics::CreateProcessMetrics(ProcessHandle process
) {
161 return new ProcessMetrics(process
);
164 // On linux, we return vsize.
165 size_t ProcessMetrics::GetPagefileUsage() const {
166 return internal::ReadProcStatsAndGetFieldAsSizeT(process_
,
170 // On linux, we return the high water mark of vsize.
171 size_t ProcessMetrics::GetPeakPagefileUsage() const {
172 return ReadProcStatusAndGetFieldAsSizeT(process_
, "VmPeak") * 1024;
175 // On linux, we return RSS.
176 size_t ProcessMetrics::GetWorkingSetSize() const {
177 return internal::ReadProcStatsAndGetFieldAsSizeT(process_
, internal::VM_RSS
) *
181 // On linux, we return the high water mark of RSS.
182 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
183 return ReadProcStatusAndGetFieldAsSizeT(process_
, "VmHWM") * 1024;
186 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes
,
187 size_t* shared_bytes
) {
188 WorkingSetKBytes ws_usage
;
189 if (!GetWorkingSetKBytes(&ws_usage
))
193 *private_bytes
= ws_usage
.priv
* 1024;
196 *shared_bytes
= ws_usage
.shared
* 1024;
201 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes
* ws_usage
) const {
202 #if defined(OS_CHROMEOS)
203 if (GetWorkingSetKBytesTotmaps(ws_usage
))
206 return GetWorkingSetKBytesStatm(ws_usage
);
209 double ProcessMetrics::GetCPUUsage() {
210 TimeTicks time
= TimeTicks::Now();
212 if (last_cpu_
== 0) {
213 // First call, just set the last values.
214 last_cpu_time_
= time
;
215 last_cpu_
= GetProcessCPU(process_
);
219 int64 time_delta
= (time
- last_cpu_time_
).InMicroseconds();
220 DCHECK_NE(time_delta
, 0);
224 int cpu
= GetProcessCPU(process_
);
226 // We have the number of jiffies in the time period. Convert to percentage.
227 // Note this means we will go *over* 100 in the case where multiple threads
228 // are together adding to more than one CPU's worth.
229 TimeDelta cpu_time
= internal::ClockTicksToTimeDelta(cpu
);
230 TimeDelta last_cpu_time
= internal::ClockTicksToTimeDelta(last_cpu_
);
231 double percentage
= 100.0 * (cpu_time
- last_cpu_time
).InSecondsF() /
232 TimeDelta::FromMicroseconds(time_delta
).InSecondsF();
234 last_cpu_time_
= time
;
240 // To have /proc/self/io file you must enable CONFIG_TASK_IO_ACCOUNTING
241 // in your kernel configuration.
242 bool ProcessMetrics::GetIOCounters(IoCounters
* io_counters
) const {
243 // Synchronously reading files in /proc does not hit the disk.
244 ThreadRestrictions::ScopedAllowIO allow_io
;
246 std::string proc_io_contents
;
247 FilePath io_file
= internal::GetProcPidDir(process_
).Append("io");
248 if (!ReadFileToString(io_file
, &proc_io_contents
))
251 io_counters
->OtherOperationCount
= 0;
252 io_counters
->OtherTransferCount
= 0;
255 SplitStringIntoKeyValuePairs(proc_io_contents
, ':', '\n', &pairs
);
256 TrimKeyValuePairs(&pairs
);
257 for (size_t i
= 0; i
< pairs
.size(); ++i
) {
258 const std::string
& key
= pairs
[i
].first
;
259 const std::string
& value_str
= pairs
[i
].second
;
260 uint64
* target_counter
= NULL
;
262 target_counter
= &io_counters
->ReadOperationCount
;
263 else if (key
== "syscw")
264 target_counter
= &io_counters
->WriteOperationCount
;
265 else if (key
== "rchar")
266 target_counter
= &io_counters
->ReadTransferCount
;
267 else if (key
== "wchar")
268 target_counter
= &io_counters
->WriteTransferCount
;
271 bool converted
= StringToUint64(value_str
, target_counter
);
277 ProcessMetrics::ProcessMetrics(ProcessHandle process
)
279 last_system_time_(0),
280 #if defined(OS_LINUX)
281 last_absolute_idle_wakeups_(0),
284 processor_count_
= SysInfo::NumberOfProcessors();
287 #if defined(OS_CHROMEOS)
288 // Private, Shared and Proportional working set sizes are obtained from
289 // /proc/<pid>/totmaps
290 bool ProcessMetrics::GetWorkingSetKBytesTotmaps(WorkingSetKBytes
*ws_usage
)
292 // The format of /proc/<pid>/totmaps is:
296 // Shared_Clean: 1008 kB
297 // Shared_Dirty: 4012 kB
298 // Private_Clean: 4 kB
299 // Private_Dirty: 1096 kB
300 // Referenced: XXX kB
302 // AnonHugePages: XXX kB
305 const size_t kPssIndex
= (1 * 3) + 1;
306 const size_t kPrivate_CleanIndex
= (4 * 3) + 1;
307 const size_t kPrivate_DirtyIndex
= (5 * 3) + 1;
308 const size_t kSwapIndex
= (9 * 3) + 1;
310 std::string totmaps_data
;
312 FilePath totmaps_file
= internal::GetProcPidDir(process_
).Append("totmaps");
313 ThreadRestrictions::ScopedAllowIO allow_io
;
314 bool ret
= ReadFileToString(totmaps_file
, &totmaps_data
);
315 if (!ret
|| totmaps_data
.length() == 0)
319 std::vector
<std::string
> totmaps_fields
;
320 SplitStringAlongWhitespace(totmaps_data
, &totmaps_fields
);
322 DCHECK_EQ("Pss:", totmaps_fields
[kPssIndex
-1]);
323 DCHECK_EQ("Private_Clean:", totmaps_fields
[kPrivate_CleanIndex
- 1]);
324 DCHECK_EQ("Private_Dirty:", totmaps_fields
[kPrivate_DirtyIndex
- 1]);
325 DCHECK_EQ("Swap:", totmaps_fields
[kSwapIndex
-1]);
328 int private_clean
= 0;
329 int private_dirty
= 0;
332 ret
&= StringToInt(totmaps_fields
[kPssIndex
], &pss
);
333 ret
&= StringToInt(totmaps_fields
[kPrivate_CleanIndex
], &private_clean
);
334 ret
&= StringToInt(totmaps_fields
[kPrivate_DirtyIndex
], &private_dirty
);
335 ret
&= StringToInt(totmaps_fields
[kSwapIndex
], &swap
);
337 // On ChromeOS swap is to zram. We count this as private / shared, as
338 // increased swap decreases available RAM to user processes, which would
339 // otherwise create surprising results.
340 ws_usage
->priv
= private_clean
+ private_dirty
+ swap
;
341 ws_usage
->shared
= pss
+ swap
;
342 ws_usage
->shareable
= 0;
343 ws_usage
->swapped
= swap
;
348 // Private and Shared working set sizes are obtained from /proc/<pid>/statm.
349 bool ProcessMetrics::GetWorkingSetKBytesStatm(WorkingSetKBytes
* ws_usage
)
351 // Use statm instead of smaps because smaps is:
352 // a) Large and slow to parse.
353 // b) Unavailable in the SUID sandbox.
355 // First we need to get the page size, since everything is measured in pages.
356 // For details, see: man 5 proc.
357 const int page_size_kb
= getpagesize() / 1024;
358 if (page_size_kb
<= 0)
363 FilePath statm_file
= internal::GetProcPidDir(process_
).Append("statm");
364 // Synchronously reading files in /proc does not hit the disk.
365 ThreadRestrictions::ScopedAllowIO allow_io
;
366 bool ret
= ReadFileToString(statm_file
, &statm
);
367 if (!ret
|| statm
.length() == 0)
371 std::vector
<std::string
> statm_vec
;
372 SplitString(statm
, ' ', &statm_vec
);
373 if (statm_vec
.size() != 7)
374 return false; // Not the format we expect.
376 int statm_rss
, statm_shared
;
378 ret
&= StringToInt(statm_vec
[1], &statm_rss
);
379 ret
&= StringToInt(statm_vec
[2], &statm_shared
);
381 ws_usage
->priv
= (statm_rss
- statm_shared
) * page_size_kb
;
382 ws_usage
->shared
= statm_shared
* page_size_kb
;
384 // Sharable is not calculated, as it does not provide interesting data.
385 ws_usage
->shareable
= 0;
387 #if defined(OS_CHROMEOS)
388 // Can't get swapped memory from statm.
389 ws_usage
->swapped
= 0;
395 size_t GetSystemCommitCharge() {
396 SystemMemoryInfoKB meminfo
;
397 if (!GetSystemMemoryInfo(&meminfo
))
399 return meminfo
.total
- meminfo
.free
- meminfo
.buffers
- meminfo
.cached
;
402 int ParseProcStatCPU(const std::string
& input
) {
403 // |input| may be empty if the process disappeared somehow.
404 // e.g. http://crbug.com/145811.
408 size_t start
= input
.find_last_of(')');
409 if (start
== input
.npos
)
412 // Number of spaces remaining until reaching utime's index starting after the
414 int num_spaces_remaining
= internal::VM_UTIME
- 1;
417 while ((i
= input
.find(' ', i
+ 1)) != input
.npos
) {
418 // Validate the assumption that there aren't any contiguous spaces
419 // in |input| before utime.
420 DCHECK_NE(input
[i
- 1], ' ');
421 if (--num_spaces_remaining
== 0) {
424 if (sscanf(&input
.data()[i
], "%d %d", &utime
, &stime
) != 2)
427 return utime
+ stime
;
434 const char kProcSelfExe
[] = "/proc/self/exe";
436 int GetNumberOfThreads(ProcessHandle process
) {
437 return internal::ReadProcStatsAndGetFieldAsInt64(process
,
438 internal::VM_NUMTHREADS
);
443 // The format of /proc/diskstats is:
444 // Device major number
445 // Device minor number
447 // Field 1 -- # of reads completed
448 // This is the total number of reads completed successfully.
449 // Field 2 -- # of reads merged, field 6 -- # of writes merged
450 // Reads and writes which are adjacent to each other may be merged for
451 // efficiency. Thus two 4K reads may become one 8K read before it is
452 // ultimately handed to the disk, and so it will be counted (and queued)
453 // as only one I/O. This field lets you know how often this was done.
454 // Field 3 -- # of sectors read
455 // This is the total number of sectors read successfully.
456 // Field 4 -- # of milliseconds spent reading
457 // This is the total number of milliseconds spent by all reads (as
458 // measured from __make_request() to end_that_request_last()).
459 // Field 5 -- # of writes completed
460 // This is the total number of writes completed successfully.
461 // Field 6 -- # of writes merged
462 // See the description of field 2.
463 // Field 7 -- # of sectors written
464 // This is the total number of sectors written successfully.
465 // Field 8 -- # of milliseconds spent writing
466 // This is the total number of milliseconds spent by all writes (as
467 // measured from __make_request() to end_that_request_last()).
468 // Field 9 -- # of I/Os currently in progress
469 // The only field that should go to zero. Incremented as requests are
470 // given to appropriate struct request_queue and decremented as they
472 // Field 10 -- # of milliseconds spent doing I/Os
473 // This field increases so long as field 9 is nonzero.
474 // Field 11 -- weighted # of milliseconds spent doing I/Os
475 // This field is incremented at each I/O start, I/O completion, I/O
476 // merge, or read of these stats by the number of I/Os in progress
477 // (field 9) times the number of milliseconds spent doing I/O since the
478 // last update of this field. This can provide an easy measure of both
479 // I/O completion time and the backlog that may be accumulating.
481 const size_t kDiskDriveName
= 2;
482 const size_t kDiskReads
= 3;
483 const size_t kDiskReadsMerged
= 4;
484 const size_t kDiskSectorsRead
= 5;
485 const size_t kDiskReadTime
= 6;
486 const size_t kDiskWrites
= 7;
487 const size_t kDiskWritesMerged
= 8;
488 const size_t kDiskSectorsWritten
= 9;
489 const size_t kDiskWriteTime
= 10;
490 const size_t kDiskIO
= 11;
491 const size_t kDiskIOTime
= 12;
492 const size_t kDiskWeightedIOTime
= 13;
496 SystemMemoryInfoKB::SystemMemoryInfoKB() {
521 scoped_ptr
<Value
> SystemMemoryInfoKB::ToValue() const {
522 scoped_ptr
<DictionaryValue
> res(new DictionaryValue());
524 res
->SetInteger("total", total
);
525 res
->SetInteger("free", free
);
526 res
->SetInteger("buffers", buffers
);
527 res
->SetInteger("cached", cached
);
528 res
->SetInteger("active_anon", active_anon
);
529 res
->SetInteger("inactive_anon", inactive_anon
);
530 res
->SetInteger("active_file", active_file
);
531 res
->SetInteger("inactive_file", inactive_file
);
532 res
->SetInteger("swap_total", swap_total
);
533 res
->SetInteger("swap_free", swap_free
);
534 res
->SetInteger("swap_used", swap_total
- swap_free
);
535 res
->SetInteger("dirty", dirty
);
536 res
->SetInteger("pswpin", pswpin
);
537 res
->SetInteger("pswpout", pswpout
);
538 res
->SetInteger("pgmajfault", pgmajfault
);
540 res
->SetInteger("shmem", shmem
);
541 res
->SetInteger("slab", slab
);
542 res
->SetInteger("gem_objects", gem_objects
);
543 res
->SetInteger("gem_size", gem_size
);
549 // exposed for testing
550 bool ParseProcMeminfo(const std::string
& meminfo_data
,
551 SystemMemoryInfoKB
* meminfo
) {
552 // The format of /proc/meminfo is:
554 // MemTotal: 8235324 kB
555 // MemFree: 1628304 kB
556 // Buffers: 429596 kB
557 // Cached: 4728232 kB
559 // There is no guarantee on the ordering or position
560 // though it doesn't appear to change very often
562 // As a basic sanity check, let's make sure we at least get non-zero
566 std::vector
<std::string
> meminfo_lines
;
567 Tokenize(meminfo_data
, "\n", &meminfo_lines
);
568 for (std::vector
<std::string
>::iterator it
= meminfo_lines
.begin();
569 it
!= meminfo_lines
.end(); ++it
) {
570 std::vector
<std::string
> tokens
;
571 SplitStringAlongWhitespace(*it
, &tokens
);
572 // HugePages_* only has a number and no suffix so we can't rely on
573 // there being exactly 3 tokens.
574 if (tokens
.size() <= 1) {
575 DLOG(WARNING
) << "meminfo: tokens: " << tokens
.size()
576 << " malformed line: " << *it
;
581 if (tokens
[0] == "MemTotal:")
582 target
= &meminfo
->total
;
583 else if (tokens
[0] == "MemFree:")
584 target
= &meminfo
->free
;
585 else if (tokens
[0] == "Buffers:")
586 target
= &meminfo
->buffers
;
587 else if (tokens
[0] == "Cached:")
588 target
= &meminfo
->cached
;
589 else if (tokens
[0] == "Active(anon):")
590 target
= &meminfo
->active_anon
;
591 else if (tokens
[0] == "Inactive(anon):")
592 target
= &meminfo
->inactive_anon
;
593 else if (tokens
[0] == "Active(file):")
594 target
= &meminfo
->active_file
;
595 else if (tokens
[0] == "Inactive(file):")
596 target
= &meminfo
->inactive_file
;
597 else if (tokens
[0] == "SwapTotal:")
598 target
= &meminfo
->swap_total
;
599 else if (tokens
[0] == "SwapFree:")
600 target
= &meminfo
->swap_free
;
601 else if (tokens
[0] == "Dirty:")
602 target
= &meminfo
->dirty
;
603 #if defined(OS_CHROMEOS)
604 // Chrome OS has a tweaked kernel that allows us to query Shmem, which is
605 // usually video memory otherwise invisible to the OS.
606 else if (tokens
[0] == "Shmem:")
607 target
= &meminfo
->shmem
;
608 else if (tokens
[0] == "Slab:")
609 target
= &meminfo
->slab
;
612 StringToInt(tokens
[1], target
);
615 // Make sure we got a valid MemTotal.
616 return meminfo
->total
> 0;
619 // exposed for testing
620 bool ParseProcVmstat(const std::string
& vmstat_data
,
621 SystemMemoryInfoKB
* meminfo
) {
622 // The format of /proc/vmstat is:
624 // nr_free_pages 299878
625 // nr_inactive_anon 239863
626 // nr_active_anon 1318966
627 // nr_inactive_file 2015629
630 // We iterate through the whole file because the position of the
631 // fields are dependent on the kernel version and configuration.
633 std::vector
<std::string
> vmstat_lines
;
634 Tokenize(vmstat_data
, "\n", &vmstat_lines
);
635 for (std::vector
<std::string
>::iterator it
= vmstat_lines
.begin();
636 it
!= vmstat_lines
.end(); ++it
) {
637 std::vector
<std::string
> tokens
;
638 SplitString(*it
, ' ', &tokens
);
639 if (tokens
.size() != 2)
642 if (tokens
[0] == "pswpin") {
643 StringToInt(tokens
[1], &meminfo
->pswpin
);
644 } else if (tokens
[0] == "pswpout") {
645 StringToInt(tokens
[1], &meminfo
->pswpout
);
646 } else if (tokens
[0] == "pgmajfault") {
647 StringToInt(tokens
[1], &meminfo
->pgmajfault
);
654 bool GetSystemMemoryInfo(SystemMemoryInfoKB
* meminfo
) {
655 // Synchronously reading files in /proc and /sys are safe.
656 ThreadRestrictions::ScopedAllowIO allow_io
;
658 // Used memory is: total - free - buffers - caches
659 FilePath
meminfo_file("/proc/meminfo");
660 std::string meminfo_data
;
661 if (!ReadFileToString(meminfo_file
, &meminfo_data
)) {
662 DLOG(WARNING
) << "Failed to open " << meminfo_file
.value();
666 if (!ParseProcMeminfo(meminfo_data
, meminfo
)) {
667 DLOG(WARNING
) << "Failed to parse " << meminfo_file
.value();
671 #if defined(OS_CHROMEOS)
672 // Report on Chrome OS GEM object graphics memory. /run/debugfs_gpu is a
673 // bind mount into /sys/kernel/debug and synchronously reading the in-memory
674 // files in /sys is fast.
675 #if defined(ARCH_CPU_ARM_FAMILY)
676 FilePath
geminfo_file("/run/debugfs_gpu/exynos_gem_objects");
678 FilePath
geminfo_file("/run/debugfs_gpu/i915_gem_objects");
680 std::string geminfo_data
;
681 meminfo
->gem_objects
= -1;
682 meminfo
->gem_size
= -1;
683 if (ReadFileToString(geminfo_file
, &geminfo_data
)) {
684 int gem_objects
= -1;
685 long long gem_size
= -1;
686 int num_res
= sscanf(geminfo_data
.c_str(),
687 "%d objects, %lld bytes",
688 &gem_objects
, &gem_size
);
690 meminfo
->gem_objects
= gem_objects
;
691 meminfo
->gem_size
= gem_size
;
695 #if defined(ARCH_CPU_ARM_FAMILY)
696 // Incorporate Mali graphics memory if present.
697 FilePath
mali_memory_file("/sys/class/misc/mali0/device/memory");
698 std::string mali_memory_data
;
699 if (ReadFileToString(mali_memory_file
, &mali_memory_data
)) {
700 long long mali_size
= -1;
701 int num_res
= sscanf(mali_memory_data
.c_str(), "%lld bytes", &mali_size
);
703 meminfo
->gem_size
+= mali_size
;
705 #endif // defined(ARCH_CPU_ARM_FAMILY)
706 #endif // defined(OS_CHROMEOS)
708 FilePath
vmstat_file("/proc/vmstat");
709 std::string vmstat_data
;
710 if (!ReadFileToString(vmstat_file
, &vmstat_data
)) {
711 DLOG(WARNING
) << "Failed to open " << vmstat_file
.value();
714 if (!ParseProcVmstat(vmstat_data
, meminfo
)) {
715 DLOG(WARNING
) << "Failed to parse " << vmstat_file
.value();
722 SystemDiskInfo::SystemDiskInfo() {
733 weighted_io_time
= 0;
736 scoped_ptr
<Value
> SystemDiskInfo::ToValue() const {
737 scoped_ptr
<DictionaryValue
> res(new DictionaryValue());
739 // Write out uint64 variables as doubles.
740 // Note: this may discard some precision, but for JS there's no other option.
741 res
->SetDouble("reads", static_cast<double>(reads
));
742 res
->SetDouble("reads_merged", static_cast<double>(reads_merged
));
743 res
->SetDouble("sectors_read", static_cast<double>(sectors_read
));
744 res
->SetDouble("read_time", static_cast<double>(read_time
));
745 res
->SetDouble("writes", static_cast<double>(writes
));
746 res
->SetDouble("writes_merged", static_cast<double>(writes_merged
));
747 res
->SetDouble("sectors_written", static_cast<double>(sectors_written
));
748 res
->SetDouble("write_time", static_cast<double>(write_time
));
749 res
->SetDouble("io", static_cast<double>(io
));
750 res
->SetDouble("io_time", static_cast<double>(io_time
));
751 res
->SetDouble("weighted_io_time", static_cast<double>(weighted_io_time
));
756 bool IsValidDiskName(const std::string
& candidate
) {
757 if (candidate
.length() < 3)
759 if (candidate
[1] == 'd' &&
760 (candidate
[0] == 'h' || candidate
[0] == 's' || candidate
[0] == 'v')) {
762 for (size_t i
= 2; i
< candidate
.length(); ++i
) {
763 if (!islower(candidate
[i
]))
769 const char kMMCName
[] = "mmcblk";
770 const size_t kMMCNameLen
= strlen(kMMCName
);
771 if (candidate
.length() < kMMCNameLen
+ 1)
773 if (candidate
.compare(0, kMMCNameLen
, kMMCName
) != 0)
777 for (size_t i
= kMMCNameLen
; i
< candidate
.length(); ++i
) {
778 if (!isdigit(candidate
[i
]))
784 bool GetSystemDiskInfo(SystemDiskInfo
* diskinfo
) {
785 // Synchronously reading files in /proc does not hit the disk.
786 ThreadRestrictions::ScopedAllowIO allow_io
;
788 FilePath
diskinfo_file("/proc/diskstats");
789 std::string diskinfo_data
;
790 if (!ReadFileToString(diskinfo_file
, &diskinfo_data
)) {
791 DLOG(WARNING
) << "Failed to open " << diskinfo_file
.value();
795 std::vector
<std::string
> diskinfo_lines
;
796 size_t line_count
= Tokenize(diskinfo_data
, "\n", &diskinfo_lines
);
797 if (line_count
== 0) {
798 DLOG(WARNING
) << "No lines found";
803 diskinfo
->reads_merged
= 0;
804 diskinfo
->sectors_read
= 0;
805 diskinfo
->read_time
= 0;
806 diskinfo
->writes
= 0;
807 diskinfo
->writes_merged
= 0;
808 diskinfo
->sectors_written
= 0;
809 diskinfo
->write_time
= 0;
811 diskinfo
->io_time
= 0;
812 diskinfo
->weighted_io_time
= 0;
815 uint64 reads_merged
= 0;
816 uint64 sectors_read
= 0;
817 uint64 read_time
= 0;
819 uint64 writes_merged
= 0;
820 uint64 sectors_written
= 0;
821 uint64 write_time
= 0;
824 uint64 weighted_io_time
= 0;
826 for (size_t i
= 0; i
< line_count
; i
++) {
827 std::vector
<std::string
> disk_fields
;
828 SplitStringAlongWhitespace(diskinfo_lines
[i
], &disk_fields
);
830 // Fields may have overflowed and reset to zero.
831 if (IsValidDiskName(disk_fields
[kDiskDriveName
])) {
832 StringToUint64(disk_fields
[kDiskReads
], &reads
);
833 StringToUint64(disk_fields
[kDiskReadsMerged
], &reads_merged
);
834 StringToUint64(disk_fields
[kDiskSectorsRead
], §ors_read
);
835 StringToUint64(disk_fields
[kDiskReadTime
], &read_time
);
836 StringToUint64(disk_fields
[kDiskWrites
], &writes
);
837 StringToUint64(disk_fields
[kDiskWritesMerged
], &writes_merged
);
838 StringToUint64(disk_fields
[kDiskSectorsWritten
], §ors_written
);
839 StringToUint64(disk_fields
[kDiskWriteTime
], &write_time
);
840 StringToUint64(disk_fields
[kDiskIO
], &io
);
841 StringToUint64(disk_fields
[kDiskIOTime
], &io_time
);
842 StringToUint64(disk_fields
[kDiskWeightedIOTime
], &weighted_io_time
);
844 diskinfo
->reads
+= reads
;
845 diskinfo
->reads_merged
+= reads_merged
;
846 diskinfo
->sectors_read
+= sectors_read
;
847 diskinfo
->read_time
+= read_time
;
848 diskinfo
->writes
+= writes
;
849 diskinfo
->writes_merged
+= writes_merged
;
850 diskinfo
->sectors_written
+= sectors_written
;
851 diskinfo
->write_time
+= write_time
;
853 diskinfo
->io_time
+= io_time
;
854 diskinfo
->weighted_io_time
+= weighted_io_time
;
861 #if defined(OS_CHROMEOS)
862 scoped_ptr
<Value
> SwapInfo::ToValue() const {
863 scoped_ptr
<DictionaryValue
> res(new DictionaryValue());
865 // Write out uint64 variables as doubles.
866 // Note: this may discard some precision, but for JS there's no other option.
867 res
->SetDouble("num_reads", static_cast<double>(num_reads
));
868 res
->SetDouble("num_writes", static_cast<double>(num_writes
));
869 res
->SetDouble("orig_data_size", static_cast<double>(orig_data_size
));
870 res
->SetDouble("compr_data_size", static_cast<double>(compr_data_size
));
871 res
->SetDouble("mem_used_total", static_cast<double>(mem_used_total
));
872 if (compr_data_size
> 0)
873 res
->SetDouble("compression_ratio", static_cast<double>(orig_data_size
) /
874 static_cast<double>(compr_data_size
));
876 res
->SetDouble("compression_ratio", 0);
881 void GetSwapInfo(SwapInfo
* swap_info
) {
882 // Synchronously reading files in /sys/block/zram0 does not hit the disk.
883 ThreadRestrictions::ScopedAllowIO allow_io
;
885 FilePath
zram_path("/sys/block/zram0");
886 uint64 orig_data_size
= ReadFileToUint64(zram_path
.Append("orig_data_size"));
887 if (orig_data_size
<= 4096) {
888 // A single page is compressed at startup, and has a high compression
889 // ratio. We ignore this as it doesn't indicate any real swapping.
890 swap_info
->orig_data_size
= 0;
891 swap_info
->num_reads
= 0;
892 swap_info
->num_writes
= 0;
893 swap_info
->compr_data_size
= 0;
894 swap_info
->mem_used_total
= 0;
897 swap_info
->orig_data_size
= orig_data_size
;
898 swap_info
->num_reads
= ReadFileToUint64(zram_path
.Append("num_reads"));
899 swap_info
->num_writes
= ReadFileToUint64(zram_path
.Append("num_writes"));
900 swap_info
->compr_data_size
=
901 ReadFileToUint64(zram_path
.Append("compr_data_size"));
902 swap_info
->mem_used_total
=
903 ReadFileToUint64(zram_path
.Append("mem_used_total"));
905 #endif // defined(OS_CHROMEOS)
907 #if defined(OS_LINUX)
908 int ProcessMetrics::GetIdleWakeupsPerSecond() {
910 const char kWakeupStat
[] = "se.statistics.nr_wakeups";
911 return ReadProcSchedAndGetFieldAsUint64(process_
, kWakeupStat
, &wake_ups
) ?
912 CalculateIdleWakeupsPerSecond(wake_ups
) : 0;
914 #endif // defined(OS_LINUX)