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

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: Remove the trace_event changes. 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
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 CHECK(ParseProcStatusAndGetField(status, field, &value));
Lei Zhang 2015/10/07 16:10:54 CHECK() is a bit strong. Any project that uses thi
ssid 2015/10/07 17:11:34 Sorry I beleived NOTREACHED was CHECK. Just realiz
64 SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs); 64 return value;
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;
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 } 65 }
87 66
88 #if defined(OS_LINUX) 67 #if defined(OS_LINUX)
89 // Read /proc/<pid>/sched and look for |field|. On succes, return true and 68 // Read /proc/<pid>/sched and look for |field|. On succes, return true and
90 // write the value for |field| into |result|. 69 // write the value for |field| into |result|.
91 // Only works for fields in the form of "field : uint_value" 70 // Only works for fields in the form of "field : uint_value"
92 bool ReadProcSchedAndGetFieldAsUint64(pid_t pid, 71 bool ReadProcSchedAndGetFieldAsUint64(pid_t pid,
93 const std::string& field, 72 const std::string& field,
94 uint64* result) { 73 uint64* result) {
95 std::string sched_data; 74 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) 404 if (sscanf(&input.data()[i], "%d %d", &utime, &stime) != 2)
426 return -1; 405 return -1;
427 406
428 return utime + stime; 407 return utime + stime;
429 } 408 }
430 } 409 }
431 410
432 return -1; 411 return -1;
433 } 412 }
434 413
414 bool ParseProcStatusAndGetField(const std::string& proc_status_contents,
415 const std::string& field,
416 size_t* value) {
417 StringPairs pairs;
418 SplitStringIntoKeyValuePairs(proc_status_contents, ':', '\n', &pairs);
419 TrimKeyValuePairs(&pairs);
420 for (size_t i = 0; i < pairs.size(); ++i) {
421 const std::string& key = pairs[i].first;
422 const std::string& value_str = pairs[i].second;
423 if (key == field) {
424 std::vector<StringPiece> split_value_str = SplitStringPiece(
425 value_str, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
426 if (split_value_str.size() != 2 || split_value_str[1] != "kB") {
427 return false;
428 }
429 if (StringToSizeT(split_value_str[0], value)) {
430 *value *= 1024;
431 return true;
432 }
433 return false;
434 }
435 }
436 return false;
437 }
438
435 const char kProcSelfExe[] = "/proc/self/exe"; 439 const char kProcSelfExe[] = "/proc/self/exe";
436 440
437 int GetNumberOfThreads(ProcessHandle process) { 441 int GetNumberOfThreads(ProcessHandle process) {
438 return internal::ReadProcStatsAndGetFieldAsInt64(process, 442 return internal::ReadProcStatsAndGetFieldAsInt64(process,
439 internal::VM_NUMTHREADS); 443 internal::VM_NUMTHREADS);
440 } 444 }
441 445
442 namespace { 446 namespace {
443 447
444 // The format of /proc/diskstats is: 448 // The format of /proc/diskstats is:
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 #if defined(OS_LINUX) 908 #if defined(OS_LINUX)
905 int ProcessMetrics::GetIdleWakeupsPerSecond() { 909 int ProcessMetrics::GetIdleWakeupsPerSecond() {
906 uint64 wake_ups; 910 uint64 wake_ups;
907 const char kWakeupStat[] = "se.statistics.nr_wakeups"; 911 const char kWakeupStat[] = "se.statistics.nr_wakeups";
908 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? 912 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ?
909 CalculateIdleWakeupsPerSecond(wake_ups) : 0; 913 CalculateIdleWakeupsPerSecond(wake_ups) : 0;
910 } 914 }
911 #endif // defined(OS_LINUX) 915 #endif // defined(OS_LINUX)
912 916
913 } // namespace base 917 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698