OLD | NEW |
---|---|
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 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
392 return ret; | 392 return ret; |
393 } | 393 } |
394 | 394 |
395 size_t GetSystemCommitCharge() { | 395 size_t GetSystemCommitCharge() { |
396 SystemMemoryInfoKB meminfo; | 396 SystemMemoryInfoKB meminfo; |
397 if (!GetSystemMemoryInfo(&meminfo)) | 397 if (!GetSystemMemoryInfo(&meminfo)) |
398 return 0; | 398 return 0; |
399 return meminfo.total - meminfo.free - meminfo.buffers - meminfo.cached; | 399 return meminfo.total - meminfo.free - meminfo.buffers - meminfo.cached; |
400 } | 400 } |
401 | 401 |
402 // Exposed for testing. | |
403 int ParseProcStatCPU(const std::string& input) { | 402 int ParseProcStatCPU(const std::string& input) { |
404 std::vector<std::string> proc_stats; | 403 if (input.empty()) |
405 if (!internal::ParseProcStats(input, &proc_stats)) | |
406 return -1; | 404 return -1; |
407 | 405 |
408 if (proc_stats.size() <= internal::VM_STIME) | 406 int j = 0; |
407 int last_space_index = 0; | |
408 int utime_start_index = 0; | |
409 bool prev_was_space = false; | |
410 for (auto& ch : input) { | |
411 if (ch == ' ') { | |
412 if (!prev_was_space) | |
413 ++last_space_index; | |
414 | |
415 prev_was_space = true; | |
416 } else { | |
417 if (ch == ')') { | |
418 // Restart the counter, we're looking to count from the last ')' we | |
danakj
2015/02/03 20:35:51
Can you use input.rfind to get the last ')'?
This
afakhry
2015/02/03 21:21:59
I understand the need for readability. But this is
| |
419 // encounter. | |
420 last_space_index = 0; | |
421 } else if (prev_was_space && last_space_index == internal::VM_UTIME - 2) { | |
422 utime_start_index = j + 1; | |
423 break; | |
424 } | |
425 prev_was_space = false; | |
426 } | |
427 ++j; | |
428 } | |
429 | |
430 if (last_space_index < internal::VM_UTIME - 2) | |
409 return -1; | 431 return -1; |
410 int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME); | 432 |
411 int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME); | 433 int utime = 0; |
434 int stime = 0; | |
435 if (sscanf(&input.data()[utime_start_index], "%d %d", &utime, &stime) != 2) | |
436 return -1; | |
437 | |
412 return utime + stime; | 438 return utime + stime; |
413 } | 439 } |
414 | 440 |
415 const char kProcSelfExe[] = "/proc/self/exe"; | 441 const char kProcSelfExe[] = "/proc/self/exe"; |
416 | 442 |
417 int GetNumberOfThreads(ProcessHandle process) { | 443 int GetNumberOfThreads(ProcessHandle process) { |
418 return internal::ReadProcStatsAndGetFieldAsInt64(process, | 444 return internal::ReadProcStatsAndGetFieldAsInt64(process, |
419 internal::VM_NUMTHREADS); | 445 internal::VM_NUMTHREADS); |
420 } | 446 } |
421 | 447 |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
888 #if defined(OS_LINUX) | 914 #if defined(OS_LINUX) |
889 int ProcessMetrics::GetIdleWakeupsPerSecond() { | 915 int ProcessMetrics::GetIdleWakeupsPerSecond() { |
890 uint64 wake_ups; | 916 uint64 wake_ups; |
891 const char kWakeupStat[] = "se.statistics.nr_wakeups"; | 917 const char kWakeupStat[] = "se.statistics.nr_wakeups"; |
892 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? | 918 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? |
893 CalculateIdleWakeupsPerSecond(wake_ups) : 0; | 919 CalculateIdleWakeupsPerSecond(wake_ups) : 0; |
894 } | 920 } |
895 #endif // defined(OS_LINUX) | 921 #endif // defined(OS_LINUX) |
896 | 922 |
897 } // namespace base | 923 } // namespace base |
OLD | NEW |