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