195f83716bf3840c79f98fa15861a1783d80c34b
[babeltrace.git] / formats / ctf / ir / clock.c
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-writer/writer-internal.h>
31 #include <babeltrace/compiler.h>
32 #include <inttypes.h>
33
34 static
35 void bt_ctf_clock_destroy(struct bt_ctf_ref *ref);
36
37 BT_HIDDEN
38 struct bt_ctf_clock *_bt_ctf_clock_create(void)
39 {
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;
49 bt_ctf_ref_init(&clock->ref_count);
50 end:
51 return clock;
52 }
53
54 BT_HIDDEN
55 int bt_ctf_clock_set_name(struct bt_ctf_clock *clock,
56 const char *name)
57 {
58 int ret = 0;
59
60 if (validate_identifier(name)) {
61 ret = -1;
62 goto end;
63 }
64
65 if (clock->name) {
66 g_string_free(clock->name, TRUE);
67 }
68
69 clock->name = g_string_new(name);
70 if (!clock->name) {
71 ret = -1;
72 goto end;
73 }
74 end:
75 return ret;
76 }
77
78 struct 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) {
90 goto error_destroy;
91 }
92
93 ret = babeltrace_uuid_generate(clock->uuid);
94 if (ret) {
95 goto error_destroy;
96 }
97
98 clock->uuid_set = 1;
99 return clock;
100 error_destroy:
101 bt_ctf_clock_destroy(&clock->ref_count);
102 error:
103 return NULL;
104 }
105
106 const 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
118 end:
119 return ret;
120 }
121
122 const 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 }
133 end:
134 return ret;
135 }
136
137 int 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
146 clock->description = g_string_new(desc);
147 ret = clock->description ? 0 : -1;
148 end:
149 return ret;
150 }
151
152 uint64_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;
161 end:
162 return ret;
163 }
164
165 int 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;
175 end:
176 return ret;
177 }
178
179 uint64_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;
188 end:
189 return ret;
190 }
191
192 int 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;
202 end:
203 return ret;
204 }
205
206 uint64_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;
215 end:
216 return ret;
217 }
218
219 int 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;
229 end:
230 return ret;
231 }
232
233 uint64_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;
242 end:
243 return ret;
244 }
245
246 int 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;
256 end:
257 return ret;
258 }
259
260 int 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;
269 end:
270 return ret;
271 }
272
273 int 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;
283 end:
284 return ret;
285 }
286
287 const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock)
288 {
289 const unsigned char *ret;
290
291 if (!clock || !clock->uuid_set) {
292 ret = NULL;
293 goto end;
294 }
295
296 ret = clock->uuid;
297 end:
298 return ret;
299 }
300
301 int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid)
302 {
303 int ret = 0;
304
305 if (!clock || !uuid || clock->frozen) {
306 ret = -1;
307 goto end;
308 }
309
310 memcpy(clock->uuid, uuid, sizeof(uuid_t));
311 clock->uuid_set = 1;
312 end:
313 return ret;
314 }
315
316 uint64_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;
325 end:
326 return ret;
327 }
328
329 int 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;
340 end:
341 return ret;
342 }
343
344 void 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
353 void 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
362 BT_HIDDEN
363 void bt_ctf_clock_freeze(struct bt_ctf_clock *clock)
364 {
365 if (!clock) {
366 return;
367 }
368
369 clock->frozen = 1;
370 }
371
372 BT_HIDDEN
373 void 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
410 static
411 void 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.036471 seconds and 3 git commands to generate.