lttng-ust(3): order environment variables
[lttng-ust.git] / liblttng-ust / clock.h
CommitLineData
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
34struct 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 42extern struct lttng_trace_clock *lttng_trace_clock;
518d7abb 43
f9364363
MD
44void lttng_ust_clock_init(void);
45
46/* Use the kernel MONOTONIC clock. */
518d7abb 47
28b12049 48static __inline__
f9364363 49uint64_t trace_clock_read64_monotonic(void)
9c6bb081
JD
50{
51 struct timespec ts;
9c6bb081 52
8d8a24c8 53 clock_gettime(CLOCK_MONOTONIC, &ts);
dc190cc1 54 return ((uint64_t) ts.tv_sec * 1000000000ULL) + ts.tv_nsec;
9c6bb081
JD
55}
56
28b12049 57static __inline__
f9364363 58uint64_t trace_clock_freq_monotonic(void)
518d7abb 59{
28b12049
MD
60 return 1000000000ULL;
61}
62
63static __inline__
f9364363 64int trace_clock_uuid_monotonic(char *uuid)
28b12049 65{
939950af
MD
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 }
19d8b1b3
MD
79 len = fread(uuid, 1, LTTNG_UST_UUID_STR_LEN - 1, fp);
80 if (len < LTTNG_UST_UUID_STR_LEN - 1) {
939950af
MD
81 ret = -EINVAL;
82 goto end;
83 }
19d8b1b3 84 uuid[LTTNG_UST_UUID_STR_LEN - 1] = '\0';
939950af
MD
85end:
86 fclose(fp);
87 return ret;
518d7abb
PMF
88}
89
f9364363
MD
90static __inline__
91const char *trace_clock_name_monotonic(void)
92{
93 return "monotonic";
94}
95
96static __inline__
97const char *trace_clock_description_monotonic(void)
98{
99 return "Monotonic Clock";
100}
101
102static __inline__
103uint64_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
115static __inline__
116uint64_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
128static __inline__
129int 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
142static __inline__
143const 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
155static __inline__
156const 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
9c6bb081 168#endif /* _UST_CLOCK_H */
This page took 0.035699 seconds and 4 git commands to generate.