Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_util.h" | 5 #include "base/process_util.h" |
| 6 | 6 |
| 7 #include <sys/resource.h> | |
| 8 #include <sys/time.h> | |
| 7 #include <sys/types.h> | 9 #include <sys/types.h> |
| 8 #include <unistd.h> | 10 #include <unistd.h> |
| 9 | 11 |
| 10 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/logging.h" | |
| 11 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
| 12 | 15 |
| 16 const int kMicrosecondsPerSecond = 1000000; | |
| 17 | |
| 13 namespace base { | 18 namespace base { |
| 14 | 19 |
| 15 int GetCurrentProcId() { | 20 int GetCurrentProcId() { |
| 16 return getpid(); | 21 return getpid(); |
| 17 } | 22 } |
| 18 | 23 |
| 19 ProcessHandle GetCurrentProcessHandle() { | 24 ProcessHandle GetCurrentProcessHandle() { |
| 20 return GetCurrentProcId(); | 25 return GetCurrentProcId(); |
| 21 } | 26 } |
| 22 | 27 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 39 | 44 |
| 40 void EnableTerminationOnHeapCorruption() { | 45 void EnableTerminationOnHeapCorruption() { |
| 41 // On POSIX, there nothing to do AFAIK. | 46 // On POSIX, there nothing to do AFAIK. |
| 42 } | 47 } |
| 43 | 48 |
| 44 void RaiseProcessToHighPriority() { | 49 void RaiseProcessToHighPriority() { |
| 45 // On POSIX, we don't actually do anything here. We could try to nice() or | 50 // On POSIX, we don't actually do anything here. We could try to nice() or |
| 46 // setpriority() or sched_getscheduler, but these all require extra rights. | 51 // setpriority() or sched_getscheduler, but these all require extra rights. |
| 47 } | 52 } |
| 48 | 53 |
| 54 namespace { | |
| 55 | |
| 56 int64 TimeValToMicroseconds(const struct timeval& tv) { | |
| 57 return tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec; | |
| 58 } | |
| 59 | |
| 60 } | |
| 61 | |
| 62 int ProcessMetrics::GetCPUUsage() { | |
| 63 int retval; | |
|
pink (ping after 24hrs)
2008/12/02 19:36:11
declare retval where it's initialized or give it a
| |
| 64 struct timeval now; | |
| 65 struct rusage usage; | |
| 66 | |
| 67 retval = gettimeofday(&now, NULL); | |
| 68 if (retval) | |
| 69 return 0; | |
| 70 retval = getrusage(RUSAGE_SELF, &usage); | |
| 71 if (retval) | |
| 72 return 0; | |
| 73 | |
| 74 int64 system_time = (TimeValToMicroseconds(usage.ru_stime) + | |
| 75 TimeValToMicroseconds(usage.ru_utime)) / | |
| 76 processor_count_; | |
|
pink (ping after 24hrs)
2008/12/02 19:36:11
does |processor_count_| get scaled to an int64 or
Avi (use Gerrit)
2008/12/02 19:40:31
I'm pretty sure int64/int32 causes the int32 to be
| |
| 77 int64 time = TimeValToMicroseconds(now); | |
| 78 | |
| 79 if ((last_system_time_ == 0) || (last_time_ == 0)) { | |
|
pink (ping after 24hrs)
2008/12/02 19:36:11
don't need the extra parens, but not a biggie.
Avi (use Gerrit)
2008/12/02 19:40:31
Straight from the Windows code.
| |
| 80 // First call, just set the last values. | |
| 81 last_system_time_ = system_time; | |
| 82 last_time_ = time; | |
| 83 return 0; | |
| 84 } | |
| 85 | |
| 86 int64 system_time_delta = system_time - last_system_time_; | |
| 87 int64 time_delta = time - last_time_; | |
| 88 DCHECK(time_delta != 0); | |
| 89 if (time_delta == 0) | |
| 90 return 0; | |
| 91 | |
| 92 // We add time_delta / 2 so the result is rounded. | |
| 93 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) / | |
| 94 time_delta); | |
| 95 | |
| 96 last_system_time_ = system_time; | |
| 97 last_time_ = time; | |
| 98 | |
| 99 return cpu; | |
| 100 } | |
| 101 | |
| 49 } // namespace base | 102 } // namespace base |
| OLD | NEW |