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