27ecccd91cc2da6e1ad97bb1b80ede3a66030a42
[lttng-ust.git] / liblttng-ust / lttng-clock.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #define _LGPL_SOURCE
8 #include <error.h>
9 #include <dlfcn.h>
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <usterr-signal-safe.h>
14 #include <lttng/ust-clock.h>
15 #include <urcu/system.h>
16 #include <urcu/arch.h>
17
18 #include "clock.h"
19 #include "getenv.h"
20
21 struct lttng_ust_trace_clock *lttng_ust_trace_clock;
22
23 static
24 struct lttng_ust_trace_clock user_tc;
25
26 static
27 void *clock_handle;
28
29 static
30 uint64_t trace_clock_freq_monotonic(void)
31 {
32 return 1000000000ULL;
33 }
34
35 static
36 int trace_clock_uuid_monotonic(char *uuid)
37 {
38 int ret = 0;
39 size_t len;
40 FILE *fp;
41
42 /*
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.
46 */
47 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
48 if (!fp) {
49 return -ENOENT;
50 }
51 len = fread(uuid, 1, LTTNG_UST_UUID_STR_LEN - 1, fp);
52 if (len < LTTNG_UST_UUID_STR_LEN - 1) {
53 ret = -EINVAL;
54 goto end;
55 }
56 uuid[LTTNG_UST_UUID_STR_LEN - 1] = '\0';
57 end:
58 fclose(fp);
59 return ret;
60 }
61
62 static
63 const char *trace_clock_name_monotonic(void)
64 {
65 return "monotonic";
66 }
67
68 static
69 const char *trace_clock_description_monotonic(void)
70 {
71 return "Monotonic Clock";
72 }
73
74 int lttng_ust_trace_clock_set_read64_cb(lttng_ust_clock_read64_function read64_cb)
75 {
76 if (CMM_LOAD_SHARED(lttng_ust_trace_clock))
77 return -EBUSY;
78 user_tc.read64 = read64_cb;
79 return 0;
80 }
81
82 int lttng_ust_trace_clock_get_read64_cb(lttng_ust_clock_read64_function *read64_cb)
83 {
84 struct lttng_ust_trace_clock *ltc = CMM_LOAD_SHARED(lttng_ust_trace_clock);
85
86 if (caa_likely(!ltc)) {
87 *read64_cb = &trace_clock_read64_monotonic;
88 } else {
89 cmm_read_barrier_depends(); /* load ltc before content */
90 *read64_cb = ltc->read64;
91 }
92 return 0;
93 }
94
95 int lttng_ust_trace_clock_set_freq_cb(lttng_ust_clock_freq_function freq_cb)
96 {
97 if (CMM_LOAD_SHARED(lttng_ust_trace_clock))
98 return -EBUSY;
99 user_tc.freq = freq_cb;
100 return 0;
101 }
102
103 int lttng_ust_trace_clock_get_freq_cb(lttng_ust_clock_freq_function *freq_cb)
104 {
105 struct lttng_ust_trace_clock *ltc = CMM_LOAD_SHARED(lttng_ust_trace_clock);
106
107 if (caa_likely(!ltc)) {
108 *freq_cb = &trace_clock_freq_monotonic;
109 } else {
110 cmm_read_barrier_depends(); /* load ltc before content */
111 *freq_cb = ltc->freq;
112 }
113 return 0;
114 }
115
116 int lttng_ust_trace_clock_set_uuid_cb(lttng_ust_clock_uuid_function uuid_cb)
117 {
118 if (CMM_LOAD_SHARED(lttng_ust_trace_clock))
119 return -EBUSY;
120 user_tc.uuid = uuid_cb;
121 return 0;
122 }
123
124 int lttng_ust_trace_clock_get_uuid_cb(lttng_ust_clock_uuid_function *uuid_cb)
125 {
126 struct lttng_ust_trace_clock *ltc = CMM_LOAD_SHARED(lttng_ust_trace_clock);
127
128 if (caa_likely(!ltc)) {
129 *uuid_cb = &trace_clock_uuid_monotonic;
130 } else {
131 cmm_read_barrier_depends(); /* load ltc before content */
132 *uuid_cb = ltc->uuid;
133 }
134 return 0;
135 }
136
137 int lttng_ust_trace_clock_set_name_cb(lttng_ust_clock_name_function name_cb)
138 {
139 if (CMM_LOAD_SHARED(lttng_ust_trace_clock))
140 return -EBUSY;
141 user_tc.name = name_cb;
142 return 0;
143 }
144
145 int lttng_ust_trace_clock_get_name_cb(lttng_ust_clock_name_function *name_cb)
146 {
147 struct lttng_ust_trace_clock *ltc = CMM_LOAD_SHARED(lttng_ust_trace_clock);
148
149 if (caa_likely(!ltc)) {
150 *name_cb = &trace_clock_name_monotonic;
151 } else {
152 cmm_read_barrier_depends(); /* load ltc before content */
153 *name_cb = ltc->name;
154 }
155 return 0;
156 }
157
158 int lttng_ust_trace_clock_set_description_cb(lttng_ust_clock_description_function description_cb)
159 {
160 if (CMM_LOAD_SHARED(lttng_ust_trace_clock))
161 return -EBUSY;
162 user_tc.description = description_cb;
163 return 0;
164 }
165
166 int lttng_ust_trace_clock_get_description_cb(lttng_ust_clock_description_function *description_cb)
167 {
168 struct lttng_ust_trace_clock *ltc = CMM_LOAD_SHARED(lttng_ust_trace_clock);
169
170 if (caa_likely(!ltc)) {
171 *description_cb = &trace_clock_description_monotonic;
172 } else {
173 cmm_read_barrier_depends(); /* load ltc before content */
174 *description_cb = ltc->description;
175 }
176 return 0;
177 }
178
179 int lttng_ust_enable_trace_clock_override(void)
180 {
181 if (CMM_LOAD_SHARED(lttng_ust_trace_clock))
182 return -EBUSY;
183 if (!user_tc.read64)
184 return -EINVAL;
185 if (!user_tc.freq)
186 return -EINVAL;
187 if (!user_tc.name)
188 return -EINVAL;
189 if (!user_tc.description)
190 return -EINVAL;
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);
194 return 0;
195 }
196
197 void lttng_ust_clock_init(void)
198 {
199 const char *libname;
200 void (*libinit)(void);
201
202 if (clock_handle)
203 return;
204 libname = lttng_ust_getenv("LTTNG_UST_CLOCK_PLUGIN");
205 if (!libname)
206 return;
207 clock_handle = dlopen(libname, RTLD_NOW);
208 if (!clock_handle) {
209 PERROR("Cannot load LTTng UST clock override library %s",
210 libname);
211 return;
212 }
213 dlerror();
214 libinit = (void (*)(void)) dlsym(clock_handle,
215 "lttng_ust_clock_plugin_init");
216 if (!libinit) {
217 PERROR("Cannot find LTTng UST clock override library %s initialization function lttng_ust_clock_plugin_init()",
218 libname);
219 return;
220 }
221 libinit();
222 }
This page took 0.032552 seconds and 3 git commands to generate.