Commit | Line | Data |
---|---|---|
ac0c6bdd | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
ac0c6bdd PP |
3 | * |
4 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
6 | * | |
0235b0db | 7 | * Babeltrace CTF writer - Clock |
ac0c6bdd PP |
8 | */ |
9 | ||
350ad6c1 | 10 | #define BT_LOG_TAG "CTF-WRITER/CLOCK" |
67d2ce02 | 11 | #include "logging.h" |
f6ccaed9 | 12 | |
578e048b | 13 | #include "common/assert.h" |
6162e6b7 | 14 | #include "common/uuid.h" |
578e048b | 15 | #include "compat/compiler.h" |
217cf9d3 PP |
16 | #include <babeltrace2-ctf-writer/utils.h> |
17 | #include <babeltrace2-ctf-writer/object.h> | |
ac0c6bdd PP |
18 | #include <inttypes.h> |
19 | ||
578e048b MJ |
20 | #include "clock-class.h" |
21 | #include "clock.h" | |
22 | #include "object.h" | |
23 | #include "writer.h" | |
24 | ||
ac0c6bdd | 25 | static |
e1e02a22 | 26 | void bt_ctf_clock_destroy(struct bt_ctf_object *obj); |
ac0c6bdd PP |
27 | |
28 | struct bt_ctf_clock *bt_ctf_clock_create(const char *name) | |
29 | { | |
75c3fca1 | 30 | int ret; |
ac0c6bdd | 31 | struct bt_ctf_clock *clock = NULL; |
6162e6b7 | 32 | bt_uuid_t cc_uuid; |
ac0c6bdd | 33 | |
67d2ce02 | 34 | BT_CTF_ASSERT_PRE_NON_NULL(name, "Name"); |
ac0c6bdd | 35 | clock = g_new0(struct bt_ctf_clock, 1); |
ac0c6bdd PP |
36 | if (!clock) { |
37 | goto error; | |
38 | } | |
39 | ||
e1e02a22 | 40 | bt_ctf_object_init_shared(&clock->base, bt_ctf_clock_destroy); |
ac0c6bdd | 41 | clock->value = 0; |
f3534905 PP |
42 | |
43 | /* Pre-2.0.0 backward compatibility: default frequency is 1 GHz */ | |
16ca5ff0 | 44 | clock->clock_class = (void *) bt_ctf_clock_class_create(name, 1000000000); |
ac0c6bdd PP |
45 | if (!clock->clock_class) { |
46 | goto error; | |
47 | } | |
75c3fca1 PP |
48 | |
49 | /* Automatically set clock class's UUID. */ | |
6162e6b7 | 50 | bt_uuid_generate(cc_uuid); |
75c3fca1 | 51 | |
16ca5ff0 | 52 | ret = bt_ctf_clock_class_set_uuid(clock->clock_class, cc_uuid); |
98b15851 | 53 | BT_ASSERT_DBG(ret == 0); |
ac0c6bdd PP |
54 | return clock; |
55 | ||
56 | error: | |
e1e02a22 | 57 | BT_CTF_OBJECT_PUT_REF_AND_RESET(clock); |
ac0c6bdd PP |
58 | return clock; |
59 | } | |
60 | ||
61 | const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock) | |
62 | { | |
67d2ce02 | 63 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 64 | return bt_ctf_clock_class_get_name(clock->clock_class); |
ac0c6bdd PP |
65 | } |
66 | ||
67 | const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock) | |
68 | { | |
67d2ce02 | 69 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 70 | return bt_ctf_clock_class_get_description(clock->clock_class); |
ac0c6bdd PP |
71 | } |
72 | ||
73 | int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc) | |
74 | { | |
67d2ce02 | 75 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 76 | return bt_ctf_clock_class_set_description(clock->clock_class, |
3dca2276 | 77 | desc); |
ac0c6bdd PP |
78 | } |
79 | ||
80 | uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock) | |
81 | { | |
67d2ce02 | 82 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 83 | return bt_ctf_clock_class_get_frequency(clock->clock_class); |
ac0c6bdd PP |
84 | } |
85 | ||
86 | int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq) | |
87 | { | |
67d2ce02 | 88 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 89 | return bt_ctf_clock_class_set_frequency(clock->clock_class, |
3dca2276 | 90 | freq); |
ac0c6bdd PP |
91 | } |
92 | ||
93 | uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock) | |
94 | { | |
67d2ce02 | 95 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 96 | return bt_ctf_clock_class_get_precision(clock->clock_class); |
ac0c6bdd PP |
97 | } |
98 | ||
99 | int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision) | |
100 | { | |
67d2ce02 | 101 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 102 | return bt_ctf_clock_class_set_precision(clock->clock_class, |
3dca2276 | 103 | precision); |
ac0c6bdd PP |
104 | } |
105 | ||
106 | int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s) | |
107 | { | |
67d2ce02 | 108 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 109 | return bt_ctf_clock_class_get_offset_s(clock->clock_class, |
3dca2276 | 110 | offset_s); |
ac0c6bdd PP |
111 | } |
112 | ||
113 | int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s) | |
114 | { | |
67d2ce02 | 115 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 116 | return bt_ctf_clock_class_set_offset_s(clock->clock_class, |
3dca2276 | 117 | offset_s); |
ac0c6bdd PP |
118 | } |
119 | ||
120 | int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset) | |
121 | { | |
67d2ce02 | 122 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 123 | return bt_ctf_clock_class_get_offset_cycles(clock->clock_class, |
3dca2276 | 124 | offset); |
ac0c6bdd PP |
125 | } |
126 | ||
127 | int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset) | |
128 | { | |
67d2ce02 | 129 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 130 | return bt_ctf_clock_class_set_offset_cycles(clock->clock_class, |
3dca2276 | 131 | offset); |
ac0c6bdd PP |
132 | } |
133 | ||
134 | int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock) | |
135 | { | |
67d2ce02 | 136 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 137 | return bt_ctf_clock_class_is_absolute(clock->clock_class); |
ac0c6bdd PP |
138 | } |
139 | ||
140 | int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute) | |
141 | { | |
67d2ce02 | 142 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 143 | return bt_ctf_clock_class_set_is_absolute(clock->clock_class, |
3dca2276 | 144 | is_absolute); |
ac0c6bdd PP |
145 | } |
146 | ||
6162e6b7 | 147 | const uint8_t *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock) |
ac0c6bdd | 148 | { |
67d2ce02 | 149 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 150 | return bt_ctf_clock_class_get_uuid(clock->clock_class); |
ac0c6bdd PP |
151 | } |
152 | ||
6162e6b7 | 153 | int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const uint8_t *uuid) |
ac0c6bdd | 154 | { |
67d2ce02 | 155 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 156 | return bt_ctf_clock_class_set_uuid(clock->clock_class, uuid); |
ac0c6bdd PP |
157 | } |
158 | ||
159 | int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time) | |
160 | { | |
ac0c6bdd | 161 | int64_t value; |
16ca5ff0 | 162 | struct bt_ctf_clock_class *cc; |
ac0c6bdd | 163 | |
67d2ce02 | 164 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 165 | cc = clock->clock_class; |
ac0c6bdd PP |
166 | |
167 | /* Common case where cycles are actually nanoseconds */ | |
3dca2276 | 168 | if (cc->frequency == 1000000000) { |
ac0c6bdd PP |
169 | value = time; |
170 | } else { | |
171 | value = (uint64_t) (((double) time * | |
3dca2276 | 172 | (double) cc->frequency) / 1e9); |
ac0c6bdd PP |
173 | } |
174 | ||
67d2ce02 | 175 | BT_CTF_ASSERT_PRE(clock->value <= value, |
f6ccaed9 PP |
176 | "CTF writer clock value must be updated monotonically: " |
177 | "prev-value=%" PRId64 ", new-value=%" PRId64, | |
178 | clock->value, value); | |
ac0c6bdd | 179 | clock->value = value; |
f6ccaed9 | 180 | return 0; |
ac0c6bdd PP |
181 | } |
182 | ||
ac0c6bdd PP |
183 | BT_HIDDEN |
184 | int bt_ctf_clock_get_value(struct bt_ctf_clock *clock, uint64_t *value) | |
185 | { | |
67d2ce02 MJ |
186 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
187 | BT_CTF_ASSERT_PRE_NON_NULL(value, "Value"); | |
ac0c6bdd | 188 | *value = clock->value; |
f6ccaed9 | 189 | return 0; |
ac0c6bdd PP |
190 | } |
191 | ||
192 | static | |
e1e02a22 | 193 | void bt_ctf_clock_destroy(struct bt_ctf_object *obj) |
ac0c6bdd PP |
194 | { |
195 | struct bt_ctf_clock *clock; | |
196 | ||
197 | clock = container_of(obj, struct bt_ctf_clock, base); | |
e1e02a22 | 198 | bt_ctf_object_put_ref(clock->clock_class); |
ac0c6bdd PP |
199 | g_free(clock); |
200 | } | |
3dca2276 PP |
201 | |
202 | BT_HIDDEN | |
203 | void bt_ctf_clock_class_serialize(struct bt_ctf_clock_class *clock_class, | |
204 | struct metadata_context *context) | |
205 | { | |
6162e6b7 | 206 | uint8_t *uuid; |
3dca2276 PP |
207 | |
208 | BT_LOGD("Serializing clock class's metadata: clock-class-addr=%p, " | |
209 | "name=\"%s\", metadata-context-addr=%p", clock_class, | |
16ca5ff0 | 210 | bt_ctf_clock_class_get_name(clock_class), |
3dca2276 PP |
211 | context); |
212 | ||
213 | if (!clock_class || !context) { | |
214 | BT_LOGW("Invalid parameter: clock class or metadata context is NULL: " | |
215 | "clock-class-addr=%p, name=\"%s\", metadata-context-addr=%p", | |
216 | clock_class, | |
16ca5ff0 | 217 | bt_ctf_clock_class_get_name(clock_class), |
3dca2276 PP |
218 | context); |
219 | return; | |
220 | } | |
221 | ||
16ca5ff0 | 222 | uuid = clock_class->uuid; |
3dca2276 PP |
223 | g_string_append(context->string, "clock {\n"); |
224 | g_string_append_printf(context->string, "\tname = %s;\n", | |
16ca5ff0 | 225 | clock_class->name->str); |
3dca2276 | 226 | |
16ca5ff0 | 227 | if (clock_class->uuid_set) { |
3dca2276 | 228 | g_string_append_printf(context->string, |
6162e6b7 MJ |
229 | "\tuuid = \"" BT_UUID_FMT "\";\n", |
230 | BT_UUID_FMT_VALUES(uuid)); | |
3dca2276 PP |
231 | } |
232 | ||
16ca5ff0 | 233 | if (clock_class->description) { |
3dca2276 | 234 | g_string_append_printf(context->string, "\tdescription = \"%s\";\n", |
16ca5ff0 | 235 | clock_class->description->str); |
3dca2276 PP |
236 | } |
237 | ||
238 | g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n", | |
16ca5ff0 | 239 | clock_class->frequency); |
3dca2276 | 240 | g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n", |
16ca5ff0 | 241 | clock_class->precision); |
3dca2276 | 242 | g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n", |
16ca5ff0 | 243 | clock_class->offset_s); |
3dca2276 | 244 | g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n", |
16ca5ff0 | 245 | clock_class->offset); |
3dca2276 | 246 | g_string_append_printf(context->string, "\tabsolute = %s;\n", |
16ca5ff0 | 247 | clock_class->absolute ? "true" : "false"); |
3dca2276 PP |
248 | g_string_append(context->string, "};\n\n"); |
249 | } |