04346968f090bb343ac78f1c5ea6aba3480316a8
[babeltrace.git] / lib / ctf-writer / clock.c
1 /*
2 * clock.c
3 *
4 * Babeltrace CTF IR - 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 #include <babeltrace/ctf-writer/clock-internal.h>
31 #include <babeltrace/ctf-ir/clock-class.h>
32 #include <babeltrace/ctf-ir/clock-class-internal.h>
33 #include <babeltrace/ctf-ir/utils.h>
34 #include <babeltrace/compat/uuid-internal.h>
35 #include <babeltrace/ref.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/compiler-internal.h>
38 #include <inttypes.h>
39
40 static
41 void bt_ctf_clock_destroy(struct bt_object *obj);
42
43 struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
44 {
45 int ret;
46 struct bt_ctf_clock *clock = NULL;
47 unsigned char cc_uuid[BABELTRACE_UUID_LEN];
48
49 if (!name) {
50 goto error;
51 }
52
53 clock = g_new0(struct bt_ctf_clock, 1);
54
55 if (!clock) {
56 goto error;
57 }
58
59 bt_object_init(clock, bt_ctf_clock_destroy);
60 clock->value = 0;
61 clock->clock_class = bt_ctf_clock_class_create(name);
62 if (!clock->clock_class) {
63 goto error;
64 }
65
66 /* Automatically set clock class's UUID. */
67 ret = bt_uuid_generate(cc_uuid);
68 if (ret) {
69 goto error;
70 }
71
72 ret = bt_ctf_clock_class_set_uuid(clock->clock_class, cc_uuid);
73 assert(ret == 0);
74 return clock;
75
76 error:
77 BT_PUT(clock);
78 return clock;
79 }
80
81 const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock)
82 {
83 const char *name = NULL;
84
85 if (clock) {
86 name = bt_ctf_clock_class_get_name(clock->clock_class);
87 }
88
89 return name;
90 }
91
92 const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock)
93 {
94 const char *description = NULL;
95
96 if (clock) {
97 description = bt_ctf_clock_class_get_description(
98 clock->clock_class);
99 }
100
101 return description;
102 }
103
104 int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
105 {
106 int ret = -1;
107
108 if (clock) {
109 ret = bt_ctf_clock_class_set_description(clock->clock_class,
110 desc);
111 }
112
113 return ret;
114 }
115
116 uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock)
117 {
118 uint64_t freq = -1ULL;
119
120 if (clock) {
121 freq = bt_ctf_clock_class_get_frequency(clock->clock_class);
122 }
123
124 return freq;
125 }
126
127 int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
128 {
129 int ret = -1;
130
131 if (clock) {
132 ret = bt_ctf_clock_class_set_frequency(clock->clock_class,
133 freq);
134 }
135
136 return ret;
137 }
138
139 uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock)
140 {
141 uint64_t precision = -1ULL;
142
143 if (clock) {
144 precision = bt_ctf_clock_class_get_precision(
145 clock->clock_class);
146 }
147
148 return precision;
149 }
150
151 int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
152 {
153 int ret = -1;
154
155 if (clock) {
156 ret = bt_ctf_clock_class_set_precision(clock->clock_class,
157 precision);
158 }
159
160 return ret;
161 }
162
163 int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s)
164 {
165 int ret = -1;
166
167 if (clock) {
168 ret = bt_ctf_clock_class_get_offset_s(clock->clock_class,
169 offset_s);
170 }
171
172 return ret;
173 }
174
175 int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s)
176 {
177 int ret = -1;
178
179 if (clock) {
180 ret = bt_ctf_clock_class_set_offset_s(clock->clock_class,
181 offset_s);
182 }
183
184 return ret;
185 }
186
187 int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset)
188 {
189 int ret = -1;
190
191 if (clock) {
192 ret = bt_ctf_clock_class_get_offset_cycles(clock->clock_class,
193 offset);
194 }
195
196 return ret;
197 }
198
199 int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset)
200 {
201 int ret = -1;
202
203 if (clock) {
204 ret = bt_ctf_clock_class_set_offset_cycles(clock->clock_class,
205 offset);
206 }
207
208 return ret;
209 }
210
211 int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock)
212 {
213 int is_absolute = -1;
214
215 if (clock) {
216 is_absolute = bt_ctf_clock_class_is_absolute(
217 clock->clock_class);
218 }
219
220 return is_absolute;
221 }
222
223 int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
224 {
225 int ret = -1;
226
227 if (clock) {
228 ret = bt_ctf_clock_class_set_is_absolute(clock->clock_class,
229 is_absolute);
230 }
231
232 return ret;
233 }
234
235 const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock)
236 {
237 const unsigned char *uuid = NULL;
238
239 if (clock) {
240 uuid = bt_ctf_clock_class_get_uuid(clock->clock_class);
241 }
242
243 return uuid;
244 }
245
246 int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid)
247 {
248 int ret = -1;
249
250 if (clock) {
251 ret = bt_ctf_clock_class_set_uuid(clock->clock_class, uuid);
252 }
253
254 return ret;
255 }
256
257 int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
258 {
259 int ret = 0;
260 int64_t value;
261
262 if (!clock) {
263 ret = -1;
264 goto end;
265 }
266
267 /* Common case where cycles are actually nanoseconds */
268 if (clock->clock_class->frequency == 1000000000) {
269 value = time;
270 } else {
271 value = (uint64_t) (((double) time *
272 (double) clock->clock_class->frequency) / 1e9);
273 }
274
275 if (clock->value > value) {
276 /* Timestamps must be strictly monotonic. */
277 ret = -1;
278 goto end;
279 }
280
281 clock->value = value;
282 end:
283 return ret;
284 }
285
286 void bt_ctf_clock_get(struct bt_ctf_clock *clock)
287 {
288 bt_get(clock);
289 }
290
291 void bt_ctf_clock_put(struct bt_ctf_clock *clock)
292 {
293 bt_put(clock);
294 }
295
296 BT_HIDDEN
297 int bt_ctf_clock_get_value(struct bt_ctf_clock *clock, uint64_t *value)
298 {
299 int ret = 0;
300
301 if (!clock || !value) {
302 ret = -1;
303 goto end;
304 }
305
306 *value = clock->value;
307 end:
308 return ret;
309 }
310
311 static
312 void bt_ctf_clock_destroy(struct bt_object *obj)
313 {
314 struct bt_ctf_clock *clock;
315
316 clock = container_of(obj, struct bt_ctf_clock, base);
317 bt_put(clock->clock_class);
318 g_free(clock);
319 }
This page took 0.036625 seconds and 3 git commands to generate.