Remove component prefix from graph, connection and port filenames
[babeltrace.git] / plugins / utils / trimmer / iterator.c
CommitLineData
cab3f160
JG
1/*
2 * iterator.c
3 *
4 * Babeltrace Trace Trimmer Iterator
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
29#include "trimmer.h"
30#include "iterator.h"
33b34c43
PP
31#include <babeltrace/component/notification/iterator.h>
32#include <babeltrace/component/notification/notification.h>
33#include <babeltrace/component/notification/event.h>
34#include <babeltrace/component/notification/stream.h>
35#include <babeltrace/component/notification/packet.h>
d71dcf2c 36#include <babeltrace/component/component-filter.h>
7d55361f
JG
37#include <babeltrace/component/port.h>
38#include <babeltrace/component/connection.h>
44d3cbf0
JG
39#include <babeltrace/ctf-ir/event.h>
40#include <babeltrace/ctf-ir/stream.h>
41#include <babeltrace/ctf-ir/stream-class.h>
ac0c6bdd 42#include <babeltrace/ctf-ir/clock-class.h>
44d3cbf0
JG
43#include <babeltrace/ctf-ir/packet.h>
44#include <babeltrace/ctf-ir/trace.h>
72208f03 45#include <babeltrace/ctf-ir/fields.h>
44d3cbf0 46#include <assert.h>
8b0ce102 47#include <plugins-common.h>
44d3cbf0 48
d3eb6e8f 49BT_HIDDEN
44d3cbf0
JG
50void trimmer_iterator_destroy(struct bt_notification_iterator *it)
51{
52 struct trimmer_iterator *it_data;
53
54 it_data = bt_notification_iterator_get_private_data(it);
55 assert(it_data);
56
44d3cbf0 57 bt_put(it_data->current_notification);
99cdb69e 58 bt_put(it_data->input_iterator);
44d3cbf0
JG
59 g_free(it_data);
60}
cab3f160
JG
61
62BT_HIDDEN
d3eb6e8f
PP
63enum bt_notification_iterator_status trimmer_iterator_init(
64 struct bt_component *component,
8b0ce102
PP
65 struct bt_notification_iterator *iterator,
66 UNUSED_VAR void *init_method_data)
cab3f160 67{
d3eb6e8f
PP
68 enum bt_notification_iterator_status ret =
69 BT_NOTIFICATION_ITERATOR_STATUS_OK;
cab3f160 70 enum bt_notification_iterator_status it_ret;
99cdb69e
JG
71 struct bt_port *input_port = NULL;
72 struct bt_connection *connection = NULL;
44d3cbf0
JG
73 struct trimmer_iterator *it_data = g_new0(struct trimmer_iterator, 1);
74
75 if (!it_data) {
d3eb6e8f 76 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
44d3cbf0
JG
77 goto end;
78 }
79
99cdb69e
JG
80 /* Create a new iterator on the upstream component. */
81 input_port = bt_component_filter_get_default_input_port(component);
82 assert(input_port);
83 connection = bt_port_get_connection(input_port, 0);
84 assert(connection);
85
86 it_data->input_iterator = bt_connection_create_notification_iterator(
87 connection);
88 if (!it_data->input_iterator) {
89 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
90 goto end;
91 }
92
44d3cbf0
JG
93 it_ret = bt_notification_iterator_set_private_data(iterator, it_data);
94 if (it_ret) {
95 goto end;
96 }
cab3f160 97end:
99cdb69e
JG
98 bt_put(connection);
99 bt_put(input_port);
cab3f160
JG
100 return ret;
101}
102
103BT_HIDDEN
104struct bt_notification *trimmer_iterator_get(
105 struct bt_notification_iterator *iterator)
106{
44d3cbf0
JG
107 struct trimmer_iterator *trim_it;
108
109 trim_it = bt_notification_iterator_get_private_data(iterator);
110 assert(trim_it);
111
112 if (!trim_it->current_notification) {
113 enum bt_notification_iterator_status it_ret;
114
115 it_ret = trimmer_iterator_next(iterator);
116 if (it_ret) {
117 goto end;
118 }
119 }
120end:
121 return bt_get(trim_it->current_notification);
122}
123
528debdf
MD
124static
125int update_lazy_bound(struct trimmer_bound *bound, const char *name,
55595636 126 int64_t ts, bool *lazy_update)
528debdf
MD
127{
128 struct tm tm;
129 int64_t value;
130 time_t timeval;
131
55595636
MD
132 *lazy_update = false;
133
528debdf
MD
134 if (!bound->lazy) {
135 return 0;
136 }
137 tm.tm_isdst = -1;
138 timeval = ts / NSEC_PER_SEC;
139
140 if (bound->lazy_values.gmt) {
141 /* Get day, month, year. */
142 if (!gmtime_r(&timeval, &tm)) {
55595636 143 printf_error("Failure in gmtime_r()");
528debdf
MD
144 goto error;
145 }
146 tm.tm_sec = bound->lazy_values.ss;
147 tm.tm_min = bound->lazy_values.mm;
148 tm.tm_hour = bound->lazy_values.hh;
149 timeval = timegm(&tm);
150 if (timeval < 0) {
55595636
MD
151 printf_error("Failure in timegm(), incorrectly formatted %s timestamp",
152 name);
528debdf
MD
153 goto error;
154 }
155 } else {
156 /* Get day, month, year. */
157 if (!localtime_r(&timeval, &tm)) {
55595636 158 printf_error("Failure in localtime_r()");
528debdf
MD
159 goto error;
160 }
161 tm.tm_sec = bound->lazy_values.ss;
162 tm.tm_min = bound->lazy_values.mm;
163 tm.tm_hour = bound->lazy_values.hh;
164 timeval = mktime(&tm);
165 if (timeval < 0) {
55595636 166 printf_error("Failure in mktime(), incorrectly formatted %s timestamp",
528debdf
MD
167 name);
168 goto error;
169 }
170 }
171 value = (int64_t) timeval;
172 value *= NSEC_PER_SEC;
173 value += bound->lazy_values.ns;
174 bound->value = value;
175 bound->set = true;
176 bound->lazy = false;
55595636 177 *lazy_update = true;
528debdf
MD
178 return 0;
179
180error:
528debdf
MD
181 return -1;
182}
183
44d3cbf0 184static
55595636
MD
185enum bt_notification_iterator_status
186evaluate_event_notification(struct bt_notification *notification,
187 struct trimmer_bound *begin, struct trimmer_bound *end,
188 bool *_event_in_range)
44d3cbf0 189{
72208f03
JG
190 int64_t ts;
191 int clock_ret;
192 struct bt_ctf_event *event = NULL;
193 bool in_range = true;
ac0c6bdd 194 struct bt_ctf_clock_class *clock_class = NULL;
72208f03 195 struct bt_ctf_trace *trace = NULL;
44d3cbf0 196 struct bt_ctf_stream *stream = NULL;
72208f03
JG
197 struct bt_ctf_stream_class *stream_class = NULL;
198 struct bt_ctf_clock_value *clock_value = NULL;
55595636
MD
199 enum bt_notification_iterator_status ret =
200 BT_NOTIFICATION_ITERATOR_STATUS_OK;
201 bool lazy_update = false;
44d3cbf0 202
72208f03
JG
203 event = bt_notification_event_get_event(notification);
204 assert(event);
44d3cbf0 205
72208f03
JG
206 stream = bt_ctf_event_get_stream(event);
207 assert(stream);
44d3cbf0 208
72208f03
JG
209 stream_class = bt_ctf_stream_get_class(stream);
210 assert(stream_class);
211
212 trace = bt_ctf_stream_class_get_trace(stream_class);
213 assert(trace);
214
215 /* FIXME multi-clock? */
ac0c6bdd
PP
216 clock_class = bt_ctf_trace_get_clock_class(trace, 0);
217 if (!clock_class) {
72208f03 218 goto end;
44d3cbf0 219 }
44d3cbf0 220
ac0c6bdd 221 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
72208f03 222 if (!clock_value) {
55595636
MD
223 printf_error("Failed to retrieve clock value");
224 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
72208f03 225 goto end;
44d3cbf0 226 }
72208f03
JG
227
228 clock_ret = bt_ctf_clock_value_get_value_ns_from_epoch(
229 clock_value, &ts);
230 if (clock_ret) {
55595636
MD
231 printf_error("Failed to retrieve clock value timestamp");
232 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
44d3cbf0
JG
233 goto end;
234 }
55595636
MD
235 if (update_lazy_bound(begin, "begin", ts, &lazy_update)) {
236 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
528debdf
MD
237 goto end;
238 }
55595636
MD
239 if (update_lazy_bound(end, "end", ts, &lazy_update)) {
240 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
528debdf
MD
241 goto end;
242 }
55595636
MD
243 if (lazy_update && begin->set && end->set) {
244 if (begin->value > end->value) {
245 printf_error("Unexpected: time range begin value is above end value");
246 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
247 goto end;
248 }
249 }
72208f03
JG
250 if (begin->set && ts < begin->value) {
251 in_range = false;
252 }
253 if (end->set && ts > end->value) {
254 in_range = false;
255 }
44d3cbf0 256end:
72208f03 257 bt_put(event);
ac0c6bdd 258 bt_put(clock_class);
72208f03
JG
259 bt_put(trace);
260 bt_put(stream);
261 bt_put(stream_class);
262 bt_put(clock_value);
55595636
MD
263 *_event_in_range = in_range;
264 return ret;
44d3cbf0
JG
265}
266
44d3cbf0 267static
72208f03 268int ns_from_integer_field(struct bt_ctf_field *integer, int64_t *ns)
44d3cbf0 269{
72208f03
JG
270 int ret = 0;
271 int is_signed;
272 uint64_t raw_clock_value;
273 struct bt_ctf_field_type *integer_type = NULL;
ac0c6bdd 274 struct bt_ctf_clock_class *clock_class = NULL;
44d3cbf0
JG
275 struct bt_ctf_clock_value *clock_value = NULL;
276
72208f03
JG
277 integer_type = bt_ctf_field_get_type(integer);
278 assert(integer_type);
ac0c6bdd
PP
279 clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(
280 integer_type);
281 if (!clock_class) {
72208f03 282 ret = -1;
44d3cbf0
JG
283 goto end;
284 }
285
72208f03
JG
286 is_signed = bt_ctf_field_type_integer_get_signed(integer_type);
287 if (!is_signed) {
288 ret = bt_ctf_field_unsigned_integer_get_value(integer,
289 &raw_clock_value);
290 if (ret) {
44d3cbf0
JG
291 goto end;
292 }
72208f03
JG
293 } else {
294 /* Signed clock values are unsupported. */
295 goto end;
296 }
44d3cbf0 297
ac0c6bdd 298 clock_value = bt_ctf_clock_value_create(clock_class, raw_clock_value);
72208f03
JG
299 if (!clock_value) {
300 goto end;
301 }
44d3cbf0 302
72208f03
JG
303 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns);
304end:
305 bt_put(integer_type);
ac0c6bdd 306 bt_put(clock_class);
72208f03
JG
307 bt_put(clock_value);
308 return ret;
309}
44d3cbf0 310
72208f03 311static
55595636
MD
312enum bt_notification_iterator_status evaluate_packet_notification(
313 struct bt_notification *notification,
314 struct trimmer_bound *begin, struct trimmer_bound *end,
315 bool *_packet_in_range)
72208f03 316{
55595636
MD
317 enum bt_notification_iterator_status ret =
318 BT_NOTIFICATION_ITERATOR_STATUS_OK;
72208f03
JG
319 int64_t begin_ns, pkt_begin_ns, end_ns, pkt_end_ns;
320 bool in_range = true;
321 struct bt_ctf_packet *packet = NULL;
322 struct bt_ctf_field *packet_context = NULL,
323 *timestamp_begin = NULL,
324 *timestamp_end = NULL;
325
326 switch (bt_notification_get_type(notification)) {
327 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
328 packet = bt_notification_packet_begin_get_packet(notification);
44d3cbf0 329 break;
72208f03
JG
330 case BT_NOTIFICATION_TYPE_PACKET_END:
331 packet = bt_notification_packet_end_get_packet(notification);
332 break;
333 default:
55595636 334 break;
44d3cbf0 335 }
72208f03
JG
336 assert(packet);
337
338 packet_context = bt_ctf_packet_get_context(packet);
339 if (!packet_context) {
340 goto end;
341 }
342
343 if (!bt_ctf_field_is_structure(packet_context)) {
344 goto end;
345 }
346
347 timestamp_begin = bt_ctf_field_structure_get_field(
348 packet_context, "timestamp_begin");
349 if (!timestamp_begin || !bt_ctf_field_is_integer(timestamp_begin)) {
350 goto end;
351 }
352 timestamp_end = bt_ctf_field_structure_get_field(
353 packet_context, "timestamp_end");
354 if (!timestamp_end || !bt_ctf_field_is_integer(timestamp_end)) {
355 goto end;
356 }
357
55595636 358 if (ns_from_integer_field(timestamp_begin, &pkt_begin_ns)) {
72208f03
JG
359 goto end;
360 }
55595636 361 if (ns_from_integer_field(timestamp_end, &pkt_end_ns)) {
72208f03
JG
362 goto end;
363 }
364
365 begin_ns = begin->set ? begin->value : INT64_MIN;
366 end_ns = end->set ? end->value : INT64_MAX;
367
368 /*
369 * Accept if there is any overlap between the selected region and the
370 * packet.
371 */
372 in_range = (pkt_end_ns >= begin_ns) && (pkt_begin_ns <= end_ns);
373end:
55595636 374 *_packet_in_range = in_range;
72208f03
JG
375 bt_put(packet);
376 bt_put(packet_context);
377 bt_put(timestamp_begin);
378 bt_put(timestamp_end);
55595636 379 return ret;
72208f03
JG
380}
381
382/* Return true if the notification should be forwarded. */
383static
55595636
MD
384enum bt_notification_iterator_status evaluate_notification(
385 struct bt_notification *notification,
386 struct trimmer_bound *begin, struct trimmer_bound *end,
387 bool *in_range)
72208f03 388{
72208f03 389 enum bt_notification_type type;
55595636
MD
390 enum bt_notification_iterator_status ret =
391 BT_NOTIFICATION_ITERATOR_STATUS_OK;
72208f03 392
b61397c4 393 *in_range = true;
72208f03
JG
394 type = bt_notification_get_type(notification);
395 switch (type) {
396 case BT_NOTIFICATION_TYPE_EVENT:
55595636
MD
397 ret = evaluate_event_notification(notification, begin,
398 end, in_range);
72208f03
JG
399 break;
400 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
401 case BT_NOTIFICATION_TYPE_PACKET_END:
55595636
MD
402 ret = evaluate_packet_notification(notification, begin,
403 end, in_range);
72208f03 404 break;
44d3cbf0 405 default:
72208f03 406 /* Accept all other notifications. */
44d3cbf0
JG
407 break;
408 }
55595636 409 return ret;
cab3f160
JG
410}
411
412BT_HIDDEN
413enum bt_notification_iterator_status trimmer_iterator_next(
414 struct bt_notification_iterator *iterator)
415{
44d3cbf0
JG
416 struct trimmer_iterator *trim_it = NULL;
417 struct bt_component *component = NULL;
418 struct trimmer *trimmer = NULL;
419 struct bt_notification_iterator *source_it = NULL;
420 enum bt_notification_iterator_status ret =
55595636 421 BT_NOTIFICATION_ITERATOR_STATUS_OK;
72208f03 422 bool notification_in_range = false;
cab3f160 423
44d3cbf0
JG
424 trim_it = bt_notification_iterator_get_private_data(iterator);
425 assert(trim_it);
426
427 component = bt_notification_iterator_get_component(iterator);
428 assert(component);
429 trimmer = bt_component_get_private_data(component);
430 assert(trimmer);
431
99cdb69e
JG
432 source_it = trim_it->input_iterator;
433 assert(source_it);
44d3cbf0 434
72208f03 435 while (!notification_in_range) {
44d3cbf0
JG
436 struct bt_notification *notification;
437
438 ret = bt_notification_iterator_next(source_it);
439 if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
440 goto end;
441 }
442
443 notification = bt_notification_iterator_get_notification(
444 source_it);
445 if (!notification) {
446 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
447 goto end;
448 }
449
55595636
MD
450 ret = evaluate_notification(notification,
451 &trimmer->begin, &trimmer->end,
452 &notification_in_range);
72208f03 453 if (notification_in_range) {
44d3cbf0
JG
454 BT_MOVE(trim_it->current_notification, notification);
455 } else {
456 bt_put(notification);
457 }
6d5f6792
JG
458
459 if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
460 break;
461 }
44d3cbf0 462 }
cab3f160 463end:
44d3cbf0 464 bt_put(component);
cab3f160
JG
465 return ret;
466}
467
468BT_HIDDEN
469enum bt_notification_iterator_status trimmer_iterator_seek_time(
470 struct bt_notification_iterator *iterator, int64_t time)
471{
472 enum bt_notification_iterator_status ret;
473
474 ret = BT_NOTIFICATION_ITERATOR_STATUS_OK;
475end:
476 return ret;
477}
This page took 0.051661 seconds and 4 git commands to generate.