2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 #include <lttng/ust-clock.h>
14 #include <urcu/system.h>
15 #include <urcu/arch.h>
17 #include "common/logging.h"
20 #include "common/getenv.h"
22 struct lttng_ust_trace_clock
*lttng_ust_trace_clock
;
25 struct lttng_ust_trace_clock user_tc
;
31 uint64_t trace_clock_freq_monotonic(void)
37 int trace_clock_uuid_monotonic(char *uuid
)
44 * boot_id needs to be read once before being used concurrently
45 * to deal with a Linux kernel race. A fix is proposed for
46 * upstream, but the work-around is needed for older kernels.
48 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
52 len
= fread(uuid
, 1, LTTNG_UST_UUID_STR_LEN
- 1, fp
);
53 if (len
< LTTNG_UST_UUID_STR_LEN
- 1) {
57 uuid
[LTTNG_UST_UUID_STR_LEN
- 1] = '\0';
64 const char *trace_clock_name_monotonic(void)
70 const char *trace_clock_description_monotonic(void)
72 return "Monotonic Clock";
75 int lttng_ust_trace_clock_set_read64_cb(lttng_ust_clock_read64_function read64_cb
)
77 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
79 user_tc
.read64
= read64_cb
;
83 int lttng_ust_trace_clock_get_read64_cb(lttng_ust_clock_read64_function
*read64_cb
)
85 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
87 if (caa_likely(!ltc
)) {
88 *read64_cb
= &trace_clock_read64_monotonic
;
90 cmm_read_barrier_depends(); /* load ltc before content */
91 *read64_cb
= ltc
->read64
;
96 int lttng_ust_trace_clock_set_freq_cb(lttng_ust_clock_freq_function freq_cb
)
98 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
100 user_tc
.freq
= freq_cb
;
104 int lttng_ust_trace_clock_get_freq_cb(lttng_ust_clock_freq_function
*freq_cb
)
106 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
108 if (caa_likely(!ltc
)) {
109 *freq_cb
= &trace_clock_freq_monotonic
;
111 cmm_read_barrier_depends(); /* load ltc before content */
112 *freq_cb
= ltc
->freq
;
117 int lttng_ust_trace_clock_set_uuid_cb(lttng_ust_clock_uuid_function uuid_cb
)
119 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
121 user_tc
.uuid
= uuid_cb
;
125 int lttng_ust_trace_clock_get_uuid_cb(lttng_ust_clock_uuid_function
*uuid_cb
)
127 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
129 if (caa_likely(!ltc
)) {
130 *uuid_cb
= &trace_clock_uuid_monotonic
;
132 cmm_read_barrier_depends(); /* load ltc before content */
133 *uuid_cb
= ltc
->uuid
;
138 int lttng_ust_trace_clock_set_name_cb(lttng_ust_clock_name_function name_cb
)
140 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
142 user_tc
.name
= name_cb
;
146 int lttng_ust_trace_clock_get_name_cb(lttng_ust_clock_name_function
*name_cb
)
148 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
150 if (caa_likely(!ltc
)) {
151 *name_cb
= &trace_clock_name_monotonic
;
153 cmm_read_barrier_depends(); /* load ltc before content */
154 *name_cb
= ltc
->name
;
159 int lttng_ust_trace_clock_set_description_cb(lttng_ust_clock_description_function description_cb
)
161 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
163 user_tc
.description
= description_cb
;
167 int lttng_ust_trace_clock_get_description_cb(lttng_ust_clock_description_function
*description_cb
)
169 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
171 if (caa_likely(!ltc
)) {
172 *description_cb
= &trace_clock_description_monotonic
;
174 cmm_read_barrier_depends(); /* load ltc before content */
175 *description_cb
= ltc
->description
;
180 int lttng_ust_enable_trace_clock_override(void)
182 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
190 if (!user_tc
.description
)
192 /* Use default uuid cb when NULL */
193 cmm_smp_mb(); /* Store callbacks before trace clock */
194 CMM_STORE_SHARED(lttng_ust_trace_clock
, &user_tc
);
198 void lttng_ust_clock_init(void)
201 void (*libinit
)(void);
205 libname
= lttng_ust_getenv("LTTNG_UST_CLOCK_PLUGIN");
208 clock_handle
= dlopen(libname
, RTLD_NOW
);
210 PERROR("Cannot load LTTng UST clock override library %s",
215 libinit
= (void (*)(void)) dlsym(clock_handle
,
216 "lttng_ust_clock_plugin_init");
218 PERROR("Cannot find LTTng UST clock override library %s initialization function lttng_ust_clock_plugin_init()",