OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/base/cpu_time.h" |
| 12 #include "webrtc/base/logging.h" |
| 13 #include "webrtc/base/timeutils.h" |
| 14 |
| 15 #if defined(WEBRTC_LINUX) |
| 16 #include <time.h> |
| 17 #elif defined(WEBRTC_MAC) |
| 18 #include <unistd.h> |
| 19 #include <sys/resource.h> |
| 20 #include <sys/times.h> |
| 21 #elif defined(WEBRTC_WIN) |
| 22 #include <windows.h> |
| 23 #endif |
| 24 |
| 25 namespace rtc { |
| 26 |
| 27 int64_t GetCpuTime() { |
| 28 #if defined(WEBRTC_LINUX) |
| 29 struct timespec ts; |
| 30 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) { |
| 31 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec; |
| 32 } else { |
| 33 LOG_ERR(LS_ERROR) <<"clock_gettime() failed."; |
| 34 } |
| 35 #elif defined(WEBRTC_MAC) |
| 36 struct rusage rusage; |
| 37 if (getrusage(RUSAGE_SELF, &rusage) == 0) { |
| 38 return rusage.ru_utime.tv_sec * kNumNanosecsPerSec + |
| 39 rusage.ru_utime.tv_usec * kNumNanosecsPerMicrosec; |
| 40 } else { |
| 41 LOG_ERR(LS_ERROR) << "getrusage() failed."; |
| 42 } |
| 43 #elif defined(WEBRTC_WIN) |
| 44 // FILETIME resolution is 100 nanosecs. |
| 45 static const int64_t kNanosecsPerFiletime = 100; |
| 46 FILETIME createTime; |
| 47 FILETIME exitTime; |
| 48 FILETIME kernelTime; |
| 49 FILETIME userTime; |
| 50 if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, |
| 51 &userTime) != 0) { |
| 52 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) |
| 53 + userTime.dwLowDateTime) * kNanosecsPerFiletime; |
| 54 } else { |
| 55 LOG_ERR(LS_ERROR) << "GetProcessTimes() failed."; |
| 56 } |
| 57 #else |
| 58 LOG(LS_ERROR) << "No function to get CPU time"; |
| 59 #endif |
| 60 return -1; |
| 61 } |
| 62 |
| 63 } // namespace rtc |
OLD | NEW |