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

Side by Side Diff: base/process/process_metrics_linux.cc

Issue 888193002: ProcessMetrics::GetCPUUsage() is Slow (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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
« no previous file with comments | « no previous file | base/process/process_metrics_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 if (input[i - 1] != ' ' && --num_spaces_remaining == 0) {
danakj 2015/02/04 18:33:06 can you leave a comment like // Skips past contigu
afakhry 2015/02/04 19:05:20 It's unlikey but it's better be safe. The original
420 utime_start_index = i;
421 break;
422 }
423 }
424
425 if (num_spaces_remaining != 0)
danakj 2015/02/04 18:33:06 nit: maybe slightly more obvious to check utime_st
afakhry 2015/02/04 19:05:20 Done.
426 return -1;
427
428 int utime = 0;
429 int stime = 0;
430 if (sscanf(&input.data()[utime_start_index], "%d %d", &utime, &stime) != 2)
431 return -1;
432
412 return utime + stime; 433 return utime + stime;
413 } 434 }
414 435
415 const char kProcSelfExe[] = "/proc/self/exe"; 436 const char kProcSelfExe[] = "/proc/self/exe";
416 437
417 int GetNumberOfThreads(ProcessHandle process) { 438 int GetNumberOfThreads(ProcessHandle process) {
418 return internal::ReadProcStatsAndGetFieldAsInt64(process, 439 return internal::ReadProcStatsAndGetFieldAsInt64(process,
419 internal::VM_NUMTHREADS); 440 internal::VM_NUMTHREADS);
420 } 441 }
421 442
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after
888 #if defined(OS_LINUX) 909 #if defined(OS_LINUX)
889 int ProcessMetrics::GetIdleWakeupsPerSecond() { 910 int ProcessMetrics::GetIdleWakeupsPerSecond() {
890 uint64 wake_ups; 911 uint64 wake_ups;
891 const char kWakeupStat[] = "se.statistics.nr_wakeups"; 912 const char kWakeupStat[] = "se.statistics.nr_wakeups";
892 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? 913 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ?
893 CalculateIdleWakeupsPerSecond(wake_ups) : 0; 914 CalculateIdleWakeupsPerSecond(wake_ups) : 0;
894 } 915 }
895 #endif // defined(OS_LINUX) 916 #endif // defined(OS_LINUX)
896 917
897 } // namespace base 918 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/process/process_metrics_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698