ctf.fs: bt_ctf_notif_iter_create(): assert() that all medops exist
[babeltrace.git] / formats / ctf / ir / clock-class.c
1 /*
2 * clock-class.c
3 *
4 * Babeltrace CTF IR - Clock class
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-class-internal.h>
30 #include <babeltrace/ctf-ir/utils.h>
31 #include <babeltrace/ref.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/compiler.h>
34 #include <inttypes.h>
35
36 static
37 void bt_ctf_clock_class_destroy(struct bt_object *obj);
38
39 BT_HIDDEN
40 bool bt_ctf_clock_class_is_valid(struct bt_ctf_clock_class *clock_class)
41 {
42 return clock_class && clock_class->name;
43 }
44
45 int bt_ctf_clock_class_set_name(struct bt_ctf_clock_class *clock_class,
46 const char *name)
47 {
48 int ret = 0;
49
50 if (!clock_class || clock_class->frozen) {
51 ret = -1;
52 goto end;
53 }
54
55 if (bt_ctf_validate_identifier(name)) {
56 ret = -1;
57 goto end;
58 }
59
60 if (clock_class->name) {
61 g_string_assign(clock_class->name, name);
62 } else {
63 clock_class->name = g_string_new(name);
64 if (!clock_class->name) {
65 ret = -1;
66 goto end;
67 }
68 }
69
70 end:
71 return ret;
72 }
73
74 struct bt_ctf_clock_class *bt_ctf_clock_class_create(const char *name)
75 {
76 int ret;
77 struct bt_ctf_clock_class *clock_class =
78 g_new0(struct bt_ctf_clock_class, 1);
79
80 if (!clock_class) {
81 goto error;
82 }
83
84 clock_class->precision = 1;
85 clock_class->frequency = 1000000000;
86 bt_object_init(clock_class, bt_ctf_clock_class_destroy);
87
88 if (name) {
89 ret = bt_ctf_clock_class_set_name(clock_class, name);
90 if (ret) {
91 goto error;
92 }
93 }
94
95 ret = bt_uuid_generate(clock_class->uuid);
96 if (ret) {
97 goto error;
98 }
99
100 clock_class->uuid_set = 1;
101 return clock_class;
102 error:
103 BT_PUT(clock_class);
104 return clock_class;
105 }
106
107 const char *bt_ctf_clock_class_get_name(struct bt_ctf_clock_class *clock_class)
108 {
109 const char *ret = NULL;
110
111 if (!clock_class) {
112 goto end;
113 }
114
115 if (clock_class->name) {
116 ret = clock_class->name->str;
117 }
118
119 end:
120 return ret;
121 }
122
123 const char *bt_ctf_clock_class_get_description(
124 struct bt_ctf_clock_class *clock_class)
125 {
126 const char *ret = NULL;
127
128 if (!clock_class) {
129 goto end;
130 }
131
132 if (clock_class->description) {
133 ret = clock_class->description->str;
134 }
135 end:
136 return ret;
137 }
138
139 int bt_ctf_clock_class_set_description(struct bt_ctf_clock_class *clock_class,
140 const char *desc)
141 {
142 int ret = 0;
143
144 if (!clock_class || !desc || clock_class->frozen) {
145 ret = -1;
146 goto end;
147 }
148
149 clock_class->description = g_string_new(desc);
150 ret = clock_class->description ? 0 : -1;
151 end:
152 return ret;
153 }
154
155 uint64_t bt_ctf_clock_class_get_frequency(
156 struct bt_ctf_clock_class *clock_class)
157 {
158 uint64_t ret = -1ULL;
159
160 if (!clock_class) {
161 goto end;
162 }
163
164 ret = clock_class->frequency;
165 end:
166 return ret;
167 }
168
169 int bt_ctf_clock_class_set_frequency(struct bt_ctf_clock_class *clock_class,
170 uint64_t freq)
171 {
172 int ret = 0;
173
174 if (!clock_class || clock_class->frozen) {
175 ret = -1;
176 goto end;
177 }
178
179 clock_class->frequency = freq;
180 end:
181 return ret;
182 }
183
184 uint64_t bt_ctf_clock_class_get_precision(struct bt_ctf_clock_class *clock_class)
185 {
186 uint64_t ret = -1ULL;
187
188 if (!clock_class) {
189 goto end;
190 }
191
192 ret = clock_class->precision;
193 end:
194 return ret;
195 }
196
197 int bt_ctf_clock_class_set_precision(struct bt_ctf_clock_class *clock_class,
198 uint64_t precision)
199 {
200 int ret = 0;
201
202 if (!clock_class || clock_class->frozen) {
203 ret = -1;
204 goto end;
205 }
206
207 clock_class->precision = precision;
208 end:
209 return ret;
210 }
211
212 int bt_ctf_clock_class_get_offset_s(struct bt_ctf_clock_class *clock_class,
213 int64_t *offset_s)
214 {
215 int ret = 0;
216
217 if (!clock_class || !offset_s) {
218 ret = -1;
219 goto end;
220 }
221
222 *offset_s = clock_class->offset_s;
223 end:
224 return ret;
225 }
226
227 int bt_ctf_clock_class_set_offset_s(struct bt_ctf_clock_class *clock_class,
228 int64_t offset_s)
229 {
230 int ret = 0;
231
232 if (!clock_class || clock_class->frozen) {
233 ret = -1;
234 goto end;
235 }
236
237 clock_class->offset_s = offset_s;
238 end:
239 return ret;
240 }
241
242 int bt_ctf_clock_class_get_offset_cycles(struct bt_ctf_clock_class *clock_class,
243 int64_t *offset)
244 {
245 int ret = 0;
246
247 if (!clock_class || !offset) {
248 ret = -1;
249 goto end;
250 }
251
252 *offset = clock_class->offset;
253 end:
254 return ret;
255 }
256
257 int bt_ctf_clock_class_set_offset_cycles(struct bt_ctf_clock_class *clock_class,
258 int64_t offset)
259 {
260 int ret = 0;
261
262 if (!clock_class || clock_class->frozen) {
263 ret = -1;
264 goto end;
265 }
266
267 clock_class->offset = offset;
268 end:
269 return ret;
270 }
271
272 int bt_ctf_clock_class_get_is_absolute(struct bt_ctf_clock_class *clock_class)
273 {
274 int ret = -1;
275
276 if (!clock_class) {
277 goto end;
278 }
279
280 ret = clock_class->absolute;
281 end:
282 return ret;
283 }
284
285 int bt_ctf_clock_class_set_is_absolute(struct bt_ctf_clock_class *clock_class,
286 int is_absolute)
287 {
288 int ret = 0;
289
290 if (!clock_class || clock_class->frozen) {
291 ret = -1;
292 goto end;
293 }
294
295 clock_class->absolute = !!is_absolute;
296 end:
297 return ret;
298 }
299
300 const unsigned char *bt_ctf_clock_class_get_uuid(
301 struct bt_ctf_clock_class *clock_class)
302 {
303 const unsigned char *ret;
304
305 if (!clock_class || !clock_class->uuid_set) {
306 ret = NULL;
307 goto end;
308 }
309
310 ret = clock_class->uuid;
311 end:
312 return ret;
313 }
314
315 int bt_ctf_clock_class_set_uuid(struct bt_ctf_clock_class *clock_class,
316 const unsigned char *uuid)
317 {
318 int ret = 0;
319
320 if (!clock_class || !uuid || clock_class->frozen) {
321 ret = -1;
322 goto end;
323 }
324
325 memcpy(clock_class->uuid, uuid, sizeof(uuid_t));
326 clock_class->uuid_set = 1;
327 end:
328 return ret;
329 }
330
331 static uint64_t ns_from_value(uint64_t frequency, uint64_t value)
332 {
333 uint64_t ns;
334
335 if (frequency == 1000000000) {
336 ns = value;
337 } else {
338 ns = (uint64_t) ((1e9 * (double) value) / (double) frequency);
339 }
340
341 return ns;
342 }
343
344 BT_HIDDEN
345 void bt_ctf_clock_class_freeze(struct bt_ctf_clock_class *clock_class)
346 {
347 if (!clock_class) {
348 return;
349 }
350
351 clock_class->frozen = 1;
352 }
353
354 BT_HIDDEN
355 void bt_ctf_clock_class_serialize(struct bt_ctf_clock_class *clock_class,
356 struct metadata_context *context)
357 {
358 unsigned char *uuid;
359
360 if (!clock_class || !context) {
361 return;
362 }
363
364 uuid = clock_class->uuid;
365 g_string_append(context->string, "clock {\n");
366 g_string_append_printf(context->string, "\tname = %s;\n",
367 clock_class->name->str);
368 g_string_append_printf(context->string,
369 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
370 uuid[0], uuid[1], uuid[2], uuid[3],
371 uuid[4], uuid[5], uuid[6], uuid[7],
372 uuid[8], uuid[9], uuid[10], uuid[11],
373 uuid[12], uuid[13], uuid[14], uuid[15]);
374 if (clock_class->description) {
375 g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
376 clock_class->description->str);
377 }
378
379 g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n",
380 clock_class->frequency);
381 g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n",
382 clock_class->precision);
383 g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n",
384 clock_class->offset_s);
385 g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
386 clock_class->offset);
387 g_string_append_printf(context->string, "\tabsolute = %s;\n",
388 clock_class->absolute ? "TRUE" : "FALSE");
389 g_string_append(context->string, "};\n\n");
390 }
391
392 static
393 void bt_ctf_clock_class_destroy(struct bt_object *obj)
394 {
395 struct bt_ctf_clock_class *clock_class;
396
397 clock_class = container_of(obj, struct bt_ctf_clock_class, base);
398 if (clock_class->name) {
399 g_string_free(clock_class->name, TRUE);
400 }
401 if (clock_class->description) {
402 g_string_free(clock_class->description, TRUE);
403 }
404
405 g_free(clock_class);
406 }
407
408 static
409 void bt_ctf_clock_value_destroy(struct bt_object *obj)
410 {
411 struct bt_ctf_clock_value *value;
412
413 if (!obj) {
414 return;
415 }
416
417 value = container_of(obj, struct bt_ctf_clock_value, base);
418 bt_put(value->clock_class);
419 g_free(value);
420 }
421
422 struct bt_ctf_clock_value *bt_ctf_clock_value_create(
423 struct bt_ctf_clock_class *clock_class, uint64_t value)
424 {
425 struct bt_ctf_clock_value *ret = NULL;
426
427 if (!clock_class) {
428 goto end;
429 }
430
431 ret = g_new0(struct bt_ctf_clock_value, 1);
432 if (!ret) {
433 goto end;
434 }
435
436 bt_object_init(ret, bt_ctf_clock_value_destroy);
437 ret->clock_class = bt_get(clock_class);
438 ret->value = value;
439 end:
440 return ret;
441 }
442
443 int bt_ctf_clock_value_get_value(
444 struct bt_ctf_clock_value *clock_value, uint64_t *raw_value)
445 {
446 int ret = 0;
447
448 if (!clock_value || !raw_value) {
449 ret = -1;
450 goto end;
451 }
452
453 *raw_value = clock_value->value;
454 end:
455 return ret;
456 }
457
458 int bt_ctf_clock_value_get_value_ns_from_epoch(struct bt_ctf_clock_value *value,
459 int64_t *ret_value_ns)
460 {
461 int ret = 0;
462 int64_t ns;
463
464 if (!value || !ret_value_ns) {
465 ret = -1;
466 goto end;
467 }
468
469 /* Initialize nanosecond timestamp to clock's offset in seconds. */
470 ns = value->clock_class->offset_s * 1000000000;
471
472 /* Add offset in cycles, converted to nanoseconds. */
473 ns += ns_from_value(value->clock_class->frequency,
474 value->clock_class->offset);
475
476 /* Add given value, converter to nanoseconds. */
477 ns += ns_from_value(value->clock_class->frequency, value->value);
478
479 *ret_value_ns = ns;
480 end:
481 return ret;
482 }
483
484 struct bt_ctf_clock_class *bt_ctf_clock_value_get_class(
485 struct bt_ctf_clock_value *clock_value)
486 {
487 struct bt_ctf_clock_class *clock_class = NULL;
488
489 if (!clock_value) {
490 goto end;
491 }
492
493 clock_class = bt_get(clock_value->clock_class);
494
495 end:
496 return clock_class;
497 }
This page took 0.045187 seconds and 4 git commands to generate.