Commit | Line | Data |
---|---|---|
8d8a24c8 MD |
1 | /* |
2 | * Copyright (C) 2010 Pierre-Marc Fournier | |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
518d7abb PMF |
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 | |
8d8a24c8 MD |
7 | * License as published by the Free Software Foundation; version 2.1 of |
8 | * the License. | |
518d7abb PMF |
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 | ||
9c6bb081 JD |
20 | #ifndef _UST_CLOCK_H |
21 | #define _UST_CLOCK_H | |
518d7abb | 22 | |
ca4525b5 | 23 | #include <time.h> |
518d7abb | 24 | #include <sys/time.h> |
9edd34bd MD |
25 | #include <stdint.h> |
26 | #include <stddef.h> | |
939950af | 27 | #include <stdio.h> |
f9364363 MD |
28 | #include <urcu/system.h> |
29 | #include <urcu/arch.h> | |
30 | #include <lttng/ust-clock.h> | |
31 | ||
eda498b8 | 32 | #include "lttng-ust-uuid.h" |
518d7abb | 33 | |
f9364363 MD |
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 | }; | |
518d7abb | 41 | |
f9364363 | 42 | extern struct lttng_trace_clock *lttng_trace_clock; |
518d7abb | 43 | |
f9364363 MD |
44 | void lttng_ust_clock_init(void); |
45 | ||
46 | /* Use the kernel MONOTONIC clock. */ | |
518d7abb | 47 | |
28b12049 | 48 | static __inline__ |
f9364363 | 49 | uint64_t trace_clock_read64_monotonic(void) |
9c6bb081 JD |
50 | { |
51 | struct timespec ts; | |
9c6bb081 | 52 | |
77686037 MD |
53 | if (caa_unlikely(clock_gettime(CLOCK_MONOTONIC, &ts))) { |
54 | ts.tv_sec = 0; | |
55 | ts.tv_nsec = 0; | |
56 | } | |
dc190cc1 | 57 | return ((uint64_t) ts.tv_sec * 1000000000ULL) + ts.tv_nsec; |
9c6bb081 JD |
58 | } |
59 | ||
28b12049 | 60 | static __inline__ |
f9364363 | 61 | uint64_t trace_clock_freq_monotonic(void) |
518d7abb | 62 | { |
28b12049 MD |
63 | return 1000000000ULL; |
64 | } | |
65 | ||
66 | static __inline__ | |
f9364363 | 67 | int trace_clock_uuid_monotonic(char *uuid) |
28b12049 | 68 | { |
939950af MD |
69 | int ret = 0; |
70 | size_t len; | |
71 | FILE *fp; | |
72 | ||
73 | /* | |
74 | * boot_id needs to be read once before being used concurrently | |
75 | * to deal with a Linux kernel race. A fix is proposed for | |
76 | * upstream, but the work-around is needed for older kernels. | |
77 | */ | |
78 | fp = fopen("/proc/sys/kernel/random/boot_id", "r"); | |
79 | if (!fp) { | |
80 | return -ENOENT; | |
81 | } | |
19d8b1b3 MD |
82 | len = fread(uuid, 1, LTTNG_UST_UUID_STR_LEN - 1, fp); |
83 | if (len < LTTNG_UST_UUID_STR_LEN - 1) { | |
939950af MD |
84 | ret = -EINVAL; |
85 | goto end; | |
86 | } | |
19d8b1b3 | 87 | uuid[LTTNG_UST_UUID_STR_LEN - 1] = '\0'; |
939950af MD |
88 | end: |
89 | fclose(fp); | |
90 | return ret; | |
518d7abb PMF |
91 | } |
92 | ||
f9364363 MD |
93 | static __inline__ |
94 | const char *trace_clock_name_monotonic(void) | |
95 | { | |
96 | return "monotonic"; | |
97 | } | |
98 | ||
99 | static __inline__ | |
100 | const char *trace_clock_description_monotonic(void) | |
101 | { | |
102 | return "Monotonic Clock"; | |
103 | } | |
104 | ||
105 | static __inline__ | |
106 | uint64_t trace_clock_read64(void) | |
107 | { | |
108 | struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock); | |
109 | ||
110 | if (caa_likely(!ltc)) { | |
111 | return trace_clock_read64_monotonic(); | |
112 | } else { | |
113 | cmm_read_barrier_depends(); /* load ltc before content */ | |
114 | return ltc->read64(); | |
115 | } | |
116 | } | |
117 | ||
118 | static __inline__ | |
119 | uint64_t trace_clock_freq(void) | |
120 | { | |
121 | struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock); | |
122 | ||
123 | if (!ltc) { | |
124 | return trace_clock_freq_monotonic(); | |
125 | } else { | |
126 | cmm_read_barrier_depends(); /* load ltc before content */ | |
127 | return ltc->freq(); | |
128 | } | |
129 | } | |
130 | ||
131 | static __inline__ | |
132 | int trace_clock_uuid(char *uuid) | |
133 | { | |
134 | struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock); | |
135 | ||
136 | cmm_read_barrier_depends(); /* load ltc before content */ | |
137 | /* Use default UUID cb when NULL */ | |
138 | if (!ltc || !ltc->uuid) { | |
139 | return trace_clock_uuid_monotonic(uuid); | |
140 | } else { | |
141 | return ltc->uuid(uuid); | |
142 | } | |
143 | } | |
144 | ||
145 | static __inline__ | |
146 | const char *trace_clock_name(void) | |
147 | { | |
148 | struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock); | |
149 | ||
150 | if (!ltc) { | |
151 | return trace_clock_name_monotonic(); | |
152 | } else { | |
153 | cmm_read_barrier_depends(); /* load ltc before content */ | |
154 | return ltc->name(); | |
155 | } | |
156 | } | |
157 | ||
158 | static __inline__ | |
159 | const char *trace_clock_description(void) | |
160 | { | |
161 | struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock); | |
162 | ||
163 | if (!ltc) { | |
164 | return trace_clock_description_monotonic(); | |
165 | } else { | |
166 | cmm_read_barrier_depends(); /* load ltc before content */ | |
167 | return ltc->description(); | |
168 | } | |
169 | } | |
170 | ||
9c6bb081 | 171 | #endif /* _UST_CLOCK_H */ |