You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/cpu_time.h"
  11. #include "rtc_base/logging.h"
  12. #include "rtc_base/time_utils.h"
  13. #if defined(WEBRTC_LINUX)
  14. #include <time.h>
  15. #elif defined(WEBRTC_MAC)
  16. #include <mach/mach_init.h>
  17. #include <mach/mach_port.h>
  18. #include <mach/thread_act.h>
  19. #include <mach/thread_info.h>
  20. #include <sys/resource.h>
  21. #include <sys/times.h>
  22. #include <sys/types.h>
  23. #include <unistd.h>
  24. #elif defined(WEBRTC_WIN)
  25. #include <windows.h>
  26. #endif
  27. #if defined(WEBRTC_WIN)
  28. namespace {
  29. // FILETIME resolution is 100 nanosecs.
  30. const int64_t kNanosecsPerFiletime = 100;
  31. } // namespace
  32. #endif
  33. namespace rtc {
  34. int64_t GetProcessCpuTimeNanos() {
  35. #if defined(WEBRTC_LINUX)
  36. struct timespec ts;
  37. if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
  38. return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
  39. } else {
  40. RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
  41. }
  42. #elif defined(WEBRTC_MAC)
  43. struct rusage rusage;
  44. if (getrusage(RUSAGE_SELF, &rusage) == 0) {
  45. return rusage.ru_utime.tv_sec * kNumNanosecsPerSec +
  46. rusage.ru_utime.tv_usec * kNumNanosecsPerMicrosec;
  47. } else {
  48. RTC_LOG_ERR(LS_ERROR) << "getrusage() failed.";
  49. }
  50. #elif defined(WEBRTC_WIN)
  51. FILETIME createTime;
  52. FILETIME exitTime;
  53. FILETIME kernelTime;
  54. FILETIME userTime;
  55. if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime,
  56. &userTime) != 0) {
  57. return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
  58. userTime.dwLowDateTime) *
  59. kNanosecsPerFiletime;
  60. } else {
  61. RTC_LOG_ERR(LS_ERROR) << "GetProcessTimes() failed.";
  62. }
  63. #elif defined(WEBRTC_FUCHSIA)
  64. RTC_LOG_ERR(LS_ERROR) << "GetProcessCpuTimeNanos() not implemented";
  65. return 0;
  66. #else
  67. // Not implemented yet.
  68. static_assert(
  69. false, "GetProcessCpuTimeNanos() platform support not yet implemented.");
  70. #endif
  71. return -1;
  72. }
  73. int64_t GetThreadCpuTimeNanos() {
  74. #if defined(WEBRTC_LINUX)
  75. struct timespec ts;
  76. if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) {
  77. return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
  78. } else {
  79. RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
  80. }
  81. #elif defined(WEBRTC_MAC)
  82. mach_port_t thread_port = mach_thread_self();
  83. thread_basic_info_data_t info;
  84. mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
  85. kern_return_t kr =
  86. thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)&info, &count);
  87. mach_port_deallocate(mach_task_self(), thread_port);
  88. if (kr == KERN_SUCCESS) {
  89. return info.user_time.seconds * kNumNanosecsPerSec +
  90. info.user_time.microseconds * kNumNanosecsPerMicrosec;
  91. } else {
  92. RTC_LOG_ERR(LS_ERROR) << "thread_info() failed.";
  93. }
  94. #elif defined(WEBRTC_WIN)
  95. FILETIME createTime;
  96. FILETIME exitTime;
  97. FILETIME kernelTime;
  98. FILETIME userTime;
  99. if (GetThreadTimes(GetCurrentThread(), &createTime, &exitTime, &kernelTime,
  100. &userTime) != 0) {
  101. return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
  102. userTime.dwLowDateTime) *
  103. kNanosecsPerFiletime;
  104. } else {
  105. RTC_LOG_ERR(LS_ERROR) << "GetThreadTimes() failed.";
  106. }
  107. #elif defined(WEBRTC_FUCHSIA)
  108. RTC_LOG_ERR(LS_ERROR) << "GetThreadCpuTimeNanos() not implemented";
  109. return 0;
  110. #else
  111. // Not implemented yet.
  112. static_assert(
  113. false, "GetThreadCpuTimeNanos() platform support not yet implemented.");
  114. #endif
  115. return -1;
  116. }
  117. } // namespace rtc