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 for (const StringPiece
& line
: SplitStringPiece(
567 meminfo_data
, "\n", KEEP_WHITESPACE
, SPLIT_WANT_NONEMPTY
)) {
568 std::vector
<StringPiece
> tokens
= SplitStringPiece(
569 line
, kWhitespaceASCII
, TRIM_WHITESPACE
, SPLIT_WANT_NONEMPTY
);
570 // HugePages_* only has a number and no suffix so we can't rely on
571 // there being exactly 3 tokens.
572 if (tokens
.size() <= 1) {
573 DLOG(WARNING
) << "meminfo: tokens: " << tokens
.size()
574 << " malformed line: " << line
.as_string();
579 if (tokens
[0] == "MemTotal:")
580 target
= &meminfo
->total
;
581 else if (tokens
[0] == "MemFree:")
582 target
= &meminfo
->free
;
583 else if (tokens
[0] == "Buffers:")
584 target
= &meminfo
->buffers
;
585 else if (tokens
[0] == "Cached:")
586 target
= &meminfo
->cached
;
587 else if (tokens
[0] == "Active(anon):")
588 target
= &meminfo
->active_anon
;
589 else if (tokens
[0] == "Inactive(anon):")
590 target
= &meminfo
->inactive_anon
;
591 else if (tokens
[0] == "Active(file):")
592 target
= &meminfo
->active_file
;
593 else if (tokens
[0] == "Inactive(file):")
594 target
= &meminfo
->inactive_file
;
595 else if (tokens
[0] == "SwapTotal:")
596 target
= &meminfo
->swap_total
;
597 else if (tokens
[0] == "SwapFree:")
598 target
= &meminfo
->swap_free
;
599 else if (tokens
[0] == "Dirty:")
600 target
= &meminfo
->dirty
;
601 #if defined(OS_CHROMEOS)
602 // Chrome OS has a tweaked kernel that allows us to query Shmem, which is
603 // usually video memory otherwise invisible to the OS.
604 else if (tokens
[0] == "Shmem:")
605 target
= &meminfo
->shmem
;
606 else if (tokens
[0] == "Slab:")
607 target
= &meminfo
->slab
;
610 StringToInt(tokens
[1], target
);
613 // Make sure we got a valid MemTotal.
614 return meminfo
->total
> 0;
617 // exposed for testing
618 bool ParseProcVmstat(const std::string
& vmstat_data
,
619 SystemMemoryInfoKB
* meminfo
) {
620 // The format of /proc/vmstat is:
622 // nr_free_pages 299878
623 // nr_inactive_anon 239863
624 // nr_active_anon 1318966
625 // nr_inactive_file 2015629
628 // We iterate through the whole file because the position of the
629 // fields are dependent on the kernel version and configuration.
631 for (const StringPiece
& line
: SplitStringPiece(
632 vmstat_data
, "\n", KEEP_WHITESPACE
, SPLIT_WANT_NONEMPTY
)) {
633 std::vector
<StringPiece
> tokens
= SplitStringPiece(
634 line
, " ", KEEP_WHITESPACE
, SPLIT_WANT_NONEMPTY
);
635 if (tokens
.size() != 2)
638 if (tokens
[0] == "pswpin") {
639 StringToInt(tokens
[1], &meminfo
->pswpin
);
640 } else if (tokens
[0] == "pswpout") {
641 StringToInt(tokens
[1], &meminfo
->pswpout
);
642 } else if (tokens
[0] == "pgmajfault") {
643 StringToInt(tokens
[1], &meminfo
->pgmajfault
);
650 bool GetSystemMemoryInfo(SystemMemoryInfoKB
* meminfo
) {
651 // Synchronously reading files in /proc and /sys are safe.
652 ThreadRestrictions::ScopedAllowIO allow_io
;
654 // Used memory is: total - free - buffers - caches
655 FilePath
meminfo_file("/proc/meminfo");
656 std::string meminfo_data
;
657 if (!ReadFileToString(meminfo_file
, &meminfo_data
)) {
658 DLOG(WARNING
) << "Failed to open " << meminfo_file
.value();
662 if (!ParseProcMeminfo(meminfo_data
, meminfo
)) {
663 DLOG(WARNING
) << "Failed to parse " << meminfo_file
.value();
667 #if defined(OS_CHROMEOS)
668 // Report on Chrome OS GEM object graphics memory. /run/debugfs_gpu is a
669 // bind mount into /sys/kernel/debug and synchronously reading the in-memory
670 // files in /sys is fast.
671 #if defined(ARCH_CPU_ARM_FAMILY)
672 FilePath
geminfo_file("/run/debugfs_gpu/exynos_gem_objects");
674 FilePath
geminfo_file("/run/debugfs_gpu/i915_gem_objects");
676 std::string geminfo_data
;
677 meminfo
->gem_objects
= -1;
678 meminfo
->gem_size
= -1;
679 if (ReadFileToString(geminfo_file
, &geminfo_data
)) {
680 int gem_objects
= -1;
681 long long gem_size
= -1;
682 int num_res
= sscanf(geminfo_data
.c_str(),
683 "%d objects, %lld bytes",
684 &gem_objects
, &gem_size
);
686 meminfo
->gem_objects
= gem_objects
;
687 meminfo
->gem_size
= gem_size
;
691 #if defined(ARCH_CPU_ARM_FAMILY)
692 // Incorporate Mali graphics memory if present.
693 FilePath
mali_memory_file("/sys/class/misc/mali0/device/memory");
694 std::string mali_memory_data
;
695 if (ReadFileToString(mali_memory_file
, &mali_memory_data
)) {
696 long long mali_size
= -1;
697 int num_res
= sscanf(mali_memory_data
.c_str(), "%lld bytes", &mali_size
);
699 meminfo
->gem_size
+= mali_size
;
701 #endif // defined(ARCH_CPU_ARM_FAMILY)
702 #endif // defined(OS_CHROMEOS)
704 FilePath
vmstat_file("/proc/vmstat");
705 std::string vmstat_data
;
706 if (!ReadFileToString(vmstat_file
, &vmstat_data
)) {
707 DLOG(WARNING
) << "Failed to open " << vmstat_file
.value();
710 if (!ParseProcVmstat(vmstat_data
, meminfo
)) {
711 DLOG(WARNING
) << "Failed to parse " << vmstat_file
.value();
718 SystemDiskInfo::SystemDiskInfo() {
729 weighted_io_time
= 0;
732 scoped_ptr
<Value
> SystemDiskInfo::ToValue() const {
733 scoped_ptr
<DictionaryValue
> res(new DictionaryValue());
735 // Write out uint64 variables as doubles.
736 // Note: this may discard some precision, but for JS there's no other option.
737 res
->SetDouble("reads", static_cast<double>(reads
));
738 res
->SetDouble("reads_merged", static_cast<double>(reads_merged
));
739 res
->SetDouble("sectors_read", static_cast<double>(sectors_read
));
740 res
->SetDouble("read_time", static_cast<double>(read_time
));
741 res
->SetDouble("writes", static_cast<double>(writes
));
742 res
->SetDouble("writes_merged", static_cast<double>(writes_merged
));
743 res
->SetDouble("sectors_written", static_cast<double>(sectors_written
));
744 res
->SetDouble("write_time", static_cast<double>(write_time
));
745 res
->SetDouble("io", static_cast<double>(io
));
746 res
->SetDouble("io_time", static_cast<double>(io_time
));
747 res
->SetDouble("weighted_io_time", static_cast<double>(weighted_io_time
));
752 bool IsValidDiskName(const std::string
& candidate
) {
753 if (candidate
.length() < 3)
755 if (candidate
[1] == 'd' &&
756 (candidate
[0] == 'h' || candidate
[0] == 's' || candidate
[0] == 'v')) {
758 for (size_t i
= 2; i
< candidate
.length(); ++i
) {
759 if (!islower(candidate
[i
]))
765 const char kMMCName
[] = "mmcblk";
766 const size_t kMMCNameLen
= strlen(kMMCName
);
767 if (candidate
.length() < kMMCNameLen
+ 1)
769 if (candidate
.compare(0, kMMCNameLen
, kMMCName
) != 0)
773 for (size_t i
= kMMCNameLen
; i
< candidate
.length(); ++i
) {
774 if (!isdigit(candidate
[i
]))
780 bool GetSystemDiskInfo(SystemDiskInfo
* diskinfo
) {
781 // Synchronously reading files in /proc does not hit the disk.
782 ThreadRestrictions::ScopedAllowIO allow_io
;
784 FilePath
diskinfo_file("/proc/diskstats");
785 std::string diskinfo_data
;
786 if (!ReadFileToString(diskinfo_file
, &diskinfo_data
)) {
787 DLOG(WARNING
) << "Failed to open " << diskinfo_file
.value();
791 std::vector
<StringPiece
> diskinfo_lines
= SplitStringPiece(
792 diskinfo_data
, "\n", KEEP_WHITESPACE
, SPLIT_WANT_NONEMPTY
);
793 if (diskinfo_lines
.size() == 0) {
794 DLOG(WARNING
) << "No lines found";
799 diskinfo
->reads_merged
= 0;
800 diskinfo
->sectors_read
= 0;
801 diskinfo
->read_time
= 0;
802 diskinfo
->writes
= 0;
803 diskinfo
->writes_merged
= 0;
804 diskinfo
->sectors_written
= 0;
805 diskinfo
->write_time
= 0;
807 diskinfo
->io_time
= 0;
808 diskinfo
->weighted_io_time
= 0;
811 uint64 reads_merged
= 0;
812 uint64 sectors_read
= 0;
813 uint64 read_time
= 0;
815 uint64 writes_merged
= 0;
816 uint64 sectors_written
= 0;
817 uint64 write_time
= 0;
820 uint64 weighted_io_time
= 0;
822 for (const StringPiece
& line
: diskinfo_lines
) {
823 std::vector
<StringPiece
> disk_fields
= SplitStringPiece(
824 line
, kWhitespaceASCII
, TRIM_WHITESPACE
, SPLIT_WANT_NONEMPTY
);
826 // Fields may have overflowed and reset to zero.
827 if (IsValidDiskName(disk_fields
[kDiskDriveName
].as_string())) {
828 StringToUint64(disk_fields
[kDiskReads
], &reads
);
829 StringToUint64(disk_fields
[kDiskReadsMerged
], &reads_merged
);
830 StringToUint64(disk_fields
[kDiskSectorsRead
], §ors_read
);
831 StringToUint64(disk_fields
[kDiskReadTime
], &read_time
);
832 StringToUint64(disk_fields
[kDiskWrites
], &writes
);
833 StringToUint64(disk_fields
[kDiskWritesMerged
], &writes_merged
);
834 StringToUint64(disk_fields
[kDiskSectorsWritten
], §ors_written
);
835 StringToUint64(disk_fields
[kDiskWriteTime
], &write_time
);
836 StringToUint64(disk_fields
[kDiskIO
], &io
);
837 StringToUint64(disk_fields
[kDiskIOTime
], &io_time
);
838 StringToUint64(disk_fields
[kDiskWeightedIOTime
], &weighted_io_time
);
840 diskinfo
->reads
+= reads
;
841 diskinfo
->reads_merged
+= reads_merged
;
842 diskinfo
->sectors_read
+= sectors_read
;
843 diskinfo
->read_time
+= read_time
;
844 diskinfo
->writes
+= writes
;
845 diskinfo
->writes_merged
+= writes_merged
;
846 diskinfo
->sectors_written
+= sectors_written
;
847 diskinfo
->write_time
+= write_time
;
849 diskinfo
->io_time
+= io_time
;
850 diskinfo
->weighted_io_time
+= weighted_io_time
;
857 #if defined(OS_CHROMEOS)
858 scoped_ptr
<Value
> SwapInfo::ToValue() const {
859 scoped_ptr
<DictionaryValue
> res(new DictionaryValue());
861 // Write out uint64 variables as doubles.
862 // Note: this may discard some precision, but for JS there's no other option.
863 res
->SetDouble("num_reads", static_cast<double>(num_reads
));
864 res
->SetDouble("num_writes", static_cast<double>(num_writes
));
865 res
->SetDouble("orig_data_size", static_cast<double>(orig_data_size
));
866 res
->SetDouble("compr_data_size", static_cast<double>(compr_data_size
));
867 res
->SetDouble("mem_used_total", static_cast<double>(mem_used_total
));
868 if (compr_data_size
> 0)
869 res
->SetDouble("compression_ratio", static_cast<double>(orig_data_size
) /
870 static_cast<double>(compr_data_size
));
872 res
->SetDouble("compression_ratio", 0);
877 void GetSwapInfo(SwapInfo
* swap_info
) {
878 // Synchronously reading files in /sys/block/zram0 does not hit the disk.
879 ThreadRestrictions::ScopedAllowIO allow_io
;
881 FilePath
zram_path("/sys/block/zram0");
882 uint64 orig_data_size
= ReadFileToUint64(zram_path
.Append("orig_data_size"));
883 if (orig_data_size
<= 4096) {
884 // A single page is compressed at startup, and has a high compression
885 // ratio. We ignore this as it doesn't indicate any real swapping.
886 swap_info
->orig_data_size
= 0;
887 swap_info
->num_reads
= 0;
888 swap_info
->num_writes
= 0;
889 swap_info
->compr_data_size
= 0;
890 swap_info
->mem_used_total
= 0;
893 swap_info
->orig_data_size
= orig_data_size
;
894 swap_info
->num_reads
= ReadFileToUint64(zram_path
.Append("num_reads"));
895 swap_info
->num_writes
= ReadFileToUint64(zram_path
.Append("num_writes"));
896 swap_info
->compr_data_size
=
897 ReadFileToUint64(zram_path
.Append("compr_data_size"));
898 swap_info
->mem_used_total
=
899 ReadFileToUint64(zram_path
.Append("mem_used_total"));
901 #endif // defined(OS_CHROMEOS)
903 #if defined(OS_LINUX)
904 int ProcessMetrics::GetIdleWakeupsPerSecond() {
906 const char kWakeupStat
[] = "se.statistics.nr_wakeups";
907 return ReadProcSchedAndGetFieldAsUint64(process_
, kWakeupStat
, &wake_ups
) ?
908 CalculateIdleWakeupsPerSecond(wake_ups
) : 0;
910 #endif // defined(OS_LINUX)