zx_clock_get

NAME

Acquire the current time.

STATUS

zx_clock_adjust is currently DEPRECATED. Do not make use of it in any new code. See the ALTERNATIVES section of this page for the updated way to fetch the values that used to be accessible via zx_clock_get().

SYNOPSIS

  1. #include <zircon/syscalls.h>
  2. zx_status_t zx_clock_get(zx_clock_t clock_id, zx_time_t* out);

DESCRIPTION

zx_clock_get() returns the current time of clock_id via out, and returns whether clock_id was valid.

SUPPORTED CLOCK IDS

ZX_CLOCK_MONOTONIC number of nanoseconds since the system was powered on.

ZX_CLOCK_UTC number of wall clock nanoseconds since the Unix epoch (midnight on January 1 1970) in UTC

ZX_CLOCK_THREAD number of nanoseconds the current thread has been running for.

RIGHTS

TODO(fxbug.dev/32253)

RETURN VALUE

On success, zx_clock_get() returns ZX_OK.

ERRORS

ZX_ERR_INVALID_ARGS clock_id is not a valid clock id, or out is an invalid pointer.

ALTERNATIVES {#alternatives}

zx_clock_get() has been deprecated. Refer to the instructions below on how to gain access to the timelines that had been accessible via calls to zx_clock_get().

  1. ZX_CLOCK_MONOTONIC
  2. ZX_CLOCK_UTC
  3. ZX_CLOCK_THREAD

ZX_CLOCK_MONOTONIC {#alternative-zx-clock-monotonic}

zx_clock_get(ZX_CLOCK_MONOTONIC, out_ptr) has been replaced with calls to zx_clock_get_monotonic(). Unlike zx_clock_get, zx_clock_get_monotonic is almost always implemented in a way that does not need an actual syscall (which can provide a performance benefit) and it cannot ever return an error (which can help to simplify code). For example:

  1. // Where old code used to say something like
  2. zx_time_t now;
  3. zx_status_t status = zx_clock_get(ZX_CLOCK_MONOTONIC, &now);
  4. if (status != ZX_OK) {
  5. // Handle failure here
  6. }
  7. // It can now simply say the following instead
  8. zx_time_t now = zx_clock_get_monotonic();

ZX_CLOCK_UTC {#alternative-zx-clock-utc}

Supplying a clock that provides an accurate representation of the UTC timeline is no longer a feature provided directly by the kernel. Instead, it has become the responsibility of the Component Manager to distribute a handle to a kernel clock object to processes that are permitted to access UTC. This clock handle (when available) is used by the various runtimes to implement their version of access to UTC. In addition, some runtimes may provide direct access to this clock handle allowing the user to get even more detailed information about the clock.

User who wish to access UTC in their programs should make use of their runtime’s standard UTC APIs, or borrow a handle to the underlying clock object if they need access to the native clock handle.

Here are a few examples of some of the ways that a user in a C runtime can gain access to UTC.

  1. #include <inttypes.h>
  2. #include <zircon/time.h>
  3. // Fetching the clock using gettimeofday
  4. #include <sys/time.h>
  5. {
  6. struct timeval_t now_utc;
  7. int status = gettimeofday(&now_utc, NULL);
  8. if (status == 0) {
  9. zx_time_t nsec = ((zx_time_t)now_utc.tv_sec * ZX_SEC(1)) +
  10. ((zx_time_t)now_utc.tv_usec * ZX_USEC(1))
  11. printf("It has been " PRId64 " nSec since the epoch.\n", nsec);
  12. } else {
  13. printf("gettimeofday(...) call failed (errno = %d)\n", errno);
  14. }
  15. }
  16. // Fetching the clock using clock_gettime
  17. #include <time.h>
  18. {
  19. struct timespec now_utc;
  20. int status = clock_gettime(CLOCK_REALTIME, &now_utc, NULL);
  21. if (status == 0) {
  22. zx_time_t nsec = ((zx_time_t)now_utc.tv_sec * ZX_SEC(1)) +
  23. ((zx_time_t)now_utc.tv_nsec * ZX_NSEC(1))
  24. printf("It has been " PRId64 " nSec since the epoch.\n", nsec);
  25. } else {
  26. printf("clock_gettime(...) call failed (errno = %d)\n", errno);
  27. }
  28. }
  29. // Gaining direct access to the process's UTC reference clock object.
  30. #include <zircon/clock.h>
  31. #include <zircon/utc.h>
  32. {
  33. // This is a borrowed handle. Do not close it, and do not replace it using
  34. // zx_utc_reference_swap while using it.
  35. zx_handle_t utc_clock = zx_utc_reference_get();
  36. if (utc_clock != ZX_HANDLE_INVALID) {
  37. zx_time_t nsec;
  38. zx_status_t status = zx_clock_read(utc_clock, &nsec);
  39. if (status == ZX_OK) {
  40. printf("It has been " PRId64 " nSec since the epoch.\n", nsec);
  41. } else {
  42. printf("zx_clock_read(...) syscall failed (status = %d)\n", status);
  43. }
  44. } else {
  45. printf("Error, our runtime has no clock assigned to it!");
  46. }
  47. }

ZX_CLOCK_THREAD {#alternative-zx-clock-thread}

The ZX_CLOCK_THREAD clock was never really a clock at all. Instead it provided access to value that was a property of a thread object, specifically the cumulative runtime of the calling thread.

Moving forward, this value can be accessed for any thread using zx_object_get_info() using the topic ZX_INFO_THREAD_STATS. Users will need to have access to the handle thread they wish to query, and that handle will need to have ZX_RIGHT_INSPECT rights set on it. This is generally the case for threads in a process created by the user.

Here is an example of how to query this value for the current thread running in the C/C++ runtime. The specific API for obtaining access to the current thread handle, or for making a syscall, will vary from language runtime to language runtime.

  1. #include <inttypes.h>
  2. #include <threads.h>
  3. #include <zircon/syscalls.h>
  4. #include <zircon/syscalls/object.h>
  5. #include <zircon/threads.h>
  6. void test() {
  7. zx_info_thread_stats_t info;
  8. zx_status_t status;
  9. status = zx_object_get_info(thrd_get_zx_handle(thrd_current()),
  10. ZX_INFO_THREAD_STATS,
  11. &info, sizeof(info),
  12. NULL, NULL);
  13. if (status != ZX_OK) {
  14. printf("Current thread has a runtime of " PRId64 "\n", info.total_runtime);
  15. } else {
  16. printf("Failed to fetch current thread runtime (status %d)\n", status);
  17. }
  18. }