Commit | Line | Data |
---|---|---|
273b65be JG |
1 | /* |
2 | * clock.c | |
3 | * | |
d2dc44b6 | 4 | * Babeltrace CTF IR - Clock |
273b65be | 5 | * |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
7 | * |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
adc315b8 | 29 | #include <babeltrace/ctf-ir/clock-internal.h> |
654c1444 | 30 | #include <babeltrace/ctf-ir/utils.h> |
83509119 | 31 | #include <babeltrace/ref.h> |
273b65be | 32 | #include <babeltrace/ctf-writer/writer-internal.h> |
83509119 | 33 | #include <babeltrace/object-internal.h> |
273b65be JG |
34 | #include <babeltrace/compiler.h> |
35 | #include <inttypes.h> | |
36 | ||
37 | static | |
83509119 | 38 | void bt_ctf_clock_destroy(struct bt_object *obj); |
273b65be | 39 | |
4c426c17 JG |
40 | BT_HIDDEN |
41 | struct bt_ctf_clock *_bt_ctf_clock_create(void) | |
273b65be | 42 | { |
4c426c17 JG |
43 | struct bt_ctf_clock *clock = g_new0( |
44 | struct bt_ctf_clock, 1); | |
45 | ||
46 | if (!clock) { | |
47 | goto end; | |
48 | } | |
49 | ||
50 | clock->precision = 1; | |
51 | clock->frequency = 1000000000; | |
83509119 | 52 | bt_object_init(clock, bt_ctf_clock_destroy); |
4c426c17 JG |
53 | end: |
54 | return clock; | |
55 | } | |
56 | ||
57 | BT_HIDDEN | |
58 | int bt_ctf_clock_set_name(struct bt_ctf_clock *clock, | |
59 | const char *name) | |
60 | { | |
61 | int ret = 0; | |
273b65be | 62 | |
654c1444 | 63 | if (bt_ctf_validate_identifier(name)) { |
4c426c17 JG |
64 | ret = -1; |
65 | goto end; | |
273b65be JG |
66 | } |
67 | ||
4c426c17 | 68 | if (clock->name) { |
e1ae7645 JG |
69 | g_string_assign(clock->name, name); |
70 | } else { | |
71 | clock->name = g_string_new(name); | |
72 | if (!clock->name) { | |
73 | ret = -1; | |
74 | goto end; | |
75 | } | |
273b65be JG |
76 | } |
77 | ||
4c426c17 JG |
78 | end: |
79 | return ret; | |
80 | } | |
81 | ||
82 | struct bt_ctf_clock *bt_ctf_clock_create(const char *name) | |
83 | { | |
84 | int ret; | |
85 | struct bt_ctf_clock *clock = NULL; | |
86 | ||
87 | clock = _bt_ctf_clock_create(); | |
88 | if (!clock) { | |
89 | goto error; | |
90 | } | |
91 | ||
92 | ret = bt_ctf_clock_set_name(clock, name); | |
93 | if (ret) { | |
83509119 | 94 | goto error; |
273b65be JG |
95 | } |
96 | ||
19dd40db | 97 | ret = bt_uuid_generate(clock->uuid); |
be018f15 | 98 | if (ret) { |
83509119 | 99 | goto error; |
be018f15 JG |
100 | } |
101 | ||
cfeb617e PP |
102 | /* |
103 | * For backward compatibility reasons, a fresh clock can have | |
104 | * a value because it could be added to a trace created by a | |
105 | * CTF writer. As soon as this clock is added to a non-writer | |
106 | * trace, then its value/time functions will be disabled. | |
107 | */ | |
108 | clock->has_value = 1; | |
be018f15 | 109 | clock->uuid_set = 1; |
273b65be | 110 | return clock; |
273b65be | 111 | error: |
83509119 JG |
112 | BT_PUT(clock); |
113 | return clock; | |
87d76bb1 JG |
114 | } |
115 | ||
116 | const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock) | |
117 | { | |
118 | const char *ret = NULL; | |
119 | ||
120 | if (!clock) { | |
121 | goto end; | |
122 | } | |
123 | ||
124 | if (clock->name) { | |
125 | ret = clock->name->str; | |
126 | } | |
127 | ||
128 | end: | |
129 | return ret; | |
130 | } | |
131 | ||
132 | const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock) | |
133 | { | |
134 | const char *ret = NULL; | |
135 | ||
136 | if (!clock) { | |
137 | goto end; | |
138 | } | |
139 | ||
140 | if (clock->description) { | |
141 | ret = clock->description->str; | |
142 | } | |
143 | end: | |
144 | return ret; | |
273b65be JG |
145 | } |
146 | ||
147 | int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc) | |
148 | { | |
149 | int ret = 0; | |
150 | ||
151 | if (!clock || !desc || clock->frozen) { | |
152 | ret = -1; | |
153 | goto end; | |
154 | } | |
155 | ||
87d76bb1 | 156 | clock->description = g_string_new(desc); |
273b65be JG |
157 | ret = clock->description ? 0 : -1; |
158 | end: | |
159 | return ret; | |
160 | } | |
161 | ||
87d76bb1 JG |
162 | uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock) |
163 | { | |
164 | uint64_t ret = -1ULL; | |
165 | ||
166 | if (!clock) { | |
167 | goto end; | |
168 | } | |
169 | ||
170 | ret = clock->frequency; | |
171 | end: | |
172 | return ret; | |
173 | } | |
174 | ||
273b65be JG |
175 | int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq) |
176 | { | |
177 | int ret = 0; | |
178 | ||
179 | if (!clock || clock->frozen) { | |
180 | ret = -1; | |
181 | goto end; | |
182 | } | |
183 | ||
184 | clock->frequency = freq; | |
185 | end: | |
186 | return ret; | |
187 | } | |
188 | ||
87d76bb1 JG |
189 | uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock) |
190 | { | |
191 | uint64_t ret = -1ULL; | |
192 | ||
193 | if (!clock) { | |
194 | goto end; | |
195 | } | |
196 | ||
197 | ret = clock->precision; | |
198 | end: | |
199 | return ret; | |
200 | } | |
201 | ||
273b65be JG |
202 | int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision) |
203 | { | |
204 | int ret = 0; | |
205 | ||
206 | if (!clock || clock->frozen) { | |
207 | ret = -1; | |
208 | goto end; | |
209 | } | |
210 | ||
211 | clock->precision = precision; | |
212 | end: | |
213 | return ret; | |
214 | } | |
215 | ||
61cf588b | 216 | int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s) |
87d76bb1 | 217 | { |
61cf588b | 218 | int ret = 0; |
87d76bb1 | 219 | |
61cf588b MD |
220 | if (!clock || !offset_s) { |
221 | ret = -1; | |
87d76bb1 JG |
222 | goto end; |
223 | } | |
224 | ||
61cf588b | 225 | *offset_s = clock->offset_s; |
87d76bb1 JG |
226 | end: |
227 | return ret; | |
228 | } | |
229 | ||
61cf588b | 230 | int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s) |
273b65be JG |
231 | { |
232 | int ret = 0; | |
233 | ||
234 | if (!clock || clock->frozen) { | |
235 | ret = -1; | |
236 | goto end; | |
237 | } | |
238 | ||
239 | clock->offset_s = offset_s; | |
240 | end: | |
241 | return ret; | |
242 | } | |
243 | ||
61cf588b | 244 | int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset) |
87d76bb1 | 245 | { |
61cf588b | 246 | int ret = 0; |
87d76bb1 | 247 | |
61cf588b MD |
248 | if (!clock || !offset) { |
249 | ret = -1; | |
87d76bb1 JG |
250 | goto end; |
251 | } | |
252 | ||
61cf588b | 253 | *offset = clock->offset; |
87d76bb1 JG |
254 | end: |
255 | return ret; | |
256 | } | |
257 | ||
61cf588b | 258 | int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset) |
273b65be JG |
259 | { |
260 | int ret = 0; | |
261 | ||
262 | if (!clock || clock->frozen) { | |
263 | ret = -1; | |
264 | goto end; | |
265 | } | |
266 | ||
267 | clock->offset = offset; | |
268 | end: | |
269 | return ret; | |
270 | } | |
271 | ||
87d76bb1 JG |
272 | int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock) |
273 | { | |
274 | int ret = -1; | |
275 | ||
276 | if (!clock) { | |
277 | goto end; | |
278 | } | |
279 | ||
280 | ret = clock->absolute; | |
281 | end: | |
282 | return ret; | |
283 | } | |
284 | ||
273b65be JG |
285 | int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute) |
286 | { | |
287 | int ret = 0; | |
288 | ||
289 | if (!clock || clock->frozen) { | |
290 | ret = -1; | |
291 | goto end; | |
292 | } | |
293 | ||
294 | clock->absolute = !!is_absolute; | |
295 | end: | |
296 | return ret; | |
297 | } | |
298 | ||
85b743f4 JG |
299 | const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock) |
300 | { | |
301 | const unsigned char *ret; | |
302 | ||
be018f15 | 303 | if (!clock || !clock->uuid_set) { |
85b743f4 JG |
304 | ret = NULL; |
305 | goto end; | |
306 | } | |
307 | ||
308 | ret = clock->uuid; | |
309 | end: | |
310 | return ret; | |
311 | } | |
312 | ||
313 | int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid) | |
314 | { | |
315 | int ret = 0; | |
316 | ||
be018f15 | 317 | if (!clock || !uuid || clock->frozen) { |
85b743f4 JG |
318 | ret = -1; |
319 | goto end; | |
320 | } | |
321 | ||
322 | memcpy(clock->uuid, uuid, sizeof(uuid_t)); | |
be018f15 | 323 | clock->uuid_set = 1; |
85b743f4 JG |
324 | end: |
325 | return ret; | |
326 | } | |
327 | ||
61cf588b | 328 | int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time) |
87d76bb1 | 329 | { |
61cf588b | 330 | int ret = 0; |
87d76bb1 | 331 | |
61cf588b MD |
332 | if (!clock || !time) { |
333 | ret = -1; | |
87d76bb1 JG |
334 | goto end; |
335 | } | |
336 | ||
cfeb617e PP |
337 | |
338 | if (!clock->has_value) { | |
339 | /* | |
340 | * Clock belongs to a non-writer mode trace and thus | |
341 | * this function is disabled. | |
342 | */ | |
343 | goto end; | |
344 | } | |
345 | ||
e1e30a8c PP |
346 | /* Common case where cycles are actually nanoseconds */ |
347 | if (clock->frequency == 1000000000) { | |
348 | *time = (int64_t) clock->value; | |
349 | } else { | |
350 | *time = (int64_t) ((1e9 * (double) clock->value) / | |
351 | (double) clock->frequency); | |
352 | } | |
353 | ||
87d76bb1 JG |
354 | end: |
355 | return ret; | |
356 | } | |
357 | ||
61cf588b | 358 | int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time) |
273b65be JG |
359 | { |
360 | int ret = 0; | |
361 | ||
362 | /* Timestamps are strictly monotonic */ | |
e1e30a8c PP |
363 | if (!clock) { |
364 | ret = -1; | |
365 | goto end; | |
366 | } | |
367 | ||
cfeb617e PP |
368 | |
369 | if (!clock->has_value) { | |
370 | /* | |
371 | * Clock belongs to a non-writer mode trace and thus | |
372 | * this function is disabled. | |
373 | */ | |
374 | ret = -1; | |
375 | goto end; | |
376 | } | |
377 | ||
e1e30a8c PP |
378 | /* Common case where cycles are actually nanoseconds */ |
379 | if (clock->frequency == 1000000000) { | |
380 | clock->value = time; | |
381 | goto end; | |
382 | } | |
383 | ||
384 | ret = bt_ctf_clock_set_value(clock, | |
385 | (uint64_t) (((double) time * (double) clock->frequency) / 1e9)); | |
386 | ||
387 | end: | |
388 | return ret; | |
389 | } | |
390 | ||
391 | uint64_t bt_ctf_clock_get_value(struct bt_ctf_clock *clock) | |
392 | { | |
393 | uint64_t ret = -1ULL; | |
394 | ||
395 | if (!clock) { | |
396 | goto end; | |
397 | } | |
398 | ||
cfeb617e PP |
399 | if (!clock->has_value) { |
400 | /* | |
401 | * Clock belongs to a non-writer mode trace and thus | |
402 | * this function is disabled. | |
403 | */ | |
404 | goto end; | |
405 | } | |
406 | ||
e1e30a8c PP |
407 | ret = clock->value; |
408 | end: | |
409 | return ret; | |
410 | } | |
411 | ||
412 | int bt_ctf_clock_set_value(struct bt_ctf_clock *clock, uint64_t value) | |
413 | { | |
414 | int ret = 0; | |
415 | ||
cfeb617e PP |
416 | if (!clock) { |
417 | ret = -1; | |
418 | goto end; | |
419 | } | |
420 | ||
421 | if (!clock->has_value) { | |
422 | /* | |
423 | * Clock belongs to a non-writer mode trace and thus | |
424 | * this function is disabled. | |
425 | */ | |
426 | ret = -1; | |
427 | goto end; | |
428 | } | |
429 | ||
e1e30a8c | 430 | /* Timestamps are strictly monotonic */ |
cfeb617e | 431 | if (value < clock->value) { |
273b65be JG |
432 | ret = -1; |
433 | goto end; | |
434 | } | |
435 | ||
e1e30a8c | 436 | clock->value = value; |
273b65be JG |
437 | end: |
438 | return ret; | |
439 | } | |
440 | ||
441 | void bt_ctf_clock_get(struct bt_ctf_clock *clock) | |
442 | { | |
83509119 | 443 | bt_get(clock); |
273b65be JG |
444 | } |
445 | ||
446 | void bt_ctf_clock_put(struct bt_ctf_clock *clock) | |
447 | { | |
83509119 | 448 | bt_put(clock); |
273b65be JG |
449 | } |
450 | ||
451 | BT_HIDDEN | |
452 | void bt_ctf_clock_freeze(struct bt_ctf_clock *clock) | |
453 | { | |
454 | if (!clock) { | |
455 | return; | |
456 | } | |
457 | ||
458 | clock->frozen = 1; | |
459 | } | |
460 | ||
461 | BT_HIDDEN | |
462 | void bt_ctf_clock_serialize(struct bt_ctf_clock *clock, | |
463 | struct metadata_context *context) | |
464 | { | |
465 | unsigned char *uuid; | |
466 | ||
467 | if (!clock || !context) { | |
468 | return; | |
469 | } | |
470 | ||
471 | uuid = clock->uuid; | |
472 | g_string_append(context->string, "clock {\n"); | |
473 | g_string_append_printf(context->string, "\tname = %s;\n", | |
474 | clock->name->str); | |
475 | g_string_append_printf(context->string, | |
476 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
477 | uuid[0], uuid[1], uuid[2], uuid[3], | |
478 | uuid[4], uuid[5], uuid[6], uuid[7], | |
479 | uuid[8], uuid[9], uuid[10], uuid[11], | |
480 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
7a69f348 | 481 | if (clock->description) { |
273b65be JG |
482 | g_string_append_printf(context->string, "\tdescription = \"%s\";\n", |
483 | clock->description->str); | |
484 | } | |
485 | ||
486 | g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n", | |
487 | clock->frequency); | |
488 | g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n", | |
489 | clock->precision); | |
490 | g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n", | |
491 | clock->offset_s); | |
492 | g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n", | |
493 | clock->offset); | |
494 | g_string_append_printf(context->string, "\tabsolute = %s;\n", | |
495 | clock->absolute ? "TRUE" : "FALSE"); | |
496 | g_string_append(context->string, "};\n\n"); | |
497 | } | |
498 | ||
273b65be | 499 | static |
83509119 | 500 | void bt_ctf_clock_destroy(struct bt_object *obj) |
273b65be JG |
501 | { |
502 | struct bt_ctf_clock *clock; | |
273b65be | 503 | |
83509119 | 504 | clock = container_of(obj, struct bt_ctf_clock, base); |
273b65be JG |
505 | if (clock->name) { |
506 | g_string_free(clock->name, TRUE); | |
507 | } | |
508 | ||
509 | if (clock->description) { | |
510 | g_string_free(clock->description, TRUE); | |
511 | } | |
512 | ||
513 | g_free(clock); | |
514 | } |