blob: ad91ecace973195c495bb22791425929d33362ed [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"
12#include "rtc_base/logging.h"
13#include "rtc_base/timeutils.h"
ilnik531100d2017-02-21 11:33:2414
15#if defined(WEBRTC_LINUX)
16#include <time.h>
17#elif defined(WEBRTC_MAC)
ilnik531100d2017-02-21 11:33:2418#include <mach/mach_init.h>
Robert Sesekb0800512018-08-30 21:13:4419#include <mach/mach_port.h>
Yves Gerey665174f2018-06-19 13:03:0520#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 11:33:2425#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 10:09:2545 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 11:33:2446 }
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 10:09:2553 RTC_LOG_ERR(LS_ERROR) << "getrusage() failed.";
ilnik531100d2017-02-21 11:33:2454 }
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 10:09:2566 RTC_LOG_ERR(LS_ERROR) << "GetProcessTimes() failed.";
ilnik531100d2017-02-21 11:33:2467 }
Scott Grahamf26e2902018-10-24 22:15:3668#elif defined(WEBRTC_FUCHSIA)
69 RTC_LOG_ERR(LS_ERROR) << "GetProcessCpuTimeNanos() not implemented";
70 return 0;
ilnik531100d2017-02-21 11:33:2471#else
72 // Not implemented yet.
73 static_assert(
74 false, "GetProcessCpuTimeNanos() platform support not yet implemented.");
75#endif
76 return -1;
77}
78
79int64_t GetThreadCpuTimeNanos() {
80#if defined(WEBRTC_LINUX)
81 struct timespec ts;
82 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) {
83 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
84 } else {
Mirko Bonadei675513b2017-11-09 10:09:2585 RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
ilnik531100d2017-02-21 11:33:2486 }
87#elif defined(WEBRTC_MAC)
Robert Sesekb0800512018-08-30 21:13:4488 mach_port_t thread_port = mach_thread_self();
ilnik531100d2017-02-21 11:33:2489 thread_basic_info_data_t info;
90 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
Robert Sesekb0800512018-08-30 21:13:4491 kern_return_t kr =
92 thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)&info, &count);
93 mach_port_deallocate(mach_task_self(), thread_port);
94 if (kr == KERN_SUCCESS) {
ilnik531100d2017-02-21 11:33:2495 return info.user_time.seconds * kNumNanosecsPerSec +
96 info.user_time.microseconds * kNumNanosecsPerMicrosec;
97 } else {
Mirko Bonadei675513b2017-11-09 10:09:2598 RTC_LOG_ERR(LS_ERROR) << "thread_info() failed.";
ilnik531100d2017-02-21 11:33:2499 }
100#elif defined(WEBRTC_WIN)
101 FILETIME createTime;
102 FILETIME exitTime;
103 FILETIME kernelTime;
104 FILETIME userTime;
105 if (GetThreadTimes(GetCurrentThread(), &createTime, &exitTime, &kernelTime,
106 &userTime) != 0) {
107 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
108 userTime.dwLowDateTime) *
109 kNanosecsPerFiletime;
110 } else {
Mirko Bonadei675513b2017-11-09 10:09:25111 RTC_LOG_ERR(LS_ERROR) << "GetThreadTimes() failed.";
ilnik531100d2017-02-21 11:33:24112 }
Scott Grahamf26e2902018-10-24 22:15:36113#elif defined(WEBRTC_FUCHSIA)
114 RTC_LOG_ERR(LS_ERROR) << "GetThreadCpuTimeNanos() not implemented";
115 return 0;
ilnik531100d2017-02-21 11:33:24116#else
117 // Not implemented yet.
118 static_assert(
Scott Grahamf26e2902018-10-24 22:15:36119 false, "GetThreadCpuTimeNanos() platform support not yet implemented.");
ilnik531100d2017-02-21 11:33:24120#endif
121 return -1;
122}
123
124} // namespace rtc