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