Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / process / process_metrics_linux.cc
blobadca7c5ee23bd53914342cffd2aab166402fe920
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"
7 #include <dirent.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <unistd.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"
24 namespace base {
26 namespace {
28 void TrimKeyValuePairs(StringPairs* pairs) {
29 DCHECK(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))
42 return 0;
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))
46 return 0;
47 return file_as_uint64;
49 #endif
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) {
54 std::string status;
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))
60 return 0;
63 StringPairs pairs;
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;
69 if (key == field) {
70 std::vector<StringPiece> split_value_str = SplitStringPiece(
71 value_str, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
72 if (split_value_str.size() != 2 || split_value_str[1] != "kB") {
73 NOTREACHED();
74 return 0;
76 size_t value;
77 if (!StringToSizeT(split_value_str[0], &value)) {
78 NOTREACHED();
79 return 0;
81 return value;
84 NOTREACHED();
85 return 0;
88 #if defined(OS_LINUX)
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,
94 uint64* result) {
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))
101 return false;
104 StringPairs pairs;
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;
110 if (key == field) {
111 uint64 value;
112 if (!StringToUint64(value_str, &value))
113 return false;
114 *result = value;
115 return true;
118 return false;
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());
129 if (!dir) {
130 DPLOG(ERROR) << "opendir(" << task_path.value() << ")";
131 return -1;
134 int total_cpu = 0;
135 while (struct dirent* ent = readdir(dir)) {
136 pid_t tid = internal::ProcDirSlotToPid(ent->d_name);
137 if (!tid)
138 continue;
140 // Synchronously reading files in /proc does not hit the disk.
141 ThreadRestrictions::ScopedAllowIO allow_io;
143 std::string stat;
144 FilePath stat_path =
145 task_path.Append(ent->d_name).Append(internal::kStatFile);
146 if (ReadFileToString(stat_path, &stat)) {
147 int cpu = ParseProcStatCPU(stat);
148 if (cpu > 0)
149 total_cpu += cpu;
152 closedir(dir);
154 return total_cpu;
157 } // namespace
159 // static
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_,
167 internal::VM_VSIZE);
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) *
178 getpagesize();
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))
190 return false;
192 if (private_bytes)
193 *private_bytes = ws_usage.priv * 1024;
195 if (shared_bytes)
196 *shared_bytes = ws_usage.shared * 1024;
198 return true;
201 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
202 #if defined(OS_CHROMEOS)
203 if (GetWorkingSetKBytesTotmaps(ws_usage))
204 return true;
205 #endif
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_);
216 return 0;
219 int64 time_delta = (time - last_cpu_time_).InMicroseconds();
220 DCHECK_NE(time_delta, 0);
221 if (time_delta == 0)
222 return 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;
235 last_cpu_ = cpu;
237 return percentage;
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))
249 return false;
251 io_counters->OtherOperationCount = 0;
252 io_counters->OtherTransferCount = 0;
254 StringPairs pairs;
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;
261 if (key == "syscr")
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;
269 if (!target_counter)
270 continue;
271 bool converted = StringToUint64(value_str, target_counter);
272 DCHECK(converted);
274 return true;
277 ProcessMetrics::ProcessMetrics(ProcessHandle process)
278 : process_(process),
279 last_system_time_(0),
280 #if defined(OS_LINUX)
281 last_absolute_idle_wakeups_(0),
282 #endif
283 last_cpu_(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)
291 const {
292 // The format of /proc/<pid>/totmaps is:
294 // Rss: 6120 kB
295 // Pss: 3335 kB
296 // Shared_Clean: 1008 kB
297 // Shared_Dirty: 4012 kB
298 // Private_Clean: 4 kB
299 // Private_Dirty: 1096 kB
300 // Referenced: XXX kB
301 // Anonymous: XXX kB
302 // AnonHugePages: XXX kB
303 // Swap: XXX kB
304 // Locked: 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)
316 return false;
319 std::vector<std::string> totmaps_fields = SplitString(
320 totmaps_data, base::kWhitespaceASCII, base::KEEP_WHITESPACE,
321 base::SPLIT_WANT_NONEMPTY);
323 DCHECK_EQ("Pss:", totmaps_fields[kPssIndex-1]);
324 DCHECK_EQ("Private_Clean:", totmaps_fields[kPrivate_CleanIndex - 1]);
325 DCHECK_EQ("Private_Dirty:", totmaps_fields[kPrivate_DirtyIndex - 1]);
326 DCHECK_EQ("Swap:", totmaps_fields[kSwapIndex-1]);
328 int pss = 0;
329 int private_clean = 0;
330 int private_dirty = 0;
331 int swap = 0;
332 bool ret = true;
333 ret &= StringToInt(totmaps_fields[kPssIndex], &pss);
334 ret &= StringToInt(totmaps_fields[kPrivate_CleanIndex], &private_clean);
335 ret &= StringToInt(totmaps_fields[kPrivate_DirtyIndex], &private_dirty);
336 ret &= StringToInt(totmaps_fields[kSwapIndex], &swap);
338 // On ChromeOS swap is to zram. We count this as private / shared, as
339 // increased swap decreases available RAM to user processes, which would
340 // otherwise create surprising results.
341 ws_usage->priv = private_clean + private_dirty + swap;
342 ws_usage->shared = pss + swap;
343 ws_usage->shareable = 0;
344 ws_usage->swapped = swap;
345 return ret;
347 #endif
349 // Private and Shared working set sizes are obtained from /proc/<pid>/statm.
350 bool ProcessMetrics::GetWorkingSetKBytesStatm(WorkingSetKBytes* ws_usage)
351 const {
352 // Use statm instead of smaps because smaps is:
353 // a) Large and slow to parse.
354 // b) Unavailable in the SUID sandbox.
356 // First we need to get the page size, since everything is measured in pages.
357 // For details, see: man 5 proc.
358 const int page_size_kb = getpagesize() / 1024;
359 if (page_size_kb <= 0)
360 return false;
362 std::string statm;
364 FilePath statm_file = internal::GetProcPidDir(process_).Append("statm");
365 // Synchronously reading files in /proc does not hit the disk.
366 ThreadRestrictions::ScopedAllowIO allow_io;
367 bool ret = ReadFileToString(statm_file, &statm);
368 if (!ret || statm.length() == 0)
369 return false;
372 std::vector<StringPiece> statm_vec = SplitStringPiece(
373 statm, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
374 if (statm_vec.size() != 7)
375 return false; // Not the format we expect.
377 int statm_rss, statm_shared;
378 bool ret = true;
379 ret &= StringToInt(statm_vec[1], &statm_rss);
380 ret &= StringToInt(statm_vec[2], &statm_shared);
382 ws_usage->priv = (statm_rss - statm_shared) * page_size_kb;
383 ws_usage->shared = statm_shared * page_size_kb;
385 // Sharable is not calculated, as it does not provide interesting data.
386 ws_usage->shareable = 0;
388 #if defined(OS_CHROMEOS)
389 // Can't get swapped memory from statm.
390 ws_usage->swapped = 0;
391 #endif
393 return ret;
396 size_t GetSystemCommitCharge() {
397 SystemMemoryInfoKB meminfo;
398 if (!GetSystemMemoryInfo(&meminfo))
399 return 0;
400 return meminfo.total - meminfo.free - meminfo.buffers - meminfo.cached;
403 int ParseProcStatCPU(const std::string& input) {
404 // |input| may be empty if the process disappeared somehow.
405 // e.g. http://crbug.com/145811.
406 if (input.empty())
407 return -1;
409 size_t start = input.find_last_of(')');
410 if (start == input.npos)
411 return -1;
413 // Number of spaces remaining until reaching utime's index starting after the
414 // last ')'.
415 int num_spaces_remaining = internal::VM_UTIME - 1;
417 size_t i = start;
418 while ((i = input.find(' ', i + 1)) != input.npos) {
419 // Validate the assumption that there aren't any contiguous spaces
420 // in |input| before utime.
421 DCHECK_NE(input[i - 1], ' ');
422 if (--num_spaces_remaining == 0) {
423 int utime = 0;
424 int stime = 0;
425 if (sscanf(&input.data()[i], "%d %d", &utime, &stime) != 2)
426 return -1;
428 return utime + stime;
432 return -1;
435 const char kProcSelfExe[] = "/proc/self/exe";
437 int GetNumberOfThreads(ProcessHandle process) {
438 return internal::ReadProcStatsAndGetFieldAsInt64(process,
439 internal::VM_NUMTHREADS);
442 namespace {
444 // The format of /proc/diskstats is:
445 // Device major number
446 // Device minor number
447 // Device name
448 // Field 1 -- # of reads completed
449 // This is the total number of reads completed successfully.
450 // Field 2 -- # of reads merged, field 6 -- # of writes merged
451 // Reads and writes which are adjacent to each other may be merged for
452 // efficiency. Thus two 4K reads may become one 8K read before it is
453 // ultimately handed to the disk, and so it will be counted (and queued)
454 // as only one I/O. This field lets you know how often this was done.
455 // Field 3 -- # of sectors read
456 // This is the total number of sectors read successfully.
457 // Field 4 -- # of milliseconds spent reading
458 // This is the total number of milliseconds spent by all reads (as
459 // measured from __make_request() to end_that_request_last()).
460 // Field 5 -- # of writes completed
461 // This is the total number of writes completed successfully.
462 // Field 6 -- # of writes merged
463 // See the description of field 2.
464 // Field 7 -- # of sectors written
465 // This is the total number of sectors written successfully.
466 // Field 8 -- # of milliseconds spent writing
467 // This is the total number of milliseconds spent by all writes (as
468 // measured from __make_request() to end_that_request_last()).
469 // Field 9 -- # of I/Os currently in progress
470 // The only field that should go to zero. Incremented as requests are
471 // given to appropriate struct request_queue and decremented as they
472 // finish.
473 // Field 10 -- # of milliseconds spent doing I/Os
474 // This field increases so long as field 9 is nonzero.
475 // Field 11 -- weighted # of milliseconds spent doing I/Os
476 // This field is incremented at each I/O start, I/O completion, I/O
477 // merge, or read of these stats by the number of I/Os in progress
478 // (field 9) times the number of milliseconds spent doing I/O since the
479 // last update of this field. This can provide an easy measure of both
480 // I/O completion time and the backlog that may be accumulating.
482 const size_t kDiskDriveName = 2;
483 const size_t kDiskReads = 3;
484 const size_t kDiskReadsMerged = 4;
485 const size_t kDiskSectorsRead = 5;
486 const size_t kDiskReadTime = 6;
487 const size_t kDiskWrites = 7;
488 const size_t kDiskWritesMerged = 8;
489 const size_t kDiskSectorsWritten = 9;
490 const size_t kDiskWriteTime = 10;
491 const size_t kDiskIO = 11;
492 const size_t kDiskIOTime = 12;
493 const size_t kDiskWeightedIOTime = 13;
495 } // namespace
497 SystemMemoryInfoKB::SystemMemoryInfoKB() {
498 total = 0;
499 free = 0;
500 buffers = 0;
501 cached = 0;
502 active_anon = 0;
503 inactive_anon = 0;
504 active_file = 0;
505 inactive_file = 0;
506 swap_total = 0;
507 swap_free = 0;
508 dirty = 0;
510 pswpin = 0;
511 pswpout = 0;
512 pgmajfault = 0;
514 #ifdef OS_CHROMEOS
515 shmem = 0;
516 slab = 0;
517 gem_objects = -1;
518 gem_size = -1;
519 #endif
522 scoped_ptr<Value> SystemMemoryInfoKB::ToValue() const {
523 scoped_ptr<DictionaryValue> res(new DictionaryValue());
525 res->SetInteger("total", total);
526 res->SetInteger("free", free);
527 res->SetInteger("buffers", buffers);
528 res->SetInteger("cached", cached);
529 res->SetInteger("active_anon", active_anon);
530 res->SetInteger("inactive_anon", inactive_anon);
531 res->SetInteger("active_file", active_file);
532 res->SetInteger("inactive_file", inactive_file);
533 res->SetInteger("swap_total", swap_total);
534 res->SetInteger("swap_free", swap_free);
535 res->SetInteger("swap_used", swap_total - swap_free);
536 res->SetInteger("dirty", dirty);
537 res->SetInteger("pswpin", pswpin);
538 res->SetInteger("pswpout", pswpout);
539 res->SetInteger("pgmajfault", pgmajfault);
540 #ifdef OS_CHROMEOS
541 res->SetInteger("shmem", shmem);
542 res->SetInteger("slab", slab);
543 res->SetInteger("gem_objects", gem_objects);
544 res->SetInteger("gem_size", gem_size);
545 #endif
547 return res.Pass();
550 // exposed for testing
551 bool ParseProcMeminfo(const std::string& meminfo_data,
552 SystemMemoryInfoKB* meminfo) {
553 // The format of /proc/meminfo is:
555 // MemTotal: 8235324 kB
556 // MemFree: 1628304 kB
557 // Buffers: 429596 kB
558 // Cached: 4728232 kB
559 // ...
560 // There is no guarantee on the ordering or position
561 // though it doesn't appear to change very often
563 // As a basic sanity check, let's make sure we at least get non-zero
564 // MemTotal value
565 meminfo->total = 0;
567 for (const StringPiece& line : SplitStringPiece(
568 meminfo_data, "\n", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY)) {
569 std::vector<StringPiece> tokens = SplitStringPiece(
570 line, kWhitespaceASCII, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
571 // HugePages_* only has a number and no suffix so we can't rely on
572 // there being exactly 3 tokens.
573 if (tokens.size() <= 1) {
574 DLOG(WARNING) << "meminfo: tokens: " << tokens.size()
575 << " malformed line: " << line.as_string();
576 continue;
579 int* target = NULL;
580 if (tokens[0] == "MemTotal:")
581 target = &meminfo->total;
582 else if (tokens[0] == "MemFree:")
583 target = &meminfo->free;
584 else if (tokens[0] == "Buffers:")
585 target = &meminfo->buffers;
586 else if (tokens[0] == "Cached:")
587 target = &meminfo->cached;
588 else if (tokens[0] == "Active(anon):")
589 target = &meminfo->active_anon;
590 else if (tokens[0] == "Inactive(anon):")
591 target = &meminfo->inactive_anon;
592 else if (tokens[0] == "Active(file):")
593 target = &meminfo->active_file;
594 else if (tokens[0] == "Inactive(file):")
595 target = &meminfo->inactive_file;
596 else if (tokens[0] == "SwapTotal:")
597 target = &meminfo->swap_total;
598 else if (tokens[0] == "SwapFree:")
599 target = &meminfo->swap_free;
600 else if (tokens[0] == "Dirty:")
601 target = &meminfo->dirty;
602 #if defined(OS_CHROMEOS)
603 // Chrome OS has a tweaked kernel that allows us to query Shmem, which is
604 // usually video memory otherwise invisible to the OS.
605 else if (tokens[0] == "Shmem:")
606 target = &meminfo->shmem;
607 else if (tokens[0] == "Slab:")
608 target = &meminfo->slab;
609 #endif
610 if (target)
611 StringToInt(tokens[1], target);
614 // Make sure we got a valid MemTotal.
615 return meminfo->total > 0;
618 // exposed for testing
619 bool ParseProcVmstat(const std::string& vmstat_data,
620 SystemMemoryInfoKB* meminfo) {
621 // The format of /proc/vmstat is:
623 // nr_free_pages 299878
624 // nr_inactive_anon 239863
625 // nr_active_anon 1318966
626 // nr_inactive_file 2015629
627 // ...
629 // We iterate through the whole file because the position of the
630 // fields are dependent on the kernel version and configuration.
632 for (const StringPiece& line : SplitStringPiece(
633 vmstat_data, "\n", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY)) {
634 std::vector<StringPiece> tokens = SplitStringPiece(
635 line, " ", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
636 if (tokens.size() != 2)
637 continue;
639 if (tokens[0] == "pswpin") {
640 StringToInt(tokens[1], &meminfo->pswpin);
641 } else if (tokens[0] == "pswpout") {
642 StringToInt(tokens[1], &meminfo->pswpout);
643 } else if (tokens[0] == "pgmajfault") {
644 StringToInt(tokens[1], &meminfo->pgmajfault);
648 return true;
651 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
652 // Synchronously reading files in /proc and /sys are safe.
653 ThreadRestrictions::ScopedAllowIO allow_io;
655 // Used memory is: total - free - buffers - caches
656 FilePath meminfo_file("/proc/meminfo");
657 std::string meminfo_data;
658 if (!ReadFileToString(meminfo_file, &meminfo_data)) {
659 DLOG(WARNING) << "Failed to open " << meminfo_file.value();
660 return false;
663 if (!ParseProcMeminfo(meminfo_data, meminfo)) {
664 DLOG(WARNING) << "Failed to parse " << meminfo_file.value();
665 return false;
668 #if defined(OS_CHROMEOS)
669 // Report on Chrome OS GEM object graphics memory. /run/debugfs_gpu is a
670 // bind mount into /sys/kernel/debug and synchronously reading the in-memory
671 // files in /sys is fast.
672 #if defined(ARCH_CPU_ARM_FAMILY)
673 FilePath geminfo_file("/run/debugfs_gpu/exynos_gem_objects");
674 #else
675 FilePath geminfo_file("/run/debugfs_gpu/i915_gem_objects");
676 #endif
677 std::string geminfo_data;
678 meminfo->gem_objects = -1;
679 meminfo->gem_size = -1;
680 if (ReadFileToString(geminfo_file, &geminfo_data)) {
681 int gem_objects = -1;
682 long long gem_size = -1;
683 int num_res = sscanf(geminfo_data.c_str(),
684 "%d objects, %lld bytes",
685 &gem_objects, &gem_size);
686 if (num_res == 2) {
687 meminfo->gem_objects = gem_objects;
688 meminfo->gem_size = gem_size;
692 #if defined(ARCH_CPU_ARM_FAMILY)
693 // Incorporate Mali graphics memory if present.
694 FilePath mali_memory_file("/sys/class/misc/mali0/device/memory");
695 std::string mali_memory_data;
696 if (ReadFileToString(mali_memory_file, &mali_memory_data)) {
697 long long mali_size = -1;
698 int num_res = sscanf(mali_memory_data.c_str(), "%lld bytes", &mali_size);
699 if (num_res == 1)
700 meminfo->gem_size += mali_size;
702 #endif // defined(ARCH_CPU_ARM_FAMILY)
703 #endif // defined(OS_CHROMEOS)
705 FilePath vmstat_file("/proc/vmstat");
706 std::string vmstat_data;
707 if (!ReadFileToString(vmstat_file, &vmstat_data)) {
708 DLOG(WARNING) << "Failed to open " << vmstat_file.value();
709 return false;
711 if (!ParseProcVmstat(vmstat_data, meminfo)) {
712 DLOG(WARNING) << "Failed to parse " << vmstat_file.value();
713 return false;
716 return true;
719 SystemDiskInfo::SystemDiskInfo() {
720 reads = 0;
721 reads_merged = 0;
722 sectors_read = 0;
723 read_time = 0;
724 writes = 0;
725 writes_merged = 0;
726 sectors_written = 0;
727 write_time = 0;
728 io = 0;
729 io_time = 0;
730 weighted_io_time = 0;
733 scoped_ptr<Value> SystemDiskInfo::ToValue() const {
734 scoped_ptr<DictionaryValue> res(new DictionaryValue());
736 // Write out uint64 variables as doubles.
737 // Note: this may discard some precision, but for JS there's no other option.
738 res->SetDouble("reads", static_cast<double>(reads));
739 res->SetDouble("reads_merged", static_cast<double>(reads_merged));
740 res->SetDouble("sectors_read", static_cast<double>(sectors_read));
741 res->SetDouble("read_time", static_cast<double>(read_time));
742 res->SetDouble("writes", static_cast<double>(writes));
743 res->SetDouble("writes_merged", static_cast<double>(writes_merged));
744 res->SetDouble("sectors_written", static_cast<double>(sectors_written));
745 res->SetDouble("write_time", static_cast<double>(write_time));
746 res->SetDouble("io", static_cast<double>(io));
747 res->SetDouble("io_time", static_cast<double>(io_time));
748 res->SetDouble("weighted_io_time", static_cast<double>(weighted_io_time));
750 return res.Pass();
753 bool IsValidDiskName(const std::string& candidate) {
754 if (candidate.length() < 3)
755 return false;
756 if (candidate[1] == 'd' &&
757 (candidate[0] == 'h' || candidate[0] == 's' || candidate[0] == 'v')) {
758 // [hsv]d[a-z]+ case
759 for (size_t i = 2; i < candidate.length(); ++i) {
760 if (!islower(candidate[i]))
761 return false;
763 return true;
766 const char kMMCName[] = "mmcblk";
767 const size_t kMMCNameLen = strlen(kMMCName);
768 if (candidate.length() < kMMCNameLen + 1)
769 return false;
770 if (candidate.compare(0, kMMCNameLen, kMMCName) != 0)
771 return false;
773 // mmcblk[0-9]+ case
774 for (size_t i = kMMCNameLen; i < candidate.length(); ++i) {
775 if (!isdigit(candidate[i]))
776 return false;
778 return true;
781 bool GetSystemDiskInfo(SystemDiskInfo* diskinfo) {
782 // Synchronously reading files in /proc does not hit the disk.
783 ThreadRestrictions::ScopedAllowIO allow_io;
785 FilePath diskinfo_file("/proc/diskstats");
786 std::string diskinfo_data;
787 if (!ReadFileToString(diskinfo_file, &diskinfo_data)) {
788 DLOG(WARNING) << "Failed to open " << diskinfo_file.value();
789 return false;
792 std::vector<StringPiece> diskinfo_lines = SplitStringPiece(
793 diskinfo_data, "\n", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
794 if (diskinfo_lines.size() == 0) {
795 DLOG(WARNING) << "No lines found";
796 return false;
799 diskinfo->reads = 0;
800 diskinfo->reads_merged = 0;
801 diskinfo->sectors_read = 0;
802 diskinfo->read_time = 0;
803 diskinfo->writes = 0;
804 diskinfo->writes_merged = 0;
805 diskinfo->sectors_written = 0;
806 diskinfo->write_time = 0;
807 diskinfo->io = 0;
808 diskinfo->io_time = 0;
809 diskinfo->weighted_io_time = 0;
811 uint64 reads = 0;
812 uint64 reads_merged = 0;
813 uint64 sectors_read = 0;
814 uint64 read_time = 0;
815 uint64 writes = 0;
816 uint64 writes_merged = 0;
817 uint64 sectors_written = 0;
818 uint64 write_time = 0;
819 uint64 io = 0;
820 uint64 io_time = 0;
821 uint64 weighted_io_time = 0;
823 for (const StringPiece& line : diskinfo_lines) {
824 std::vector<StringPiece> disk_fields = SplitStringPiece(
825 line, kWhitespaceASCII, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
827 // Fields may have overflowed and reset to zero.
828 if (IsValidDiskName(disk_fields[kDiskDriveName].as_string())) {
829 StringToUint64(disk_fields[kDiskReads], &reads);
830 StringToUint64(disk_fields[kDiskReadsMerged], &reads_merged);
831 StringToUint64(disk_fields[kDiskSectorsRead], &sectors_read);
832 StringToUint64(disk_fields[kDiskReadTime], &read_time);
833 StringToUint64(disk_fields[kDiskWrites], &writes);
834 StringToUint64(disk_fields[kDiskWritesMerged], &writes_merged);
835 StringToUint64(disk_fields[kDiskSectorsWritten], &sectors_written);
836 StringToUint64(disk_fields[kDiskWriteTime], &write_time);
837 StringToUint64(disk_fields[kDiskIO], &io);
838 StringToUint64(disk_fields[kDiskIOTime], &io_time);
839 StringToUint64(disk_fields[kDiskWeightedIOTime], &weighted_io_time);
841 diskinfo->reads += reads;
842 diskinfo->reads_merged += reads_merged;
843 diskinfo->sectors_read += sectors_read;
844 diskinfo->read_time += read_time;
845 diskinfo->writes += writes;
846 diskinfo->writes_merged += writes_merged;
847 diskinfo->sectors_written += sectors_written;
848 diskinfo->write_time += write_time;
849 diskinfo->io += io;
850 diskinfo->io_time += io_time;
851 diskinfo->weighted_io_time += weighted_io_time;
855 return true;
858 #if defined(OS_CHROMEOS)
859 scoped_ptr<Value> SwapInfo::ToValue() const {
860 scoped_ptr<DictionaryValue> res(new DictionaryValue());
862 // Write out uint64 variables as doubles.
863 // Note: this may discard some precision, but for JS there's no other option.
864 res->SetDouble("num_reads", static_cast<double>(num_reads));
865 res->SetDouble("num_writes", static_cast<double>(num_writes));
866 res->SetDouble("orig_data_size", static_cast<double>(orig_data_size));
867 res->SetDouble("compr_data_size", static_cast<double>(compr_data_size));
868 res->SetDouble("mem_used_total", static_cast<double>(mem_used_total));
869 if (compr_data_size > 0)
870 res->SetDouble("compression_ratio", static_cast<double>(orig_data_size) /
871 static_cast<double>(compr_data_size));
872 else
873 res->SetDouble("compression_ratio", 0);
875 return res.Pass();
878 void GetSwapInfo(SwapInfo* swap_info) {
879 // Synchronously reading files in /sys/block/zram0 does not hit the disk.
880 ThreadRestrictions::ScopedAllowIO allow_io;
882 FilePath zram_path("/sys/block/zram0");
883 uint64 orig_data_size = ReadFileToUint64(zram_path.Append("orig_data_size"));
884 if (orig_data_size <= 4096) {
885 // A single page is compressed at startup, and has a high compression
886 // ratio. We ignore this as it doesn't indicate any real swapping.
887 swap_info->orig_data_size = 0;
888 swap_info->num_reads = 0;
889 swap_info->num_writes = 0;
890 swap_info->compr_data_size = 0;
891 swap_info->mem_used_total = 0;
892 return;
894 swap_info->orig_data_size = orig_data_size;
895 swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads"));
896 swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes"));
897 swap_info->compr_data_size =
898 ReadFileToUint64(zram_path.Append("compr_data_size"));
899 swap_info->mem_used_total =
900 ReadFileToUint64(zram_path.Append("mem_used_total"));
902 #endif // defined(OS_CHROMEOS)
904 #if defined(OS_LINUX)
905 int ProcessMetrics::GetIdleWakeupsPerSecond() {
906 uint64 wake_ups;
907 const char kWakeupStat[] = "se.statistics.nr_wakeups";
908 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ?
909 CalculateIdleWakeupsPerSecond(wake_ups) : 0;
911 #endif // defined(OS_LINUX)
913 } // namespace base