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 // |input| may be empty if the process disappeared somehow. |
405 if (!internal::ParseProcStats(input, &proc_stats)) | 404 // e.g. http://crbug.com/145811. |
405 if (input.empty()) | |
406 return -1; | 406 return -1; |
407 | 407 |
408 if (proc_stats.size() <= internal::VM_STIME) | 408 size_t start = input.find_last_of(')'); |
409 if (start == input.npos) | |
409 return -1; | 410 return -1; |
410 int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME); | 411 |
411 int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME); | 412 // Number of spaces remaining until reaching utime's index starting after the |
413 // last ')'. | |
414 int num_spaces_remaining = internal::VM_UTIME - 1; | |
415 int utime_start_index = 0; | |
416 | |
417 size_t i = start; | |
418 while ((i = input.find(' ', i + 1)) != input.npos) { | |
419 // Validate the assumption that there aren't any contiguous spaces | |
420 // in |input| before utime. | |
421 DCHECK_NE(input[i - 1], ' '); | |
422 if (--num_spaces_remaining == 0) { | |
423 utime_start_index = i; | |
danakj
2015/02/04 19:52:44
cool thanks. one more comment, i think we could sa
afakhry
2015/02/04 20:07:05
I like that! :) Done!
| |
424 break; | |
425 } | |
426 } | |
427 | |
428 if (utime_start_index == 0) | |
429 return -1; | |
430 | |
431 int utime = 0; | |
432 int stime = 0; | |
433 if (sscanf(&input.data()[utime_start_index], "%d %d", &utime, &stime) != 2) | |
434 return -1; | |
435 | |
412 return utime + stime; | 436 return utime + stime; |
413 } | 437 } |
414 | 438 |
415 const char kProcSelfExe[] = "/proc/self/exe"; | 439 const char kProcSelfExe[] = "/proc/self/exe"; |
416 | 440 |
417 int GetNumberOfThreads(ProcessHandle process) { | 441 int GetNumberOfThreads(ProcessHandle process) { |
418 return internal::ReadProcStatsAndGetFieldAsInt64(process, | 442 return internal::ReadProcStatsAndGetFieldAsInt64(process, |
419 internal::VM_NUMTHREADS); | 443 internal::VM_NUMTHREADS); |
420 } | 444 } |
421 | 445 |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
888 #if defined(OS_LINUX) | 912 #if defined(OS_LINUX) |
889 int ProcessMetrics::GetIdleWakeupsPerSecond() { | 913 int ProcessMetrics::GetIdleWakeupsPerSecond() { |
890 uint64 wake_ups; | 914 uint64 wake_ups; |
891 const char kWakeupStat[] = "se.statistics.nr_wakeups"; | 915 const char kWakeupStat[] = "se.statistics.nr_wakeups"; |
892 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? | 916 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? |
893 CalculateIdleWakeupsPerSecond(wake_ups) : 0; | 917 CalculateIdleWakeupsPerSecond(wake_ups) : 0; |
894 } | 918 } |
895 #endif // defined(OS_LINUX) | 919 #endif // defined(OS_LINUX) |
896 | 920 |
897 } // namespace base | 921 } // namespace base |
OLD | NEW |