4 * Babeltrace Trace Trimmer
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/plugin/plugin-dev.h>
30 #include <babeltrace/component/component.h>
31 #include <babeltrace/component/component-filter.h>
32 #include <babeltrace/component/notification/notification.h>
33 #include <babeltrace/component/notification/iterator.h>
34 #include <babeltrace/component/notification/event.h>
40 void destroy_trimmer_data(struct trimmer
*trimmer
)
46 struct trimmer
*create_trimmer_data(void)
48 struct trimmer
*trimmer
;
50 trimmer
= g_new0(struct trimmer
, 1);
59 void destroy_trimmer(struct bt_component
*component
)
61 void *data
= bt_component_get_private_data(component
);
63 destroy_trimmer_data(data
);
67 * Parses a timestamp, figuring out its format.
69 * Returns a negative value if anything goes wrong.
73 * YYYY-MM-DD hh:mm:ss.ns
83 int timestamp_from_arg(const char *arg
, struct trimmer
*trimmer
,
84 struct trimmer_bound
*result_bound
, bool gmt
)
88 unsigned int year
, month
, day
, hh
, mm
, ss
, ns
;
90 /* YYYY-MM-DD hh:mm:ss.ns */
91 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u.%u",
92 &year
, &month
, &day
, &hh
, &mm
, &ss
, &ns
);
100 .tm_year
= year
- 1900,
106 result
= timegm(&tm
);
111 result
= mktime(&tm
);
116 value
= (int64_t) result
;
117 value
*= NSEC_PER_SEC
;
119 if (!trimmer
->date
) {
120 trimmer
->year
= year
;
121 trimmer
->month
= month
;
123 trimmer
->date
= true;
128 ret
= sscanf(arg
, "%u:%u:%u.%u",
131 if (!trimmer
->date
) {
132 /* We don't know which day until we get an event. */
133 result_bound
->lazy_values
.hh
= hh
;
134 result_bound
->lazy_values
.mm
= mm
;
135 result_bound
->lazy_values
.ss
= ss
;
136 result_bound
->lazy_values
.ns
= ns
;
137 result_bound
->lazy_values
.gmt
= gmt
;
144 .tm_mday
= trimmer
->day
,
145 .tm_mon
= trimmer
->month
- 1,
146 .tm_year
= trimmer
->year
- 1900,
152 result
= timegm(&tm
);
157 result
= mktime(&tm
);
162 value
= (int64_t) result
;
163 value
*= NSEC_PER_SEC
;
169 ret
= sscanf(arg
, "-%u.%u",
172 value
= -ss
* NSEC_PER_SEC
;
177 ret
= sscanf(arg
, "%u.%u",
180 value
= ss
* NSEC_PER_SEC
;
185 /* YYYY-MM-DD hh:mm:ss */
186 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u",
187 &year
, &month
, &day
, &hh
, &mm
, &ss
);
195 .tm_year
= year
- 1900,
210 value
*= NSEC_PER_SEC
;
211 if (!trimmer
->date
) {
212 trimmer
->year
= year
;
213 trimmer
->month
= month
;
215 trimmer
->date
= true;
220 ret
= sscanf(arg
, "%u:%u:%u",
223 if (!trimmer
->date
) {
224 /* We don't know which day until we get an event. */
225 result_bound
->lazy_values
.hh
= hh
;
226 result_bound
->lazy_values
.mm
= mm
;
227 result_bound
->lazy_values
.ss
= ss
;
228 result_bound
->lazy_values
.ns
= 0;
229 result_bound
->lazy_values
.gmt
= gmt
;
236 .tm_mday
= trimmer
->day
,
237 .tm_mon
= trimmer
->month
- 1,
238 .tm_year
= trimmer
->year
- 1900,
244 result
= timegm(&tm
);
249 result
= mktime(&tm
);
254 value
= (int64_t) result
;
255 value
*= NSEC_PER_SEC
;
260 ret
= sscanf(arg
, "-%u",
263 value
= -ss
* NSEC_PER_SEC
;
267 ret
= sscanf(arg
, "%u",
270 value
= ss
* NSEC_PER_SEC
;
278 result_bound
->value
= value
;
279 result_bound
->set
= true;
283 result_bound
->lazy
= true;
288 enum bt_component_status
init_from_params(struct trimmer
*trimmer
, struct bt_value
*params
)
290 struct bt_value
*value
= NULL
;
292 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
296 value
= bt_value_map_get(params
, "clock-gmt");
298 enum bt_value_status value_ret
;
300 value_ret
= bt_value_bool_get(value
, &gmt
);
302 ret
= BT_COMPONENT_STATUS_INVALID
;
303 printf_error("Failed to retrieve clock-gmt value. Expecting a boolean");
307 if (ret
!= BT_COMPONENT_STATUS_OK
) {
311 value
= bt_value_map_get(params
, "begin");
313 enum bt_value_status value_ret
;
316 value_ret
= bt_value_string_get(value
, &str
);
317 if (value_ret
|| timestamp_from_arg(str
,
318 trimmer
, &trimmer
->begin
, gmt
)) {
319 ret
= BT_COMPONENT_STATUS_INVALID
;
320 printf_error("Failed to retrieve begin value. Expecting a timestamp string");
324 if (ret
!= BT_COMPONENT_STATUS_OK
) {
328 value
= bt_value_map_get(params
, "end");
330 enum bt_value_status value_ret
;
333 value_ret
= bt_value_string_get(value
, &str
);
334 if (value_ret
|| timestamp_from_arg(str
,
335 trimmer
, &trimmer
->end
, gmt
)) {
336 ret
= BT_COMPONENT_STATUS_INVALID
;
337 printf_error("Failed to retrieve end value. Expecting a timestamp string");
342 if (trimmer
->begin
.set
&& trimmer
->end
.set
) {
343 if (trimmer
->begin
.value
> trimmer
->end
.value
) {
344 printf_error("Unexpected: time range begin value is above end value");
345 ret
= BT_COMPONENT_STATUS_INVALID
;
351 enum bt_component_status
trimmer_component_init(
352 struct bt_component
*component
, struct bt_value
*params
,
353 void *init_method_data
)
355 enum bt_component_status ret
;
356 struct trimmer
*trimmer
= create_trimmer_data();
357 (void) init_method_data
;
360 ret
= BT_COMPONENT_STATUS_NOMEM
;
364 ret
= bt_component_set_private_data(component
, trimmer
);
365 if (ret
!= BT_COMPONENT_STATUS_OK
) {
369 ret
= init_from_params(trimmer
, params
);
373 destroy_trimmer_data(trimmer
);
377 /* Initialize plug-in entry points. */
379 BT_PLUGIN_DESCRIPTION("Babeltrace Trace Trimmer Plug-In.");
380 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
381 BT_PLUGIN_LICENSE("MIT");
382 BT_PLUGIN_FILTER_COMPONENT_CLASS(trimmer
, trimmer_iterator_init
);
383 BT_PLUGIN_FILTER_COMPONENT_CLASS_DESCRIPTION(trimmer
,
384 "Ensure that trace notifications outside of a given range are filtered-out.");
385 BT_PLUGIN_FILTER_COMPONENT_CLASS_INIT_METHOD(trimmer
, trimmer_component_init
);
386 BT_PLUGIN_FILTER_COMPONENT_CLASS_DESTROY_METHOD(trimmer
, destroy_trimmer
);