blob: d3fee50c4981f18342a642c25846700f6d7befb9 [file] [log] [blame]
ilnik531100d2017-02-21 11:33:241/*
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 04:47:3111#include "rtc_base/cpu_time.h"
Yves Gerey3e707812018-11-28 15:47:4912
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0014#include "rtc_base/time_utils.h"
ilnik531100d2017-02-21 11:33:2415
16#if defined(WEBRTC_LINUX)
17#include <time.h>
18#elif defined(WEBRTC_MAC)
ilnik531100d2017-02-21 11:33:2419#include <mach/mach_init.h>
Robert Sesekb0800512018-08-30 21:13:4420#include <mach/mach_port.h>
Yves Gerey665174f2018-06-19 13:03:0521#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>
ilnik531100d2017-02-21 11:33:2426#include <unistd.h>
27#elif defined(WEBRTC_WIN)
28#include <windows.h>
Sarah Phamf1fa6ac72023-01-15 23:43:3429#elif defined(WEBRTC_FUCHSIA)
30#include <lib/zx/process.h>
31#include <lib/zx/thread.h>
32#include <zircon/status.h>
ilnik531100d2017-02-21 11:33:2433#endif
34
35#if defined(WEBRTC_WIN)
36namespace {
37// FILETIME resolution is 100 nanosecs.
38const int64_t kNanosecsPerFiletime = 100;
39} // namespace
40#endif
41
42namespace rtc {
43
44int64_t GetProcessCpuTimeNanos() {
Nico Weberb8612c72021-08-20 15:01:3545#if defined(WEBRTC_FUCHSIA)
Sarah Phamf1fa6ac72023-01-15 23:43:3446 zx_info_task_runtime_t runtime_info;
47 zx_status_t status =
48 zx::process::self()->get_info(ZX_INFO_TASK_RUNTIME, &runtime_info,
49 sizeof(runtime_info), nullptr, nullptr);
50 if (status == ZX_OK) {
51 return runtime_info.cpu_time;
52 } else {
53 RTC_LOG_ERR(LS_ERROR) << "get_info() failed: "
54 << zx_status_get_string(status);
55 }
56#elif defined(WEBRTC_LINUX)
ilnik531100d2017-02-21 11:33:2457 struct timespec ts;
58 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
59 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
60 } else {
Mirko Bonadei675513b2017-11-09 10:09:2561 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 11:33:2462 }
63#elif defined(WEBRTC_MAC)
64 struct rusage rusage;
65 if (getrusage(RUSAGE_SELF, &rusage) == 0) {
66 return rusage.ru_utime.tv_sec * kNumNanosecsPerSec +
67 rusage.ru_utime.tv_usec * kNumNanosecsPerMicrosec;
68 } else {
Mirko Bonadei675513b2017-11-09 10:09:2569 RTC_LOG_ERR(LS_ERROR) << "getrusage() failed.";
ilnik531100d2017-02-21 11:33:2470 }
71#elif defined(WEBRTC_WIN)
72 FILETIME createTime;
73 FILETIME exitTime;
74 FILETIME kernelTime;
75 FILETIME userTime;
76 if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime,
77 &userTime) != 0) {
78 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
79 userTime.dwLowDateTime) *
80 kNanosecsPerFiletime;
81 } else {
Mirko Bonadei675513b2017-11-09 10:09:2582 RTC_LOG_ERR(LS_ERROR) << "GetProcessTimes() failed.";
ilnik531100d2017-02-21 11:33:2483 }
84#else
85 // Not implemented yet.
86 static_assert(
87 false, "GetProcessCpuTimeNanos() platform support not yet implemented.");
88#endif
89 return -1;
90}
91
92int64_t GetThreadCpuTimeNanos() {
Nico Weberb8612c72021-08-20 15:01:3593#if defined(WEBRTC_FUCHSIA)
Sarah Phamf1fa6ac72023-01-15 23:43:3494 zx_info_task_runtime_t runtime_info;
95 zx_status_t status =
96 zx::thread::self()->get_info(ZX_INFO_TASK_RUNTIME, &runtime_info,
97 sizeof(runtime_info), nullptr, nullptr);
98 if (status == ZX_OK) {
99 return runtime_info.cpu_time;
100 } else {
101 RTC_LOG_ERR(LS_ERROR) << "get_info() failed: "
102 << zx_status_get_string(status);
103 }
104#elif defined(WEBRTC_LINUX)
ilnik531100d2017-02-21 11:33:24105 struct timespec ts;
106 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) {
107 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
108 } else {
Mirko Bonadei675513b2017-11-09 10:09:25109 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 11:33:24110 }
111#elif defined(WEBRTC_MAC)
Robert Sesekb0800512018-08-30 21:13:44112 mach_port_t thread_port = mach_thread_self();
ilnik531100d2017-02-21 11:33:24113 thread_basic_info_data_t info;
114 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
Robert Sesekb0800512018-08-30 21:13:44115 kern_return_t kr =
116 thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)&info, &count);
117 mach_port_deallocate(mach_task_self(), thread_port);
118 if (kr == KERN_SUCCESS) {
ilnik531100d2017-02-21 11:33:24119 return info.user_time.seconds * kNumNanosecsPerSec +
120 info.user_time.microseconds * kNumNanosecsPerMicrosec;
121 } else {
Mirko Bonadei675513b2017-11-09 10:09:25122 RTC_LOG_ERR(LS_ERROR) << "thread_info() failed.";
ilnik531100d2017-02-21 11:33:24123 }
124#elif defined(WEBRTC_WIN)
125 FILETIME createTime;
126 FILETIME exitTime;
127 FILETIME kernelTime;
128 FILETIME userTime;
129 if (GetThreadTimes(GetCurrentThread(), &createTime, &exitTime, &kernelTime,
130 &userTime) != 0) {
131 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
132 userTime.dwLowDateTime) *
133 kNanosecsPerFiletime;
134 } else {
Mirko Bonadei675513b2017-11-09 10:09:25135 RTC_LOG_ERR(LS_ERROR) << "GetThreadTimes() failed.";
ilnik531100d2017-02-21 11:33:24136 }
137#else
138 // Not implemented yet.
139 static_assert(
Scott Grahamf26e2902018-10-24 22:15:36140 false, "GetThreadCpuTimeNanos() platform support not yet implemented.");
ilnik531100d2017-02-21 11:33:24141#endif
142 return -1;
ilnik531100d2017-02-21 11:33:24143}
144
145} // namespace rtc