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