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