Commit | Line | Data |
---|---|---|
1c822dfb JG |
1 | /* |
2 | * clock.c | |
3 | * | |
4 | * Babeltrace CTF IR - Clock | |
5 | * | |
6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
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 | ||
29 | #include <babeltrace/ctf-ir/clock-internal.h> | |
30 | #include <babeltrace/ctf-ir/utils.h> | |
31 | #include <babeltrace/ref.h> | |
32 | #include <babeltrace/ctf-writer/writer-internal.h> | |
33 | #include <babeltrace/object-internal.h> | |
34 | #include <babeltrace/compiler.h> | |
35 | #include <inttypes.h> | |
36 | ||
37 | static | |
38 | void bt_ctf_clock_destroy(struct bt_object *obj); | |
39 | ||
40 | BT_HIDDEN | |
41 | struct bt_ctf_clock *_bt_ctf_clock_create(void) | |
42 | { | |
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; | |
52 | bt_object_init(clock, bt_ctf_clock_destroy); | |
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; | |
62 | ||
63 | if (bt_ctf_validate_identifier(name)) { | |
64 | ret = -1; | |
65 | goto end; | |
66 | } | |
67 | ||
68 | if (clock->name) { | |
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 | } | |
76 | } | |
77 | ||
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) { | |
94 | goto error; | |
95 | } | |
96 | ||
97 | ret = bt_uuid_generate(clock->uuid); | |
98 | if (ret) { | |
99 | goto error; | |
100 | } | |
101 | ||
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; | |
109 | clock->uuid_set = 1; | |
110 | return clock; | |
111 | error: | |
112 | BT_PUT(clock); | |
113 | return clock; | |
114 | } | |
115 | ||
98923034 | 116 | BT_HIDDEN |
1c822dfb JG |
117 | const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock) |
118 | { | |
119 | const char *ret = NULL; | |
120 | ||
121 | if (!clock) { | |
122 | goto end; | |
123 | } | |
124 | ||
125 | if (clock->name) { | |
126 | ret = clock->name->str; | |
127 | } | |
128 | ||
129 | end: | |
130 | return ret; | |
131 | } | |
132 | ||
98923034 | 133 | BT_HIDDEN |
1c822dfb JG |
134 | const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock) |
135 | { | |
136 | const char *ret = NULL; | |
137 | ||
138 | if (!clock) { | |
139 | goto end; | |
140 | } | |
141 | ||
142 | if (clock->description) { | |
143 | ret = clock->description->str; | |
144 | } | |
145 | end: | |
146 | return ret; | |
147 | } | |
148 | ||
149 | int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc) | |
150 | { | |
151 | int ret = 0; | |
152 | ||
153 | if (!clock || !desc || clock->frozen) { | |
154 | ret = -1; | |
155 | goto end; | |
156 | } | |
157 | ||
158 | clock->description = g_string_new(desc); | |
159 | ret = clock->description ? 0 : -1; | |
160 | end: | |
161 | return ret; | |
162 | } | |
163 | ||
98923034 | 164 | BT_HIDDEN |
1c822dfb JG |
165 | uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock) |
166 | { | |
167 | uint64_t ret = -1ULL; | |
168 | ||
169 | if (!clock) { | |
170 | goto end; | |
171 | } | |
172 | ||
173 | ret = clock->frequency; | |
174 | end: | |
175 | return ret; | |
176 | } | |
177 | ||
178 | int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq) | |
179 | { | |
180 | int ret = 0; | |
181 | ||
182 | if (!clock || clock->frozen) { | |
183 | ret = -1; | |
184 | goto end; | |
185 | } | |
186 | ||
187 | clock->frequency = freq; | |
188 | end: | |
189 | return ret; | |
190 | } | |
191 | ||
98923034 | 192 | BT_HIDDEN |
1c822dfb JG |
193 | uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock) |
194 | { | |
195 | uint64_t ret = -1ULL; | |
196 | ||
197 | if (!clock) { | |
198 | goto end; | |
199 | } | |
200 | ||
201 | ret = clock->precision; | |
202 | end: | |
203 | return ret; | |
204 | } | |
205 | ||
206 | int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision) | |
207 | { | |
208 | int ret = 0; | |
209 | ||
210 | if (!clock || clock->frozen) { | |
211 | ret = -1; | |
212 | goto end; | |
213 | } | |
214 | ||
215 | clock->precision = precision; | |
216 | end: | |
217 | return ret; | |
218 | } | |
219 | ||
98923034 | 220 | BT_HIDDEN |
1c822dfb JG |
221 | int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s) |
222 | { | |
223 | int ret = 0; | |
224 | ||
225 | if (!clock || !offset_s) { | |
226 | ret = -1; | |
227 | goto end; | |
228 | } | |
229 | ||
230 | *offset_s = clock->offset_s; | |
231 | end: | |
232 | return ret; | |
233 | } | |
234 | ||
235 | int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s) | |
236 | { | |
237 | int ret = 0; | |
238 | ||
239 | if (!clock || clock->frozen) { | |
240 | ret = -1; | |
241 | goto end; | |
242 | } | |
243 | ||
244 | clock->offset_s = offset_s; | |
245 | end: | |
246 | return ret; | |
247 | } | |
248 | ||
98923034 | 249 | BT_HIDDEN |
1c822dfb JG |
250 | int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset) |
251 | { | |
252 | int ret = 0; | |
253 | ||
254 | if (!clock || !offset) { | |
255 | ret = -1; | |
256 | goto end; | |
257 | } | |
258 | ||
259 | *offset = clock->offset; | |
260 | end: | |
261 | return ret; | |
262 | } | |
263 | ||
264 | int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset) | |
265 | { | |
266 | int ret = 0; | |
267 | ||
268 | if (!clock || clock->frozen) { | |
269 | ret = -1; | |
270 | goto end; | |
271 | } | |
272 | ||
273 | clock->offset = offset; | |
274 | end: | |
275 | return ret; | |
276 | } | |
277 | ||
98923034 | 278 | BT_HIDDEN |
1c822dfb JG |
279 | int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock) |
280 | { | |
281 | int ret = -1; | |
282 | ||
283 | if (!clock) { | |
284 | goto end; | |
285 | } | |
286 | ||
287 | ret = clock->absolute; | |
288 | end: | |
289 | return ret; | |
290 | } | |
291 | ||
292 | int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute) | |
293 | { | |
294 | int ret = 0; | |
295 | ||
296 | if (!clock || clock->frozen) { | |
297 | ret = -1; | |
298 | goto end; | |
299 | } | |
300 | ||
301 | clock->absolute = !!is_absolute; | |
302 | end: | |
303 | return ret; | |
304 | } | |
305 | ||
98923034 | 306 | BT_HIDDEN |
1c822dfb JG |
307 | const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock) |
308 | { | |
309 | const unsigned char *ret; | |
310 | ||
311 | if (!clock || !clock->uuid_set) { | |
312 | ret = NULL; | |
313 | goto end; | |
314 | } | |
315 | ||
316 | ret = clock->uuid; | |
317 | end: | |
318 | return ret; | |
319 | } | |
320 | ||
98923034 | 321 | BT_HIDDEN |
1c822dfb JG |
322 | int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid) |
323 | { | |
324 | int ret = 0; | |
325 | ||
326 | if (!clock || !uuid || clock->frozen) { | |
327 | ret = -1; | |
328 | goto end; | |
329 | } | |
330 | ||
331 | memcpy(clock->uuid, uuid, sizeof(uuid_t)); | |
332 | clock->uuid_set = 1; | |
333 | end: | |
334 | return ret; | |
335 | } | |
336 | ||
98923034 | 337 | static |
1c822dfb JG |
338 | uint64_t ns_from_value(uint64_t frequency, uint64_t value) |
339 | { | |
340 | uint64_t ns; | |
341 | ||
342 | if (frequency == 1000000000) { | |
343 | ns = value; | |
344 | } else { | |
345 | ns = (uint64_t) ((1e9 * (double) value) / (double) frequency); | |
346 | } | |
347 | ||
348 | return ns; | |
349 | } | |
350 | ||
98923034 | 351 | BT_HIDDEN |
1c822dfb JG |
352 | int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time) |
353 | { | |
354 | int ret = 0; | |
355 | ||
356 | if (!clock || !time) { | |
357 | ret = -1; | |
358 | goto end; | |
359 | } | |
360 | ||
361 | ||
362 | if (!clock->has_value) { | |
363 | /* | |
364 | * Clock belongs to a non-writer mode trace and thus | |
365 | * this function is disabled. | |
366 | */ | |
367 | goto end; | |
368 | } | |
369 | ||
370 | *time = (int64_t) ns_from_value(clock->frequency, clock->value); | |
371 | ||
372 | end: | |
373 | return ret; | |
374 | } | |
375 | ||
376 | int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time) | |
377 | { | |
378 | int ret = 0; | |
379 | int64_t value; | |
380 | ||
381 | /* Timestamps are strictly monotonic */ | |
382 | if (!clock) { | |
383 | ret = -1; | |
384 | goto end; | |
385 | } | |
386 | ||
387 | ||
388 | if (!clock->has_value) { | |
389 | /* | |
390 | * Clock belongs to a non-writer mode trace and thus | |
391 | * this function is disabled. | |
392 | */ | |
393 | ret = -1; | |
394 | goto end; | |
395 | } | |
396 | ||
397 | /* Common case where cycles are actually nanoseconds */ | |
398 | if (clock->frequency == 1000000000) { | |
399 | value = time; | |
400 | } else { | |
401 | value = (uint64_t) (((double) time * | |
402 | (double) clock->frequency) / 1e9); | |
403 | } | |
404 | ||
405 | ret = bt_ctf_clock_set_value(clock, value); | |
406 | end: | |
407 | return ret; | |
408 | } | |
409 | ||
98923034 | 410 | BT_HIDDEN |
1c822dfb JG |
411 | uint64_t bt_ctf_clock_get_value(struct bt_ctf_clock *clock) |
412 | { | |
413 | uint64_t ret = -1ULL; | |
414 | ||
415 | if (!clock) { | |
416 | goto end; | |
417 | } | |
418 | ||
419 | if (!clock->has_value) { | |
420 | /* | |
421 | * Clock belongs to a non-writer mode trace and thus | |
422 | * this function is disabled. | |
423 | */ | |
424 | goto end; | |
425 | } | |
426 | ||
427 | ret = clock->value; | |
428 | end: | |
429 | return ret; | |
430 | } | |
431 | ||
98923034 | 432 | BT_HIDDEN |
1c822dfb JG |
433 | int bt_ctf_clock_set_value(struct bt_ctf_clock *clock, uint64_t value) |
434 | { | |
435 | int ret = 0; | |
436 | ||
437 | if (!clock) { | |
438 | ret = -1; | |
439 | goto end; | |
440 | } | |
441 | ||
442 | if (!clock->has_value) { | |
443 | /* | |
444 | * Clock belongs to a non-writer mode trace and thus | |
445 | * this function is disabled. | |
446 | */ | |
447 | ret = -1; | |
448 | goto end; | |
449 | } | |
450 | ||
451 | /* Timestamps are strictly monotonic */ | |
452 | if (value < clock->value) { | |
453 | ret = -1; | |
454 | goto end; | |
455 | } | |
456 | ||
457 | clock->value = value; | |
458 | end: | |
459 | return ret; | |
460 | } | |
461 | ||
462 | void bt_ctf_clock_get(struct bt_ctf_clock *clock) | |
463 | { | |
464 | bt_get(clock); | |
465 | } | |
466 | ||
467 | void bt_ctf_clock_put(struct bt_ctf_clock *clock) | |
468 | { | |
469 | bt_put(clock); | |
470 | } | |
471 | ||
472 | BT_HIDDEN | |
473 | void bt_ctf_clock_freeze(struct bt_ctf_clock *clock) | |
474 | { | |
475 | if (!clock) { | |
476 | return; | |
477 | } | |
478 | ||
479 | clock->frozen = 1; | |
480 | } | |
481 | ||
482 | BT_HIDDEN | |
483 | void bt_ctf_clock_serialize(struct bt_ctf_clock *clock, | |
484 | struct metadata_context *context) | |
485 | { | |
486 | unsigned char *uuid; | |
487 | ||
488 | if (!clock || !context) { | |
489 | return; | |
490 | } | |
491 | ||
492 | uuid = clock->uuid; | |
493 | g_string_append(context->string, "clock {\n"); | |
494 | g_string_append_printf(context->string, "\tname = %s;\n", | |
495 | clock->name->str); | |
496 | g_string_append_printf(context->string, | |
497 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
498 | uuid[0], uuid[1], uuid[2], uuid[3], | |
499 | uuid[4], uuid[5], uuid[6], uuid[7], | |
500 | uuid[8], uuid[9], uuid[10], uuid[11], | |
501 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
502 | if (clock->description) { | |
503 | g_string_append_printf(context->string, "\tdescription = \"%s\";\n", | |
504 | clock->description->str); | |
505 | } | |
506 | ||
507 | g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n", | |
508 | clock->frequency); | |
509 | g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n", | |
510 | clock->precision); | |
511 | g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n", | |
512 | clock->offset_s); | |
513 | g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n", | |
514 | clock->offset); | |
515 | g_string_append_printf(context->string, "\tabsolute = %s;\n", | |
516 | clock->absolute ? "TRUE" : "FALSE"); | |
517 | g_string_append(context->string, "};\n\n"); | |
518 | } | |
519 | ||
520 | static | |
521 | void bt_ctf_clock_destroy(struct bt_object *obj) | |
522 | { | |
523 | struct bt_ctf_clock *clock; | |
524 | ||
525 | clock = container_of(obj, struct bt_ctf_clock, base); | |
526 | if (clock->name) { | |
527 | g_string_free(clock->name, TRUE); | |
528 | } | |
529 | ||
530 | if (clock->description) { | |
531 | g_string_free(clock->description, TRUE); | |
532 | } | |
533 | ||
534 | g_free(clock); | |
535 | } | |
536 | ||
98923034 | 537 | BT_HIDDEN |
1c822dfb JG |
538 | int64_t bt_ctf_clock_ns_from_value(struct bt_ctf_clock *clock, uint64_t value) |
539 | { | |
540 | int64_t ns = -1ULL; | |
541 | ||
542 | if (!clock) { | |
543 | goto end; | |
544 | } | |
545 | ||
546 | /* Initialize nanosecond timestamp to clock's offset in seconds */ | |
547 | ns = clock->offset_s * 1000000000; | |
548 | ||
549 | /* Add offset in cycles, converted to nanoseconds */ | |
550 | ns += ns_from_value(clock->frequency, clock->offset); | |
551 | ||
552 | /* Add given value, converter to nanoseconds */ | |
553 | ns += ns_from_value(clock->frequency, value); | |
554 | ||
555 | end: | |
556 | return ns; | |
557 | } |