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 "rtc_base/cpu_time.h"
12
13 #include "rtc_base/logging.h"
14 #include "rtc_base/time_utils.h"
15
16 #if defined(WEBRTC_LINUX)
17 #include <time.h>
18 #elif defined(WEBRTC_MAC)
19 #include <mach/mach_init.h>
20 #include <mach/mach_port.h>
21 #include <mach/thread_act.h>
22 #include <mach/thread_info.h>
23 #include <sys/resource.h>
24 #include <sys/times.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #elif defined(WEBRTC_WIN)
28 #include <windows.h>
29 #endif
30
31 #if defined(WEBRTC_WIN)
32 namespace {
33 // FILETIME resolution is 100 nanosecs.
34 const int64_t kNanosecsPerFiletime = 100;
35 } // namespace
36 #endif
37
38 namespace rtc {
39
GetProcessCpuTimeNanos()40 int64_t GetProcessCpuTimeNanos() {
41 #if defined(WEBRTC_FUCHSIA)
42 RTC_LOG_ERR(LS_ERROR) << "GetProcessCpuTimeNanos() not implemented";
43 return 0;
44 #else
45 #if defined(WEBRTC_LINUX)
46 struct timespec ts;
47 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
48 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
49 } else {
50 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
51 }
52 #elif defined(WEBRTC_MAC)
53 struct rusage rusage;
54 if (getrusage(RUSAGE_SELF, &rusage) == 0) {
55 return rusage.ru_utime.tv_sec * kNumNanosecsPerSec +
56 rusage.ru_utime.tv_usec * kNumNanosecsPerMicrosec;
57 } else {
58 RTC_LOG_ERR(LS_ERROR) << "getrusage() failed.";
59 }
60 #elif defined(WEBRTC_WIN)
61 FILETIME createTime;
62 FILETIME exitTime;
63 FILETIME kernelTime;
64 FILETIME userTime;
65 if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime,
66 &userTime) != 0) {
67 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
68 userTime.dwLowDateTime) *
69 kNanosecsPerFiletime;
70 } else {
71 RTC_LOG_ERR(LS_ERROR) << "GetProcessTimes() failed.";
72 }
73 #else
74 // Not implemented yet.
75 static_assert(
76 false, "GetProcessCpuTimeNanos() platform support not yet implemented.");
77 #endif
78 return -1;
79 #endif // defined(WEBRTC_FUCHSIA)
80 }
81
GetThreadCpuTimeNanos()82 int64_t GetThreadCpuTimeNanos() {
83 #if defined(WEBRTC_FUCHSIA)
84 RTC_LOG_ERR(LS_ERROR) << "GetThreadCpuTimeNanos() not implemented";
85 return 0;
86 #else
87 #if defined(WEBRTC_LINUX)
88 struct timespec ts;
89 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) {
90 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
91 } else {
92 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
93 }
94 #elif defined(WEBRTC_MAC)
95 mach_port_t thread_port = mach_thread_self();
96 thread_basic_info_data_t info;
97 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
98 kern_return_t kr =
99 thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)&info, &count);
100 mach_port_deallocate(mach_task_self(), thread_port);
101 if (kr == KERN_SUCCESS) {
102 return info.user_time.seconds * kNumNanosecsPerSec +
103 info.user_time.microseconds * kNumNanosecsPerMicrosec;
104 } else {
105 RTC_LOG_ERR(LS_ERROR) << "thread_info() failed.";
106 }
107 #elif defined(WEBRTC_WIN)
108 FILETIME createTime;
109 FILETIME exitTime;
110 FILETIME kernelTime;
111 FILETIME userTime;
112 if (GetThreadTimes(GetCurrentThread(), &createTime, &exitTime, &kernelTime,
113 &userTime) != 0) {
114 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
115 userTime.dwLowDateTime) *
116 kNanosecsPerFiletime;
117 } else {
118 RTC_LOG_ERR(LS_ERROR) << "GetThreadTimes() failed.";
119 }
120 #else
121 // Not implemented yet.
122 static_assert(
123 false, "GetThreadCpuTimeNanos() platform support not yet implemented.");
124 #endif
125 return -1;
126 #endif // defined(WEBRTC_FUCHSIA)
127 }
128
129 } // namespace rtc
130