blob: a8e542b5a2a87dbd5103998dcfae636edffe94f2 [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"
Yves Gerey3e707812018-11-28 16:47:49 +010012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080014#include "rtc_base/time_utils.h"
ilnik531100d2017-02-21 03:33:24 -080015
16#if defined(WEBRTC_LINUX)
17#include <time.h>
18#elif defined(WEBRTC_MAC)
ilnik531100d2017-02-21 03:33:24 -080019#include <mach/mach_init.h>
Robert Sesekb0800512018-08-30 17:13:44 -040020#include <mach/mach_port.h>
Yves Gerey665174f2018-06-19 15:03:05 +020021#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 03:33:24 -080026#include <unistd.h>
27#elif defined(WEBRTC_WIN)
28#include <windows.h>
29#endif
30
31#if defined(WEBRTC_WIN)
32namespace {
33// FILETIME resolution is 100 nanosecs.
34const int64_t kNanosecsPerFiletime = 100;
35} // namespace
36#endif
37
38namespace rtc {
39
40int64_t GetProcessCpuTimeNanos() {
Nico Weberb8612c72021-08-20 11:01:35 -040041#if defined(WEBRTC_FUCHSIA)
42 RTC_LOG_ERR(LS_ERROR) << "GetProcessCpuTimeNanos() not implemented";
43 return 0;
44#else
ilnik531100d2017-02-21 03:33:24 -080045#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 {
Mirko Bonadei675513b2017-11-09 11:09:25 +010050 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 03:33:24 -080051 }
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 {
Mirko Bonadei675513b2017-11-09 11:09:25 +010058 RTC_LOG_ERR(LS_ERROR) << "getrusage() failed.";
ilnik531100d2017-02-21 03:33:24 -080059 }
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 {
Mirko Bonadei675513b2017-11-09 11:09:25 +010071 RTC_LOG_ERR(LS_ERROR) << "GetProcessTimes() failed.";
ilnik531100d2017-02-21 03:33:24 -080072 }
73#else
74 // Not implemented yet.
75 static_assert(
76 false, "GetProcessCpuTimeNanos() platform support not yet implemented.");
77#endif
78 return -1;
Nico Weberb8612c72021-08-20 11:01:35 -040079#endif // defined(WEBRTC_FUCHSIA)
ilnik531100d2017-02-21 03:33:24 -080080}
81
82int64_t GetThreadCpuTimeNanos() {
Nico Weberb8612c72021-08-20 11:01:35 -040083#if defined(WEBRTC_FUCHSIA)
84 RTC_LOG_ERR(LS_ERROR) << "GetThreadCpuTimeNanos() not implemented";
85 return 0;
86#else
ilnik531100d2017-02-21 03:33:24 -080087#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 {
Mirko Bonadei675513b2017-11-09 11:09:25 +010092 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 03:33:24 -080093 }
94#elif defined(WEBRTC_MAC)
Robert Sesekb0800512018-08-30 17:13:44 -040095 mach_port_t thread_port = mach_thread_self();
ilnik531100d2017-02-21 03:33:24 -080096 thread_basic_info_data_t info;
97 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
Robert Sesekb0800512018-08-30 17:13:44 -040098 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) {
ilnik531100d2017-02-21 03:33:24 -0800102 return info.user_time.seconds * kNumNanosecsPerSec +
103 info.user_time.microseconds * kNumNanosecsPerMicrosec;
104 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100105 RTC_LOG_ERR(LS_ERROR) << "thread_info() failed.";
ilnik531100d2017-02-21 03:33:24 -0800106 }
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 {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100118 RTC_LOG_ERR(LS_ERROR) << "GetThreadTimes() failed.";
ilnik531100d2017-02-21 03:33:24 -0800119 }
120#else
121 // Not implemented yet.
122 static_assert(
Scott Grahamf26e2902018-10-24 15:15:36 -0700123 false, "GetThreadCpuTimeNanos() platform support not yet implemented.");
ilnik531100d2017-02-21 03:33:24 -0800124#endif
125 return -1;
Nico Weberb8612c72021-08-20 11:01:35 -0400126#endif // defined(WEBRTC_FUCHSIA)
ilnik531100d2017-02-21 03:33:24 -0800127}
128
129} // namespace rtc