diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index eb8044266e..4a9a5463ed 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -1848,6 +1848,9 @@ rtc_library("rtc_base_tests_utils") { "//third_party/abseil-cpp/absl/strings", "//third_party/abseil-cpp/absl/types:optional", ] + if (is_fuchsia) { + deps += [ "//third_party/fuchsia-sdk/sdk/pkg/zx" ] + } } rtc_library("task_queue_for_test") { diff --git a/rtc_base/cpu_time.cc b/rtc_base/cpu_time.cc index a8e542b5a2..d3fee50c49 100644 --- a/rtc_base/cpu_time.cc +++ b/rtc_base/cpu_time.cc @@ -26,6 +26,10 @@ #include #elif defined(WEBRTC_WIN) #include +#elif defined(WEBRTC_FUCHSIA) +#include +#include +#include #endif #if defined(WEBRTC_WIN) @@ -39,10 +43,17 @@ namespace rtc { int64_t GetProcessCpuTimeNanos() { #if defined(WEBRTC_FUCHSIA) - RTC_LOG_ERR(LS_ERROR) << "GetProcessCpuTimeNanos() not implemented"; - return 0; -#else -#if defined(WEBRTC_LINUX) + zx_info_task_runtime_t runtime_info; + zx_status_t status = + zx::process::self()->get_info(ZX_INFO_TASK_RUNTIME, &runtime_info, + sizeof(runtime_info), nullptr, nullptr); + if (status == ZX_OK) { + return runtime_info.cpu_time; + } else { + RTC_LOG_ERR(LS_ERROR) << "get_info() failed: " + << zx_status_get_string(status); + } +#elif defined(WEBRTC_LINUX) struct timespec ts; if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) { return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec; @@ -76,15 +87,21 @@ int64_t GetProcessCpuTimeNanos() { false, "GetProcessCpuTimeNanos() platform support not yet implemented."); #endif return -1; -#endif // defined(WEBRTC_FUCHSIA) } int64_t GetThreadCpuTimeNanos() { #if defined(WEBRTC_FUCHSIA) - RTC_LOG_ERR(LS_ERROR) << "GetThreadCpuTimeNanos() not implemented"; - return 0; -#else -#if defined(WEBRTC_LINUX) + zx_info_task_runtime_t runtime_info; + zx_status_t status = + zx::thread::self()->get_info(ZX_INFO_TASK_RUNTIME, &runtime_info, + sizeof(runtime_info), nullptr, nullptr); + if (status == ZX_OK) { + return runtime_info.cpu_time; + } else { + RTC_LOG_ERR(LS_ERROR) << "get_info() failed: " + << zx_status_get_string(status); + } +#elif defined(WEBRTC_LINUX) struct timespec ts; if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) { return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec; @@ -123,7 +140,6 @@ int64_t GetThreadCpuTimeNanos() { false, "GetThreadCpuTimeNanos() platform support not yet implemented."); #endif return -1; -#endif // defined(WEBRTC_FUCHSIA) } } // namespace rtc