Commit | Line | Data |
---|---|---|
e11fcffc MD |
1 | /* |
2 | * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
3 | * | |
4 | * This library is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU Lesser General Public | |
6 | * License as published by the Free Software Foundation; version 2.1 of | |
7 | * the License. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * Lesser General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public | |
15 | * License along with this library; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
3fbec7dc | 20 | #define _LGPL_SOURCE |
e11fcffc MD |
21 | #include <error.h> |
22 | #include <dlfcn.h> | |
23 | #include <stdlib.h> | |
fb31eb73 | 24 | #include <stdint.h> |
e11fcffc MD |
25 | #include <usterr-signal-safe.h> |
26 | #include <lttng/ust-clock.h> | |
27 | #include <urcu/system.h> | |
28 | #include <urcu/arch.h> | |
29 | ||
30 | #include "clock.h" | |
730bf2af | 31 | #include "getenv.h" |
e11fcffc MD |
32 | |
33 | struct lttng_trace_clock *lttng_trace_clock; | |
34 | ||
35 | static | |
36 | struct lttng_trace_clock user_tc; | |
37 | ||
4bc1ccd7 MD |
38 | static |
39 | void *clock_handle; | |
40 | ||
e11fcffc MD |
41 | int lttng_ust_trace_clock_set_read64_cb(uint64_t (*read64)(void)) |
42 | { | |
43 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
44 | return -EBUSY; | |
45 | user_tc.read64 = read64; | |
46 | return 0; | |
47 | } | |
48 | ||
49 | int lttng_ust_trace_clock_set_freq_cb(uint64_t (*freq)(void)) | |
50 | { | |
51 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
52 | return -EBUSY; | |
53 | user_tc.freq = freq; | |
54 | return 0; | |
55 | } | |
56 | ||
57 | int lttng_ust_trace_clock_set_uuid_cb(int (*uuid)(char *uuid)) | |
58 | { | |
59 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
60 | return -EBUSY; | |
61 | user_tc.uuid = uuid; | |
62 | return 0; | |
63 | } | |
64 | ||
65 | int lttng_ust_trace_clock_set_name_cb(const char *(*name)(void)) | |
66 | { | |
67 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
68 | return -EBUSY; | |
69 | user_tc.name = name; | |
70 | return 0; | |
71 | } | |
72 | ||
73 | int lttng_ust_trace_clock_set_description_cb(const char *(*description)(void)) | |
74 | { | |
75 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
76 | return -EBUSY; | |
77 | user_tc.description = description; | |
78 | return 0; | |
79 | } | |
80 | ||
81 | int lttng_ust_enable_trace_clock_override(void) | |
82 | { | |
83 | if (CMM_LOAD_SHARED(lttng_trace_clock)) | |
84 | return -EBUSY; | |
85 | if (!user_tc.read64) | |
86 | return -EINVAL; | |
87 | if (!user_tc.freq) | |
88 | return -EINVAL; | |
89 | if (!user_tc.name) | |
90 | return -EINVAL; | |
91 | if (!user_tc.description) | |
92 | return -EINVAL; | |
93 | /* Use default uuid cb when NULL */ | |
94 | cmm_smp_mb(); /* Store callbacks before trace clock */ | |
95 | CMM_STORE_SHARED(lttng_trace_clock, &user_tc); | |
96 | return 0; | |
97 | } | |
98 | ||
99 | void lttng_ust_clock_init(void) | |
100 | { | |
101 | const char *libname; | |
e11fcffc MD |
102 | void (*libinit)(void); |
103 | ||
4bc1ccd7 MD |
104 | if (clock_handle) |
105 | return; | |
6f626d28 | 106 | libname = lttng_getenv("LTTNG_UST_CLOCK_PLUGIN"); |
e11fcffc MD |
107 | if (!libname) |
108 | return; | |
4bc1ccd7 MD |
109 | clock_handle = dlopen(libname, RTLD_NOW); |
110 | if (!clock_handle) { | |
e11fcffc MD |
111 | PERROR("Cannot load LTTng UST clock override library %s", |
112 | libname); | |
113 | return; | |
114 | } | |
115 | dlerror(); | |
4bc1ccd7 | 116 | libinit = (void (*)(void)) dlsym(clock_handle, |
e11fcffc MD |
117 | "lttng_ust_clock_plugin_init"); |
118 | if (!libinit) { | |
119 | PERROR("Cannot find LTTng UST clock override library %s initialization function lttng_ust_clock_plugin_init()", | |
120 | libname); | |
121 | return; | |
122 | } | |
123 | libinit(); | |
124 | } |