Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: base/process/process_metrics_linux.cc

Issue 1384363002: [NOT TO COMMIT YET] Using proc/pid/status file contents to get process metrics in linux. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@smaps_fscan
Patch Set: Fixes. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/process/process_metrics.h ('k') | base/process/process_metrics_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/process/process_metrics.h" 5 #include "base/process/process_metrics.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 #include <sys/time.h> 10 #include <sys/time.h>
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 // Only works for fields in the form of "Field: value kB". 52 // Only works for fields in the form of "Field: value kB".
53 size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) { 53 size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) {
54 std::string status; 54 std::string status;
55 { 55 {
56 // Synchronously reading files in /proc does not hit the disk. 56 // Synchronously reading files in /proc does not hit the disk.
57 ThreadRestrictions::ScopedAllowIO allow_io; 57 ThreadRestrictions::ScopedAllowIO allow_io;
58 FilePath stat_file = internal::GetProcPidDir(pid).Append("status"); 58 FilePath stat_file = internal::GetProcPidDir(pid).Append("status");
59 if (!ReadFileToString(stat_file, &status)) 59 if (!ReadFileToString(stat_file, &status))
60 return 0; 60 return 0;
61 } 61 }
62 62 size_t value;
63 StringPairs pairs; 63 bool res = ParseProcStatusAndGetField(status, field, &value);
64 SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs); 64 DCHECK(res);
65 TrimKeyValuePairs(&pairs); 65 return value;
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;
75 }
76 size_t value;
77 if (!StringToSizeT(split_value_str[0], &value)) {
78 NOTREACHED();
79 return 0;
80 }
81 return value;
82 }
83 }
84 NOTREACHED();
85 return 0;
86 } 66 }
87 67
88 #if defined(OS_LINUX) 68 #if defined(OS_LINUX)
89 // Read /proc/<pid>/sched and look for |field|. On succes, return true and 69 // Read /proc/<pid>/sched and look for |field|. On succes, return true and
90 // write the value for |field| into |result|. 70 // write the value for |field| into |result|.
91 // Only works for fields in the form of "field : uint_value" 71 // Only works for fields in the form of "field : uint_value"
92 bool ReadProcSchedAndGetFieldAsUint64(pid_t pid, 72 bool ReadProcSchedAndGetFieldAsUint64(pid_t pid,
93 const std::string& field, 73 const std::string& field,
94 uint64* result) { 74 uint64* result) {
95 std::string sched_data; 75 std::string sched_data;
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 if (sscanf(&input.data()[i], "%d %d", &utime, &stime) != 2) 405 if (sscanf(&input.data()[i], "%d %d", &utime, &stime) != 2)
426 return -1; 406 return -1;
427 407
428 return utime + stime; 408 return utime + stime;
429 } 409 }
430 } 410 }
431 411
432 return -1; 412 return -1;
433 } 413 }
434 414
415 bool ParseProcStatusAndGetField(const StringPiece& proc_status_contents,
416 const std::string& field,
417 size_t* value) {
418 StringPairs pairs;
419 SplitStringIntoKeyValuePairs(proc_status_contents, ':', '\n', &pairs);
420 TrimKeyValuePairs(&pairs);
421 for (size_t i = 0; i < pairs.size(); ++i) {
422 const std::string& key = pairs[i].first;
423 const std::string& value_str = pairs[i].second;
424 if (key == field) {
425 std::vector<StringPiece> split_value_str = SplitStringPiece(
426 value_str, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
427 if (split_value_str.size() != 2 || split_value_str[1] != "kB") {
428 return false;
429 }
430 if (StringToSizeT(split_value_str[0], value)) {
431 *value *= 1024;
432 return true;
433 }
434 return false;
435 }
436 }
437 return false;
438 }
439
435 const char kProcSelfExe[] = "/proc/self/exe"; 440 const char kProcSelfExe[] = "/proc/self/exe";
436 441
437 int GetNumberOfThreads(ProcessHandle process) { 442 int GetNumberOfThreads(ProcessHandle process) {
438 return internal::ReadProcStatsAndGetFieldAsInt64(process, 443 return internal::ReadProcStatsAndGetFieldAsInt64(process,
439 internal::VM_NUMTHREADS); 444 internal::VM_NUMTHREADS);
440 } 445 }
441 446
442 namespace { 447 namespace {
443 448
444 // The format of /proc/diskstats is: 449 // The format of /proc/diskstats is:
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 #if defined(OS_LINUX) 909 #if defined(OS_LINUX)
905 int ProcessMetrics::GetIdleWakeupsPerSecond() { 910 int ProcessMetrics::GetIdleWakeupsPerSecond() {
906 uint64 wake_ups; 911 uint64 wake_ups;
907 const char kWakeupStat[] = "se.statistics.nr_wakeups"; 912 const char kWakeupStat[] = "se.statistics.nr_wakeups";
908 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? 913 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ?
909 CalculateIdleWakeupsPerSecond(wake_ups) : 0; 914 CalculateIdleWakeupsPerSecond(wake_ups) : 0;
910 } 915 }
911 #endif // defined(OS_LINUX) 916 #endif // defined(OS_LINUX)
912 917
913 } // namespace base 918 } // namespace base
OLDNEW
« no previous file with comments | « base/process/process_metrics.h ('k') | base/process/process_metrics_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698