From f1fa6ac7bfeb056ea961ebe649fe8fe51464f202 Mon Sep 17 00:00:00 2001 From: Sarah Pham Date: Mon, 16 Jan 2023 10:43:34 +1100 Subject: [PATCH] Implement time functions for Fuchsia. Implement GetProcessCpuTimeNanos and GetThreadCpuTimeNanos for Fuchsia. This is needed for the tests call_perf_tests and video_pc_full_stack_tests on Fuchsia. Bug: fuchsia:115601 Change-Id: Idd10db93d4087d10896ae3fde6abbc37176f625e Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/290920 Reviewed-by: Christoffer Jansson Reviewed-by: Mirko Bonadei Commit-Queue: Sarah Pham Cr-Commit-Position: refs/heads/main@{#39119} --- rtc_base/BUILD.gn | 3 +++ rtc_base/cpu_time.cc | 36 ++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 10 deletions(-) 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