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

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 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 last_space_index = 0;
407 int utime_start_index = 0;
408 bool prev_was_space = false;
409 auto start = input.rfind(')');
danakj 2015/02/04 00:19:11 Thanks. Did you consider a regular expression for
410 for (auto j = start; j < input.size(); ++j) {
411 if (input[j] == ' ') {
412 if (!prev_was_space)
413 ++last_space_index;
414
415 prev_was_space = true;
416 } else {
417 if (prev_was_space && last_space_index == internal::VM_UTIME - 2) {
418 utime_start_index = j + 1;
419 break;
420 }
421
422 prev_was_space = false;
423 }
424 }
425
426 if (last_space_index < internal::VM_UTIME - 2)
409 return -1; 427 return -1;
410 int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME); 428
411 int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME); 429 int utime = 0;
430 int stime = 0;
431 if (sscanf(&input.data()[utime_start_index], "%d %d", &utime, &stime) != 2)
432 return -1;
433
412 return utime + stime; 434 return utime + stime;
413 } 435 }
414 436
415 const char kProcSelfExe[] = "/proc/self/exe"; 437 const char kProcSelfExe[] = "/proc/self/exe";
416 438
417 int GetNumberOfThreads(ProcessHandle process) { 439 int GetNumberOfThreads(ProcessHandle process) {
418 return internal::ReadProcStatsAndGetFieldAsInt64(process, 440 return internal::ReadProcStatsAndGetFieldAsInt64(process,
419 internal::VM_NUMTHREADS); 441 internal::VM_NUMTHREADS);
420 } 442 }
421 443
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after
888 #if defined(OS_LINUX) 910 #if defined(OS_LINUX)
889 int ProcessMetrics::GetIdleWakeupsPerSecond() { 911 int ProcessMetrics::GetIdleWakeupsPerSecond() {
890 uint64 wake_ups; 912 uint64 wake_ups;
891 const char kWakeupStat[] = "se.statistics.nr_wakeups"; 913 const char kWakeupStat[] = "se.statistics.nr_wakeups";
892 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? 914 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ?
893 CalculateIdleWakeupsPerSecond(wake_ups) : 0; 915 CalculateIdleWakeupsPerSecond(wake_ups) : 0;
894 } 916 }
895 #endif // defined(OS_LINUX) 917 #endif // defined(OS_LINUX)
896 918
897 } // namespace base 919 } // 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