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