blob: de4a6bd6844b1eb4322b417d175fa0f166c8ee23 [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)
ilnik531100d2017-02-21 03:33:24 -080018#include <mach/mach_init.h>
Robert Sesekb0800512018-08-30 17:13:44 -040019#include <mach/mach_port.h>
Yves Gerey665174f2018-06-19 15:03:05 +020020#include <mach/thread_act.h>
21#include <mach/thread_info.h>
22#include <sys/resource.h>
23#include <sys/times.h>
24#include <sys/types.h>
ilnik531100d2017-02-21 03:33:24 -080025#include <unistd.h>
26#elif defined(WEBRTC_WIN)
27#include <windows.h>
28#endif
29
30#if defined(WEBRTC_WIN)
31namespace {
32// FILETIME resolution is 100 nanosecs.
33const int64_t kNanosecsPerFiletime = 100;
34} // namespace
35#endif
36
37namespace rtc {
38
39int64_t GetProcessCpuTimeNanos() {
40#if defined(WEBRTC_LINUX)
41 struct timespec ts;
42 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
43 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
44 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010045 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 03:33:24 -080046 }
47#elif defined(WEBRTC_MAC)
48 struct rusage rusage;
49 if (getrusage(RUSAGE_SELF, &rusage) == 0) {
50 return rusage.ru_utime.tv_sec * kNumNanosecsPerSec +
51 rusage.ru_utime.tv_usec * kNumNanosecsPerMicrosec;
52 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010053 RTC_LOG_ERR(LS_ERROR) << "getrusage() failed.";
ilnik531100d2017-02-21 03:33:24 -080054 }
55#elif defined(WEBRTC_WIN)
56 FILETIME createTime;
57 FILETIME exitTime;
58 FILETIME kernelTime;
59 FILETIME userTime;
60 if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime,
61 &userTime) != 0) {
62 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
63 userTime.dwLowDateTime) *
64 kNanosecsPerFiletime;
65 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010066 RTC_LOG_ERR(LS_ERROR) << "GetProcessTimes() failed.";
ilnik531100d2017-02-21 03:33:24 -080067 }
68#else
69 // Not implemented yet.
70 static_assert(
71 false, "GetProcessCpuTimeNanos() platform support not yet implemented.");
72#endif
73 return -1;
74}
75
76int64_t GetThreadCpuTimeNanos() {
77#if defined(WEBRTC_LINUX)
78 struct timespec ts;
79 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) {
80 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
81 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010082 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 03:33:24 -080083 }
84#elif defined(WEBRTC_MAC)
Robert Sesekb0800512018-08-30 17:13:44 -040085 mach_port_t thread_port = mach_thread_self();
ilnik531100d2017-02-21 03:33:24 -080086 thread_basic_info_data_t info;
87 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
Robert Sesekb0800512018-08-30 17:13:44 -040088 kern_return_t kr =
89 thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)&info, &count);
90 mach_port_deallocate(mach_task_self(), thread_port);
91 if (kr == KERN_SUCCESS) {
ilnik531100d2017-02-21 03:33:24 -080092 return info.user_time.seconds * kNumNanosecsPerSec +
93 info.user_time.microseconds * kNumNanosecsPerMicrosec;
94 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010095 RTC_LOG_ERR(LS_ERROR) << "thread_info() failed.";
ilnik531100d2017-02-21 03:33:24 -080096 }
97#elif defined(WEBRTC_WIN)
98 FILETIME createTime;
99 FILETIME exitTime;
100 FILETIME kernelTime;
101 FILETIME userTime;
102 if (GetThreadTimes(GetCurrentThread(), &createTime, &exitTime, &kernelTime,
103 &userTime) != 0) {
104 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
105 userTime.dwLowDateTime) *
106 kNanosecsPerFiletime;
107 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100108 RTC_LOG_ERR(LS_ERROR) << "GetThreadTimes() failed.";
ilnik531100d2017-02-21 03:33:24 -0800109 }
110#else
111 // Not implemented yet.
112 static_assert(
113 false, "GetProcessCpuTimeNanos() platform support not yet implemented.");
114#endif
115 return -1;
116}
117
118} // namespace rtc