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
);
40 struct bt_ctf_clock
*bt_ctf_clock_create_empty(void)
42 struct bt_ctf_clock
*clock
= g_new0(
43 struct bt_ctf_clock
, 1);
50 clock
->frequency
= 1000000000;
51 bt_object_init(clock
, bt_ctf_clock_destroy
);
57 bool bt_ctf_clock_is_valid(struct bt_ctf_clock
*clock
)
59 return clock
&& clock
->name
;
62 int bt_ctf_clock_set_name(struct bt_ctf_clock
*clock
,
67 if (!clock
|| clock
->frozen
) {
72 if (bt_ctf_validate_identifier(name
)) {
78 g_string_assign(clock
->name
, name
);
80 clock
->name
= g_string_new(name
);
91 struct bt_ctf_clock
*bt_ctf_clock_create(const char *name
)
94 struct bt_ctf_clock
*clock
= NULL
;
96 clock
= bt_ctf_clock_create_empty();
101 ret
= bt_ctf_clock_set_name(clock
, name
);
106 ret
= bt_uuid_generate(clock
->uuid
);
112 * For backward compatibility reasons, a fresh clock can have
113 * a value because it could be added to a trace created by a
114 * CTF writer. As soon as this clock is added to a non-writer
115 * trace, then its value/time functions will be disabled.
117 clock
->has_value
= 1;
125 const char *bt_ctf_clock_get_name(struct bt_ctf_clock
*clock
)
127 const char *ret
= NULL
;
134 ret
= clock
->name
->str
;
141 const char *bt_ctf_clock_get_description(struct bt_ctf_clock
*clock
)
143 const char *ret
= NULL
;
149 if (clock
->description
) {
150 ret
= clock
->description
->str
;
156 int bt_ctf_clock_set_description(struct bt_ctf_clock
*clock
, const char *desc
)
160 if (!clock
|| !desc
|| clock
->frozen
) {
165 clock
->description
= g_string_new(desc
);
166 ret
= clock
->description
? 0 : -1;
171 uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock
*clock
)
173 uint64_t ret
= -1ULL;
179 ret
= clock
->frequency
;
184 int bt_ctf_clock_set_frequency(struct bt_ctf_clock
*clock
, uint64_t freq
)
188 if (!clock
|| clock
->frozen
) {
193 clock
->frequency
= freq
;
198 uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock
*clock
)
200 uint64_t ret
= -1ULL;
206 ret
= clock
->precision
;
211 int bt_ctf_clock_set_precision(struct bt_ctf_clock
*clock
, uint64_t precision
)
215 if (!clock
|| clock
->frozen
) {
220 clock
->precision
= precision
;
225 int bt_ctf_clock_get_offset_s(struct bt_ctf_clock
*clock
, int64_t *offset_s
)
229 if (!clock
|| !offset_s
) {
234 *offset_s
= clock
->offset_s
;
239 int bt_ctf_clock_set_offset_s(struct bt_ctf_clock
*clock
, int64_t offset_s
)
243 if (!clock
|| clock
->frozen
) {
248 clock
->offset_s
= offset_s
;
253 int bt_ctf_clock_get_offset(struct bt_ctf_clock
*clock
, int64_t *offset
)
257 if (!clock
|| !offset
) {
262 *offset
= clock
->offset
;
267 int bt_ctf_clock_set_offset(struct bt_ctf_clock
*clock
, int64_t offset
)
271 if (!clock
|| clock
->frozen
) {
276 clock
->offset
= offset
;
281 int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock
*clock
)
289 ret
= clock
->absolute
;
294 int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock
*clock
, int is_absolute
)
298 if (!clock
|| clock
->frozen
) {
303 clock
->absolute
= !!is_absolute
;
308 const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock
*clock
)
310 const unsigned char *ret
;
312 if (!clock
|| !clock
->uuid_set
) {
322 int bt_ctf_clock_set_uuid(struct bt_ctf_clock
*clock
, const unsigned char *uuid
)
326 if (!clock
|| !uuid
|| clock
->frozen
) {
331 memcpy(clock
->uuid
, uuid
, sizeof(uuid_t
));
337 uint64_t ns_from_value(uint64_t frequency
, uint64_t value
)
341 if (frequency
== 1000000000) {
344 ns
= (uint64_t) ((1e9
* (double) value
) / (double) frequency
);
350 int bt_ctf_clock_get_time(struct bt_ctf_clock
*clock
, int64_t *time
)
354 if (!clock
|| !time
) {
360 if (!clock
->has_value
) {
362 * Clock belongs to a non-writer mode trace and thus
363 * this function is disabled.
368 *time
= (int64_t) ns_from_value(clock
->frequency
, clock
->value
);
374 int bt_ctf_clock_set_time(struct bt_ctf_clock
*clock
, int64_t time
)
379 /* Timestamps are strictly monotonic */
386 if (!clock
->has_value
) {
388 * Clock belongs to a non-writer mode trace and thus
389 * this function is disabled.
395 /* Common case where cycles are actually nanoseconds */
396 if (clock
->frequency
== 1000000000) {
399 value
= (uint64_t) (((double) time
*
400 (double) clock
->frequency
) / 1e9
);
403 ret
= bt_ctf_clock_set_value(clock
, value
);
408 uint64_t bt_ctf_clock_get_value(struct bt_ctf_clock
*clock
)
410 uint64_t ret
= -1ULL;
416 if (!clock
->has_value
) {
418 * Clock belongs to a non-writer mode trace and thus
419 * this function is disabled.
429 int bt_ctf_clock_set_value(struct bt_ctf_clock
*clock
, uint64_t value
)
438 if (!clock
->has_value
) {
440 * Clock belongs to a non-writer mode trace and thus
441 * this function is disabled.
447 /* Timestamps are strictly monotonic */
448 if (value
< clock
->value
) {
453 clock
->value
= value
;
458 void bt_ctf_clock_get(struct bt_ctf_clock
*clock
)
463 void bt_ctf_clock_put(struct bt_ctf_clock
*clock
)
469 void bt_ctf_clock_freeze(struct bt_ctf_clock
*clock
)
479 void bt_ctf_clock_serialize(struct bt_ctf_clock
*clock
,
480 struct metadata_context
*context
)
484 if (!clock
|| !context
) {
489 g_string_append(context
->string
, "clock {\n");
490 g_string_append_printf(context
->string
, "\tname = %s;\n",
492 g_string_append_printf(context
->string
,
493 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
494 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
495 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
496 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
497 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
498 if (clock
->description
) {
499 g_string_append_printf(context
->string
, "\tdescription = \"%s\";\n",
500 clock
->description
->str
);
503 g_string_append_printf(context
->string
, "\tfreq = %" PRIu64
";\n",
505 g_string_append_printf(context
->string
, "\tprecision = %" PRIu64
";\n",
507 g_string_append_printf(context
->string
, "\toffset_s = %" PRIu64
";\n",
509 g_string_append_printf(context
->string
, "\toffset = %" PRIu64
";\n",
511 g_string_append_printf(context
->string
, "\tabsolute = %s;\n",
512 clock
->absolute
? "TRUE" : "FALSE");
513 g_string_append(context
->string
, "};\n\n");
517 void bt_ctf_clock_destroy(struct bt_object
*obj
)
519 struct bt_ctf_clock
*clock
;
521 clock
= container_of(obj
, struct bt_ctf_clock
, base
);
523 g_string_free(clock
->name
, TRUE
);
526 if (clock
->description
) {
527 g_string_free(clock
->description
, TRUE
);
533 int64_t bt_ctf_clock_ns_from_value(struct bt_ctf_clock
*clock
, uint64_t value
)
541 /* Initialize nanosecond timestamp to clock's offset in seconds */
542 ns
= clock
->offset_s
* 1000000000;
544 /* Add offset in cycles, converted to nanoseconds */
545 ns
+= ns_from_value(clock
->frequency
, clock
->offset
);
547 /* Add given value, converter to nanoseconds */
548 ns
+= ns_from_value(clock
->frequency
, value
);
This page took 0.041614 seconds and 4 git commands to generate.