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