2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 #include <usterr-signal-safe.h>
14 #include <lttng/ust-clock.h>
15 #include <urcu/system.h>
16 #include <urcu/arch.h>
21 struct lttng_ust_trace_clock
*lttng_ust_trace_clock
;
24 struct lttng_ust_trace_clock user_tc
;
30 uint64_t trace_clock_freq_monotonic(void)
36 int trace_clock_uuid_monotonic(char *uuid
)
43 * boot_id needs to be read once before being used concurrently
44 * to deal with a Linux kernel race. A fix is proposed for
45 * upstream, but the work-around is needed for older kernels.
47 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
51 len
= fread(uuid
, 1, LTTNG_UST_UUID_STR_LEN
- 1, fp
);
52 if (len
< LTTNG_UST_UUID_STR_LEN
- 1) {
56 uuid
[LTTNG_UST_UUID_STR_LEN
- 1] = '\0';
63 const char *trace_clock_name_monotonic(void)
69 const char *trace_clock_description_monotonic(void)
71 return "Monotonic Clock";
74 int lttng_ust_trace_clock_set_read64_cb(lttng_ust_clock_read64_function read64_cb
)
76 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
78 user_tc
.read64
= read64_cb
;
82 int lttng_ust_trace_clock_get_read64_cb(lttng_ust_clock_read64_function
*read64_cb
)
84 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
86 if (caa_likely(!ltc
)) {
87 *read64_cb
= &trace_clock_read64_monotonic
;
89 cmm_read_barrier_depends(); /* load ltc before content */
90 *read64_cb
= ltc
->read64
;
95 int lttng_ust_trace_clock_set_freq_cb(lttng_ust_clock_freq_function freq_cb
)
97 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
99 user_tc
.freq
= freq_cb
;
103 int lttng_ust_trace_clock_get_freq_cb(lttng_ust_clock_freq_function
*freq_cb
)
105 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
107 if (caa_likely(!ltc
)) {
108 *freq_cb
= &trace_clock_freq_monotonic
;
110 cmm_read_barrier_depends(); /* load ltc before content */
111 *freq_cb
= ltc
->freq
;
116 int lttng_ust_trace_clock_set_uuid_cb(lttng_ust_clock_uuid_function uuid_cb
)
118 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
120 user_tc
.uuid
= uuid_cb
;
124 int lttng_ust_trace_clock_get_uuid_cb(lttng_ust_clock_uuid_function
*uuid_cb
)
126 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
128 if (caa_likely(!ltc
)) {
129 *uuid_cb
= &trace_clock_uuid_monotonic
;
131 cmm_read_barrier_depends(); /* load ltc before content */
132 *uuid_cb
= ltc
->uuid
;
137 int lttng_ust_trace_clock_set_name_cb(lttng_ust_clock_name_function name_cb
)
139 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
141 user_tc
.name
= name_cb
;
145 int lttng_ust_trace_clock_get_name_cb(lttng_ust_clock_name_function
*name_cb
)
147 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
149 if (caa_likely(!ltc
)) {
150 *name_cb
= &trace_clock_name_monotonic
;
152 cmm_read_barrier_depends(); /* load ltc before content */
153 *name_cb
= ltc
->name
;
158 int lttng_ust_trace_clock_set_description_cb(lttng_ust_clock_description_function description_cb
)
160 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
162 user_tc
.description
= description_cb
;
166 int lttng_ust_trace_clock_get_description_cb(lttng_ust_clock_description_function
*description_cb
)
168 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
170 if (caa_likely(!ltc
)) {
171 *description_cb
= &trace_clock_description_monotonic
;
173 cmm_read_barrier_depends(); /* load ltc before content */
174 *description_cb
= ltc
->description
;
179 int lttng_ust_enable_trace_clock_override(void)
181 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
189 if (!user_tc
.description
)
191 /* Use default uuid cb when NULL */
192 cmm_smp_mb(); /* Store callbacks before trace clock */
193 CMM_STORE_SHARED(lttng_ust_trace_clock
, &user_tc
);
197 void lttng_ust_clock_init(void)
200 void (*libinit
)(void);
204 libname
= lttng_ust_getenv("LTTNG_UST_CLOCK_PLUGIN");
207 clock_handle
= dlopen(libname
, RTLD_NOW
);
209 PERROR("Cannot load LTTng UST clock override library %s",
214 libinit
= (void (*)(void)) dlsym(clock_handle
,
215 "lttng_ust_clock_plugin_init");
217 PERROR("Cannot find LTTng UST clock override library %s initialization function lttng_ust_clock_plugin_init()",