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}
This commit is contained in:
Sarah Pham 2023-01-16 10:43:34 +11:00 committed by WebRTC LUCI CQ
parent 40b5bd72d0
commit f1fa6ac7bf
2 changed files with 29 additions and 10 deletions

View file

@ -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") {

View file

@ -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 @@ 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