Implement clock override plugin support
[lttng-ust.git] / liblttng-ust / clock.h
1 /*
2 * Copyright (C) 2010 Pierre-Marc Fournier
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; version 2.1 of
8 * the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifndef _UST_CLOCK_H
21 #define _UST_CLOCK_H
22
23 #include <time.h>
24 #include <sys/time.h>
25 #include <stdint.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <urcu/system.h>
29 #include <urcu/arch.h>
30 #include <lttng/ust-clock.h>
31
32 #include "lttng-ust-uuid.h"
33
34 struct lttng_trace_clock {
35 uint64_t (*read64)(void);
36 uint64_t (*freq)(void);
37 int (*uuid)(char *uuid);
38 const char *(*name)(void);
39 const char *(*description)(void);
40 };
41
42 extern struct lttng_trace_clock *lttng_trace_clock;
43
44 void lttng_ust_clock_init(void);
45
46 /* Use the kernel MONOTONIC clock. */
47
48 static __inline__
49 uint64_t trace_clock_read64_monotonic(void)
50 {
51 struct timespec ts;
52
53 clock_gettime(CLOCK_MONOTONIC, &ts);
54 return ((uint64_t) ts.tv_sec * 1000000000ULL) + ts.tv_nsec;
55 }
56
57 static __inline__
58 uint64_t trace_clock_freq_monotonic(void)
59 {
60 return 1000000000ULL;
61 }
62
63 static __inline__
64 int trace_clock_uuid_monotonic(char *uuid)
65 {
66 int ret = 0;
67 size_t len;
68 FILE *fp;
69
70 /*
71 * boot_id needs to be read once before being used concurrently
72 * to deal with a Linux kernel race. A fix is proposed for
73 * upstream, but the work-around is needed for older kernels.
74 */
75 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
76 if (!fp) {
77 return -ENOENT;
78 }
79 len = fread(uuid, 1, LTTNG_UST_UUID_STR_LEN - 1, fp);
80 if (len < LTTNG_UST_UUID_STR_LEN - 1) {
81 ret = -EINVAL;
82 goto end;
83 }
84 uuid[LTTNG_UST_UUID_STR_LEN - 1] = '\0';
85 end:
86 fclose(fp);
87 return ret;
88 }
89
90 static __inline__
91 const char *trace_clock_name_monotonic(void)
92 {
93 return "monotonic";
94 }
95
96 static __inline__
97 const char *trace_clock_description_monotonic(void)
98 {
99 return "Monotonic Clock";
100 }
101
102 static __inline__
103 uint64_t trace_clock_read64(void)
104 {
105 struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock);
106
107 if (caa_likely(!ltc)) {
108 return trace_clock_read64_monotonic();
109 } else {
110 cmm_read_barrier_depends(); /* load ltc before content */
111 return ltc->read64();
112 }
113 }
114
115 static __inline__
116 uint64_t trace_clock_freq(void)
117 {
118 struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock);
119
120 if (!ltc) {
121 return trace_clock_freq_monotonic();
122 } else {
123 cmm_read_barrier_depends(); /* load ltc before content */
124 return ltc->freq();
125 }
126 }
127
128 static __inline__
129 int trace_clock_uuid(char *uuid)
130 {
131 struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock);
132
133 cmm_read_barrier_depends(); /* load ltc before content */
134 /* Use default UUID cb when NULL */
135 if (!ltc || !ltc->uuid) {
136 return trace_clock_uuid_monotonic(uuid);
137 } else {
138 return ltc->uuid(uuid);
139 }
140 }
141
142 static __inline__
143 const char *trace_clock_name(void)
144 {
145 struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock);
146
147 if (!ltc) {
148 return trace_clock_name_monotonic();
149 } else {
150 cmm_read_barrier_depends(); /* load ltc before content */
151 return ltc->name();
152 }
153 }
154
155 static __inline__
156 const char *trace_clock_description(void)
157 {
158 struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock);
159
160 if (!ltc) {
161 return trace_clock_description_monotonic();
162 } else {
163 cmm_read_barrier_depends(); /* load ltc before content */
164 return ltc->description();
165 }
166 }
167
168 #endif /* _UST_CLOCK_H */
This page took 0.032846 seconds and 5 git commands to generate.