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> |
83509119 | 31 | #include <babeltrace/ref.h> |
273b65be | 32 | #include <babeltrace/ctf-writer/writer-internal.h> |
83509119 | 33 | #include <babeltrace/object-internal.h> |
273b65be JG |
34 | #include <babeltrace/compiler.h> |
35 | #include <inttypes.h> | |
36 | ||
37 | static | |
83509119 | 38 | void bt_ctf_clock_destroy(struct bt_object *obj); |
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; | |
83509119 | 52 | bt_object_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) { | |
83509119 | 94 | goto error; |
273b65be JG |
95 | } |
96 | ||
be018f15 JG |
97 | ret = babeltrace_uuid_generate(clock->uuid); |
98 | if (ret) { | |
83509119 | 99 | goto error; |
be018f15 JG |
100 | } |
101 | ||
102 | clock->uuid_set = 1; | |
273b65be | 103 | return clock; |
273b65be | 104 | error: |
83509119 JG |
105 | BT_PUT(clock); |
106 | return clock; | |
87d76bb1 JG |
107 | } |
108 | ||
109 | const 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 | ||
121 | end: | |
122 | return ret; | |
123 | } | |
124 | ||
125 | const 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 | } | |
136 | end: | |
137 | return ret; | |
273b65be JG |
138 | } |
139 | ||
140 | int 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 | ||
87d76bb1 | 149 | clock->description = g_string_new(desc); |
273b65be JG |
150 | ret = clock->description ? 0 : -1; |
151 | end: | |
152 | return ret; | |
153 | } | |
154 | ||
87d76bb1 JG |
155 | uint64_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; | |
164 | end: | |
165 | return ret; | |
166 | } | |
167 | ||
273b65be JG |
168 | int 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; | |
178 | end: | |
179 | return ret; | |
180 | } | |
181 | ||
87d76bb1 JG |
182 | uint64_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; | |
191 | end: | |
192 | return ret; | |
193 | } | |
194 | ||
273b65be JG |
195 | int 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; | |
205 | end: | |
206 | return ret; | |
207 | } | |
208 | ||
87d76bb1 JG |
209 | uint64_t bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock) |
210 | { | |
211 | uint64_t ret = -1ULL; | |
212 | ||
213 | if (!clock) { | |
214 | goto end; | |
215 | } | |
216 | ||
217 | ret = clock->offset_s; | |
218 | end: | |
219 | return ret; | |
220 | } | |
221 | ||
273b65be JG |
222 | int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s) |
223 | { | |
224 | int ret = 0; | |
225 | ||
226 | if (!clock || clock->frozen) { | |
227 | ret = -1; | |
228 | goto end; | |
229 | } | |
230 | ||
231 | clock->offset_s = offset_s; | |
232 | end: | |
233 | return ret; | |
234 | } | |
235 | ||
87d76bb1 JG |
236 | uint64_t bt_ctf_clock_get_offset(struct bt_ctf_clock *clock) |
237 | { | |
238 | uint64_t ret = -1ULL; | |
239 | ||
240 | if (!clock) { | |
241 | goto end; | |
242 | } | |
243 | ||
244 | ret = clock->offset; | |
245 | end: | |
246 | return ret; | |
247 | } | |
248 | ||
273b65be JG |
249 | int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset) |
250 | { | |
251 | int ret = 0; | |
252 | ||
253 | if (!clock || clock->frozen) { | |
254 | ret = -1; | |
255 | goto end; | |
256 | } | |
257 | ||
258 | clock->offset = offset; | |
259 | end: | |
260 | return ret; | |
261 | } | |
262 | ||
87d76bb1 JG |
263 | int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock) |
264 | { | |
265 | int ret = -1; | |
266 | ||
267 | if (!clock) { | |
268 | goto end; | |
269 | } | |
270 | ||
271 | ret = clock->absolute; | |
272 | end: | |
273 | return ret; | |
274 | } | |
275 | ||
273b65be JG |
276 | int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute) |
277 | { | |
278 | int ret = 0; | |
279 | ||
280 | if (!clock || clock->frozen) { | |
281 | ret = -1; | |
282 | goto end; | |
283 | } | |
284 | ||
285 | clock->absolute = !!is_absolute; | |
286 | end: | |
287 | return ret; | |
288 | } | |
289 | ||
85b743f4 JG |
290 | const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock) |
291 | { | |
292 | const unsigned char *ret; | |
293 | ||
be018f15 | 294 | if (!clock || !clock->uuid_set) { |
85b743f4 JG |
295 | ret = NULL; |
296 | goto end; | |
297 | } | |
298 | ||
299 | ret = clock->uuid; | |
300 | end: | |
301 | return ret; | |
302 | } | |
303 | ||
304 | int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid) | |
305 | { | |
306 | int ret = 0; | |
307 | ||
be018f15 | 308 | if (!clock || !uuid || clock->frozen) { |
85b743f4 JG |
309 | ret = -1; |
310 | goto end; | |
311 | } | |
312 | ||
313 | memcpy(clock->uuid, uuid, sizeof(uuid_t)); | |
be018f15 | 314 | clock->uuid_set = 1; |
85b743f4 JG |
315 | end: |
316 | return ret; | |
317 | } | |
318 | ||
87d76bb1 JG |
319 | uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock) |
320 | { | |
321 | uint64_t ret = -1ULL; | |
322 | ||
323 | if (!clock) { | |
324 | goto end; | |
325 | } | |
326 | ||
327 | ret = clock->time; | |
328 | end: | |
329 | return ret; | |
330 | } | |
331 | ||
273b65be JG |
332 | int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time) |
333 | { | |
334 | int ret = 0; | |
335 | ||
336 | /* Timestamps are strictly monotonic */ | |
337 | if (!clock || time < clock->time) { | |
338 | ret = -1; | |
339 | goto end; | |
340 | } | |
341 | ||
342 | clock->time = time; | |
343 | end: | |
344 | return ret; | |
345 | } | |
346 | ||
347 | void bt_ctf_clock_get(struct bt_ctf_clock *clock) | |
348 | { | |
83509119 | 349 | bt_get(clock); |
273b65be JG |
350 | } |
351 | ||
352 | void bt_ctf_clock_put(struct bt_ctf_clock *clock) | |
353 | { | |
83509119 | 354 | bt_put(clock); |
273b65be JG |
355 | } |
356 | ||
357 | BT_HIDDEN | |
358 | void bt_ctf_clock_freeze(struct bt_ctf_clock *clock) | |
359 | { | |
360 | if (!clock) { | |
361 | return; | |
362 | } | |
363 | ||
364 | clock->frozen = 1; | |
365 | } | |
366 | ||
367 | BT_HIDDEN | |
368 | void bt_ctf_clock_serialize(struct bt_ctf_clock *clock, | |
369 | struct metadata_context *context) | |
370 | { | |
371 | unsigned char *uuid; | |
372 | ||
373 | if (!clock || !context) { | |
374 | return; | |
375 | } | |
376 | ||
377 | uuid = clock->uuid; | |
378 | g_string_append(context->string, "clock {\n"); | |
379 | g_string_append_printf(context->string, "\tname = %s;\n", | |
380 | clock->name->str); | |
381 | g_string_append_printf(context->string, | |
382 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
383 | uuid[0], uuid[1], uuid[2], uuid[3], | |
384 | uuid[4], uuid[5], uuid[6], uuid[7], | |
385 | uuid[8], uuid[9], uuid[10], uuid[11], | |
386 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
7a69f348 | 387 | if (clock->description) { |
273b65be JG |
388 | g_string_append_printf(context->string, "\tdescription = \"%s\";\n", |
389 | clock->description->str); | |
390 | } | |
391 | ||
392 | g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n", | |
393 | clock->frequency); | |
394 | g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n", | |
395 | clock->precision); | |
396 | g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n", | |
397 | clock->offset_s); | |
398 | g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n", | |
399 | clock->offset); | |
400 | g_string_append_printf(context->string, "\tabsolute = %s;\n", | |
401 | clock->absolute ? "TRUE" : "FALSE"); | |
402 | g_string_append(context->string, "};\n\n"); | |
403 | } | |
404 | ||
273b65be | 405 | static |
83509119 | 406 | void bt_ctf_clock_destroy(struct bt_object *obj) |
273b65be JG |
407 | { |
408 | struct bt_ctf_clock *clock; | |
273b65be | 409 | |
83509119 | 410 | clock = container_of(obj, struct bt_ctf_clock, base); |
273b65be JG |
411 | if (clock->name) { |
412 | g_string_free(clock->name, TRUE); | |
413 | } | |
414 | ||
415 | if (clock->description) { | |
416 | g_string_free(clock->description, TRUE); | |
417 | } | |
418 | ||
419 | g_free(clock); | |
420 | } |