Rename bt_ctf_X -> bt_X, maintain backward compat. for pre-2.0 CTF writer
[babeltrace.git] / lib / ctf-writer / clock.c
1 /*
2 * clock.c
3 *
4 * Babeltrace CTF IR - Clock
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include <babeltrace/ctf-writer/clock-internal.h>
31 #include <babeltrace/ctf-ir/clock-class.h>
32 #include <babeltrace/ctf-ir/clock-class-internal.h>
33 #include <babeltrace/ctf-ir/utils.h>
34 #include <babeltrace/compat/uuid-internal.h>
35 #include <babeltrace/ref.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/compiler-internal.h>
38 #include <inttypes.h>
39
40 static
41 void bt_ctf_clock_destroy(struct bt_object *obj);
42
43 struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
44 {
45 int ret;
46 struct bt_ctf_clock *clock = NULL;
47 unsigned char cc_uuid[BABELTRACE_UUID_LEN];
48
49 if (!name) {
50 goto error;
51 }
52
53 clock = g_new0(struct bt_ctf_clock, 1);
54
55 if (!clock) {
56 goto error;
57 }
58
59 bt_object_init(clock, bt_ctf_clock_destroy);
60 clock->value = 0;
61
62 /* Pre-2.0.0 backward compatibility: default frequency is 1 GHz */
63 clock->clock_class = bt_clock_class_create(name, 1000000000);
64 if (!clock->clock_class) {
65 goto error;
66 }
67
68 /* Automatically set clock class's UUID. */
69 ret = bt_uuid_generate(cc_uuid);
70 if (ret) {
71 goto error;
72 }
73
74 ret = bt_clock_class_set_uuid(clock->clock_class, cc_uuid);
75 assert(ret == 0);
76 return clock;
77
78 error:
79 BT_PUT(clock);
80 return clock;
81 }
82
83 const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock)
84 {
85 const char *name = NULL;
86
87 if (clock) {
88 name = bt_clock_class_get_name(clock->clock_class);
89 }
90
91 return name;
92 }
93
94 const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock)
95 {
96 const char *description = NULL;
97
98 if (clock) {
99 description = bt_clock_class_get_description(
100 clock->clock_class);
101 }
102
103 return description;
104 }
105
106 int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
107 {
108 int ret = -1;
109
110 if (clock) {
111 ret = bt_clock_class_set_description(clock->clock_class,
112 desc);
113 }
114
115 return ret;
116 }
117
118 uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock)
119 {
120 uint64_t freq = -1ULL;
121
122 if (clock) {
123 freq = bt_clock_class_get_frequency(clock->clock_class);
124 }
125
126 return freq;
127 }
128
129 int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
130 {
131 int ret = -1;
132
133 if (clock) {
134 ret = bt_clock_class_set_frequency(clock->clock_class,
135 freq);
136 }
137
138 return ret;
139 }
140
141 uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock)
142 {
143 uint64_t precision = -1ULL;
144
145 if (clock) {
146 precision = bt_clock_class_get_precision(
147 clock->clock_class);
148 }
149
150 return precision;
151 }
152
153 int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
154 {
155 int ret = -1;
156
157 if (clock) {
158 ret = bt_clock_class_set_precision(clock->clock_class,
159 precision);
160 }
161
162 return ret;
163 }
164
165 int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s)
166 {
167 int ret = -1;
168
169 if (clock) {
170 ret = bt_clock_class_get_offset_s(clock->clock_class,
171 offset_s);
172 }
173
174 return ret;
175 }
176
177 int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s)
178 {
179 int ret = -1;
180
181 if (clock) {
182 ret = bt_clock_class_set_offset_s(clock->clock_class,
183 offset_s);
184 }
185
186 return ret;
187 }
188
189 int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset)
190 {
191 int ret = -1;
192
193 if (clock) {
194 ret = bt_clock_class_get_offset_cycles(clock->clock_class,
195 offset);
196 }
197
198 return ret;
199 }
200
201 int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset)
202 {
203 int ret = -1;
204
205 if (clock) {
206 ret = bt_clock_class_set_offset_cycles(clock->clock_class,
207 offset);
208 }
209
210 return ret;
211 }
212
213 int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock)
214 {
215 int is_absolute = -1;
216
217 if (clock) {
218 is_absolute = bt_clock_class_is_absolute(
219 clock->clock_class);
220 }
221
222 return is_absolute;
223 }
224
225 int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
226 {
227 int ret = -1;
228
229 if (clock) {
230 ret = bt_clock_class_set_is_absolute(clock->clock_class,
231 is_absolute);
232 }
233
234 return ret;
235 }
236
237 const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock)
238 {
239 const unsigned char *uuid = NULL;
240
241 if (clock) {
242 uuid = bt_clock_class_get_uuid(clock->clock_class);
243 }
244
245 return uuid;
246 }
247
248 int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid)
249 {
250 int ret = -1;
251
252 if (clock) {
253 ret = bt_clock_class_set_uuid(clock->clock_class, uuid);
254 }
255
256 return ret;
257 }
258
259 int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
260 {
261 int ret = 0;
262 int64_t value;
263
264 if (!clock) {
265 ret = -1;
266 goto end;
267 }
268
269 /* Common case where cycles are actually nanoseconds */
270 if (clock->clock_class->frequency == 1000000000) {
271 value = time;
272 } else {
273 value = (uint64_t) (((double) time *
274 (double) clock->clock_class->frequency) / 1e9);
275 }
276
277 if (clock->value > value) {
278 /* Timestamps must be strictly monotonic. */
279 ret = -1;
280 goto end;
281 }
282
283 clock->value = value;
284 end:
285 return ret;
286 }
287
288 void bt_ctf_clock_get(struct bt_ctf_clock *clock)
289 {
290 bt_get(clock);
291 }
292
293 void bt_ctf_clock_put(struct bt_ctf_clock *clock)
294 {
295 bt_put(clock);
296 }
297
298 BT_HIDDEN
299 int bt_ctf_clock_get_value(struct bt_ctf_clock *clock, uint64_t *value)
300 {
301 int ret = 0;
302
303 if (!clock || !value) {
304 ret = -1;
305 goto end;
306 }
307
308 *value = clock->value;
309 end:
310 return ret;
311 }
312
313 static
314 void bt_ctf_clock_destroy(struct bt_object *obj)
315 {
316 struct bt_ctf_clock *clock;
317
318 clock = container_of(obj, struct bt_ctf_clock, base);
319 bt_put(clock->clock_class);
320 g_free(clock);
321 }
This page took 0.035384 seconds and 4 git commands to generate.