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 <jansson@google.com>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Sarah Pham <smpham@google.com>
Cr-Commit-Position: refs/heads/main@{#39119}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index eb80442..4a9a546 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -1848,6 +1848,9 @@
"//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 a8e542b..d3fee50 100644
--- a/rtc_base/cpu_time.cc
+++ b/rtc_base/cpu_time.cc
@@ -26,6 +26,10 @@
#include <unistd.h>
#elif defined(WEBRTC_WIN)
#include <windows.h>
+#elif defined(WEBRTC_FUCHSIA)
+#include <lib/zx/process.h>
+#include <lib/zx/thread.h>
+#include <zircon/status.h>
#endif
#if defined(WEBRTC_WIN)
@@ -39,10 +43,17 @@
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 @@
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 @@
false, "GetThreadCpuTimeNanos() platform support not yet implemented.");
#endif
return -1;
-#endif // defined(WEBRTC_FUCHSIA)
}
} // namespace rtc