Index: base/process/process_metrics_linux.cc |
diff --git a/base/process/process_metrics_linux.cc b/base/process/process_metrics_linux.cc |
index be166e33e322959bb7d10111e0746dbe5680c27f..9467ae8a54a9312b90ead46a0d9928842c173143 100644 |
--- a/base/process/process_metrics_linux.cc |
+++ b/base/process/process_metrics_linux.cc |
@@ -76,6 +76,38 @@ size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) { |
return 0; |
} |
+// Read /proc/<pid>/sched and look for |field|. On succes, return true and |
+// write the value for |field| into |result|. |
+// Only works for fields in the form of "field : uint_value" |
+bool ReadProcSchedAndGetFieldAsUint64(pid_t pid, |
+ const std::string& field, |
+ uint64* result) { |
+ std::string sched_data; |
+ { |
+ // Synchronously reading files in /proc is safe. |
Nico
2014/09/07 00:10:57
"safe" isn't why we have this, it's about speed. I
Lei Zhang
2014/09/09 06:13:14
Filed all over. It's copy+pasted from elsewhere in
|
+ ThreadRestrictions::ScopedAllowIO allow_io; |
+ FilePath sched_file = internal::GetProcPidDir(pid).Append("sched"); |
+ if (!ReadFileToString(sched_file, &sched_data)) |
+ return false; |
+ } |
+ |
+ std::vector<std::pair<std::string, std::string> > pairs; |
+ SplitStringIntoKeyValuePairs(sched_data, ':', '\n', &pairs); |
+ for (size_t i = 0; i < pairs.size(); ++i) { |
+ std::string key, value_str; |
+ TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key); |
+ TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value_str); |
Nico
2014/09/07 00:10:57
This looks pretty similar to line 72 of https://co
Lei Zhang
2014/09/09 06:13:14
Done.
|
+ if (key == field) { |
+ uint64 value; |
+ if (!StringToUint64(value_str, &value)) |
+ return false; |
+ *result = value; |
+ return true; |
+ } |
+ } |
+ return false; |
+} |
+ |
// Get the total CPU of a single process. Return value is number of jiffies |
// on success or -1 on error. |
int GetProcessCPU(pid_t pid) { |
@@ -234,6 +266,7 @@ bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { |
ProcessMetrics::ProcessMetrics(ProcessHandle process) |
: process_(process), |
last_system_time_(0), |
+ last_absolute_idle_wakeups_(0), |
last_cpu_(0) { |
processor_count_ = SysInfo::NumberOfProcessors(); |
} |
@@ -839,4 +872,11 @@ void GetSwapInfo(SwapInfo* swap_info) { |
} |
#endif // defined(OS_CHROMEOS) |
+int ProcessMetrics::GetIdleWakeupsPerSecond() { |
+ uint64 wake_ups; |
+ const char kWakeupStat[] = "se.statistics.nr_wakeups"; |
+ return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? |
+ CalculateIdleWakeupsPerSecond(wake_ups) : 0; |
+} |
+ |
} // namespace base |