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/graph/component.h>
31 #include <babeltrace/graph/private-component.h>
32 #include <babeltrace/graph/component-filter.h>
33 #include <babeltrace/graph/notification.h>
34 #include <babeltrace/graph/notification-iterator.h>
35 #include <babeltrace/graph/private-notification-iterator.h>
36 #include <babeltrace/graph/private-component-filter.h>
37 #include <babeltrace/graph/notification-event.h>
38 #include <plugins-common.h>
44 void destroy_trimmer_data(struct trimmer
*trimmer
)
50 struct trimmer
*create_trimmer_data(void)
52 struct trimmer
*trimmer
;
54 trimmer
= g_new0(struct trimmer
, 1);
62 void finalize_trimmer(struct bt_private_component
*component
)
64 void *data
= bt_private_component_get_user_data(component
);
66 destroy_trimmer_data(data
);
70 * Parses a timestamp, figuring out its format.
72 * Returns a negative value if anything goes wrong.
76 * YYYY-MM-DD hh:mm:ss.ns
86 int timestamp_from_arg(const char *arg
, struct trimmer
*trimmer
,
87 struct trimmer_bound
*result_bound
, bt_bool gmt
)
91 unsigned int year
, month
, day
, hh
, mm
, ss
, ns
;
93 /* YYYY-MM-DD hh:mm:ss.ns */
94 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u.%u",
95 &year
, &month
, &day
, &hh
, &mm
, &ss
, &ns
);
103 .tm_year
= year
- 1900,
109 result
= timegm(&tm
);
114 result
= mktime(&tm
);
119 value
= (int64_t) result
;
120 value
*= NSEC_PER_SEC
;
122 if (!trimmer
->date
) {
123 trimmer
->year
= year
;
124 trimmer
->month
= month
;
126 trimmer
->date
= true;
131 ret
= sscanf(arg
, "%u:%u:%u.%u",
134 if (!trimmer
->date
) {
135 /* We don't know which day until we get an event. */
136 result_bound
->lazy_values
.hh
= hh
;
137 result_bound
->lazy_values
.mm
= mm
;
138 result_bound
->lazy_values
.ss
= ss
;
139 result_bound
->lazy_values
.ns
= ns
;
140 result_bound
->lazy_values
.gmt
= gmt
;
147 .tm_mday
= trimmer
->day
,
148 .tm_mon
= trimmer
->month
- 1,
149 .tm_year
= trimmer
->year
- 1900,
155 result
= timegm(&tm
);
160 result
= mktime(&tm
);
165 value
= (int64_t) result
;
166 value
*= NSEC_PER_SEC
;
172 ret
= sscanf(arg
, "-%u.%u",
175 value
= -ss
* NSEC_PER_SEC
;
180 ret
= sscanf(arg
, "%u.%u",
183 value
= ss
* NSEC_PER_SEC
;
188 /* YYYY-MM-DD hh:mm:ss */
189 ret
= sscanf(arg
, "%u-%u-%u %u:%u:%u",
190 &year
, &month
, &day
, &hh
, &mm
, &ss
);
198 .tm_year
= year
- 1900,
213 value
*= NSEC_PER_SEC
;
214 if (!trimmer
->date
) {
215 trimmer
->year
= year
;
216 trimmer
->month
= month
;
218 trimmer
->date
= true;
223 ret
= sscanf(arg
, "%u:%u:%u",
226 if (!trimmer
->date
) {
227 /* We don't know which day until we get an event. */
228 result_bound
->lazy_values
.hh
= hh
;
229 result_bound
->lazy_values
.mm
= mm
;
230 result_bound
->lazy_values
.ss
= ss
;
231 result_bound
->lazy_values
.ns
= 0;
232 result_bound
->lazy_values
.gmt
= gmt
;
239 .tm_mday
= trimmer
->day
,
240 .tm_mon
= trimmer
->month
- 1,
241 .tm_year
= trimmer
->year
- 1900,
247 result
= timegm(&tm
);
252 result
= mktime(&tm
);
257 value
= (int64_t) result
;
258 value
*= NSEC_PER_SEC
;
263 ret
= sscanf(arg
, "-%u",
266 value
= -ss
* NSEC_PER_SEC
;
270 ret
= sscanf(arg
, "%u",
273 value
= ss
* NSEC_PER_SEC
;
281 result_bound
->value
= value
;
282 result_bound
->set
= true;
286 result_bound
->lazy
= true;
291 enum bt_component_status
init_from_params(struct trimmer
*trimmer
,
292 struct bt_value
*params
)
294 struct bt_value
*value
= NULL
;
295 bt_bool gmt
= BT_FALSE
;
296 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
300 value
= bt_value_map_get(params
, "clock-gmt");
302 enum bt_value_status value_ret
;
304 value_ret
= bt_value_bool_get(value
, &gmt
);
306 ret
= BT_COMPONENT_STATUS_INVALID
;
307 printf_error("Failed to retrieve clock-gmt value. Expecting a boolean");
311 if (ret
!= BT_COMPONENT_STATUS_OK
) {
315 value
= bt_value_map_get(params
, "begin");
317 enum bt_value_status value_ret
;
320 value_ret
= bt_value_string_get(value
, &str
);
321 if (value_ret
|| timestamp_from_arg(str
,
322 trimmer
, &trimmer
->begin
, gmt
)) {
323 ret
= BT_COMPONENT_STATUS_INVALID
;
324 printf_error("Failed to retrieve begin value. Expecting a timestamp string");
328 if (ret
!= BT_COMPONENT_STATUS_OK
) {
332 value
= bt_value_map_get(params
, "end");
334 enum bt_value_status value_ret
;
337 value_ret
= bt_value_string_get(value
, &str
);
338 if (value_ret
|| timestamp_from_arg(str
,
339 trimmer
, &trimmer
->end
, gmt
)) {
340 ret
= BT_COMPONENT_STATUS_INVALID
;
341 printf_error("Failed to retrieve end value. Expecting a timestamp string");
346 if (trimmer
->begin
.set
&& trimmer
->end
.set
) {
347 if (trimmer
->begin
.value
> trimmer
->end
.value
) {
348 printf_error("Unexpected: time range begin value is above end value");
349 ret
= BT_COMPONENT_STATUS_INVALID
;
355 enum bt_component_status
trimmer_component_init(
356 struct bt_private_component
*component
, struct bt_value
*params
,
357 UNUSED_VAR
void *init_method_data
)
359 enum bt_component_status ret
;
360 struct trimmer
*trimmer
= create_trimmer_data();
361 struct bt_private_port
*priv_port
= NULL
;
364 ret
= BT_COMPONENT_STATUS_NOMEM
;
368 /* Create input and output ports */
369 priv_port
= bt_private_component_filter_add_input_private_port(
370 component
, "in", NULL
);
376 priv_port
= bt_private_component_filter_add_output_private_port(
377 component
, "out", NULL
);
382 ret
= bt_private_component_set_user_data(component
, trimmer
);
383 if (ret
!= BT_COMPONENT_STATUS_OK
) {
387 ret
= init_from_params(trimmer
, params
);
392 destroy_trimmer_data(trimmer
);
393 ret
= BT_COMPONENT_STATUS_ERROR
;