ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/cpu_time.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 14 | #include "rtc_base/time_utils.h" |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 15 | |
| 16 | #if defined(WEBRTC_LINUX) |
| 17 | #include <time.h> |
| 18 | #elif defined(WEBRTC_MAC) |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 19 | #include <mach/mach_init.h> |
Robert Sesek | b080051 | 2018-08-30 21:13:44 | [diff] [blame] | 20 | #include <mach/mach_port.h> |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 21 | #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> |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #elif defined(WEBRTC_WIN) |
| 28 | #include <windows.h> |
Sarah Pham | f1fa6ac7 | 2023-01-15 23:43:34 | [diff] [blame] | 29 | #elif defined(WEBRTC_FUCHSIA) |
| 30 | #include <lib/zx/process.h> |
| 31 | #include <lib/zx/thread.h> |
| 32 | #include <zircon/status.h> |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | #if defined(WEBRTC_WIN) |
| 36 | namespace { |
| 37 | // FILETIME resolution is 100 nanosecs. |
| 38 | const int64_t kNanosecsPerFiletime = 100; |
| 39 | } // namespace |
| 40 | #endif |
| 41 | |
| 42 | namespace rtc { |
| 43 | |
| 44 | int64_t GetProcessCpuTimeNanos() { |
Nico Weber | b8612c7 | 2021-08-20 15:01:35 | [diff] [blame] | 45 | #if defined(WEBRTC_FUCHSIA) |
Sarah Pham | f1fa6ac7 | 2023-01-15 23:43:34 | [diff] [blame] | 46 | 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) |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 57 | 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 Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 61 | RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed."; |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 62 | } |
| 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 Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 69 | RTC_LOG_ERR(LS_ERROR) << "getrusage() failed."; |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 70 | } |
| 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 Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 82 | RTC_LOG_ERR(LS_ERROR) << "GetProcessTimes() failed."; |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 83 | } |
| 84 | #else |
| 85 | // Not implemented yet. |
| 86 | static_assert( |
| 87 | false, "GetProcessCpuTimeNanos() platform support not yet implemented."); |
| 88 | #endif |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | int64_t GetThreadCpuTimeNanos() { |
Nico Weber | b8612c7 | 2021-08-20 15:01:35 | [diff] [blame] | 93 | #if defined(WEBRTC_FUCHSIA) |
Sarah Pham | f1fa6ac7 | 2023-01-15 23:43:34 | [diff] [blame] | 94 | 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) |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 105 | 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 Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 109 | RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed."; |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 110 | } |
| 111 | #elif defined(WEBRTC_MAC) |
Robert Sesek | b080051 | 2018-08-30 21:13:44 | [diff] [blame] | 112 | mach_port_t thread_port = mach_thread_self(); |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 113 | thread_basic_info_data_t info; |
| 114 | mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; |
Robert Sesek | b080051 | 2018-08-30 21:13:44 | [diff] [blame] | 115 | 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) { |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 119 | return info.user_time.seconds * kNumNanosecsPerSec + |
| 120 | info.user_time.microseconds * kNumNanosecsPerMicrosec; |
| 121 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 122 | RTC_LOG_ERR(LS_ERROR) << "thread_info() failed."; |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 123 | } |
| 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 Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 135 | RTC_LOG_ERR(LS_ERROR) << "GetThreadTimes() failed."; |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 136 | } |
| 137 | #else |
| 138 | // Not implemented yet. |
| 139 | static_assert( |
Scott Graham | f26e290 | 2018-10-24 22:15:36 | [diff] [blame] | 140 | false, "GetThreadCpuTimeNanos() platform support not yet implemented."); |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 141 | #endif |
| 142 | return -1; |
ilnik | 531100d | 2017-02-21 11:33:24 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace rtc |