| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef _UST_COMMON_CLOCK_H |
| 8 | #define _UST_COMMON_CLOCK_H |
| 9 | |
| 10 | #include <time.h> |
| 11 | #include <sys/time.h> |
| 12 | #include <stdint.h> |
| 13 | #include <stddef.h> |
| 14 | #include <stdio.h> |
| 15 | |
| 16 | #include <urcu/system.h> |
| 17 | #include <urcu/arch.h> |
| 18 | #include <lttng/ust-clock.h> |
| 19 | |
| 20 | struct lttng_ust_trace_clock { |
| 21 | uint64_t (*read64)(void); |
| 22 | uint64_t (*freq)(void); |
| 23 | int (*uuid)(char *uuid); |
| 24 | const char *(*name)(void); |
| 25 | const char *(*description)(void); |
| 26 | }; |
| 27 | |
| 28 | /* |
| 29 | * The trace clock is a public symbol of liblttng-ust-common accessed by other |
| 30 | * libraries through the trace_clock_read64 static inline function. It is |
| 31 | * initialised in the liblttng-ust-common constructor. |
| 32 | */ |
| 33 | extern struct lttng_ust_trace_clock *lttng_ust_trace_clock; |
| 34 | |
| 35 | /* Use the kernel MONOTONIC clock. */ |
| 36 | |
| 37 | static __inline__ |
| 38 | uint64_t trace_clock_read64_monotonic(void) |
| 39 | { |
| 40 | struct timespec ts; |
| 41 | |
| 42 | if (caa_unlikely(clock_gettime(CLOCK_MONOTONIC, &ts))) { |
| 43 | ts.tv_sec = 0; |
| 44 | ts.tv_nsec = 0; |
| 45 | } |
| 46 | return ((uint64_t) ts.tv_sec * 1000000000ULL) + ts.tv_nsec; |
| 47 | } |
| 48 | |
| 49 | static __inline__ |
| 50 | uint64_t trace_clock_read64(void) |
| 51 | { |
| 52 | struct lttng_ust_trace_clock *ltc = CMM_LOAD_SHARED(lttng_ust_trace_clock); |
| 53 | |
| 54 | if (caa_likely(!ltc)) { |
| 55 | return trace_clock_read64_monotonic(); |
| 56 | } else { |
| 57 | cmm_read_barrier_depends(); /* load ltc before content */ |
| 58 | return ltc->read64(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | #endif /* _UST_COMMON_CLOCK_H */ |