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

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: This is probably a more readable version 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())
danakj 2015/02/04 01:29:18 Can you leave a comment like ParseProcStats? // |
afakhry 2015/02/04 02:31:22 Done.
405 if (!internal::ParseProcStats(input, &proc_stats))
406 return -1; 404 return -1;
407 405
408 if (proc_stats.size() <= internal::VM_STIME) 406 // The index of the last space before 'utime', taking the space after the last
407 // ')' as the beginning.
408 int last_space_index = internal::VM_UTIME - 1;
danakj 2015/02/04 01:29:18 why do you call this index? they seem like counts,
afakhry 2015/02/04 02:31:22 Renamed.
409 int utime_start_index = 0;
oshima 2015/02/04 01:49:46 move these variable after next if block
afakhry 2015/02/04 02:31:22 Done.
410 auto start = input.find_last_of(')');
oshima 2015/02/04 01:49:46 just use size_t. it isn't worth it imo.
afakhry 2015/02/04 02:31:22 Done.
411 if (start == input.npos || start == 0)
danakj 2015/02/04 01:29:18 why do you have to check for 0 here?
409 return -1; 412 return -1;
410 int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME); 413
411 int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME); 414 auto end = input.size() - 1;
danakj 2015/02/04 01:29:18 can you call this last instead of end? end is usua
afakhry 2015/02/04 02:31:22 Done.
415 for (auto j = start + 1; j < end; ++j) {
danakj 2015/02/04 01:29:18 a nit, but why j instead of i? usually we'd use i
416 if (input[j] != ' ' && input[j - 1] == ' ') {
danakj 2015/02/04 01:29:18 could you just string::find(" ") from the current
oshima 2015/02/04 01:49:46 I think he wanted to deal with two space case. I'm
danakj 2015/02/04 01:52:54 Can just skip past contiguous spaces if that can h
oshima 2015/02/04 02:28:37 That's fine too. I don't have much preference.
afakhry 2015/02/04 02:31:22 Done.
417 if (--last_space_index == 0) {
418 utime_start_index = j;
419 break;
420 }
oshima 2015/02/04 01:49:46 optional: you can actually increase j here because
421 }
422 }
423
424 if (last_space_index != 0)
425 return -1;
426
427 int utime = 0;
428 int stime = 0;
429 if (sscanf(&input.data()[utime_start_index], "%d %d", &utime, &stime) != 2)
danakj 2015/02/04 01:29:18 Why not StringToInt instead of sscanf?
oshima 2015/02/04 02:28:37 Is there any issue with sscanf?
afakhry 2015/02/04 02:31:22 Because it's much simpler. No additional parsing i
430 return -1;
431
412 return utime + stime; 432 return utime + stime;
413 } 433 }
414 434
415 const char kProcSelfExe[] = "/proc/self/exe"; 435 const char kProcSelfExe[] = "/proc/self/exe";
416 436
417 int GetNumberOfThreads(ProcessHandle process) { 437 int GetNumberOfThreads(ProcessHandle process) {
418 return internal::ReadProcStatsAndGetFieldAsInt64(process, 438 return internal::ReadProcStatsAndGetFieldAsInt64(process,
419 internal::VM_NUMTHREADS); 439 internal::VM_NUMTHREADS);
420 } 440 }
421 441
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after
888 #if defined(OS_LINUX) 908 #if defined(OS_LINUX)
889 int ProcessMetrics::GetIdleWakeupsPerSecond() { 909 int ProcessMetrics::GetIdleWakeupsPerSecond() {
890 uint64 wake_ups; 910 uint64 wake_ups;
891 const char kWakeupStat[] = "se.statistics.nr_wakeups"; 911 const char kWakeupStat[] = "se.statistics.nr_wakeups";
892 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? 912 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ?
893 CalculateIdleWakeupsPerSecond(wake_ups) : 0; 913 CalculateIdleWakeupsPerSecond(wake_ups) : 0;
894 } 914 }
895 #endif // defined(OS_LINUX) 915 #endif // defined(OS_LINUX)
896 916
897 } // namespace base 917 } // 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