Avoid unnecessary inclusions in public headers
[babeltrace.git] / plugins / utils / trimmer / trimmer.c
1 /*
2 * trimmer.c
3 *
4 * Babeltrace Trace Trimmer
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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 #define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT"
30 #include "logging.h"
31
32 #include <babeltrace/compat/utc-internal.h>
33 #include <babeltrace/babeltrace.h>
34 #include <plugins-common.h>
35 #include "trimmer.h"
36 #include "iterator.h"
37 #include <assert.h>
38
39 static
40 void destroy_trimmer_data(struct trimmer *trimmer)
41 {
42 g_free(trimmer);
43 }
44
45 static
46 struct trimmer *create_trimmer_data(void)
47 {
48 return g_new0(struct trimmer, 1);
49 }
50
51 void finalize_trimmer(struct bt_private_component *component)
52 {
53 void *data = bt_private_component_get_user_data(component);
54
55 destroy_trimmer_data(data);
56 }
57
58 /*
59 * Parses a timestamp, figuring out its format.
60 *
61 * Returns a negative value if anything goes wrong.
62 *
63 * Expected formats:
64 *
65 * YYYY-MM-DD hh:mm:ss.ns
66 * hh:mm:ss.ns
67 * -ss.ns
68 * ss.ns
69 * YYYY-MM-DD hh:mm:ss
70 * hh:mm:ss
71 * -ss
72 * ss
73 */
74 static
75 int timestamp_from_arg(const char *arg, struct trimmer *trimmer,
76 struct trimmer_bound *result_bound, bt_bool gmt)
77 {
78 int ret;
79 int64_t value;
80 unsigned int year, month, day, hh, mm, ss, ns;
81
82 /* YYYY-MM-DD hh:mm:ss.ns */
83 ret = sscanf(arg, "%u-%u-%u %u:%u:%u.%u",
84 &year, &month, &day, &hh, &mm, &ss, &ns);
85 if (ret == 7) {
86 struct tm tm = {
87 .tm_sec = ss,
88 .tm_min = mm,
89 .tm_hour = hh,
90 .tm_mday = day,
91 .tm_mon = month - 1,
92 .tm_year = year - 1900,
93 .tm_isdst = -1,
94 };
95 time_t result;
96
97 if (gmt) {
98 result = bt_timegm(&tm);
99 if (result < 0) {
100 return -1;
101 }
102 } else {
103 result = mktime(&tm);
104 if (result < 0) {
105 return -1;
106 }
107 }
108 value = (int64_t) result;
109 value *= NSEC_PER_SEC;
110 value += ns;
111 if (!trimmer->date) {
112 trimmer->year = year;
113 trimmer->month = month;
114 trimmer->day = day;
115 trimmer->date = true;
116 }
117 goto set;
118 }
119 /* hh:mm:ss.ns */
120 ret = sscanf(arg, "%u:%u:%u.%u",
121 &hh, &mm, &ss, &ns);
122 if (ret == 4) {
123 if (!trimmer->date) {
124 /* We don't know which day until we get an event. */
125 result_bound->lazy_values.hh = hh;
126 result_bound->lazy_values.mm = mm;
127 result_bound->lazy_values.ss = ss;
128 result_bound->lazy_values.ns = ns;
129 result_bound->lazy_values.gmt = gmt;
130 goto lazy;
131 } else {
132 struct tm tm = {
133 .tm_sec = ss,
134 .tm_min = mm,
135 .tm_hour = hh,
136 .tm_mday = trimmer->day,
137 .tm_mon = trimmer->month - 1,
138 .tm_year = trimmer->year - 1900,
139 .tm_isdst = -1,
140 };
141 time_t result;
142
143 if (gmt) {
144 result = bt_timegm(&tm);
145 if (result < 0) {
146 return -1;
147 }
148 } else {
149 result = mktime(&tm);
150 if (result < 0) {
151 return -1;
152 }
153 }
154 value = (int64_t) result;
155 value *= NSEC_PER_SEC;
156 value += ns;
157 goto set;
158 }
159 }
160 /* -ss.ns */
161 ret = sscanf(arg, "-%u.%u",
162 &ss, &ns);
163 if (ret == 2) {
164 value = -ss * NSEC_PER_SEC;
165 value -= ns;
166 goto set;
167 }
168 /* ss.ns */
169 ret = sscanf(arg, "%u.%u",
170 &ss, &ns);
171 if (ret == 2) {
172 value = ss * NSEC_PER_SEC;
173 value += ns;
174 goto set;
175 }
176
177 /* YYYY-MM-DD hh:mm:ss */
178 ret = sscanf(arg, "%u-%u-%u %u:%u:%u",
179 &year, &month, &day, &hh, &mm, &ss);
180 if (ret == 6) {
181 struct tm tm = {
182 .tm_sec = ss,
183 .tm_min = mm,
184 .tm_hour = hh,
185 .tm_mday = day,
186 .tm_mon = month - 1,
187 .tm_year = year - 1900,
188 .tm_isdst = -1,
189 };
190
191 if (gmt) {
192 value = bt_timegm(&tm);
193 if (value < 0) {
194 return -1;
195 }
196 } else {
197 value = mktime(&tm);
198 if (value < 0) {
199 return -1;
200 }
201 }
202 value *= NSEC_PER_SEC;
203 if (!trimmer->date) {
204 trimmer->year = year;
205 trimmer->month = month;
206 trimmer->day = day;
207 trimmer->date = true;
208 }
209 goto set;
210 }
211 /* hh:mm:ss */
212 ret = sscanf(arg, "%u:%u:%u",
213 &hh, &mm, &ss);
214 if (ret == 3) {
215 if (!trimmer->date) {
216 /* We don't know which day until we get an event. */
217 result_bound->lazy_values.hh = hh;
218 result_bound->lazy_values.mm = mm;
219 result_bound->lazy_values.ss = ss;
220 result_bound->lazy_values.ns = 0;
221 result_bound->lazy_values.gmt = gmt;
222 goto lazy;
223 } else {
224 struct tm tm = {
225 .tm_sec = ss,
226 .tm_min = mm,
227 .tm_hour = hh,
228 .tm_mday = trimmer->day,
229 .tm_mon = trimmer->month - 1,
230 .tm_year = trimmer->year - 1900,
231 .tm_isdst = -1,
232 };
233 time_t result;
234
235 if (gmt) {
236 result = bt_timegm(&tm);
237 if (result < 0) {
238 return -1;
239 }
240 } else {
241 result = mktime(&tm);
242 if (result < 0) {
243 return -1;
244 }
245 }
246 value = (int64_t) result;
247 value *= NSEC_PER_SEC;
248 goto set;
249 }
250 }
251 /* -ss */
252 ret = sscanf(arg, "-%u",
253 &ss);
254 if (ret == 1) {
255 value = -ss * NSEC_PER_SEC;
256 goto set;
257 }
258 /* ss */
259 ret = sscanf(arg, "%u",
260 &ss);
261 if (ret == 1) {
262 value = ss * NSEC_PER_SEC;
263 goto set;
264 }
265
266 /* Not found. */
267 return -1;
268
269 set:
270 result_bound->value = value;
271 result_bound->set = true;
272 return 0;
273
274 lazy:
275 result_bound->lazy = true;
276 return 0;
277 }
278
279 static
280 enum bt_component_status init_from_params(struct trimmer *trimmer,
281 struct bt_value *params)
282 {
283 struct bt_value *value = NULL;
284 bt_bool gmt = BT_FALSE;
285 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
286
287 assert(params);
288
289 value = bt_value_map_get(params, "clock-gmt");
290 if (value) {
291 enum bt_value_status value_ret;
292
293 value_ret = bt_value_bool_get(value, &gmt);
294 if (value_ret) {
295 ret = BT_COMPONENT_STATUS_INVALID;
296 BT_LOGE_STR("Failed to retrieve clock-gmt value. Expecting a boolean");
297 }
298 }
299 bt_put(value);
300 if (ret != BT_COMPONENT_STATUS_OK) {
301 goto end;
302 }
303
304 value = bt_value_map_get(params, "begin");
305 if (value) {
306 enum bt_value_status value_ret;
307 const char *str;
308
309 value_ret = bt_value_string_get(value, &str);
310 if (value_ret || timestamp_from_arg(str,
311 trimmer, &trimmer->begin, gmt)) {
312 ret = BT_COMPONENT_STATUS_INVALID;
313 BT_LOGE_STR("Failed to retrieve begin value. Expecting a timestamp string");
314 }
315 }
316 bt_put(value);
317 if (ret != BT_COMPONENT_STATUS_OK) {
318 goto end;
319 }
320
321 value = bt_value_map_get(params, "end");
322 if (value) {
323 enum bt_value_status value_ret;
324 const char *str;
325
326 value_ret = bt_value_string_get(value, &str);
327 if (value_ret || timestamp_from_arg(str,
328 trimmer, &trimmer->end, gmt)) {
329 ret = BT_COMPONENT_STATUS_INVALID;
330 BT_LOGE_STR("Failed to retrieve end value. Expecting a timestamp string");
331 }
332 }
333 bt_put(value);
334 end:
335 if (trimmer->begin.set && trimmer->end.set) {
336 if (trimmer->begin.value > trimmer->end.value) {
337 BT_LOGE_STR("Unexpected: time range begin value is above end value");
338 ret = BT_COMPONENT_STATUS_INVALID;
339 }
340 }
341 return ret;
342 }
343
344 enum bt_component_status trimmer_component_init(
345 struct bt_private_component *component, struct bt_value *params,
346 UNUSED_VAR void *init_method_data)
347 {
348 enum bt_component_status ret;
349 struct trimmer *trimmer = create_trimmer_data();
350
351 if (!trimmer) {
352 ret = BT_COMPONENT_STATUS_NOMEM;
353 goto end;
354 }
355
356 /* Create input and output ports */
357 ret = bt_private_component_filter_add_input_private_port(
358 component, "in", NULL, NULL);
359 if (ret != BT_COMPONENT_STATUS_OK) {
360 goto error;
361 }
362
363 ret = bt_private_component_filter_add_output_private_port(
364 component, "out", NULL, NULL);
365 if (ret != BT_COMPONENT_STATUS_OK) {
366 goto error;
367 }
368
369 ret = bt_private_component_set_user_data(component, trimmer);
370 if (ret != BT_COMPONENT_STATUS_OK) {
371 goto error;
372 }
373
374 ret = init_from_params(trimmer, params);
375 end:
376 return ret;
377 error:
378 destroy_trimmer_data(trimmer);
379 ret = BT_COMPONENT_STATUS_ERROR;
380 return ret;
381 }
This page took 0.050137 seconds and 4 git commands to generate.