lib: rename include dir to babeltrace2
[babeltrace.git] / lib / ctf-writer / clock.c
1 /*
2 * clock.c
3 *
4 * Babeltrace CTF writer - Clock
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
30 #define BT_LOG_TAG "CTF-WRITER-CLOCK"
31 #include <babeltrace2/lib-logging-internal.h>
32
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>
42 #include <inttypes.h>
43
44 static
45 void bt_ctf_clock_destroy(struct bt_ctf_object *obj);
46
47 struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
48 {
49 int ret;
50 struct bt_ctf_clock *clock = NULL;
51 unsigned char cc_uuid[BABELTRACE_UUID_LEN];
52
53 BT_ASSERT_PRE_NON_NULL(name, "Name");
54 clock = g_new0(struct bt_ctf_clock, 1);
55 if (!clock) {
56 goto error;
57 }
58
59 bt_ctf_object_init_shared(&clock->base, bt_ctf_clock_destroy);
60 clock->value = 0;
61
62 /* Pre-2.0.0 backward compatibility: default frequency is 1 GHz */
63 clock->clock_class = (void *) bt_ctf_clock_class_create(name, 1000000000);
64 if (!clock->clock_class) {
65 goto error;
66 }
67
68 /* Automatically set clock class's UUID. */
69 ret = bt_uuid_generate(cc_uuid);
70 if (ret) {
71 goto error;
72 }
73
74 ret = bt_ctf_clock_class_set_uuid(clock->clock_class, cc_uuid);
75 BT_ASSERT(ret == 0);
76 return clock;
77
78 error:
79 BT_CTF_OBJECT_PUT_REF_AND_RESET(clock);
80 return clock;
81 }
82
83 const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock)
84 {
85 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
86 return bt_ctf_clock_class_get_name(clock->clock_class);
87 }
88
89 const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock)
90 {
91 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
92 return bt_ctf_clock_class_get_description(clock->clock_class);
93 }
94
95 int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
96 {
97 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
98 return bt_ctf_clock_class_set_description(clock->clock_class,
99 desc);
100 }
101
102 uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock)
103 {
104 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
105 return bt_ctf_clock_class_get_frequency(clock->clock_class);
106 }
107
108 int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
109 {
110 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
111 return bt_ctf_clock_class_set_frequency(clock->clock_class,
112 freq);
113 }
114
115 uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock)
116 {
117 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
118 return bt_ctf_clock_class_get_precision(clock->clock_class);
119 }
120
121 int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
122 {
123 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
124 return bt_ctf_clock_class_set_precision(clock->clock_class,
125 precision);
126 }
127
128 int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s)
129 {
130 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
131 return bt_ctf_clock_class_get_offset_s(clock->clock_class,
132 offset_s);
133 }
134
135 int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s)
136 {
137 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
138 return bt_ctf_clock_class_set_offset_s(clock->clock_class,
139 offset_s);
140 }
141
142 int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset)
143 {
144 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
145 return bt_ctf_clock_class_get_offset_cycles(clock->clock_class,
146 offset);
147 }
148
149 int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset)
150 {
151 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
152 return bt_ctf_clock_class_set_offset_cycles(clock->clock_class,
153 offset);
154 }
155
156 int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock)
157 {
158 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
159 return bt_ctf_clock_class_is_absolute(clock->clock_class);
160 }
161
162 int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
163 {
164 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
165 return bt_ctf_clock_class_set_is_absolute(clock->clock_class,
166 is_absolute);
167 }
168
169 const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock)
170 {
171 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
172 return bt_ctf_clock_class_get_uuid(clock->clock_class);
173 }
174
175 int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid)
176 {
177 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
178 return bt_ctf_clock_class_set_uuid(clock->clock_class, uuid);
179 }
180
181 int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
182 {
183 int64_t value;
184 struct bt_ctf_clock_class *cc;
185
186 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
187 cc = clock->clock_class;
188
189 /* Common case where cycles are actually nanoseconds */
190 if (cc->frequency == 1000000000) {
191 value = time;
192 } else {
193 value = (uint64_t) (((double) time *
194 (double) cc->frequency) / 1e9);
195 }
196
197 BT_ASSERT_PRE(clock->value <= value,
198 "CTF writer clock value must be updated monotonically: "
199 "prev-value=%" PRId64 ", new-value=%" PRId64,
200 clock->value, value);
201 clock->value = value;
202 return 0;
203 }
204
205 BT_HIDDEN
206 int bt_ctf_clock_get_value(struct bt_ctf_clock *clock, uint64_t *value)
207 {
208 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
209 BT_ASSERT_PRE_NON_NULL(value, "Value");
210 *value = clock->value;
211 return 0;
212 }
213
214 static
215 void bt_ctf_clock_destroy(struct bt_ctf_object *obj)
216 {
217 struct bt_ctf_clock *clock;
218
219 clock = container_of(obj, struct bt_ctf_clock, base);
220 bt_ctf_object_put_ref(clock->clock_class);
221 g_free(clock);
222 }
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,
232 bt_ctf_clock_class_get_name(clock_class),
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,
239 bt_ctf_clock_class_get_name(clock_class),
240 context);
241 return;
242 }
243
244 uuid = clock_class->uuid;
245 g_string_append(context->string, "clock {\n");
246 g_string_append_printf(context->string, "\tname = %s;\n",
247 clock_class->name->str);
248
249 if (clock_class->uuid_set) {
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
258 if (clock_class->description) {
259 g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
260 clock_class->description->str);
261 }
262
263 g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n",
264 clock_class->frequency);
265 g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n",
266 clock_class->precision);
267 g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n",
268 clock_class->offset_s);
269 g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
270 clock_class->offset);
271 g_string_append_printf(context->string, "\tabsolute = %s;\n",
272 clock_class->absolute ? "true" : "false");
273 g_string_append(context->string, "};\n\n");
274 }
This page took 0.03514 seconds and 4 git commands to generate.