blob: b95bcb136e5a48dcff60887e15b435bfd1a71385 [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() {
41#if defined(WEBRTC_LINUX)
42 struct timespec ts;
43 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
44 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
45 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010046 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 03:33:24 -080047 }
48#elif defined(WEBRTC_MAC)
49 struct rusage rusage;
50 if (getrusage(RUSAGE_SELF, &rusage) == 0) {
51 return rusage.ru_utime.tv_sec * kNumNanosecsPerSec +
52 rusage.ru_utime.tv_usec * kNumNanosecsPerMicrosec;
53 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010054 RTC_LOG_ERR(LS_ERROR) << "getrusage() failed.";
ilnik531100d2017-02-21 03:33:24 -080055 }
56#elif defined(WEBRTC_WIN)
57 FILETIME createTime;
58 FILETIME exitTime;
59 FILETIME kernelTime;
60 FILETIME userTime;
61 if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime,
62 &userTime) != 0) {
63 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
64 userTime.dwLowDateTime) *
65 kNanosecsPerFiletime;
66 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010067 RTC_LOG_ERR(LS_ERROR) << "GetProcessTimes() failed.";
ilnik531100d2017-02-21 03:33:24 -080068 }
Scott Grahamf26e2902018-10-24 15:15:36 -070069#elif defined(WEBRTC_FUCHSIA)
70 RTC_LOG_ERR(LS_ERROR) << "GetProcessCpuTimeNanos() not implemented";
71 return 0;
ilnik531100d2017-02-21 03:33:24 -080072#else
73 // Not implemented yet.
74 static_assert(
75 false, "GetProcessCpuTimeNanos() platform support not yet implemented.");
76#endif
77 return -1;
78}
79
80int64_t GetThreadCpuTimeNanos() {
81#if defined(WEBRTC_LINUX)
82 struct timespec ts;
83 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) {
84 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
85 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010086 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 03:33:24 -080087 }
88#elif defined(WEBRTC_MAC)
Robert Sesekb0800512018-08-30 17:13:44 -040089 mach_port_t thread_port = mach_thread_self();
ilnik531100d2017-02-21 03:33:24 -080090 thread_basic_info_data_t info;
91 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
Robert Sesekb0800512018-08-30 17:13:44 -040092 kern_return_t kr =
93 thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)&info, &count);
94 mach_port_deallocate(mach_task_self(), thread_port);
95 if (kr == KERN_SUCCESS) {
ilnik531100d2017-02-21 03:33:24 -080096 return info.user_time.seconds * kNumNanosecsPerSec +
97 info.user_time.microseconds * kNumNanosecsPerMicrosec;
98 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010099 RTC_LOG_ERR(LS_ERROR) << "thread_info() failed.";
ilnik531100d2017-02-21 03:33:24 -0800100 }
101#elif defined(WEBRTC_WIN)
102 FILETIME createTime;
103 FILETIME exitTime;
104 FILETIME kernelTime;
105 FILETIME userTime;
106 if (GetThreadTimes(GetCurrentThread(), &createTime, &exitTime, &kernelTime,
107 &userTime) != 0) {
108 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
109 userTime.dwLowDateTime) *
110 kNanosecsPerFiletime;
111 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100112 RTC_LOG_ERR(LS_ERROR) << "GetThreadTimes() failed.";
ilnik531100d2017-02-21 03:33:24 -0800113 }
Scott Grahamf26e2902018-10-24 15:15:36 -0700114#elif defined(WEBRTC_FUCHSIA)
115 RTC_LOG_ERR(LS_ERROR) << "GetThreadCpuTimeNanos() not implemented";
116 return 0;
ilnik531100d2017-02-21 03:33:24 -0800117#else
118 // Not implemented yet.
119 static_assert(
Scott Grahamf26e2902018-10-24 15:15:36 -0700120 false, "GetThreadCpuTimeNanos() platform support not yet implemented.");
ilnik531100d2017-02-21 03:33:24 -0800121#endif
122 return -1;
123}
124
125} // namespace rtc