blob: 6c228804d9fa20ad5c15e050798b0cddfb549b42 [file] [log] [blame]
ilnik531100d2017-02-21 03:33:24 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/cpu_time.h"
12#include "rtc_base/logging.h"
13#include "rtc_base/timeutils.h"
ilnik531100d2017-02-21 03:33:24 -080014
15#if defined(WEBRTC_LINUX)
16#include <time.h>
17#elif defined(WEBRTC_MAC)
18#include <sys/resource.h>
19#include <sys/types.h>
20#include <sys/times.h>
21#include <mach/thread_info.h>
22#include <mach/thread_act.h>
23#include <mach/mach_init.h>
24#include <unistd.h>
25#elif defined(WEBRTC_WIN)
26#include <windows.h>
27#endif
28
29#if defined(WEBRTC_WIN)
30namespace {
31// FILETIME resolution is 100 nanosecs.
32const int64_t kNanosecsPerFiletime = 100;
33} // namespace
34#endif
35
36namespace rtc {
37
38int64_t GetProcessCpuTimeNanos() {
39#if defined(WEBRTC_LINUX)
40 struct timespec ts;
41 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
42 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
43 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010044 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 03:33:24 -080045 }
46#elif defined(WEBRTC_MAC)
47 struct rusage rusage;
48 if (getrusage(RUSAGE_SELF, &rusage) == 0) {
49 return rusage.ru_utime.tv_sec * kNumNanosecsPerSec +
50 rusage.ru_utime.tv_usec * kNumNanosecsPerMicrosec;
51 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010052 RTC_LOG_ERR(LS_ERROR) << "getrusage() failed.";
ilnik531100d2017-02-21 03:33:24 -080053 }
54#elif defined(WEBRTC_WIN)
55 FILETIME createTime;
56 FILETIME exitTime;
57 FILETIME kernelTime;
58 FILETIME userTime;
59 if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime,
60 &userTime) != 0) {
61 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
62 userTime.dwLowDateTime) *
63 kNanosecsPerFiletime;
64 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010065 RTC_LOG_ERR(LS_ERROR) << "GetProcessTimes() failed.";
ilnik531100d2017-02-21 03:33:24 -080066 }
67#else
68 // Not implemented yet.
69 static_assert(
70 false, "GetProcessCpuTimeNanos() platform support not yet implemented.");
71#endif
72 return -1;
73}
74
75int64_t GetThreadCpuTimeNanos() {
76#if defined(WEBRTC_LINUX)
77 struct timespec ts;
78 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) {
79 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
80 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010081 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 03:33:24 -080082 }
83#elif defined(WEBRTC_MAC)
84 thread_basic_info_data_t info;
85 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
86 if (thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&info,
87 &count) == KERN_SUCCESS) {
88 return info.user_time.seconds * kNumNanosecsPerSec +
89 info.user_time.microseconds * kNumNanosecsPerMicrosec;
90 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010091 RTC_LOG_ERR(LS_ERROR) << "thread_info() failed.";
ilnik531100d2017-02-21 03:33:24 -080092 }
93#elif defined(WEBRTC_WIN)
94 FILETIME createTime;
95 FILETIME exitTime;
96 FILETIME kernelTime;
97 FILETIME userTime;
98 if (GetThreadTimes(GetCurrentThread(), &createTime, &exitTime, &kernelTime,
99 &userTime) != 0) {
100 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
101 userTime.dwLowDateTime) *
102 kNanosecsPerFiletime;
103 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100104 RTC_LOG_ERR(LS_ERROR) << "GetThreadTimes() failed.";
ilnik531100d2017-02-21 03:33:24 -0800105 }
106#else
107 // Not implemented yet.
108 static_assert(
109 false, "GetProcessCpuTimeNanos() platform support not yet implemented.");
110#endif
111 return -1;
112}
113
114} // namespace rtc