Add kernel and UST time namespace context
[lttng-tools.git] / src / bin / lttng-sessiond / ust-clock.h
1 /*
2 * Copyright (C) 2010 Pierre-Marc Fournier
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #ifndef _UST_CLOCK_H
10 #define _UST_CLOCK_H
11
12 #include <common/compat/time.h>
13 #include <sys/time.h>
14 #include <stdint.h>
15 #include <stddef.h>
16 #include <stdio.h>
17 #include <urcu/system.h>
18 #include <urcu/arch.h>
19 #include <lttng/ust-clock.h>
20
21 #include <common/uuid.h>
22
23 /* TRACE CLOCK */
24
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
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__
45 uint64_t trace_clock_read64_monotonic(void)
46 {
47 struct timespec ts;
48
49 if (lttng_clock_gettime(CLOCK_MONOTONIC, &ts)) {
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 }
55 return ((uint64_t) ts.tv_sec * 1000000000ULL) + ts.tv_nsec;
56 }
57
58 static __inline__
59 uint64_t trace_clock_freq_monotonic(void)
60 {
61 return 1000000000ULL;
62 }
63
64 static __inline__
65 int trace_clock_uuid_monotonic(char *uuid)
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 }
80 len = fread(uuid, 1, LTTNG_UST_UUID_STR_LEN - 1, fp);
81 if (len < LTTNG_UST_UUID_STR_LEN - 1) {
82 ret = -EINVAL;
83 goto end;
84 }
85 uuid[LTTNG_UST_UUID_STR_LEN - 1] = '\0';
86 end:
87 fclose(fp);
88 return ret;
89 }
90
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
169 #endif /* _UST_CLOCK_H */
This page took 0.032034 seconds and 4 git commands to generate.