Commit | Line | Data |
---|---|---|
ac0c6bdd PP |
1 | /* |
2 | * clock.c | |
3 | * | |
16ca5ff0 | 4 | * Babeltrace CTF writer - Clock |
ac0c6bdd PP |
5 | * |
6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
8 | * | |
9 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
10 | * | |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | * | |
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
27 | * SOFTWARE. | |
28 | */ | |
29 | ||
f6ccaed9 | 30 | #define BT_LOG_TAG "CTF-WRITER-CLOCK" |
67d2ce02 | 31 | #include "logging.h" |
f6ccaed9 | 32 | |
3fadfbc0 MJ |
33 | #include <babeltrace2/assert-internal.h> |
34 | #include <babeltrace2/compat/uuid-internal.h> | |
35 | #include <babeltrace2/compiler-internal.h> | |
36 | #include <babeltrace2/ctf-writer/clock-class-internal.h> | |
37 | #include <babeltrace2/ctf-writer/clock-internal.h> | |
38 | #include <babeltrace2/ctf-writer/utils.h> | |
39 | #include <babeltrace2/ctf-writer/writer-internal.h> | |
40 | #include <babeltrace2/ctf-writer/object-internal.h> | |
41 | #include <babeltrace2/ctf-writer/object.h> | |
ac0c6bdd PP |
42 | #include <inttypes.h> |
43 | ||
44 | static | |
e1e02a22 | 45 | void bt_ctf_clock_destroy(struct bt_ctf_object *obj); |
ac0c6bdd PP |
46 | |
47 | struct bt_ctf_clock *bt_ctf_clock_create(const char *name) | |
48 | { | |
75c3fca1 | 49 | int ret; |
ac0c6bdd | 50 | struct bt_ctf_clock *clock = NULL; |
75c3fca1 | 51 | unsigned char cc_uuid[BABELTRACE_UUID_LEN]; |
ac0c6bdd | 52 | |
67d2ce02 | 53 | BT_CTF_ASSERT_PRE_NON_NULL(name, "Name"); |
ac0c6bdd | 54 | clock = g_new0(struct bt_ctf_clock, 1); |
ac0c6bdd PP |
55 | if (!clock) { |
56 | goto error; | |
57 | } | |
58 | ||
e1e02a22 | 59 | bt_ctf_object_init_shared(&clock->base, bt_ctf_clock_destroy); |
ac0c6bdd | 60 | clock->value = 0; |
f3534905 PP |
61 | |
62 | /* Pre-2.0.0 backward compatibility: default frequency is 1 GHz */ | |
16ca5ff0 | 63 | clock->clock_class = (void *) bt_ctf_clock_class_create(name, 1000000000); |
ac0c6bdd PP |
64 | if (!clock->clock_class) { |
65 | goto error; | |
66 | } | |
75c3fca1 PP |
67 | |
68 | /* Automatically set clock class's UUID. */ | |
69 | ret = bt_uuid_generate(cc_uuid); | |
70 | if (ret) { | |
71 | goto error; | |
72 | } | |
73 | ||
16ca5ff0 | 74 | ret = bt_ctf_clock_class_set_uuid(clock->clock_class, cc_uuid); |
f6ccaed9 | 75 | BT_ASSERT(ret == 0); |
ac0c6bdd PP |
76 | return clock; |
77 | ||
78 | error: | |
e1e02a22 | 79 | BT_CTF_OBJECT_PUT_REF_AND_RESET(clock); |
ac0c6bdd PP |
80 | return clock; |
81 | } | |
82 | ||
83 | const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock) | |
84 | { | |
67d2ce02 | 85 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 86 | return bt_ctf_clock_class_get_name(clock->clock_class); |
ac0c6bdd PP |
87 | } |
88 | ||
89 | const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock) | |
90 | { | |
67d2ce02 | 91 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 92 | return bt_ctf_clock_class_get_description(clock->clock_class); |
ac0c6bdd PP |
93 | } |
94 | ||
95 | int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc) | |
96 | { | |
67d2ce02 | 97 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 98 | return bt_ctf_clock_class_set_description(clock->clock_class, |
3dca2276 | 99 | desc); |
ac0c6bdd PP |
100 | } |
101 | ||
102 | uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock) | |
103 | { | |
67d2ce02 | 104 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 105 | return bt_ctf_clock_class_get_frequency(clock->clock_class); |
ac0c6bdd PP |
106 | } |
107 | ||
108 | int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq) | |
109 | { | |
67d2ce02 | 110 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 111 | return bt_ctf_clock_class_set_frequency(clock->clock_class, |
3dca2276 | 112 | freq); |
ac0c6bdd PP |
113 | } |
114 | ||
115 | uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock) | |
116 | { | |
67d2ce02 | 117 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 118 | return bt_ctf_clock_class_get_precision(clock->clock_class); |
ac0c6bdd PP |
119 | } |
120 | ||
121 | int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision) | |
122 | { | |
67d2ce02 | 123 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 124 | return bt_ctf_clock_class_set_precision(clock->clock_class, |
3dca2276 | 125 | precision); |
ac0c6bdd PP |
126 | } |
127 | ||
128 | int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s) | |
129 | { | |
67d2ce02 | 130 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 131 | return bt_ctf_clock_class_get_offset_s(clock->clock_class, |
3dca2276 | 132 | offset_s); |
ac0c6bdd PP |
133 | } |
134 | ||
135 | int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s) | |
136 | { | |
67d2ce02 | 137 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 138 | return bt_ctf_clock_class_set_offset_s(clock->clock_class, |
3dca2276 | 139 | offset_s); |
ac0c6bdd PP |
140 | } |
141 | ||
142 | int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset) | |
143 | { | |
67d2ce02 | 144 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 145 | return bt_ctf_clock_class_get_offset_cycles(clock->clock_class, |
3dca2276 | 146 | offset); |
ac0c6bdd PP |
147 | } |
148 | ||
149 | int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset) | |
150 | { | |
67d2ce02 | 151 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 152 | return bt_ctf_clock_class_set_offset_cycles(clock->clock_class, |
3dca2276 | 153 | offset); |
ac0c6bdd PP |
154 | } |
155 | ||
156 | int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock) | |
157 | { | |
67d2ce02 | 158 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 159 | return bt_ctf_clock_class_is_absolute(clock->clock_class); |
ac0c6bdd PP |
160 | } |
161 | ||
162 | int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute) | |
163 | { | |
67d2ce02 | 164 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 165 | return bt_ctf_clock_class_set_is_absolute(clock->clock_class, |
3dca2276 | 166 | is_absolute); |
ac0c6bdd PP |
167 | } |
168 | ||
169 | const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock) | |
170 | { | |
67d2ce02 | 171 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 172 | return bt_ctf_clock_class_get_uuid(clock->clock_class); |
ac0c6bdd PP |
173 | } |
174 | ||
175 | int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid) | |
176 | { | |
67d2ce02 | 177 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 178 | return bt_ctf_clock_class_set_uuid(clock->clock_class, uuid); |
ac0c6bdd PP |
179 | } |
180 | ||
181 | int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time) | |
182 | { | |
ac0c6bdd | 183 | int64_t value; |
16ca5ff0 | 184 | struct bt_ctf_clock_class *cc; |
ac0c6bdd | 185 | |
67d2ce02 | 186 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
16ca5ff0 | 187 | cc = clock->clock_class; |
ac0c6bdd PP |
188 | |
189 | /* Common case where cycles are actually nanoseconds */ | |
3dca2276 | 190 | if (cc->frequency == 1000000000) { |
ac0c6bdd PP |
191 | value = time; |
192 | } else { | |
193 | value = (uint64_t) (((double) time * | |
3dca2276 | 194 | (double) cc->frequency) / 1e9); |
ac0c6bdd PP |
195 | } |
196 | ||
67d2ce02 | 197 | BT_CTF_ASSERT_PRE(clock->value <= value, |
f6ccaed9 PP |
198 | "CTF writer clock value must be updated monotonically: " |
199 | "prev-value=%" PRId64 ", new-value=%" PRId64, | |
200 | clock->value, value); | |
ac0c6bdd | 201 | clock->value = value; |
f6ccaed9 | 202 | return 0; |
ac0c6bdd PP |
203 | } |
204 | ||
ac0c6bdd PP |
205 | BT_HIDDEN |
206 | int bt_ctf_clock_get_value(struct bt_ctf_clock *clock, uint64_t *value) | |
207 | { | |
67d2ce02 MJ |
208 | BT_CTF_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
209 | BT_CTF_ASSERT_PRE_NON_NULL(value, "Value"); | |
ac0c6bdd | 210 | *value = clock->value; |
f6ccaed9 | 211 | return 0; |
ac0c6bdd PP |
212 | } |
213 | ||
214 | static | |
e1e02a22 | 215 | void bt_ctf_clock_destroy(struct bt_ctf_object *obj) |
ac0c6bdd PP |
216 | { |
217 | struct bt_ctf_clock *clock; | |
218 | ||
219 | clock = container_of(obj, struct bt_ctf_clock, base); | |
e1e02a22 | 220 | bt_ctf_object_put_ref(clock->clock_class); |
ac0c6bdd PP |
221 | g_free(clock); |
222 | } | |
3dca2276 PP |
223 | |
224 | BT_HIDDEN | |
225 | void bt_ctf_clock_class_serialize(struct bt_ctf_clock_class *clock_class, | |
226 | struct metadata_context *context) | |
227 | { | |
228 | unsigned char *uuid; | |
229 | ||
230 | BT_LOGD("Serializing clock class's metadata: clock-class-addr=%p, " | |
231 | "name=\"%s\", metadata-context-addr=%p", clock_class, | |
16ca5ff0 | 232 | bt_ctf_clock_class_get_name(clock_class), |
3dca2276 PP |
233 | context); |
234 | ||
235 | if (!clock_class || !context) { | |
236 | BT_LOGW("Invalid parameter: clock class or metadata context is NULL: " | |
237 | "clock-class-addr=%p, name=\"%s\", metadata-context-addr=%p", | |
238 | clock_class, | |
16ca5ff0 | 239 | bt_ctf_clock_class_get_name(clock_class), |
3dca2276 PP |
240 | context); |
241 | return; | |
242 | } | |
243 | ||
16ca5ff0 | 244 | uuid = clock_class->uuid; |
3dca2276 PP |
245 | g_string_append(context->string, "clock {\n"); |
246 | g_string_append_printf(context->string, "\tname = %s;\n", | |
16ca5ff0 | 247 | clock_class->name->str); |
3dca2276 | 248 | |
16ca5ff0 | 249 | if (clock_class->uuid_set) { |
3dca2276 PP |
250 | g_string_append_printf(context->string, |
251 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
252 | uuid[0], uuid[1], uuid[2], uuid[3], | |
253 | uuid[4], uuid[5], uuid[6], uuid[7], | |
254 | uuid[8], uuid[9], uuid[10], uuid[11], | |
255 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
256 | } | |
257 | ||
16ca5ff0 | 258 | if (clock_class->description) { |
3dca2276 | 259 | g_string_append_printf(context->string, "\tdescription = \"%s\";\n", |
16ca5ff0 | 260 | clock_class->description->str); |
3dca2276 PP |
261 | } |
262 | ||
263 | g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n", | |
16ca5ff0 | 264 | clock_class->frequency); |
3dca2276 | 265 | g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n", |
16ca5ff0 | 266 | clock_class->precision); |
3dca2276 | 267 | g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n", |
16ca5ff0 | 268 | clock_class->offset_s); |
3dca2276 | 269 | g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n", |
16ca5ff0 | 270 | clock_class->offset); |
3dca2276 | 271 | g_string_append_printf(context->string, "\tabsolute = %s;\n", |
16ca5ff0 | 272 | clock_class->absolute ? "true" : "false"); |
3dca2276 PP |
273 | g_string_append(context->string, "};\n\n"); |
274 | } |