Commit | Line | Data |
---|---|---|
d0b96690 | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2010 Pierre-Marc Fournier |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
d0b96690 | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
d0b96690 | 6 | * |
d0b96690 DG |
7 | */ |
8 | ||
9 | #ifndef _UST_CLOCK_H | |
10 | #define _UST_CLOCK_H | |
11 | ||
389fbf04 | 12 | #include <common/compat/time.h> |
d0b96690 DG |
13 | #include <sys/time.h> |
14 | #include <stdint.h> | |
15 | #include <stddef.h> | |
16 | #include <stdio.h> | |
fc0bb9fa MD |
17 | #include <urcu/system.h> |
18 | #include <urcu/arch.h> | |
19 | #include <lttng/ust-clock.h> | |
d0b96690 | 20 | |
c70636a7 | 21 | #include <common/uuid.h> |
d0b96690 DG |
22 | |
23 | /* TRACE CLOCK */ | |
24 | ||
fc0bb9fa MD |
25 | struct lttng_trace_clock { |
26 | uint64_t (*read64)(void); | |
27 | uint64_t (*freq)(void); | |
28 | int (*uuid)(char *uuid); | |
29 | const char *(*name)(void); | |
30 | const char *(*description)(void); | |
31 | }; | |
32 | ||
33 | extern struct lttng_trace_clock *lttng_trace_clock; | |
34 | ||
35 | void lttng_ust_clock_init(void); | |
36 | ||
d0b96690 DG |
37 | /* |
38 | * Currently using the kernel MONOTONIC clock, waiting for kernel-side | |
39 | * LTTng to implement mmap'd trace clock. | |
40 | */ | |
41 | ||
42 | /* Choosing correct trace clock */ | |
43 | ||
44 | static __inline__ | |
fc0bb9fa | 45 | uint64_t trace_clock_read64_monotonic(void) |
d0b96690 DG |
46 | { |
47 | struct timespec ts; | |
48 | ||
389fbf04 | 49 | if (lttng_clock_gettime(CLOCK_MONOTONIC, &ts)) { |
1cf9656c MD |
50 | /* TODO Report error cleanly up the chain. */ |
51 | PERROR("clock_gettime CLOCK_MONOTONIC"); | |
52 | ts.tv_sec = 0; | |
53 | ts.tv_nsec = 0; | |
54 | } | |
d0b96690 DG |
55 | return ((uint64_t) ts.tv_sec * 1000000000ULL) + ts.tv_nsec; |
56 | } | |
57 | ||
58 | static __inline__ | |
fc0bb9fa | 59 | uint64_t trace_clock_freq_monotonic(void) |
d0b96690 DG |
60 | { |
61 | return 1000000000ULL; | |
62 | } | |
63 | ||
64 | static __inline__ | |
fc0bb9fa | 65 | int trace_clock_uuid_monotonic(char *uuid) |
d0b96690 DG |
66 | { |
67 | int ret = 0; | |
68 | size_t len; | |
69 | FILE *fp; | |
70 | ||
71 | /* | |
72 | * boot_id needs to be read once before being used concurrently | |
73 | * to deal with a Linux kernel race. A fix is proposed for | |
74 | * upstream, but the work-around is needed for older kernels. | |
75 | */ | |
76 | fp = fopen("/proc/sys/kernel/random/boot_id", "r"); | |
77 | if (!fp) { | |
78 | return -ENOENT; | |
79 | } | |
fc0bb9fa MD |
80 | len = fread(uuid, 1, LTTNG_UST_UUID_STR_LEN - 1, fp); |
81 | if (len < LTTNG_UST_UUID_STR_LEN - 1) { | |
d0b96690 DG |
82 | ret = -EINVAL; |
83 | goto end; | |
84 | } | |
fc0bb9fa | 85 | uuid[LTTNG_UST_UUID_STR_LEN - 1] = '\0'; |
d0b96690 DG |
86 | end: |
87 | fclose(fp); | |
88 | return ret; | |
89 | } | |
90 | ||
fc0bb9fa MD |
91 | static __inline__ |
92 | const char *trace_clock_name_monotonic(void) | |
93 | { | |
94 | return "monotonic"; | |
95 | } | |
96 | ||
97 | static __inline__ | |
98 | const char *trace_clock_description_monotonic(void) | |
99 | { | |
100 | return "Monotonic Clock"; | |
101 | } | |
102 | ||
103 | static __inline__ | |
104 | uint64_t trace_clock_read64(void) | |
105 | { | |
106 | struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock); | |
107 | ||
108 | if (caa_likely(!ltc)) { | |
109 | return trace_clock_read64_monotonic(); | |
110 | } else { | |
111 | cmm_read_barrier_depends(); /* load ltc before content */ | |
112 | return ltc->read64(); | |
113 | } | |
114 | } | |
115 | ||
116 | static __inline__ | |
117 | uint64_t trace_clock_freq(void) | |
118 | { | |
119 | struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock); | |
120 | ||
121 | if (!ltc) { | |
122 | return trace_clock_freq_monotonic(); | |
123 | } else { | |
124 | cmm_read_barrier_depends(); /* load ltc before content */ | |
125 | return ltc->freq(); | |
126 | } | |
127 | } | |
128 | ||
129 | static __inline__ | |
130 | int trace_clock_uuid(char *uuid) | |
131 | { | |
132 | struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock); | |
133 | ||
134 | cmm_read_barrier_depends(); /* load ltc before content */ | |
135 | /* Use default UUID cb when NULL */ | |
136 | if (!ltc || !ltc->uuid) { | |
137 | return trace_clock_uuid_monotonic(uuid); | |
138 | } else { | |
139 | return ltc->uuid(uuid); | |
140 | } | |
141 | } | |
142 | ||
143 | static __inline__ | |
144 | const char *trace_clock_name(void) | |
145 | { | |
146 | struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock); | |
147 | ||
148 | if (!ltc) { | |
149 | return trace_clock_name_monotonic(); | |
150 | } else { | |
151 | cmm_read_barrier_depends(); /* load ltc before content */ | |
152 | return ltc->name(); | |
153 | } | |
154 | } | |
155 | ||
156 | static __inline__ | |
157 | const char *trace_clock_description(void) | |
158 | { | |
159 | struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock); | |
160 | ||
161 | if (!ltc) { | |
162 | return trace_clock_description_monotonic(); | |
163 | } else { | |
164 | cmm_read_barrier_depends(); /* load ltc before content */ | |
165 | return ltc->description(); | |
166 | } | |
167 | } | |
168 | ||
d0b96690 | 169 | #endif /* _UST_CLOCK_H */ |