Avoid unnecessary inclusions in public headers
[babeltrace.git] / plugins / utils / trimmer / trimmer.c
CommitLineData
cab3f160
JG
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
b4565e8b
PP
29#define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT"
30#include "logging.h"
31
3d2f08e7 32#include <babeltrace/compat/utc-internal.h>
9d408fca 33#include <babeltrace/babeltrace.h>
7d61fa8e 34#include <plugins-common.h>
cab3f160
JG
35#include "trimmer.h"
36#include "iterator.h"
44d3cbf0 37#include <assert.h>
cab3f160
JG
38
39static
40void destroy_trimmer_data(struct trimmer *trimmer)
41{
42 g_free(trimmer);
43}
44
45static
46struct trimmer *create_trimmer_data(void)
47{
4fde0f03 48 return g_new0(struct trimmer, 1);
cab3f160
JG
49}
50
64cadc66 51void finalize_trimmer(struct bt_private_component *component)
cab3f160 52{
890882ef 53 void *data = bt_private_component_get_user_data(component);
cab3f160
JG
54
55 destroy_trimmer_data(data);
56}
57
528debdf
MD
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 */
44d3cbf0 74static
c6dedfd4 75int timestamp_from_arg(const char *arg, struct trimmer *trimmer,
c55a9f58 76 struct trimmer_bound *result_bound, bt_bool gmt)
528debdf
MD
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) {
3d2f08e7 98 result = bt_timegm(&tm);
528debdf
MD
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;
c6dedfd4
MD
111 if (!trimmer->date) {
112 trimmer->year = year;
113 trimmer->month = month;
114 trimmer->day = day;
115 trimmer->date = true;
116 }
528debdf
MD
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) {
c6dedfd4
MD
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) {
3d2f08e7 144 result = bt_timegm(&tm);
c6dedfd4
MD
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 }
528debdf
MD
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) {
3d2f08e7 192 value = bt_timegm(&tm);
528debdf
MD
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;
c6dedfd4
MD
203 if (!trimmer->date) {
204 trimmer->year = year;
205 trimmer->month = month;
206 trimmer->day = day;
207 trimmer->date = true;
208 }
528debdf
MD
209 goto set;
210 }
211 /* hh:mm:ss */
212 ret = sscanf(arg, "%u:%u:%u",
213 &hh, &mm, &ss);
214 if (ret == 3) {
c6dedfd4
MD
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) {
3d2f08e7 236 result = bt_timegm(&tm);
c6dedfd4
MD
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 }
528debdf
MD
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
269set:
270 result_bound->value = value;
271 result_bound->set = true;
272 return 0;
273
274lazy:
275 result_bound->lazy = true;
276 return 0;
277}
278
279static
890882ef
PP
280enum bt_component_status init_from_params(struct trimmer *trimmer,
281 struct bt_value *params)
44d3cbf0
JG
282{
283 struct bt_value *value = NULL;
c55a9f58 284 bt_bool gmt = BT_FALSE;
528debdf 285 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
44d3cbf0
JG
286
287 assert(params);
288
528debdf
MD
289 value = bt_value_map_get(params, "clock-gmt");
290 if (value) {
44d3cbf0
JG
291 enum bt_value_status value_ret;
292
528debdf
MD
293 value_ret = bt_value_bool_get(value, &gmt);
294 if (value_ret) {
295 ret = BT_COMPONENT_STATUS_INVALID;
b4565e8b 296 BT_LOGE_STR("Failed to retrieve clock-gmt value. Expecting a boolean");
44d3cbf0
JG
297 }
298 }
299 bt_put(value);
528debdf
MD
300 if (ret != BT_COMPONENT_STATUS_OK) {
301 goto end;
302 }
303
304 value = bt_value_map_get(params, "begin");
305 if (value) {
44d3cbf0 306 enum bt_value_status value_ret;
528debdf 307 const char *str;
44d3cbf0 308
528debdf
MD
309 value_ret = bt_value_string_get(value, &str);
310 if (value_ret || timestamp_from_arg(str,
c6dedfd4 311 trimmer, &trimmer->begin, gmt)) {
528debdf 312 ret = BT_COMPONENT_STATUS_INVALID;
b4565e8b 313 BT_LOGE_STR("Failed to retrieve begin value. Expecting a timestamp string");
44d3cbf0
JG
314 }
315 }
316 bt_put(value);
528debdf
MD
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,
c6dedfd4 328 trimmer, &trimmer->end, gmt)) {
528debdf 329 ret = BT_COMPONENT_STATUS_INVALID;
b4565e8b 330 BT_LOGE_STR("Failed to retrieve end value. Expecting a timestamp string");
528debdf
MD
331 }
332 }
333 bt_put(value);
334end:
55595636
MD
335 if (trimmer->begin.set && trimmer->end.set) {
336 if (trimmer->begin.value > trimmer->end.value) {
b4565e8b 337 BT_LOGE_STR("Unexpected: time range begin value is above end value");
55595636
MD
338 ret = BT_COMPONENT_STATUS_INVALID;
339 }
340 }
528debdf 341 return ret;
44d3cbf0
JG
342}
343
cab3f160 344enum bt_component_status trimmer_component_init(
890882ef 345 struct bt_private_component *component, struct bt_value *params,
7d61fa8e 346 UNUSED_VAR void *init_method_data)
cab3f160
JG
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
b9d103be 356 /* Create input and output ports */
147337a3
PP
357 ret = bt_private_component_filter_add_input_private_port(
358 component, "in", NULL, NULL);
359 if (ret != BT_COMPONENT_STATUS_OK) {
b9d103be
PP
360 goto error;
361 }
362
147337a3
PP
363 ret = bt_private_component_filter_add_output_private_port(
364 component, "out", NULL, NULL);
365 if (ret != BT_COMPONENT_STATUS_OK) {
b9d103be
PP
366 goto error;
367 }
368
890882ef 369 ret = bt_private_component_set_user_data(component, trimmer);
cab3f160
JG
370 if (ret != BT_COMPONENT_STATUS_OK) {
371 goto error;
372 }
373
528debdf 374 ret = init_from_params(trimmer, params);
cab3f160
JG
375end:
376 return ret;
377error:
378 destroy_trimmer_data(trimmer);
b9d103be 379 ret = BT_COMPONENT_STATUS_ERROR;
cab3f160
JG
380 return ret;
381}
This page took 0.049866 seconds and 4 git commands to generate.