lib: append `_FUNC` to `BT_PLUGIN_{INITIALIZE,FINALIZE}*`
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace_collection_message_iterator.py
CommitLineData
d34e69cf
PP
1# The MIT License (MIT)
2#
3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21# THE SOFTWARE.
22
94e72386 23from bt2 import utils, native_bt
d34e69cf 24import bt2
88fdcc33 25import itertools
c946c9de 26from bt2 import message_iterator as bt2_message_iterator
c946c9de 27from bt2 import port as bt2_port
c535b26d 28from bt2 import component as bt2_component
94e72386
SM
29from bt2 import value as bt2_value
30from bt2 import plugin as bt2_plugin
d34e69cf 31import datetime
d34e69cf
PP
32from collections import namedtuple
33import numbers
34
35
88fdcc33
PP
36# a pair of component and ComponentSpec
37_ComponentAndSpec = namedtuple('_ComponentAndSpec', ['comp', 'spec'])
d34e69cf
PP
38
39
94e72386 40class _BaseComponentSpec:
3bd6bc48
SM
41 # Base for any component spec that can be passed to
42 # TraceCollectionMessageIterator.
94e72386
SM
43 def __init__(self, params, obj, logging_level):
44 if logging_level is not None:
45 utils._check_log_level(logging_level)
46
47 self._params = bt2.create_value(params)
48 self._obj = obj
49 self._logging_level = logging_level
50
51 @property
52 def params(self):
53 return self._params
54
55 @property
56 def obj(self):
57 return self._obj
58
59 @property
60 def logging_level(self):
61 return self._logging_level
62
63
64class ComponentSpec(_BaseComponentSpec):
3bd6bc48 65 # A component spec with a specific component class.
61d96b89
FD
66 def __init__(
67 self,
3bd6bc48 68 component_class,
61d96b89 69 params=None,
b20382e2 70 obj=None,
3bd6bc48 71 logging_level=bt2.LoggingLevel.NONE,
61d96b89 72 ):
94e72386
SM
73 if type(params) is str:
74 params = {'inputs': [params]}
75
76 super().__init__(params, obj, logging_level)
77
3bd6bc48 78 is_cc_object = isinstance(
5265f5e3
FD
79 component_class,
80 (bt2._SourceComponentClassConst, bt2._FilterComponentClassConst),
3bd6bc48
SM
81 )
82 is_user_cc_type = isinstance(
83 component_class, bt2_component._UserComponentType
84 ) and issubclass(
85 component_class, (bt2._UserSourceComponent, bt2._UserFilterComponent)
86 )
94e72386 87
3bd6bc48
SM
88 if not is_cc_object and not is_user_cc_type:
89 raise TypeError(
90 "'{}' is not a source or filter component class".format(
91 component_class.__class__.__name__
92 )
93 )
d34e69cf 94
3bd6bc48 95 self._component_class = component_class
d34e69cf
PP
96
97 @property
3bd6bc48
SM
98 def component_class(self):
99 return self._component_class
100
101 @classmethod
102 def from_named_plugin_and_component_class(
103 cls,
104 plugin_name,
105 component_class_name,
106 params=None,
107 obj=None,
108 logging_level=bt2.LoggingLevel.NONE,
109 ):
110 plugin = bt2.find_plugin(plugin_name)
111
112 if plugin is None:
113 raise ValueError('no such plugin: {}'.format(plugin_name))
114
115 if component_class_name in plugin.source_component_classes:
116 comp_class = plugin.source_component_classes[component_class_name]
117 elif component_class_name in plugin.filter_component_classes:
118 comp_class = plugin.filter_component_classes[component_class_name]
119 else:
120 raise KeyError(
121 'source or filter component class `{}` not found in plugin `{}`'.format(
122 component_class_name, plugin_name
123 )
124 )
125
126 return cls(comp_class, params, obj, logging_level)
d34e69cf 127
cc81b5ab 128
94e72386 129class AutoSourceComponentSpec(_BaseComponentSpec):
3bd6bc48 130 # A component spec that does automatic source discovery.
94e72386
SM
131 _no_obj = object()
132
133 def __init__(self, input, params=None, obj=_no_obj, logging_level=None):
134 super().__init__(params, obj, logging_level)
135 self._input = input
d34e69cf 136
b20382e2 137 @property
94e72386
SM
138 def input(self):
139 return self._input
140
141
142def _auto_discover_source_component_specs(auto_source_comp_specs, plugin_set):
143 # Transform a list of `AutoSourceComponentSpec` in a list of `ComponentSpec`
144 # using the automatic source discovery mechanism.
145 inputs = bt2.ArrayValue([spec.input for spec in auto_source_comp_specs])
146
147 if plugin_set is None:
148 plugin_set = bt2.find_plugins()
149 else:
150 utils._check_type(plugin_set, bt2_plugin._PluginSet)
151
152 res_ptr = native_bt.bt2_auto_discover_source_components(
153 inputs._ptr, plugin_set._ptr
154 )
155
156 if res_ptr is None:
157 raise bt2._MemoryError('cannot auto discover source components')
158
159 res = bt2_value._create_from_ptr(res_ptr)
160
161 assert type(res) == bt2.MapValue
162 assert 'status' in res
163
164 status = res['status']
165 utils._handle_func_status(status, 'cannot auto-discover source components')
166
167 comp_specs = []
168 comp_specs_raw = res['results']
169 assert type(comp_specs_raw) == bt2.ArrayValue
170
5f7f0be0
SM
171 used_input_indices = set()
172
94e72386
SM
173 for comp_spec_raw in comp_specs_raw:
174 assert type(comp_spec_raw) == bt2.ArrayValue
175 assert len(comp_spec_raw) == 4
176
177 plugin_name = comp_spec_raw[0]
178 assert type(plugin_name) == bt2.StringValue
179 plugin_name = str(plugin_name)
180
181 class_name = comp_spec_raw[1]
182 assert type(class_name) == bt2.StringValue
183 class_name = str(class_name)
184
185 comp_inputs = comp_spec_raw[2]
186 assert type(comp_inputs) == bt2.ArrayValue
187
188 comp_orig_indices = comp_spec_raw[3]
189 assert type(comp_orig_indices)
190
191 params = bt2.MapValue()
192 logging_level = bt2.LoggingLevel.NONE
193 obj = None
194
195 # Compute `params` for this component by piling up params given to all
196 # AutoSourceComponentSpec objects that contributed in the instantiation
197 # of this component.
198 #
199 # The effective log level for a component is the last one specified
200 # across the AutoSourceComponentSpec that contributed in its
201 # instantiation.
202 for idx in comp_orig_indices:
203 orig_spec = auto_source_comp_specs[idx]
204
205 if orig_spec.params is not None:
206 params.update(orig_spec.params)
207
208 if orig_spec.logging_level is not None:
209 logging_level = orig_spec.logging_level
210
211 if orig_spec.obj is not AutoSourceComponentSpec._no_obj:
212 obj = orig_spec.obj
213
5f7f0be0
SM
214 used_input_indices.add(int(idx))
215
94e72386
SM
216 params['inputs'] = comp_inputs
217
218 comp_specs.append(
3bd6bc48 219 ComponentSpec.from_named_plugin_and_component_class(
94e72386
SM
220 plugin_name,
221 class_name,
222 params=params,
223 obj=obj,
224 logging_level=logging_level,
225 )
226 )
227
5f7f0be0
SM
228 if len(used_input_indices) != len(inputs):
229 unused_input_indices = set(range(len(inputs))) - used_input_indices
230 unused_input_indices = sorted(unused_input_indices)
231 unused_inputs = [str(inputs[x]) for x in unused_input_indices]
232
233 msg = (
234 'Some auto source component specs did not produce any component: '
235 + ', '.join(unused_inputs)
236 )
237 raise RuntimeError(msg)
238
94e72386 239 return comp_specs
b20382e2 240
d34e69cf
PP
241
242# datetime.datetime or integral to nanoseconds
243def _get_ns(obj):
244 if obj is None:
245 return
246
247 if isinstance(obj, numbers.Real):
248 # consider that it's already in seconds
249 s = obj
250 elif isinstance(obj, datetime.datetime):
251 # s -> ns
252 s = obj.timestamp()
253 else:
61d96b89
FD
254 raise TypeError(
255 '"{}" is not an integral number or a datetime.datetime object'.format(obj)
256 )
d34e69cf
PP
257
258 return int(s * 1e9)
259
260
c535b26d 261class _TraceCollectionMessageIteratorProxySink(bt2_component._UserSinkComponent):
e3250e61 262 def __init__(self, config, params, msg_list):
c535b26d
PP
263 assert type(msg_list) is list
264 self._msg_list = msg_list
265 self._add_input_port('in')
266
267 def _user_graph_is_configured(self):
268 self._msg_iter = self._create_input_port_message_iterator(
269 self._input_ports['in']
270 )
271
272 def _user_consume(self):
273 assert self._msg_list[0] is None
274 self._msg_list[0] = next(self._msg_iter)
275
276
c946c9de 277class TraceCollectionMessageIterator(bt2_message_iterator._MessageIterator):
61d96b89
FD
278 def __init__(
279 self,
280 source_component_specs,
281 filter_component_specs=None,
282 stream_intersection_mode=False,
283 begin=None,
284 end=None,
94e72386 285 plugin_set=None,
61d96b89 286 ):
d34e69cf
PP
287 utils._check_bool(stream_intersection_mode)
288 self._stream_intersection_mode = stream_intersection_mode
289 self._begin_ns = _get_ns(begin)
290 self._end_ns = _get_ns(end)
c535b26d 291 self._msg_list = [None]
88fdcc33 292
94e72386
SM
293 # If a single item is provided, convert to a list.
294 if type(source_component_specs) in (
295 ComponentSpec,
296 AutoSourceComponentSpec,
297 str,
298 ):
88fdcc33
PP
299 source_component_specs = [source_component_specs]
300
94e72386
SM
301 # Convert any string to an AutoSourceComponentSpec.
302 def str_to_auto(item):
303 if type(item) is str:
304 item = AutoSourceComponentSpec(item)
305
306 return item
307
308 source_component_specs = [str_to_auto(s) for s in source_component_specs]
309
88fdcc33
PP
310 if type(filter_component_specs) is ComponentSpec:
311 filter_component_specs = [filter_component_specs]
312 elif filter_component_specs is None:
313 filter_component_specs = []
314
94e72386
SM
315 self._validate_source_component_specs(source_component_specs)
316 self._validate_filter_component_specs(filter_component_specs)
317
318 # Pass any `ComponentSpec` instance as-is.
319 self._src_comp_specs = [
320 spec for spec in source_component_specs if type(spec) is ComponentSpec
321 ]
322
323 # Convert any `AutoSourceComponentSpec` in concrete `ComponentSpec` instances.
324 auto_src_comp_specs = [
325 spec
326 for spec in source_component_specs
327 if type(spec) is AutoSourceComponentSpec
328 ]
329 self._src_comp_specs += _auto_discover_source_component_specs(
330 auto_src_comp_specs, plugin_set
331 )
332
88fdcc33 333 self._flt_comp_specs = filter_component_specs
d34e69cf
PP
334 self._next_suffix = 1
335 self._connect_ports = False
336
88fdcc33 337 # lists of _ComponentAndSpec
d34e69cf 338 self._src_comps_and_specs = []
88fdcc33 339 self._flt_comps_and_specs = []
d34e69cf 340
d34e69cf
PP
341 self._build_graph()
342
2532cf78
SM
343 def _compute_stream_intersections(self):
344 # Pre-compute the trimmer range to use for each port in the graph, when
345 # stream intersection mode is enabled.
346 self._stream_inter_port_to_range = {}
347
348 for src_comp_and_spec in self._src_comps_and_specs:
9db4399f 349 # Query the port's component for the `babeltrace.trace-infos`
2532cf78
SM
350 # object which contains the range for each stream, from which we can
351 # compute the intersection of the streams in each trace.
352 query_exec = bt2.QueryExecutor(
8762085c 353 src_comp_and_spec.spec.component_class,
9db4399f 354 'babeltrace.trace-infos',
8762085c 355 src_comp_and_spec.spec.params,
2532cf78
SM
356 )
357 trace_infos = query_exec.query()
358
359 for trace_info in trace_infos:
360 begin = max(
9db4399f
SM
361 [
362 stream['range-ns']['begin']
363 for stream in trace_info['stream-infos']
364 ]
2532cf78
SM
365 )
366 end = min(
9db4399f 367 [stream['range-ns']['end'] for stream in trace_info['stream-infos']]
2532cf78
SM
368 )
369
370 # Each port associated to this trace will have this computed
371 # range.
9db4399f 372 for stream in trace_info['stream-infos']:
2532cf78
SM
373 # A port name is unique within a component, but not
374 # necessarily across all components. Use a component
375 # and port name pair to make it unique across the graph.
376 port_name = str(stream['port-name'])
377 key = (src_comp_and_spec.comp.addr, port_name)
378 self._stream_inter_port_to_range[key] = (begin, end)
379
94e72386
SM
380 def _validate_source_component_specs(self, comp_specs):
381 for comp_spec in comp_specs:
382 if (
383 type(comp_spec) is not ComponentSpec
384 and type(comp_spec) is not AutoSourceComponentSpec
385 ):
386 raise TypeError(
387 '"{}" object is not a ComponentSpec or AutoSourceComponentSpec'.format(
388 type(comp_spec)
389 )
390 )
391
392 def _validate_filter_component_specs(self, comp_specs):
88fdcc33
PP
393 for comp_spec in comp_specs:
394 if type(comp_spec) is not ComponentSpec:
61d96b89
FD
395 raise TypeError(
396 '"{}" object is not a ComponentSpec'.format(type(comp_spec))
397 )
d34e69cf
PP
398
399 def __next__(self):
c535b26d
PP
400 assert self._msg_list[0] is None
401 self._graph.run_once()
402 msg = self._msg_list[0]
403 assert msg is not None
404 self._msg_list[0] = None
405 return msg
d34e69cf 406
da35796c 407 def _create_stream_intersection_trimmer(self, component, port):
2532cf78
SM
408 key = (component.addr, port.name)
409 begin, end = self._stream_inter_port_to_range[key]
410 name = 'trimmer-{}-{}'.format(component.name, port.name)
d34e69cf
PP
411 return self._create_trimmer(begin, end, name)
412
413 def _create_muxer(self):
414 plugin = bt2.find_plugin('utils')
415
416 if plugin is None:
3b2be708 417 raise RuntimeError('cannot find "utils" plugin (needed for the muxer)')
d34e69cf
PP
418
419 if 'muxer' not in plugin.filter_component_classes:
3b2be708 420 raise RuntimeError(
61d96b89
FD
421 'cannot find "muxer" filter component class in "utils" plugin'
422 )
d34e69cf
PP
423
424 comp_cls = plugin.filter_component_classes['muxer']
425 return self._graph.add_component(comp_cls, 'muxer')
426
da35796c 427 def _create_trimmer(self, begin_ns, end_ns, name):
d34e69cf
PP
428 plugin = bt2.find_plugin('utils')
429
430 if plugin is None:
3b2be708 431 raise RuntimeError('cannot find "utils" plugin (needed for the trimmer)')
d34e69cf
PP
432
433 if 'trimmer' not in plugin.filter_component_classes:
3b2be708 434 raise RuntimeError(
61d96b89
FD
435 'cannot find "trimmer" filter component class in "utils" plugin'
436 )
d34e69cf
PP
437
438 params = {}
439
da35796c
SM
440 def ns_to_string(ns):
441 s_part = ns // 1000000000
442 ns_part = ns % 1000000000
443 return '{}.{:09d}'.format(s_part, ns_part)
d34e69cf 444
da35796c
SM
445 if begin_ns is not None:
446 params['begin'] = ns_to_string(begin_ns)
447
448 if end_ns is not None:
449 params['end'] = ns_to_string(end_ns)
d34e69cf
PP
450
451 comp_cls = plugin.filter_component_classes['trimmer']
452 return self._graph.add_component(comp_cls, name, params)
453
3bd6bc48
SM
454 def _get_unique_comp_name(self, comp_cls):
455 name = comp_cls.name
61d96b89
FD
456 comps_and_specs = itertools.chain(
457 self._src_comps_and_specs, self._flt_comps_and_specs
458 )
d34e69cf 459
88fdcc33 460 if name in [comp_and_spec.comp.name for comp_and_spec in comps_and_specs]:
d34e69cf
PP
461 name += '-{}'.format(self._next_suffix)
462 self._next_suffix += 1
463
464 return name
465
3bd6bc48
SM
466 def _create_comp(self, comp_spec):
467 comp_cls = comp_spec.component_class
468 name = self._get_unique_comp_name(comp_cls)
61d96b89 469 comp = self._graph.add_component(
b20382e2 470 comp_cls, name, comp_spec.params, comp_spec.obj, comp_spec.logging_level
61d96b89 471 )
d34e69cf
PP
472 return comp
473
474 def _get_free_muxer_input_port(self):
475 for port in self._muxer_comp.input_ports.values():
476 if not port.is_connected:
477 return port
478
da35796c 479 def _connect_src_comp_port(self, component, port):
d34e69cf
PP
480 # if this trace collection iterator is in stream intersection
481 # mode, we need this connection:
482 #
483 # port -> trimmer -> muxer
484 #
485 # otherwise, simply:
486 #
487 # port -> muxer
488 if self._stream_intersection_mode:
da35796c 489 trimmer_comp = self._create_stream_intersection_trimmer(component, port)
d34e69cf
PP
490 self._graph.connect_ports(port, trimmer_comp.input_ports['in'])
491 port_to_muxer = trimmer_comp.output_ports['out']
492 else:
493 port_to_muxer = port
494
495 self._graph.connect_ports(port_to_muxer, self._get_free_muxer_input_port())
496
da35796c 497 def _graph_port_added(self, component, port):
d34e69cf
PP
498 if not self._connect_ports:
499 return
500
49e6b55c 501 if type(port) is bt2_port._InputPortConst:
d34e69cf
PP
502 return
503
da35796c 504 if component not in [comp.comp for comp in self._src_comps_and_specs]:
d34e69cf
PP
505 # do not care about non-source components (muxer, trimmer, etc.)
506 return
507
da35796c 508 self._connect_src_comp_port(component, port)
d34e69cf 509
ad400bbc 510 def _get_greatest_operative_mip_version(self):
3bd6bc48 511 def append_comp_specs_descriptors(descriptors, comp_specs):
ad400bbc 512 for comp_spec in comp_specs:
ad400bbc 513 descriptors.append(
3bd6bc48
SM
514 bt2.ComponentDescriptor(
515 comp_spec.component_class, comp_spec.params, comp_spec.obj
516 )
ad400bbc
PP
517 )
518
519 descriptors = []
3bd6bc48
SM
520 append_comp_specs_descriptors(descriptors, self._src_comp_specs)
521 append_comp_specs_descriptors(descriptors, self._flt_comp_specs)
ad400bbc
PP
522
523 if self._stream_intersection_mode:
524 # we also need at least one `flt.utils.trimmer` component
3bd6bc48
SM
525 comp_spec = ComponentSpec.from_named_plugin_and_component_class(
526 'utils', 'trimmer'
527 )
528 append_comp_specs_descriptors(descriptors, [comp_spec])
ad400bbc
PP
529
530 mip_version = bt2.get_greatest_operative_mip_version(descriptors)
531
532 if mip_version is None:
533 msg = 'failed to find an operative message interchange protocol version (components are not interoperable)'
534 raise RuntimeError(msg)
535
536 return mip_version
537
d34e69cf 538 def _build_graph(self):
ad400bbc 539 self._graph = bt2.Graph(self._get_greatest_operative_mip_version())
da35796c 540 self._graph.add_port_added_listener(self._graph_port_added)
d34e69cf
PP
541 self._muxer_comp = self._create_muxer()
542
543 if self._begin_ns is not None or self._end_ns is not None:
61d96b89
FD
544 trimmer_comp = self._create_trimmer(self._begin_ns, self._end_ns, 'trimmer')
545 self._graph.connect_ports(
546 self._muxer_comp.output_ports['out'], trimmer_comp.input_ports['in']
547 )
c535b26d 548 last_flt_out_port = trimmer_comp.output_ports['out']
d34e69cf 549 else:
c535b26d 550 last_flt_out_port = self._muxer_comp.output_ports['out']
d34e69cf 551
88fdcc33
PP
552 # create extra filter components (chained)
553 for comp_spec in self._flt_comp_specs:
3bd6bc48 554 comp = self._create_comp(comp_spec)
88fdcc33
PP
555 self._flt_comps_and_specs.append(_ComponentAndSpec(comp, comp_spec))
556
557 # connect the extra filter chain
558 for comp_and_spec in self._flt_comps_and_specs:
559 in_port = list(comp_and_spec.comp.input_ports.values())[0]
560 out_port = list(comp_and_spec.comp.output_ports.values())[0]
c535b26d
PP
561 self._graph.connect_ports(last_flt_out_port, in_port)
562 last_flt_out_port = out_port
88fdcc33 563
d34e69cf
PP
564 # Here we create the components, self._graph_port_added() is
565 # called when they add ports, but the callback returns early
566 # because self._connect_ports is False. This is because the
567 # self._graph_port_added() could not find the associated source
568 # component specification in self._src_comps_and_specs because
569 # it does not exist yet (it needs the created component to
570 # exist).
571 for comp_spec in self._src_comp_specs:
3bd6bc48 572 comp = self._create_comp(comp_spec)
88fdcc33 573 self._src_comps_and_specs.append(_ComponentAndSpec(comp, comp_spec))
d34e69cf 574
2532cf78
SM
575 if self._stream_intersection_mode:
576 self._compute_stream_intersections()
577
d34e69cf
PP
578 # Now we connect the ports which exist at this point. We allow
579 # self._graph_port_added() to automatically connect _new_ ports.
580 self._connect_ports = True
581
582 for comp_and_spec in self._src_comps_and_specs:
583 # Keep a separate list because comp_and_spec.output_ports
584 # could change during the connection of one of its ports.
585 # Any new port is handled by self._graph_port_added().
586 out_ports = [port for port in comp_and_spec.comp.output_ports.values()]
587
588 for out_port in out_ports:
da35796c 589 if out_port.is_connected:
d34e69cf
PP
590 continue
591
da35796c 592 self._connect_src_comp_port(comp_and_spec.comp, out_port)
d34e69cf 593
c535b26d
PP
594 # Add the proxy sink, passing our message list to share consumed
595 # messages with this trace collection message iterator.
596 sink = self._graph.add_component(
597 _TraceCollectionMessageIteratorProxySink, 'proxy-sink', obj=self._msg_list
598 )
599 sink_in_port = sink.input_ports['in']
600
601 # connect last filter to proxy sink
602 self._graph.connect_ports(last_flt_out_port, sink_in_port)
This page took 0.077025 seconds and 4 git commands to generate.