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..8c81e204197625fdc7acdecba2a1d0f09ffa857b |
--- /dev/null |
+++ b/webrtc/base/cpu_time.cc |
@@ -0,0 +1,64 @@ |
+/* |
+ * 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" |
+ |
+#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 { |
+ |
+// static |
+double CpuTime::GetCpuTime() { |
+#if defined(WEBRTC_LINUX) |
+ struct timespec ts; |
+ if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) != -1) { |
+ return static_cast<double>(ts.tv_sec) + |
+ static_cast<double>(ts.tv_nsec) / 1000000000.0; |
nisse-webrtc
2017/02/15 12:18:16
I'd prefer "* 1e-9", so I don't have to count the
ilnik
2017/02/15 12:53:21
Done.
|
+ } else { |
+ LOG(LS_ERROR) << "clock_gettime() failed."; |
+ } |
+#elif defined(WEBRTC_MAC) |
+ struct rusage rusage; |
+ if (getrusage(RUSAGE_SELF, &rusage) != -1) { |
+ return static_cast<double>(rusage.ru_utime.tv_sec) + |
+ static_cast<double>(rusage.ru_utime.tv_usec) / 1000000.0; |
+ } |
+#elif defined(WEBRTC_WIN) |
+ FILETIME createTime; |
+ FILETIME exitTime; |
+ FILETIME kernelTime; |
+ FILETIME userTime; |
+ if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, |
+ &userTime) != -1) { |
+ SYSTEMTIME userSystemTime; |
nisse-webrtc
2017/02/15 12:18:16
After a quick look at the windows docs, I'd recomm
ilnik
2017/02/15 12:53:21
Done.
|
+ if (FileTimeToSystemTime(&userTime, &userSystemTime) != -1) |
+ return static_cast<double>(userSystemTime.wHour) * 3600.0 + |
+ static_cast<double>(userSystemTime.wMinute) * 60.0 + |
+ static_cast<double>(userSystemTime.wSecond) + |
+ static_cast<double>(userSystemTime.wMilliseconds) / 1000.0; |
+ } else { |
+ LOG(LS_ERROR) << "GetProcessTimes() failed."; |
+ } |
+#else |
+ LOG(LS_ERROR) << "No function to get CPU time"; |
+#endif |
+ return -1.0; |
+} |
+ |
+} // namespace rtc |