Add CTF Writer implementation
[babeltrace.git] / formats / ctf / writer / clock.c
1 /*
2 * clock.c
3 *
4 * Babeltrace CTF Writer
5 *
6 * Copyright 2013 EfficiOS Inc.
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/ctf-writer/clock.h>
30 #include <babeltrace/ctf-writer/clock-internal.h>
31 #include <babeltrace/ctf-writer/writer-internal.h>
32 #include <babeltrace/compiler.h>
33 #include <inttypes.h>
34
35 static
36 void bt_ctf_clock_destroy(struct bt_ctf_ref *ref);
37
38 struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
39 {
40 struct bt_ctf_clock *clock = NULL;
41
42 if (validate_identifier(name)) {
43 goto error;
44 }
45
46 clock = g_new0(struct bt_ctf_clock, 1);
47 if (!clock) {
48 goto error;
49 }
50
51 clock->name = g_string_new(name);
52 if (!clock->name) {
53 goto error_destroy;
54 }
55
56 clock->description = g_string_new(NULL);
57 if (!clock->description) {
58 goto error_destroy;
59 }
60
61 clock->precision = 1;
62 clock->frequency = 1000000000;
63 uuid_generate(clock->uuid);
64 bt_ctf_ref_init(&clock->ref_count);
65 return clock;
66 error_destroy:
67 bt_ctf_clock_destroy(&clock->ref_count);
68 error:
69 clock = NULL;
70 return clock;
71 }
72
73 int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
74 {
75 int ret = 0;
76
77 if (!clock || !desc || clock->frozen) {
78 ret = -1;
79 goto end;
80 }
81
82 clock->description = g_string_assign(clock->description, desc);
83 ret = clock->description ? 0 : -1;
84 end:
85 return ret;
86 }
87
88 int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
89 {
90 int ret = 0;
91
92 if (!clock || clock->frozen) {
93 ret = -1;
94 goto end;
95 }
96
97 clock->frequency = freq;
98 end:
99 return ret;
100 }
101
102 int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
103 {
104 int ret = 0;
105
106 if (!clock || clock->frozen) {
107 ret = -1;
108 goto end;
109 }
110
111 clock->precision = precision;
112 end:
113 return ret;
114 }
115
116 int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s)
117 {
118 int ret = 0;
119
120 if (!clock || clock->frozen) {
121 ret = -1;
122 goto end;
123 }
124
125 clock->offset_s = offset_s;
126 end:
127 return ret;
128 }
129
130 int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset)
131 {
132 int ret = 0;
133
134 if (!clock || clock->frozen) {
135 ret = -1;
136 goto end;
137 }
138
139 clock->offset = offset;
140 end:
141 return ret;
142 }
143
144 int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
145 {
146 int ret = 0;
147
148 if (!clock || clock->frozen) {
149 ret = -1;
150 goto end;
151 }
152
153 clock->absolute = !!is_absolute;
154 end:
155 return ret;
156 }
157
158 int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time)
159 {
160 int ret = 0;
161
162 /* Timestamps are strictly monotonic */
163 if (!clock || time < clock->time) {
164 ret = -1;
165 goto end;
166 }
167
168 clock->time = time;
169 end:
170 return ret;
171 }
172
173 void bt_ctf_clock_get(struct bt_ctf_clock *clock)
174 {
175 if (!clock) {
176 return;
177 }
178
179 bt_ctf_ref_get(&clock->ref_count);
180 }
181
182 void bt_ctf_clock_put(struct bt_ctf_clock *clock)
183 {
184 if (!clock) {
185 return;
186 }
187
188 bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy);
189 }
190
191 BT_HIDDEN
192 void bt_ctf_clock_freeze(struct bt_ctf_clock *clock)
193 {
194 if (!clock) {
195 return;
196 }
197
198 clock->frozen = 1;
199 }
200
201 BT_HIDDEN
202 void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
203 struct metadata_context *context)
204 {
205 unsigned char *uuid;
206
207 if (!clock || !context) {
208 return;
209 }
210
211 uuid = clock->uuid;
212 g_string_append(context->string, "clock {\n");
213 g_string_append_printf(context->string, "\tname = %s;\n",
214 clock->name->str);
215 g_string_append_printf(context->string,
216 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
217 uuid[0], uuid[1], uuid[2], uuid[3],
218 uuid[4], uuid[5], uuid[6], uuid[7],
219 uuid[8], uuid[9], uuid[10], uuid[11],
220 uuid[12], uuid[13], uuid[14], uuid[15]);
221 if (clock->description->len) {
222 g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
223 clock->description->str);
224 }
225
226 g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n",
227 clock->frequency);
228 g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n",
229 clock->precision);
230 g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n",
231 clock->offset_s);
232 g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
233 clock->offset);
234 g_string_append_printf(context->string, "\tabsolute = %s;\n",
235 clock->absolute ? "TRUE" : "FALSE");
236 g_string_append(context->string, "};\n\n");
237 }
238
239 BT_HIDDEN
240 uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
241 {
242 return clock ? clock->time : 0;
243 }
244
245 static
246 void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
247 {
248 struct bt_ctf_clock *clock;
249
250 if (!ref) {
251 return;
252 }
253
254 clock = container_of(ref, struct bt_ctf_clock, ref_count);
255 if (clock->name) {
256 g_string_free(clock->name, TRUE);
257 }
258
259 if (clock->description) {
260 g_string_free(clock->description, TRUE);
261 }
262
263 g_free(clock);
264 }
This page took 0.033618 seconds and 4 git commands to generate.