Chromium Code Reviews| Index: webrtc/base/cpu_time.cc |
| diff --git a/webrtc/base/cpu_time.cc b/webrtc/base/cpu_time.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4e747f5d4a54eedcf3431a74fd925cf92186cf8e |
| --- /dev/null |
| +++ b/webrtc/base/cpu_time.cc |
| @@ -0,0 +1,61 @@ |
| +/* |
| + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/base/cpu_time.h" |
| +#include "webrtc/base/logging.h" |
| +#include "webrtc/base/timeutils.h" |
| + |
| +#if defined(WEBRTC_LINUX) |
| +#include <time.h> |
| +#elif defined(WEBRTC_MAC) |
| +#include <unistd.h> |
| +#include <sys/resource.h> |
| +#include <sys/times.h> |
| +#elif defined(WEBRTC_WIN) |
| +#include <windows.h> |
| +#endif |
| + |
| +namespace rtc { |
| + |
| +int64_t GetCpuTime() { |
| +#if defined(WEBRTC_LINUX) |
| + struct timespec ts; |
| + if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) != -1) { |
| + return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec; |
|
nisse-webrtc
2017/02/15 13:07:31
I think you need a cast to int64_t on one of the f
ilnik
2017/02/15 13:23:11
kNumNanosecsPerSec and other constants are already
nisse-webrtc
2017/02/15 14:27:35
You're right, that's sufficient.
|
| + } else { |
| + LOG(LS_ERROR) << "clock_gettime() failed."; |
|
nisse-webrtc
2017/02/15 13:07:31
I think you should use LOG_ERR when errno (or the
ilnik
2017/02/15 13:23:11
Done.
|
| + } |
| +#elif defined(WEBRTC_MAC) |
| + struct rusage rusage; |
| + if (getrusage(RUSAGE_SELF, &rusage) != -1) { |
| + return rusage.ru_utime.tv_sec * kNumNanosecsPerSec + |
| + rusage.ru_utime.tv_usec * kNumNanosecsPerMicrosec; |
| + } |
| +#elif defined(WEBRTC_WIN) |
| + // FILETIME resolution is 100 nanosecs. |
| + static const int64_t kNanosecsPerFiletime = 100; |
| + FILETIME createTime; |
| + FILETIME exitTime; |
| + FILETIME kernelTime; |
| + FILETIME userTime; |
| + if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, |
| + &userTime) != -1) { |
|
nisse-webrtc
2017/02/15 13:07:31
The docs doesn't specify which non-zero value is r
ilnik
2017/02/15 13:23:11
This was actually a mistake, because it returns no
nisse-webrtc
2017/02/15 14:27:35
Ooops. Good you found it.
|
| + return (static_cast<uint64_t>(userTime.dwHighDateTime) << 32) |
| + + userTime.dwLowDateTime) * kNanosecsPerFiletime; |
| + } else { |
| + LOG(LS_ERROR) << "GetProcessTimes() failed."; |
| + } |
| +#else |
| + LOG(LS_ERROR) << "No function to get CPU time"; |
| +#endif |
| + return -1; |
| +} |
| + |
| +} // namespace rtc |