ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [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 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/cpu_time.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 14 | #include "rtc_base/time_utils.h" |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 15 | |
| 16 | #if defined(WEBRTC_LINUX) |
| 17 | #include <time.h> |
| 18 | #elif defined(WEBRTC_MAC) |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 19 | #include <mach/mach_init.h> |
Robert Sesek | b080051 | 2018-08-30 17:13:44 -0400 | [diff] [blame] | 20 | #include <mach/mach_port.h> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [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 03:33:24 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #elif defined(WEBRTC_WIN) |
| 28 | #include <windows.h> |
| 29 | #endif |
| 30 | |
| 31 | #if defined(WEBRTC_WIN) |
| 32 | namespace { |
| 33 | // FILETIME resolution is 100 nanosecs. |
| 34 | const int64_t kNanosecsPerFiletime = 100; |
| 35 | } // namespace |
| 36 | #endif |
| 37 | |
| 38 | namespace rtc { |
| 39 | |
| 40 | int64_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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 46 | RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed."; |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 47 | } |
| 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 54 | RTC_LOG_ERR(LS_ERROR) << "getrusage() failed."; |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 55 | } |
| 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 67 | RTC_LOG_ERR(LS_ERROR) << "GetProcessTimes() failed."; |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 68 | } |
Scott Graham | f26e290 | 2018-10-24 15:15:36 -0700 | [diff] [blame] | 69 | #elif defined(WEBRTC_FUCHSIA) |
| 70 | RTC_LOG_ERR(LS_ERROR) << "GetProcessCpuTimeNanos() not implemented"; |
| 71 | return 0; |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 72 | #else |
| 73 | // Not implemented yet. |
| 74 | static_assert( |
| 75 | false, "GetProcessCpuTimeNanos() platform support not yet implemented."); |
| 76 | #endif |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | int64_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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 86 | RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed."; |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 87 | } |
| 88 | #elif defined(WEBRTC_MAC) |
Robert Sesek | b080051 | 2018-08-30 17:13:44 -0400 | [diff] [blame] | 89 | mach_port_t thread_port = mach_thread_self(); |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 90 | thread_basic_info_data_t info; |
| 91 | mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; |
Robert Sesek | b080051 | 2018-08-30 17:13:44 -0400 | [diff] [blame] | 92 | 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) { |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 96 | return info.user_time.seconds * kNumNanosecsPerSec + |
| 97 | info.user_time.microseconds * kNumNanosecsPerMicrosec; |
| 98 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 99 | RTC_LOG_ERR(LS_ERROR) << "thread_info() failed."; |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 100 | } |
| 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 112 | RTC_LOG_ERR(LS_ERROR) << "GetThreadTimes() failed."; |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 113 | } |
Scott Graham | f26e290 | 2018-10-24 15:15:36 -0700 | [diff] [blame] | 114 | #elif defined(WEBRTC_FUCHSIA) |
| 115 | RTC_LOG_ERR(LS_ERROR) << "GetThreadCpuTimeNanos() not implemented"; |
| 116 | return 0; |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 117 | #else |
| 118 | // Not implemented yet. |
| 119 | static_assert( |
Scott Graham | f26e290 | 2018-10-24 15:15:36 -0700 | [diff] [blame] | 120 | false, "GetThreadCpuTimeNanos() platform support not yet implemented."); |
ilnik | 531100d | 2017-02-21 03:33:24 -0800 | [diff] [blame] | 121 | #endif |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | } // namespace rtc |