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 |
412 return utime + stime; | 413 // last ')'. |
| 414 int num_spaces_remaining = internal::VM_UTIME - 1; |
| 415 |
| 416 size_t i = start; |
| 417 while ((i = input.find(' ', i + 1)) != input.npos) { |
| 418 // Validate the assumption that there aren't any contiguous spaces |
| 419 // in |input| before utime. |
| 420 DCHECK_NE(input[i - 1], ' '); |
| 421 if (--num_spaces_remaining == 0) { |
| 422 int utime = 0; |
| 423 int stime = 0; |
| 424 if (sscanf(&input.data()[i], "%d %d", &utime, &stime) != 2) |
| 425 return -1; |
| 426 |
| 427 return utime + stime; |
| 428 } |
| 429 } |
| 430 |
| 431 return -1; |
413 } | 432 } |
414 | 433 |
415 const char kProcSelfExe[] = "/proc/self/exe"; | 434 const char kProcSelfExe[] = "/proc/self/exe"; |
416 | 435 |
417 int GetNumberOfThreads(ProcessHandle process) { | 436 int GetNumberOfThreads(ProcessHandle process) { |
418 return internal::ReadProcStatsAndGetFieldAsInt64(process, | 437 return internal::ReadProcStatsAndGetFieldAsInt64(process, |
419 internal::VM_NUMTHREADS); | 438 internal::VM_NUMTHREADS); |
420 } | 439 } |
421 | 440 |
422 namespace { | 441 namespace { |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 #if defined(OS_LINUX) | 907 #if defined(OS_LINUX) |
889 int ProcessMetrics::GetIdleWakeupsPerSecond() { | 908 int ProcessMetrics::GetIdleWakeupsPerSecond() { |
890 uint64 wake_ups; | 909 uint64 wake_ups; |
891 const char kWakeupStat[] = "se.statistics.nr_wakeups"; | 910 const char kWakeupStat[] = "se.statistics.nr_wakeups"; |
892 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? | 911 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? |
893 CalculateIdleWakeupsPerSecond(wake_ups) : 0; | 912 CalculateIdleWakeupsPerSecond(wake_ups) : 0; |
894 } | 913 } |
895 #endif // defined(OS_LINUX) | 914 #endif // defined(OS_LINUX) |
896 | 915 |
897 } // namespace base | 916 } // namespace base |
OLD | NEW |