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-macros.h>
30 #include <babeltrace/plugin/component.h>
31 #include <babeltrace/plugin/filter.h>
32 #include <babeltrace/plugin/notification/notification.h>
33 #include <babeltrace/plugin/notification/iterator.h>
34 #include <babeltrace/plugin/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
,
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
;
122 ret
= sscanf(arg
, "%u:%u:%u.%u",
125 /* We don't know which day until we get an event. */
126 result_bound
->lazy_values
.hh
= hh
;
127 result_bound
->lazy_values
.mm
= mm
;
128 result_bound
->lazy_values
.ss
= ss
;
129 result_bound
->lazy_values
.ns
= ns
;
130 result_bound
->lazy_values
.gmt
= gmt
;
134 ret
= sscanf(arg
, "-%u.%u",
137 value
= -ss
* NSEC_PER_SEC
;
142 ret
= sscanf(arg
, "%u.%u",
145 value
= ss
* NSEC_PER_SEC
;
150 /* YYYY-MM-DD hh:mm:ss */
151 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u",
152 &year
, &month
, &day
, &hh
, &mm
, &ss
);
160 .tm_year
= year
- 1900,
175 value
*= NSEC_PER_SEC
;
179 ret
= sscanf(arg
, "%u:%u:%u",
182 /* We don't know which day until we get an event. */
183 result_bound
->lazy_values
.hh
= hh
;
184 result_bound
->lazy_values
.mm
= mm
;
185 result_bound
->lazy_values
.ss
= ss
;
186 result_bound
->lazy_values
.ns
= 0;
187 result_bound
->lazy_values
.gmt
= gmt
;
191 ret
= sscanf(arg
, "-%u",
194 value
= -ss
* NSEC_PER_SEC
;
198 ret
= sscanf(arg
, "%u",
201 value
= ss
* NSEC_PER_SEC
;
209 result_bound
->value
= value
;
210 result_bound
->set
= true;
214 result_bound
->lazy
= true;
219 enum bt_component_status
init_from_params(struct trimmer
*trimmer
, struct bt_value
*params
)
221 struct bt_value
*value
= NULL
;
223 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
227 value
= bt_value_map_get(params
, "clock-gmt");
229 enum bt_value_status value_ret
;
231 value_ret
= bt_value_bool_get(value
, &gmt
);
233 ret
= BT_COMPONENT_STATUS_INVALID
;
234 printf_error("Failed to retrieve clock-gmt value. Expecting a boolean.\n");
238 if (ret
!= BT_COMPONENT_STATUS_OK
) {
242 value
= bt_value_map_get(params
, "begin");
244 enum bt_value_status value_ret
;
247 value_ret
= bt_value_string_get(value
, &str
);
248 if (value_ret
|| timestamp_from_arg(str
,
249 &trimmer
->begin
, gmt
)) {
250 ret
= BT_COMPONENT_STATUS_INVALID
;
251 printf_error("Failed to retrieve begin value. Expecting a timestamp string.\n");
255 if (ret
!= BT_COMPONENT_STATUS_OK
) {
259 value
= bt_value_map_get(params
, "end");
261 enum bt_value_status value_ret
;
264 value_ret
= bt_value_string_get(value
, &str
);
265 if (value_ret
|| timestamp_from_arg(str
,
266 &trimmer
->end
, gmt
)) {
267 ret
= BT_COMPONENT_STATUS_INVALID
;
268 printf_error("Failed to retrieve end value. Expecting a timestamp string.\n");
276 enum bt_component_status
trimmer_component_init(
277 struct bt_component
*component
, struct bt_value
*params
)
279 enum bt_component_status ret
;
280 struct trimmer
*trimmer
= create_trimmer_data();
283 ret
= BT_COMPONENT_STATUS_NOMEM
;
287 ret
= bt_component_set_destroy_cb(component
,
289 if (ret
!= BT_COMPONENT_STATUS_OK
) {
293 ret
= bt_component_set_private_data(component
, trimmer
);
294 if (ret
!= BT_COMPONENT_STATUS_OK
) {
298 ret
= bt_component_filter_set_iterator_init_cb(component
,
299 trimmer_iterator_init
);
300 if (ret
!= BT_COMPONENT_STATUS_OK
) {
304 ret
= init_from_params(trimmer
, params
);
308 destroy_trimmer_data(trimmer
);
312 /* Initialize plug-in entry points. */
313 BT_PLUGIN_NAME("utils");
314 BT_PLUGIN_DESCRIPTION("Babeltrace Trace Trimmer Plug-In.");
315 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
316 BT_PLUGIN_LICENSE("MIT");
318 BT_PLUGIN_COMPONENT_CLASSES_BEGIN
319 BT_PLUGIN_FILTER_COMPONENT_CLASS_ENTRY("trimmer",
320 "Ensure that trace notifications outside of a given range are filtered-out.",
321 trimmer_component_init
)
322 BT_PLUGIN_COMPONENT_CLASSES_END