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 struct bt_ctf_clock
*_bt_ctf_clock_create(void)
43 struct bt_ctf_clock
*clock
= g_new0(
44 struct bt_ctf_clock
, 1);
51 clock
->frequency
= 1000000000;
52 bt_object_init(clock
, bt_ctf_clock_destroy
);
58 int bt_ctf_clock_set_name(struct bt_ctf_clock
*clock
,
63 if (bt_ctf_validate_identifier(name
)) {
69 g_string_assign(clock
->name
, name
);
71 clock
->name
= g_string_new(name
);
82 struct bt_ctf_clock
*bt_ctf_clock_create(const char *name
)
85 struct bt_ctf_clock
*clock
= NULL
;
87 clock
= _bt_ctf_clock_create();
92 ret
= bt_ctf_clock_set_name(clock
, name
);
97 ret
= bt_uuid_generate(clock
->uuid
);
103 * For backward compatibility reasons, a fresh clock can have
104 * a value because it could be added to a trace created by a
105 * CTF writer. As soon as this clock is added to a non-writer
106 * trace, then its value/time functions will be disabled.
108 clock
->has_value
= 1;
116 const char *bt_ctf_clock_get_name(struct bt_ctf_clock
*clock
)
118 const char *ret
= NULL
;
125 ret
= clock
->name
->str
;
132 const char *bt_ctf_clock_get_description(struct bt_ctf_clock
*clock
)
134 const char *ret
= NULL
;
140 if (clock
->description
) {
141 ret
= clock
->description
->str
;
147 int bt_ctf_clock_set_description(struct bt_ctf_clock
*clock
, const char *desc
)
151 if (!clock
|| !desc
|| clock
->frozen
) {
156 clock
->description
= g_string_new(desc
);
157 ret
= clock
->description
? 0 : -1;
162 uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock
*clock
)
164 uint64_t ret
= -1ULL;
170 ret
= clock
->frequency
;
175 int bt_ctf_clock_set_frequency(struct bt_ctf_clock
*clock
, uint64_t freq
)
179 if (!clock
|| clock
->frozen
) {
184 clock
->frequency
= freq
;
189 uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock
*clock
)
191 uint64_t ret
= -1ULL;
197 ret
= clock
->precision
;
202 int bt_ctf_clock_set_precision(struct bt_ctf_clock
*clock
, uint64_t precision
)
206 if (!clock
|| clock
->frozen
) {
211 clock
->precision
= precision
;
216 int bt_ctf_clock_get_offset_s(struct bt_ctf_clock
*clock
, int64_t *offset_s
)
220 if (!clock
|| !offset_s
) {
225 *offset_s
= clock
->offset_s
;
230 int bt_ctf_clock_set_offset_s(struct bt_ctf_clock
*clock
, int64_t offset_s
)
234 if (!clock
|| clock
->frozen
) {
239 clock
->offset_s
= offset_s
;
244 int bt_ctf_clock_get_offset(struct bt_ctf_clock
*clock
, int64_t *offset
)
248 if (!clock
|| !offset
) {
253 *offset
= clock
->offset
;
258 int bt_ctf_clock_set_offset(struct bt_ctf_clock
*clock
, int64_t offset
)
262 if (!clock
|| clock
->frozen
) {
267 clock
->offset
= offset
;
272 int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock
*clock
)
280 ret
= clock
->absolute
;
285 int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock
*clock
, int is_absolute
)
289 if (!clock
|| clock
->frozen
) {
294 clock
->absolute
= !!is_absolute
;
299 const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock
*clock
)
301 const unsigned char *ret
;
303 if (!clock
|| !clock
->uuid_set
) {
313 int bt_ctf_clock_set_uuid(struct bt_ctf_clock
*clock
, const unsigned char *uuid
)
317 if (!clock
|| !uuid
|| clock
->frozen
) {
322 memcpy(clock
->uuid
, uuid
, sizeof(uuid_t
));
328 int bt_ctf_clock_get_time(struct bt_ctf_clock
*clock
, int64_t *time
)
332 if (!clock
|| !time
) {
338 if (!clock
->has_value
) {
340 * Clock belongs to a non-writer mode trace and thus
341 * this function is disabled.
346 /* Common case where cycles are actually nanoseconds */
347 if (clock
->frequency
== 1000000000) {
348 *time
= (int64_t) clock
->value
;
350 *time
= (int64_t) ((1e9
* (double) clock
->value
) /
351 (double) clock
->frequency
);
358 int bt_ctf_clock_set_time(struct bt_ctf_clock
*clock
, int64_t time
)
362 /* Timestamps are strictly monotonic */
369 if (!clock
->has_value
) {
371 * Clock belongs to a non-writer mode trace and thus
372 * this function is disabled.
378 /* Common case where cycles are actually nanoseconds */
379 if (clock
->frequency
== 1000000000) {
384 ret
= bt_ctf_clock_set_value(clock
,
385 (uint64_t) (((double) time
* (double) clock
->frequency
) / 1e9
));
391 uint64_t bt_ctf_clock_get_value(struct bt_ctf_clock
*clock
)
393 uint64_t ret
= -1ULL;
399 if (!clock
->has_value
) {
401 * Clock belongs to a non-writer mode trace and thus
402 * this function is disabled.
412 int bt_ctf_clock_set_value(struct bt_ctf_clock
*clock
, uint64_t value
)
421 if (!clock
->has_value
) {
423 * Clock belongs to a non-writer mode trace and thus
424 * this function is disabled.
430 /* Timestamps are strictly monotonic */
431 if (value
< clock
->value
) {
436 clock
->value
= value
;
441 void bt_ctf_clock_get(struct bt_ctf_clock
*clock
)
446 void bt_ctf_clock_put(struct bt_ctf_clock
*clock
)
452 void bt_ctf_clock_freeze(struct bt_ctf_clock
*clock
)
462 void bt_ctf_clock_serialize(struct bt_ctf_clock
*clock
,
463 struct metadata_context
*context
)
467 if (!clock
|| !context
) {
472 g_string_append(context
->string
, "clock {\n");
473 g_string_append_printf(context
->string
, "\tname = %s;\n",
475 g_string_append_printf(context
->string
,
476 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
477 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
478 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
479 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
480 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
481 if (clock
->description
) {
482 g_string_append_printf(context
->string
, "\tdescription = \"%s\";\n",
483 clock
->description
->str
);
486 g_string_append_printf(context
->string
, "\tfreq = %" PRIu64
";\n",
488 g_string_append_printf(context
->string
, "\tprecision = %" PRIu64
";\n",
490 g_string_append_printf(context
->string
, "\toffset_s = %" PRIu64
";\n",
492 g_string_append_printf(context
->string
, "\toffset = %" PRIu64
";\n",
494 g_string_append_printf(context
->string
, "\tabsolute = %s;\n",
495 clock
->absolute
? "TRUE" : "FALSE");
496 g_string_append(context
->string
, "};\n\n");
500 void bt_ctf_clock_destroy(struct bt_object
*obj
)
502 struct bt_ctf_clock
*clock
;
504 clock
= container_of(obj
, struct bt_ctf_clock
, base
);
506 g_string_free(clock
->name
, TRUE
);
509 if (clock
->description
) {
510 g_string_free(clock
->description
, TRUE
);
This page took 0.057023 seconds and 5 git commands to generate.