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>
35 #include <plugins-common.h>
41 void destroy_trimmer_data(struct trimmer
*trimmer
)
47 struct trimmer
*create_trimmer_data(void)
49 struct trimmer
*trimmer
;
51 trimmer
= g_new0(struct trimmer
, 1);
60 void destroy_trimmer(struct bt_component
*component
)
62 void *data
= bt_component_get_private_data(component
);
64 destroy_trimmer_data(data
);
68 * Parses a timestamp, figuring out its format.
70 * Returns a negative value if anything goes wrong.
74 * YYYY-MM-DD hh:mm:ss.ns
84 int timestamp_from_arg(const char *arg
, struct trimmer
*trimmer
,
85 struct trimmer_bound
*result_bound
, bool gmt
)
89 unsigned int year
, month
, day
, hh
, mm
, ss
, ns
;
91 /* YYYY-MM-DD hh:mm:ss.ns */
92 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u.%u",
93 &year
, &month
, &day
, &hh
, &mm
, &ss
, &ns
);
101 .tm_year
= year
- 1900,
107 result
= timegm(&tm
);
112 result
= mktime(&tm
);
117 value
= (int64_t) result
;
118 value
*= NSEC_PER_SEC
;
120 if (!trimmer
->date
) {
121 trimmer
->year
= year
;
122 trimmer
->month
= month
;
124 trimmer
->date
= true;
129 ret
= sscanf(arg
, "%u:%u:%u.%u",
132 if (!trimmer
->date
) {
133 /* We don't know which day until we get an event. */
134 result_bound
->lazy_values
.hh
= hh
;
135 result_bound
->lazy_values
.mm
= mm
;
136 result_bound
->lazy_values
.ss
= ss
;
137 result_bound
->lazy_values
.ns
= ns
;
138 result_bound
->lazy_values
.gmt
= gmt
;
145 .tm_mday
= trimmer
->day
,
146 .tm_mon
= trimmer
->month
- 1,
147 .tm_year
= trimmer
->year
- 1900,
153 result
= timegm(&tm
);
158 result
= mktime(&tm
);
163 value
= (int64_t) result
;
164 value
*= NSEC_PER_SEC
;
170 ret
= sscanf(arg
, "-%u.%u",
173 value
= -ss
* NSEC_PER_SEC
;
178 ret
= sscanf(arg
, "%u.%u",
181 value
= ss
* NSEC_PER_SEC
;
186 /* YYYY-MM-DD hh:mm:ss */
187 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u",
188 &year
, &month
, &day
, &hh
, &mm
, &ss
);
196 .tm_year
= year
- 1900,
211 value
*= NSEC_PER_SEC
;
212 if (!trimmer
->date
) {
213 trimmer
->year
= year
;
214 trimmer
->month
= month
;
216 trimmer
->date
= true;
221 ret
= sscanf(arg
, "%u:%u:%u",
224 if (!trimmer
->date
) {
225 /* We don't know which day until we get an event. */
226 result_bound
->lazy_values
.hh
= hh
;
227 result_bound
->lazy_values
.mm
= mm
;
228 result_bound
->lazy_values
.ss
= ss
;
229 result_bound
->lazy_values
.ns
= 0;
230 result_bound
->lazy_values
.gmt
= gmt
;
237 .tm_mday
= trimmer
->day
,
238 .tm_mon
= trimmer
->month
- 1,
239 .tm_year
= trimmer
->year
- 1900,
245 result
= timegm(&tm
);
250 result
= mktime(&tm
);
255 value
= (int64_t) result
;
256 value
*= NSEC_PER_SEC
;
261 ret
= sscanf(arg
, "-%u",
264 value
= -ss
* NSEC_PER_SEC
;
268 ret
= sscanf(arg
, "%u",
271 value
= ss
* NSEC_PER_SEC
;
279 result_bound
->value
= value
;
280 result_bound
->set
= true;
284 result_bound
->lazy
= true;
289 enum bt_component_status
init_from_params(struct trimmer
*trimmer
, struct bt_value
*params
)
291 struct bt_value
*value
= NULL
;
293 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
297 value
= bt_value_map_get(params
, "clock-gmt");
299 enum bt_value_status value_ret
;
301 value_ret
= bt_value_bool_get(value
, &gmt
);
303 ret
= BT_COMPONENT_STATUS_INVALID
;
304 printf_error("Failed to retrieve clock-gmt value. Expecting a boolean");
308 if (ret
!= BT_COMPONENT_STATUS_OK
) {
312 value
= bt_value_map_get(params
, "begin");
314 enum bt_value_status value_ret
;
317 value_ret
= bt_value_string_get(value
, &str
);
318 if (value_ret
|| timestamp_from_arg(str
,
319 trimmer
, &trimmer
->begin
, gmt
)) {
320 ret
= BT_COMPONENT_STATUS_INVALID
;
321 printf_error("Failed to retrieve begin value. Expecting a timestamp string");
325 if (ret
!= BT_COMPONENT_STATUS_OK
) {
329 value
= bt_value_map_get(params
, "end");
331 enum bt_value_status value_ret
;
334 value_ret
= bt_value_string_get(value
, &str
);
335 if (value_ret
|| timestamp_from_arg(str
,
336 trimmer
, &trimmer
->end
, gmt
)) {
337 ret
= BT_COMPONENT_STATUS_INVALID
;
338 printf_error("Failed to retrieve end value. Expecting a timestamp string");
343 if (trimmer
->begin
.set
&& trimmer
->end
.set
) {
344 if (trimmer
->begin
.value
> trimmer
->end
.value
) {
345 printf_error("Unexpected: time range begin value is above end value");
346 ret
= BT_COMPONENT_STATUS_INVALID
;
352 enum bt_component_status
trimmer_component_init(
353 struct bt_component
*component
, struct bt_value
*params
,
354 UNUSED_VAR
void *init_method_data
)
356 enum bt_component_status ret
;
357 struct trimmer
*trimmer
= create_trimmer_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
);