Don't automatically generate a clock UUID in _bt_ctf_clock_create
[babeltrace.git] / formats / ctf / ir / clock.c
CommitLineData
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>
273b65be
JG
30#include <babeltrace/ctf-writer/writer-internal.h>
31#include <babeltrace/compiler.h>
32#include <inttypes.h>
33
34static
35void bt_ctf_clock_destroy(struct bt_ctf_ref *ref);
36
4c426c17
JG
37BT_HIDDEN
38struct bt_ctf_clock *_bt_ctf_clock_create(void)
273b65be 39{
4c426c17
JG
40 struct bt_ctf_clock *clock = g_new0(
41 struct bt_ctf_clock, 1);
42
43 if (!clock) {
44 goto end;
45 }
46
47 clock->precision = 1;
48 clock->frequency = 1000000000;
4c426c17
JG
49 bt_ctf_ref_init(&clock->ref_count);
50end:
51 return clock;
52}
53
54BT_HIDDEN
55int bt_ctf_clock_set_name(struct bt_ctf_clock *clock,
56 const char *name)
57{
58 int ret = 0;
273b65be
JG
59
60 if (validate_identifier(name)) {
4c426c17
JG
61 ret = -1;
62 goto end;
273b65be
JG
63 }
64
4c426c17
JG
65 if (clock->name) {
66 g_string_free(clock->name, TRUE);
273b65be
JG
67 }
68
69 clock->name = g_string_new(name);
70 if (!clock->name) {
4c426c17
JG
71 ret = -1;
72 goto end;
73 }
74end:
75 return ret;
76}
77
78struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
79{
80 int ret;
81 struct bt_ctf_clock *clock = NULL;
82
83 clock = _bt_ctf_clock_create();
84 if (!clock) {
85 goto error;
86 }
87
88 ret = bt_ctf_clock_set_name(clock, name);
89 if (ret) {
273b65be
JG
90 goto error_destroy;
91 }
92
be018f15
JG
93 ret = babeltrace_uuid_generate(clock->uuid);
94 if (ret) {
95 goto error_destroy;
96 }
97
98 clock->uuid_set = 1;
273b65be
JG
99 return clock;
100error_destroy:
101 bt_ctf_clock_destroy(&clock->ref_count);
102error:
87d76bb1
JG
103 return NULL;
104}
105
106const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock)
107{
108 const char *ret = NULL;
109
110 if (!clock) {
111 goto end;
112 }
113
114 if (clock->name) {
115 ret = clock->name->str;
116 }
117
118end:
119 return ret;
120}
121
122const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock)
123{
124 const char *ret = NULL;
125
126 if (!clock) {
127 goto end;
128 }
129
130 if (clock->description) {
131 ret = clock->description->str;
132 }
133end:
134 return ret;
273b65be
JG
135}
136
137int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
138{
139 int ret = 0;
140
141 if (!clock || !desc || clock->frozen) {
142 ret = -1;
143 goto end;
144 }
145
87d76bb1 146 clock->description = g_string_new(desc);
273b65be
JG
147 ret = clock->description ? 0 : -1;
148end:
149 return ret;
150}
151
87d76bb1
JG
152uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock)
153{
154 uint64_t ret = -1ULL;
155
156 if (!clock) {
157 goto end;
158 }
159
160 ret = clock->frequency;
161end:
162 return ret;
163}
164
273b65be
JG
165int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
166{
167 int ret = 0;
168
169 if (!clock || clock->frozen) {
170 ret = -1;
171 goto end;
172 }
173
174 clock->frequency = freq;
175end:
176 return ret;
177}
178
87d76bb1
JG
179uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock)
180{
181 uint64_t ret = -1ULL;
182
183 if (!clock) {
184 goto end;
185 }
186
187 ret = clock->precision;
188end:
189 return ret;
190}
191
273b65be
JG
192int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
193{
194 int ret = 0;
195
196 if (!clock || clock->frozen) {
197 ret = -1;
198 goto end;
199 }
200
201 clock->precision = precision;
202end:
203 return ret;
204}
205
87d76bb1
JG
206uint64_t bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock)
207{
208 uint64_t ret = -1ULL;
209
210 if (!clock) {
211 goto end;
212 }
213
214 ret = clock->offset_s;
215end:
216 return ret;
217}
218
273b65be
JG
219int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s)
220{
221 int ret = 0;
222
223 if (!clock || clock->frozen) {
224 ret = -1;
225 goto end;
226 }
227
228 clock->offset_s = offset_s;
229end:
230 return ret;
231}
232
87d76bb1
JG
233uint64_t bt_ctf_clock_get_offset(struct bt_ctf_clock *clock)
234{
235 uint64_t ret = -1ULL;
236
237 if (!clock) {
238 goto end;
239 }
240
241 ret = clock->offset;
242end:
243 return ret;
244}
245
273b65be
JG
246int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset)
247{
248 int ret = 0;
249
250 if (!clock || clock->frozen) {
251 ret = -1;
252 goto end;
253 }
254
255 clock->offset = offset;
256end:
257 return ret;
258}
259
87d76bb1
JG
260int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock)
261{
262 int ret = -1;
263
264 if (!clock) {
265 goto end;
266 }
267
268 ret = clock->absolute;
269end:
270 return ret;
271}
272
273b65be
JG
273int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
274{
275 int ret = 0;
276
277 if (!clock || clock->frozen) {
278 ret = -1;
279 goto end;
280 }
281
282 clock->absolute = !!is_absolute;
283end:
284 return ret;
285}
286
85b743f4
JG
287const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock)
288{
289 const unsigned char *ret;
290
be018f15 291 if (!clock || !clock->uuid_set) {
85b743f4
JG
292 ret = NULL;
293 goto end;
294 }
295
296 ret = clock->uuid;
297end:
298 return ret;
299}
300
301int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid)
302{
303 int ret = 0;
304
be018f15 305 if (!clock || !uuid || clock->frozen) {
85b743f4
JG
306 ret = -1;
307 goto end;
308 }
309
310 memcpy(clock->uuid, uuid, sizeof(uuid_t));
be018f15 311 clock->uuid_set = 1;
85b743f4
JG
312end:
313 return ret;
314}
315
87d76bb1
JG
316uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
317{
318 uint64_t ret = -1ULL;
319
320 if (!clock) {
321 goto end;
322 }
323
324 ret = clock->time;
325end:
326 return ret;
327}
328
273b65be
JG
329int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time)
330{
331 int ret = 0;
332
333 /* Timestamps are strictly monotonic */
334 if (!clock || time < clock->time) {
335 ret = -1;
336 goto end;
337 }
338
339 clock->time = time;
340end:
341 return ret;
342}
343
344void bt_ctf_clock_get(struct bt_ctf_clock *clock)
345{
346 if (!clock) {
347 return;
348 }
349
350 bt_ctf_ref_get(&clock->ref_count);
351}
352
353void bt_ctf_clock_put(struct bt_ctf_clock *clock)
354{
355 if (!clock) {
356 return;
357 }
358
359 bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy);
360}
361
362BT_HIDDEN
363void bt_ctf_clock_freeze(struct bt_ctf_clock *clock)
364{
365 if (!clock) {
366 return;
367 }
368
369 clock->frozen = 1;
370}
371
372BT_HIDDEN
373void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
374 struct metadata_context *context)
375{
376 unsigned char *uuid;
377
378 if (!clock || !context) {
379 return;
380 }
381
382 uuid = clock->uuid;
383 g_string_append(context->string, "clock {\n");
384 g_string_append_printf(context->string, "\tname = %s;\n",
385 clock->name->str);
386 g_string_append_printf(context->string,
387 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
388 uuid[0], uuid[1], uuid[2], uuid[3],
389 uuid[4], uuid[5], uuid[6], uuid[7],
390 uuid[8], uuid[9], uuid[10], uuid[11],
391 uuid[12], uuid[13], uuid[14], uuid[15]);
392 if (clock->description->len) {
393 g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
394 clock->description->str);
395 }
396
397 g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n",
398 clock->frequency);
399 g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n",
400 clock->precision);
401 g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n",
402 clock->offset_s);
403 g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
404 clock->offset);
405 g_string_append_printf(context->string, "\tabsolute = %s;\n",
406 clock->absolute ? "TRUE" : "FALSE");
407 g_string_append(context->string, "};\n\n");
408}
409
273b65be
JG
410static
411void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
412{
413 struct bt_ctf_clock *clock;
414
415 if (!ref) {
416 return;
417 }
418
419 clock = container_of(ref, struct bt_ctf_clock, ref_count);
420 if (clock->name) {
421 g_string_free(clock->name, TRUE);
422 }
423
424 if (clock->description) {
425 g_string_free(clock->description, TRUE);
426 }
427
428 g_free(clock);
429}
This page took 0.045897 seconds and 4 git commands to generate.