4 * Babeltrace CTF IR - Clock
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/ctf-ir/clock-internal.h>
30 #include <babeltrace/ctf-ir/utils.h>
31 #include <babeltrace/ref.h>
32 #include <babeltrace/ctf-writer/writer-internal.h>
33 #include <babeltrace/object-internal.h>
34 #include <babeltrace/compiler.h>
38 void bt_ctf_clock_destroy(struct bt_object
*obj
);
41 bool bt_ctf_clock_is_valid(struct bt_ctf_clock
*clock
)
43 return clock
&& clock
->name
;
46 int bt_ctf_clock_set_name(struct bt_ctf_clock
*clock
,
51 if (!clock
|| clock
->frozen
) {
56 if (bt_ctf_validate_identifier(name
)) {
62 g_string_assign(clock
->name
, name
);
64 clock
->name
= g_string_new(name
);
75 struct bt_ctf_clock
*bt_ctf_clock_create(const char *name
)
78 struct bt_ctf_clock
*clock
= g_new0(struct bt_ctf_clock
, 1);
85 clock
->frequency
= 1000000000;
86 bt_object_init(clock
, bt_ctf_clock_destroy
);
89 ret
= bt_ctf_clock_set_name(clock
, name
);
95 ret
= bt_uuid_generate(clock
->uuid
);
101 * For backward compatibility reasons, a fresh clock can have
102 * a value because it could be added to a trace created by a
103 * CTF writer. As soon as this clock is added to a non-writer
104 * trace, then its value/time functions will be disabled.
106 clock
->has_value
= 1;
114 const char *bt_ctf_clock_get_name(struct bt_ctf_clock
*clock
)
116 const char *ret
= NULL
;
123 ret
= clock
->name
->str
;
130 const char *bt_ctf_clock_get_description(struct bt_ctf_clock
*clock
)
132 const char *ret
= NULL
;
138 if (clock
->description
) {
139 ret
= clock
->description
->str
;
145 int bt_ctf_clock_set_description(struct bt_ctf_clock
*clock
, const char *desc
)
149 if (!clock
|| !desc
|| clock
->frozen
) {
154 clock
->description
= g_string_new(desc
);
155 ret
= clock
->description
? 0 : -1;
160 uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock
*clock
)
162 uint64_t ret
= -1ULL;
168 ret
= clock
->frequency
;
173 int bt_ctf_clock_set_frequency(struct bt_ctf_clock
*clock
, uint64_t freq
)
177 if (!clock
|| clock
->frozen
) {
182 clock
->frequency
= freq
;
187 uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock
*clock
)
189 uint64_t ret
= -1ULL;
195 ret
= clock
->precision
;
200 int bt_ctf_clock_set_precision(struct bt_ctf_clock
*clock
, uint64_t precision
)
204 if (!clock
|| clock
->frozen
) {
209 clock
->precision
= precision
;
214 int bt_ctf_clock_get_offset_s(struct bt_ctf_clock
*clock
, int64_t *offset_s
)
218 if (!clock
|| !offset_s
) {
223 *offset_s
= clock
->offset_s
;
228 int bt_ctf_clock_set_offset_s(struct bt_ctf_clock
*clock
, int64_t offset_s
)
232 if (!clock
|| clock
->frozen
) {
237 clock
->offset_s
= offset_s
;
242 int bt_ctf_clock_get_offset(struct bt_ctf_clock
*clock
, int64_t *offset
)
246 if (!clock
|| !offset
) {
251 *offset
= clock
->offset
;
256 int bt_ctf_clock_set_offset(struct bt_ctf_clock
*clock
, int64_t offset
)
260 if (!clock
|| clock
->frozen
) {
265 clock
->offset
= offset
;
270 int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock
*clock
)
278 ret
= clock
->absolute
;
283 int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock
*clock
, int is_absolute
)
287 if (!clock
|| clock
->frozen
) {
292 clock
->absolute
= !!is_absolute
;
297 const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock
*clock
)
299 const unsigned char *ret
;
301 if (!clock
|| !clock
->uuid_set
) {
311 int bt_ctf_clock_set_uuid(struct bt_ctf_clock
*clock
, const unsigned char *uuid
)
315 if (!clock
|| !uuid
|| clock
->frozen
) {
320 memcpy(clock
->uuid
, uuid
, sizeof(uuid_t
));
326 uint64_t ns_from_value(uint64_t frequency
, uint64_t value
)
330 if (frequency
== 1000000000) {
333 ns
= (uint64_t) ((1e9
* (double) value
) / (double) frequency
);
339 int bt_ctf_clock_get_time(struct bt_ctf_clock
*clock
, int64_t *time
)
343 if (!clock
|| !time
) {
349 if (!clock
->has_value
) {
351 * Clock belongs to a non-writer mode trace and thus
352 * this function is disabled.
357 *time
= (int64_t) ns_from_value(clock
->frequency
, clock
->value
);
363 int bt_ctf_clock_set_time(struct bt_ctf_clock
*clock
, int64_t time
)
368 /* Timestamps are strictly monotonic */
375 if (!clock
->has_value
) {
377 * Clock belongs to a non-writer mode trace and thus
378 * this function is disabled.
384 /* Common case where cycles are actually nanoseconds */
385 if (clock
->frequency
== 1000000000) {
388 value
= (uint64_t) (((double) time
*
389 (double) clock
->frequency
) / 1e9
);
392 ret
= bt_ctf_clock_set_value(clock
, value
);
397 uint64_t bt_ctf_clock_get_value(struct bt_ctf_clock
*clock
)
399 uint64_t ret
= -1ULL;
405 if (!clock
->has_value
) {
407 * Clock belongs to a non-writer mode trace and thus
408 * this function is disabled.
418 int bt_ctf_clock_set_value(struct bt_ctf_clock
*clock
, uint64_t value
)
427 if (!clock
->has_value
) {
429 * Clock belongs to a non-writer mode trace and thus
430 * this function is disabled.
436 /* Timestamps are strictly monotonic */
437 if (value
< clock
->value
) {
442 clock
->value
= value
;
447 void bt_ctf_clock_get(struct bt_ctf_clock
*clock
)
452 void bt_ctf_clock_put(struct bt_ctf_clock
*clock
)
458 void bt_ctf_clock_freeze(struct bt_ctf_clock
*clock
)
468 void bt_ctf_clock_serialize(struct bt_ctf_clock
*clock
,
469 struct metadata_context
*context
)
473 if (!clock
|| !context
) {
478 g_string_append(context
->string
, "clock {\n");
479 g_string_append_printf(context
->string
, "\tname = %s;\n",
481 g_string_append_printf(context
->string
,
482 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
483 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
484 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
485 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
486 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
487 if (clock
->description
) {
488 g_string_append_printf(context
->string
, "\tdescription = \"%s\";\n",
489 clock
->description
->str
);
492 g_string_append_printf(context
->string
, "\tfreq = %" PRIu64
";\n",
494 g_string_append_printf(context
->string
, "\tprecision = %" PRIu64
";\n",
496 g_string_append_printf(context
->string
, "\toffset_s = %" PRIu64
";\n",
498 g_string_append_printf(context
->string
, "\toffset = %" PRIu64
";\n",
500 g_string_append_printf(context
->string
, "\tabsolute = %s;\n",
501 clock
->absolute
? "TRUE" : "FALSE");
502 g_string_append(context
->string
, "};\n\n");
506 void bt_ctf_clock_destroy(struct bt_object
*obj
)
508 struct bt_ctf_clock
*clock
;
510 clock
= container_of(obj
, struct bt_ctf_clock
, base
);
512 g_string_free(clock
->name
, TRUE
);
515 if (clock
->description
) {
516 g_string_free(clock
->description
, TRUE
);
522 int64_t bt_ctf_clock_ns_from_value(struct bt_ctf_clock
*clock
, uint64_t value
)
530 /* Initialize nanosecond timestamp to clock's offset in seconds */
531 ns
= clock
->offset_s
* 1000000000;
533 /* Add offset in cycles, converted to nanoseconds */
534 ns
+= ns_from_value(clock
->frequency
, clock
->offset
);
536 /* Add given value, converter to nanoseconds */
537 ns
+= ns_from_value(clock
->frequency
, value
);
This page took 0.052917 seconds and 4 git commands to generate.