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/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"
34 // Read a file with a single number string and return the number as a uint64.
35 static uint64 ReadFileToUint64(const base::FilePath file) {
36 std::string file_as_string;
37 if (!ReadFileToString(file, &file_as_string))
39 base::TrimWhitespaceASCII(file_as_string, base::TRIM_ALL, &file_as_string);
40 uint64 file_as_uint64 = 0;
41 if (!base::StringToUint64(file_as_string, &file_as_uint64))
43 return file_as_uint64;
47 // Read /proc/<pid>/status and returns the value for |field|, or 0 on failure.
48 // Only works for fields in the form of "Field: value kB".
49 size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) {
50 FilePath stat_file = internal::GetProcPidDir(pid).Append("status");
53 // Synchronously reading files in /proc is safe.
54 ThreadRestrictions::ScopedAllowIO allow_io;
55 if (!ReadFileToString(stat_file, &status))
59 StringTokenizer tokenizer(status, ":\n");
60 ParsingState state = KEY_NAME;
61 StringPiece last_key_name;
62 while (tokenizer.GetNext()) {
65 last_key_name = tokenizer.token_piece();
69 DCHECK(!last_key_name.empty());
70 if (last_key_name == field) {
71 std::string value_str;
72 tokenizer.token_piece().CopyToString(&value_str);
73 std::string value_str_trimmed;
74 base::TrimWhitespaceASCII(value_str, base::TRIM_ALL,
76 std::vector<std::string> split_value_str;
77 SplitString(value_str_trimmed, ' ', &split_value_str);
78 if (split_value_str.size() != 2 || split_value_str[1] != "kB") {
83 if (!StringToSizeT(split_value_str[0], &value)) {
97 // Get the total CPU of a single process. Return value is number of jiffies
98 // on success or -1 on error.
99 int GetProcessCPU(pid_t pid) {
100 // Use /proc/<pid>/task to find all threads and parse their /stat file.
101 FilePath task_path = internal::GetProcPidDir(pid).Append("task");
103 DIR* dir = opendir(task_path.value().c_str());
105 DPLOG(ERROR) << "opendir(" << task_path.value() << ")";
110 while (struct dirent* ent = readdir(dir)) {
111 pid_t tid = internal::ProcDirSlotToPid(ent->d_name);
115 // Synchronously reading files in /proc is safe.
116 ThreadRestrictions::ScopedAllowIO allow_io;
120 task_path.Append(ent->d_name).Append(internal::kStatFile);
121 if (ReadFileToString(stat_path, &stat)) {
122 int cpu = ParseProcStatCPU(stat);
135 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) {
136 return new ProcessMetrics(process);
139 // On linux, we return vsize.
140 size_t ProcessMetrics::GetPagefileUsage() const {
141 return internal::ReadProcStatsAndGetFieldAsSizeT(process_,
145 // On linux, we return the high water mark of vsize.
146 size_t ProcessMetrics::GetPeakPagefileUsage() const {
147 return ReadProcStatusAndGetFieldAsSizeT(process_, "VmPeak") * 1024;
150 // On linux, we return RSS.
151 size_t ProcessMetrics::GetWorkingSetSize() const {
152 return internal::ReadProcStatsAndGetFieldAsSizeT(process_, internal::VM_RSS) *
156 // On linux, we return the high water mark of RSS.
157 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
158 return ReadProcStatusAndGetFieldAsSizeT(process_, "VmHWM") * 1024;
161 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
162 size_t* shared_bytes) {
163 WorkingSetKBytes ws_usage;
164 if (!GetWorkingSetKBytes(&ws_usage))
168 *private_bytes = ws_usage.priv * 1024;
171 *shared_bytes = ws_usage.shared * 1024;
176 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
177 #if defined(OS_CHROMEOS)
178 if (GetWorkingSetKBytesTotmaps(ws_usage))
181 return GetWorkingSetKBytesStatm(ws_usage);
184 double ProcessMetrics::GetCPUUsage() {
186 int retval = gettimeofday(&now, NULL);
189 int64 time = TimeValToMicroseconds(now);
191 if (last_cpu_time_ == 0) {
192 // First call, just set the last values.
193 last_cpu_time_ = time;
194 last_cpu_ = GetProcessCPU(process_);
198 int64 time_delta = time - last_cpu_time_;
199 DCHECK_NE(time_delta, 0);
203 int cpu = GetProcessCPU(process_);
205 // We have the number of jiffies in the time period. Convert to percentage.
206 // Note this means we will go *over* 100 in the case where multiple threads
207 // are together adding to more than one CPU's worth.
208 TimeDelta cpu_time = internal::ClockTicksToTimeDelta(cpu);
209 TimeDelta last_cpu_time = internal::ClockTicksToTimeDelta(last_cpu_);
210 int percentage = 100 * (cpu_time - last_cpu_time).InSecondsF() /
211 TimeDelta::FromMicroseconds(time_delta).InSecondsF();
213 last_cpu_time_ = time;
219 // To have /proc/self/io file you must enable CONFIG_TASK_IO_ACCOUNTING
220 // in your kernel configuration.
221 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
222 // Synchronously reading files in /proc is safe.
223 ThreadRestrictions::ScopedAllowIO allow_io;
225 std::string proc_io_contents;
226 FilePath io_file = internal::GetProcPidDir(process_).Append("io");
227 if (!ReadFileToString(io_file, &proc_io_contents))
230 (*io_counters).OtherOperationCount = 0;
231 (*io_counters).OtherTransferCount = 0;
233 StringTokenizer tokenizer(proc_io_contents, ": \n");
234 ParsingState state = KEY_NAME;
235 StringPiece last_key_name;
236 while (tokenizer.GetNext()) {
239 last_key_name = tokenizer.token_piece();
243 DCHECK(!last_key_name.empty());
244 if (last_key_name == "syscr") {
245 StringToInt64(tokenizer.token_piece(),
246 reinterpret_cast<int64*>(&(*io_counters).ReadOperationCount));
247 } else if (last_key_name == "syscw") {
248 StringToInt64(tokenizer.token_piece(),
249 reinterpret_cast<int64*>(&(*io_counters).WriteOperationCount));
250 } else if (last_key_name == "rchar") {
251 StringToInt64(tokenizer.token_piece(),
252 reinterpret_cast<int64*>(&(*io_counters).ReadTransferCount));
253 } else if (last_key_name == "wchar") {
254 StringToInt64(tokenizer.token_piece(),
255 reinterpret_cast<int64*>(&(*io_counters).WriteTransferCount));
264 ProcessMetrics::ProcessMetrics(ProcessHandle process)
267 last_system_time_(0),
269 processor_count_ = base::SysInfo::NumberOfProcessors();
272 #if defined(OS_CHROMEOS)
273 // Private, Shared and Proportional working set sizes are obtained from
274 // /proc/<pid>/totmaps
275 bool ProcessMetrics::GetWorkingSetKBytesTotmaps(WorkingSetKBytes *ws_usage)
277 // The format of /proc/<pid>/totmaps is:
281 // Shared_Clean: 1008 kB
282 // Shared_Dirty: 4012 kB
283 // Private_Clean: 4 kB
284 // Private_Dirty: 1096 kB
285 // Referenced: XXX kB
287 // AnonHugePages: XXX kB
290 const size_t kPssIndex = (1 * 3) + 1;
291 const size_t kPrivate_CleanIndex = (4 * 3) + 1;
292 const size_t kPrivate_DirtyIndex = (5 * 3) + 1;
293 const size_t kSwapIndex = (9 * 3) + 1;
295 std::string totmaps_data;
297 FilePath totmaps_file = internal::GetProcPidDir(process_).Append("totmaps");
298 ThreadRestrictions::ScopedAllowIO allow_io;
299 bool ret = ReadFileToString(totmaps_file, &totmaps_data);
300 if (!ret || totmaps_data.length() == 0)
304 std::vector<std::string> totmaps_fields;
305 SplitStringAlongWhitespace(totmaps_data, &totmaps_fields);
307 DCHECK_EQ("Pss:", totmaps_fields[kPssIndex-1]);
308 DCHECK_EQ("Private_Clean:", totmaps_fields[kPrivate_CleanIndex - 1]);
309 DCHECK_EQ("Private_Dirty:", totmaps_fields[kPrivate_DirtyIndex - 1]);
310 DCHECK_EQ("Swap:", totmaps_fields[kSwapIndex-1]);
313 int private_clean = 0;
314 int private_dirty = 0;
317 ret &= StringToInt(totmaps_fields[kPssIndex], &pss);
318 ret &= StringToInt(totmaps_fields[kPrivate_CleanIndex], &private_clean);
319 ret &= StringToInt(totmaps_fields[kPrivate_DirtyIndex], &private_dirty);
320 ret &= StringToInt(totmaps_fields[kSwapIndex], &swap);
322 // On ChromeOS swap is to zram. We count this as private / shared, as
323 // increased swap decreases available RAM to user processes, which would
324 // otherwise create surprising results.
325 ws_usage->priv = private_clean + private_dirty + swap;
326 ws_usage->shared = pss + swap;
327 ws_usage->shareable = 0;
328 ws_usage->swapped = swap;
333 // Private and Shared working set sizes are obtained from /proc/<pid>/statm.
334 bool ProcessMetrics::GetWorkingSetKBytesStatm(WorkingSetKBytes* ws_usage)
336 // Use statm instead of smaps because smaps is:
337 // a) Large and slow to parse.
338 // b) Unavailable in the SUID sandbox.
340 // First we need to get the page size, since everything is measured in pages.
341 // For details, see: man 5 proc.
342 const int page_size_kb = getpagesize() / 1024;
343 if (page_size_kb <= 0)
348 FilePath statm_file = internal::GetProcPidDir(process_).Append("statm");
349 // Synchronously reading files in /proc is safe.
350 ThreadRestrictions::ScopedAllowIO allow_io;
351 bool ret = ReadFileToString(statm_file, &statm);
352 if (!ret || statm.length() == 0)
356 std::vector<std::string> statm_vec;
357 SplitString(statm, ' ', &statm_vec);
358 if (statm_vec.size() != 7)
359 return false; // Not the format we expect.
361 int statm_rss, statm_shared;
363 ret &= StringToInt(statm_vec[1], &statm_rss);
364 ret &= StringToInt(statm_vec[2], &statm_shared);
366 ws_usage->priv = (statm_rss - statm_shared) * page_size_kb;
367 ws_usage->shared = statm_shared * page_size_kb;
369 // Sharable is not calculated, as it does not provide interesting data.
370 ws_usage->shareable = 0;
372 #if defined(OS_CHROMEOS)
373 // Can't get swapped memory from statm.
374 ws_usage->swapped = 0;
380 size_t GetSystemCommitCharge() {
381 SystemMemoryInfoKB meminfo;
382 if (!GetSystemMemoryInfo(&meminfo))
384 return meminfo.total - meminfo.free - meminfo.buffers - meminfo.cached;
387 // Exposed for testing.
388 int ParseProcStatCPU(const std::string& input) {
389 std::vector<std::string> proc_stats;
390 if (!internal::ParseProcStats(input, &proc_stats))
393 if (proc_stats.size() <= internal::VM_STIME)
395 int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME);
396 int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME);
397 return utime + stime;
400 const char kProcSelfExe[] = "/proc/self/exe";
402 int GetNumberOfThreads(ProcessHandle process) {
403 return internal::ReadProcStatsAndGetFieldAsInt64(process,
404 internal::VM_NUMTHREADS);
409 // The format of /proc/diskstats is:
410 // Device major number
411 // Device minor number
413 // Field 1 -- # of reads completed
414 // This is the total number of reads completed successfully.
415 // Field 2 -- # of reads merged, field 6 -- # of writes merged
416 // Reads and writes which are adjacent to each other may be merged for
417 // efficiency. Thus two 4K reads may become one 8K read before it is
418 // ultimately handed to the disk, and so it will be counted (and queued)
419 // as only one I/O. This field lets you know how often this was done.
420 // Field 3 -- # of sectors read
421 // This is the total number of sectors read successfully.
422 // Field 4 -- # of milliseconds spent reading
423 // This is the total number of milliseconds spent by all reads (as
424 // measured from __make_request() to end_that_request_last()).
425 // Field 5 -- # of writes completed
426 // This is the total number of writes completed successfully.
427 // Field 6 -- # of writes merged
428 // See the description of field 2.
429 // Field 7 -- # of sectors written
430 // This is the total number of sectors written successfully.
431 // Field 8 -- # of milliseconds spent writing
432 // This is the total number of milliseconds spent by all writes (as
433 // measured from __make_request() to end_that_request_last()).
434 // Field 9 -- # of I/Os currently in progress
435 // The only field that should go to zero. Incremented as requests are
436 // given to appropriate struct request_queue and decremented as they
438 // Field 10 -- # of milliseconds spent doing I/Os
439 // This field increases so long as field 9 is nonzero.
440 // Field 11 -- weighted # of milliseconds spent doing I/Os
441 // This field is incremented at each I/O start, I/O completion, I/O
442 // merge, or read of these stats by the number of I/Os in progress
443 // (field 9) times the number of milliseconds spent doing I/O since the
444 // last update of this field. This can provide an easy measure of both
445 // I/O completion time and the backlog that may be accumulating.
447 const size_t kDiskDriveName = 2;
448 const size_t kDiskReads = 3;
449 const size_t kDiskReadsMerged = 4;
450 const size_t kDiskSectorsRead = 5;
451 const size_t kDiskReadTime = 6;
452 const size_t kDiskWrites = 7;
453 const size_t kDiskWritesMerged = 8;
454 const size_t kDiskSectorsWritten = 9;
455 const size_t kDiskWriteTime = 10;
456 const size_t kDiskIO = 11;
457 const size_t kDiskIOTime = 12;
458 const size_t kDiskWeightedIOTime = 13;
462 SystemMemoryInfoKB::SystemMemoryInfoKB() {
487 scoped_ptr<Value> SystemMemoryInfoKB::ToValue() const {
488 scoped_ptr<DictionaryValue> res(new base::DictionaryValue());
490 res->SetInteger("total", total);
491 res->SetInteger("free", free);
492 res->SetInteger("buffers", buffers);
493 res->SetInteger("cached", cached);
494 res->SetInteger("active_anon", active_anon);
495 res->SetInteger("inactive_anon", inactive_anon);
496 res->SetInteger("active_file", active_file);
497 res->SetInteger("inactive_file", inactive_file);
498 res->SetInteger("swap_total", swap_total);
499 res->SetInteger("swap_free", swap_free);
500 res->SetInteger("swap_used", swap_total - swap_free);
501 res->SetInteger("dirty", dirty);
502 res->SetInteger("pswpin", pswpin);
503 res->SetInteger("pswpout", pswpout);
504 res->SetInteger("pgmajfault", pgmajfault);
506 res->SetInteger("shmem", shmem);
507 res->SetInteger("slab", slab);
508 res->SetInteger("gem_objects", gem_objects);
509 res->SetInteger("gem_size", gem_size);
512 return res.PassAs<Value>();
515 // exposed for testing
516 bool ParseProcMeminfo(const std::string& meminfo_data,
517 SystemMemoryInfoKB* meminfo) {
518 // The format of /proc/meminfo is:
520 // MemTotal: 8235324 kB
521 // MemFree: 1628304 kB
522 // Buffers: 429596 kB
523 // Cached: 4728232 kB
525 // There is no guarantee on the ordering or position
526 // though it doesn't appear to change very often
528 // As a basic sanity check, let's make sure we at least get non-zero
532 std::vector<std::string> meminfo_lines;
533 Tokenize(meminfo_data, "\n", &meminfo_lines);
534 for (std::vector<std::string>::iterator it = meminfo_lines.begin();
535 it != meminfo_lines.end(); ++it) {
536 std::vector<std::string> tokens;
537 SplitStringAlongWhitespace(*it, &tokens);
538 // HugePages_* only has a number and no suffix so we can't rely on
539 // there being exactly 3 tokens.
540 if (tokens.size() > 1) {
541 if (tokens[0] == "MemTotal:") {
542 StringToInt(tokens[1], &meminfo->total);
544 } if (tokens[0] == "MemFree:") {
545 StringToInt(tokens[1], &meminfo->free);
547 } if (tokens[0] == "Buffers:") {
548 StringToInt(tokens[1], &meminfo->buffers);
550 } if (tokens[0] == "Cached:") {
551 StringToInt(tokens[1], &meminfo->cached);
553 } if (tokens[0] == "Active(anon):") {
554 StringToInt(tokens[1], &meminfo->active_anon);
556 } if (tokens[0] == "Inactive(anon):") {
557 StringToInt(tokens[1], &meminfo->inactive_anon);
559 } if (tokens[0] == "Active(file):") {
560 StringToInt(tokens[1], &meminfo->active_file);
562 } if (tokens[0] == "Inactive(file):") {
563 StringToInt(tokens[1], &meminfo->inactive_file);
565 } if (tokens[0] == "SwapTotal:") {
566 StringToInt(tokens[1], &meminfo->swap_total);
568 } if (tokens[0] == "SwapFree:") {
569 StringToInt(tokens[1], &meminfo->swap_free);
571 } if (tokens[0] == "Dirty:") {
572 StringToInt(tokens[1], &meminfo->dirty);
574 #if defined(OS_CHROMEOS)
575 // Chrome OS has a tweaked kernel that allows us to query Shmem, which is
576 // usually video memory otherwise invisible to the OS.
577 } if (tokens[0] == "Shmem:") {
578 StringToInt(tokens[1], &meminfo->shmem);
580 } if (tokens[0] == "Slab:") {
581 StringToInt(tokens[1], &meminfo->slab);
586 DLOG(WARNING) << "meminfo: tokens: " << tokens.size()
587 << " malformed line: " << *it;
590 // Make sure we got a valid MemTotal.
597 // exposed for testing
598 bool ParseProcVmstat(const std::string& vmstat_data,
599 SystemMemoryInfoKB* meminfo) {
600 // The format of /proc/vmstat is:
602 // nr_free_pages 299878
603 // nr_inactive_anon 239863
604 // nr_active_anon 1318966
605 // nr_inactive_file 2015629
608 // We iterate through the whole file because the position of the
609 // fields are dependent on the kernel version and configuration.
611 std::vector<std::string> vmstat_lines;
612 Tokenize(vmstat_data, "\n", &vmstat_lines);
613 for (std::vector<std::string>::iterator it = vmstat_lines.begin();
614 it != vmstat_lines.end(); ++it) {
615 std::vector<std::string> tokens;
616 SplitString(*it, ' ', &tokens);
617 if (tokens.size() == 2) {
618 if (tokens[0] == "pswpin") {
619 StringToInt(tokens[1], &meminfo->pswpin);
621 } if (tokens[0] == "pswpout") {
622 StringToInt(tokens[1], &meminfo->pswpout);
624 } if (tokens[0] == "pgmajfault")
625 StringToInt(tokens[1], &meminfo->pgmajfault);
632 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
633 // Synchronously reading files in /proc is safe.
634 ThreadRestrictions::ScopedAllowIO allow_io;
636 // Used memory is: total - free - buffers - caches
637 FilePath meminfo_file("/proc/meminfo");
638 std::string meminfo_data;
639 if (!ReadFileToString(meminfo_file, &meminfo_data)) {
640 DLOG(WARNING) << "Failed to open " << meminfo_file.value();
644 if (!ParseProcMeminfo(meminfo_data, meminfo)) {
645 DLOG(WARNING) << "Failed to parse " << meminfo_file.value();
649 #if defined(OS_CHROMEOS)
650 // Report on Chrome OS GEM object graphics memory. /var/run/debugfs_gpu is a
651 // bind mount into /sys/kernel/debug and synchronously reading the in-memory
652 // files in /sys is fast.
653 #if defined(ARCH_CPU_ARM_FAMILY)
654 FilePath geminfo_file("/var/run/debugfs_gpu/exynos_gem_objects");
656 FilePath geminfo_file("/var/run/debugfs_gpu/i915_gem_objects");
658 std::string geminfo_data;
659 meminfo->gem_objects = -1;
660 meminfo->gem_size = -1;
661 if (ReadFileToString(geminfo_file, &geminfo_data)) {
662 int gem_objects = -1;
663 long long gem_size = -1;
664 int num_res = sscanf(geminfo_data.c_str(),
665 "%d objects, %lld bytes",
666 &gem_objects, &gem_size);
668 meminfo->gem_objects = gem_objects;
669 meminfo->gem_size = gem_size;
673 #if defined(ARCH_CPU_ARM_FAMILY)
674 // Incorporate Mali graphics memory if present.
675 FilePath mali_memory_file("/sys/class/misc/mali0/device/memory");
676 std::string mali_memory_data;
677 if (ReadFileToString(mali_memory_file, &mali_memory_data)) {
678 long long mali_size = -1;
679 int num_res = sscanf(mali_memory_data.c_str(), "%lld bytes", &mali_size);
681 meminfo->gem_size += mali_size;
683 #endif // defined(ARCH_CPU_ARM_FAMILY)
684 #endif // defined(OS_CHROMEOS)
686 FilePath vmstat_file("/proc/vmstat");
687 std::string vmstat_data;
688 if (!ReadFileToString(vmstat_file, &vmstat_data)) {
689 DLOG(WARNING) << "Failed to open " << vmstat_file.value();
692 if (!ParseProcVmstat(vmstat_data, meminfo)) {
693 DLOG(WARNING) << "Failed to parse " << vmstat_file.value();
700 SystemDiskInfo::SystemDiskInfo() {
711 weighted_io_time = 0;
714 scoped_ptr<Value> SystemDiskInfo::ToValue() const {
715 scoped_ptr<DictionaryValue> res(new base::DictionaryValue());
717 // Write out uint64 variables as doubles.
718 // Note: this may discard some precision, but for JS there's no other option.
719 res->SetDouble("reads", static_cast<double>(reads));
720 res->SetDouble("reads_merged", static_cast<double>(reads_merged));
721 res->SetDouble("sectors_read", static_cast<double>(sectors_read));
722 res->SetDouble("read_time", static_cast<double>(read_time));
723 res->SetDouble("writes", static_cast<double>(writes));
724 res->SetDouble("writes_merged", static_cast<double>(writes_merged));
725 res->SetDouble("sectors_written", static_cast<double>(sectors_written));
726 res->SetDouble("write_time", static_cast<double>(write_time));
727 res->SetDouble("io", static_cast<double>(io));
728 res->SetDouble("io_time", static_cast<double>(io_time));
729 res->SetDouble("weighted_io_time", static_cast<double>(weighted_io_time));
731 return res.PassAs<Value>();
734 bool IsValidDiskName(const std::string& candidate) {
735 if (candidate.length() < 3)
737 if (candidate.substr(0,2) == "sd" || candidate.substr(0,2) == "hd") {
739 for (size_t i = 2; i < candidate.length(); i++) {
740 if (!islower(candidate[i]))
744 if (candidate.length() < 7) {
747 if (candidate.substr(0,6) == "mmcblk") {
749 for (size_t i = 6; i < candidate.length(); i++) {
750 if (!isdigit(candidate[i]))
761 bool GetSystemDiskInfo(SystemDiskInfo* diskinfo) {
762 // Synchronously reading files in /proc is safe.
763 ThreadRestrictions::ScopedAllowIO allow_io;
765 FilePath diskinfo_file("/proc/diskstats");
766 std::string diskinfo_data;
767 if (!ReadFileToString(diskinfo_file, &diskinfo_data)) {
768 DLOG(WARNING) << "Failed to open " << diskinfo_file.value();
772 std::vector<std::string> diskinfo_lines;
773 size_t line_count = Tokenize(diskinfo_data, "\n", &diskinfo_lines);
774 if (line_count == 0) {
775 DLOG(WARNING) << "No lines found";
780 diskinfo->reads_merged = 0;
781 diskinfo->sectors_read = 0;
782 diskinfo->read_time = 0;
783 diskinfo->writes = 0;
784 diskinfo->writes_merged = 0;
785 diskinfo->sectors_written = 0;
786 diskinfo->write_time = 0;
788 diskinfo->io_time = 0;
789 diskinfo->weighted_io_time = 0;
792 uint64 reads_merged = 0;
793 uint64 sectors_read = 0;
794 uint64 read_time = 0;
796 uint64 writes_merged = 0;
797 uint64 sectors_written = 0;
798 uint64 write_time = 0;
801 uint64 weighted_io_time = 0;
803 for (size_t i = 0; i < line_count; i++) {
804 std::vector<std::string> disk_fields;
805 SplitStringAlongWhitespace(diskinfo_lines[i], &disk_fields);
807 // Fields may have overflowed and reset to zero.
808 if (IsValidDiskName(disk_fields[kDiskDriveName])) {
809 StringToUint64(disk_fields[kDiskReads], &reads);
810 StringToUint64(disk_fields[kDiskReadsMerged], &reads_merged);
811 StringToUint64(disk_fields[kDiskSectorsRead], §ors_read);
812 StringToUint64(disk_fields[kDiskReadTime], &read_time);
813 StringToUint64(disk_fields[kDiskWrites], &writes);
814 StringToUint64(disk_fields[kDiskWritesMerged], &writes_merged);
815 StringToUint64(disk_fields[kDiskSectorsWritten], §ors_written);
816 StringToUint64(disk_fields[kDiskWriteTime], &write_time);
817 StringToUint64(disk_fields[kDiskIO], &io);
818 StringToUint64(disk_fields[kDiskIOTime], &io_time);
819 StringToUint64(disk_fields[kDiskWeightedIOTime], &weighted_io_time);
821 diskinfo->reads += reads;
822 diskinfo->reads_merged += reads_merged;
823 diskinfo->sectors_read += sectors_read;
824 diskinfo->read_time += read_time;
825 diskinfo->writes += writes;
826 diskinfo->writes_merged += writes_merged;
827 diskinfo->sectors_written += sectors_written;
828 diskinfo->write_time += write_time;
830 diskinfo->io_time += io_time;
831 diskinfo->weighted_io_time += weighted_io_time;
838 #if defined(OS_CHROMEOS)
839 scoped_ptr<Value> SwapInfo::ToValue() const {
840 scoped_ptr<DictionaryValue> res(new DictionaryValue());
842 // Write out uint64 variables as doubles.
843 // Note: this may discard some precision, but for JS there's no other option.
844 res->SetDouble("num_reads", static_cast<double>(num_reads));
845 res->SetDouble("num_writes", static_cast<double>(num_writes));
846 res->SetDouble("orig_data_size", static_cast<double>(orig_data_size));
847 res->SetDouble("compr_data_size", static_cast<double>(compr_data_size));
848 res->SetDouble("mem_used_total", static_cast<double>(mem_used_total));
849 if (compr_data_size > 0)
850 res->SetDouble("compression_ratio", static_cast<double>(orig_data_size) /
851 static_cast<double>(compr_data_size));
853 res->SetDouble("compression_ratio", 0);
855 return res.PassAs<Value>();
858 void GetSwapInfo(SwapInfo* swap_info) {
859 // Synchronously reading files in /sys/block/zram0 is safe.
860 ThreadRestrictions::ScopedAllowIO allow_io;
862 base::FilePath zram_path("/sys/block/zram0");
863 uint64 orig_data_size = ReadFileToUint64(zram_path.Append("orig_data_size"));
864 if (orig_data_size <= 4096) {
865 // A single page is compressed at startup, and has a high compression
866 // ratio. We ignore this as it doesn't indicate any real swapping.
867 swap_info->orig_data_size = 0;
868 swap_info->num_reads = 0;
869 swap_info->num_writes = 0;
870 swap_info->compr_data_size = 0;
871 swap_info->mem_used_total = 0;
874 swap_info->orig_data_size = orig_data_size;
875 swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads"));
876 swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes"));
877 swap_info->compr_data_size =
878 ReadFileToUint64(zram_path.Append("compr_data_size"));
879 swap_info->mem_used_total =
880 ReadFileToUint64(zram_path.Append("mem_used_total"));
882 #endif // defined(OS_CHROMEOS)