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