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