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 <lttng/ust-events.h>
15 #include <urcu/system.h>
16 #include <urcu/arch.h>
18 #include "common/logging.h"
19 #include "common/getenv.h"
21 #include "lib/lttng-ust-common/clock.h"
23 struct lttng_ust_trace_clock
*lttng_ust_trace_clock
;
26 struct lttng_ust_trace_clock user_tc
;
32 uint64_t trace_clock_freq_monotonic(void)
38 int trace_clock_uuid_monotonic(char *uuid
)
45 * boot_id needs to be read once before being used concurrently
46 * to deal with a Linux kernel race. A fix is proposed for
47 * upstream, but the work-around is needed for older kernels.
49 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
53 len
= fread(uuid
, 1, LTTNG_UST_UUID_STR_LEN
- 1, fp
);
54 if (len
< LTTNG_UST_UUID_STR_LEN
- 1) {
58 uuid
[LTTNG_UST_UUID_STR_LEN
- 1] = '\0';
65 const char *trace_clock_name_monotonic(void)
71 const char *trace_clock_description_monotonic(void)
73 return "Monotonic Clock";
76 int lttng_ust_trace_clock_set_read64_cb(lttng_ust_clock_read64_function read64_cb
)
78 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
80 user_tc
.read64
= read64_cb
;
84 int lttng_ust_trace_clock_get_read64_cb(lttng_ust_clock_read64_function
*read64_cb
)
86 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
88 if (caa_likely(!ltc
)) {
89 *read64_cb
= &trace_clock_read64_monotonic
;
91 cmm_read_barrier_depends(); /* load ltc before content */
92 *read64_cb
= ltc
->read64
;
97 int lttng_ust_trace_clock_set_freq_cb(lttng_ust_clock_freq_function freq_cb
)
99 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
101 user_tc
.freq
= freq_cb
;
105 int lttng_ust_trace_clock_get_freq_cb(lttng_ust_clock_freq_function
*freq_cb
)
107 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
109 if (caa_likely(!ltc
)) {
110 *freq_cb
= &trace_clock_freq_monotonic
;
112 cmm_read_barrier_depends(); /* load ltc before content */
113 *freq_cb
= ltc
->freq
;
118 int lttng_ust_trace_clock_set_uuid_cb(lttng_ust_clock_uuid_function uuid_cb
)
120 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
122 user_tc
.uuid
= uuid_cb
;
126 int lttng_ust_trace_clock_get_uuid_cb(lttng_ust_clock_uuid_function
*uuid_cb
)
128 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
130 if (caa_likely(!ltc
)) {
131 *uuid_cb
= &trace_clock_uuid_monotonic
;
133 cmm_read_barrier_depends(); /* load ltc before content */
134 *uuid_cb
= ltc
->uuid
;
139 int lttng_ust_trace_clock_set_name_cb(lttng_ust_clock_name_function name_cb
)
141 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
143 user_tc
.name
= name_cb
;
147 int lttng_ust_trace_clock_get_name_cb(lttng_ust_clock_name_function
*name_cb
)
149 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
151 if (caa_likely(!ltc
)) {
152 *name_cb
= &trace_clock_name_monotonic
;
154 cmm_read_barrier_depends(); /* load ltc before content */
155 *name_cb
= ltc
->name
;
160 int lttng_ust_trace_clock_set_description_cb(lttng_ust_clock_description_function description_cb
)
162 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
164 user_tc
.description
= description_cb
;
168 int lttng_ust_trace_clock_get_description_cb(lttng_ust_clock_description_function
*description_cb
)
170 struct lttng_ust_trace_clock
*ltc
= CMM_LOAD_SHARED(lttng_ust_trace_clock
);
172 if (caa_likely(!ltc
)) {
173 *description_cb
= &trace_clock_description_monotonic
;
175 cmm_read_barrier_depends(); /* load ltc before content */
176 *description_cb
= ltc
->description
;
181 int lttng_ust_enable_trace_clock_override(void)
183 if (CMM_LOAD_SHARED(lttng_ust_trace_clock
))
191 if (!user_tc
.description
)
193 /* Use default uuid cb when NULL */
194 cmm_smp_mb(); /* Store callbacks before trace clock */
195 CMM_STORE_SHARED(lttng_ust_trace_clock
, &user_tc
);
199 void lttng_ust_clock_init(void)
202 void (*libinit
)(void);
206 libname
= lttng_ust_getenv("LTTNG_UST_CLOCK_PLUGIN");
209 clock_handle
= dlopen(libname
, RTLD_NOW
);
211 PERROR("Cannot load LTTng UST clock override library %s",
216 libinit
= (void (*)(void)) dlsym(clock_handle
,
217 "lttng_ust_clock_plugin_init");
219 PERROR("Cannot find LTTng UST clock override library %s initialization function lttng_ust_clock_plugin_init()",