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