tests: move auto source discovery test artifacts
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace_collection_message_iterator.py
CommitLineData
85dcce24
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
23from bt2 import utils
24import bt2
3d60267b 25import itertools
3fb99a22
PP
26from bt2 import message_iterator as bt2_message_iterator
27from bt2 import logging as bt2_logging
28from bt2 import port as bt2_port
c1859f69 29from bt2 import component as bt2_component
85dcce24 30import datetime
85dcce24
PP
31from collections import namedtuple
32import numbers
33
34
3d60267b
PP
35# a pair of component and ComponentSpec
36_ComponentAndSpec = namedtuple('_ComponentAndSpec', ['comp', 'spec'])
85dcce24
PP
37
38
3d60267b 39class ComponentSpec:
cfbd7cf3
FD
40 def __init__(
41 self,
42 plugin_name,
43 class_name,
44 params=None,
66964f3f 45 obj=None,
3fb99a22 46 logging_level=bt2_logging.LoggingLevel.NONE,
cfbd7cf3 47 ):
85dcce24 48 utils._check_str(plugin_name)
e8ac1aae 49 utils._check_str(class_name)
e874da19 50 utils._check_log_level(logging_level)
85dcce24 51 self._plugin_name = plugin_name
e8ac1aae 52 self._class_name = class_name
e874da19 53 self._logging_level = logging_level
66964f3f 54 self._obj = obj
85dcce24
PP
55
56 if type(params) is str:
73760435 57 self._params = bt2.create_value({'inputs': [params]})
85dcce24
PP
58 else:
59 self._params = bt2.create_value(params)
60
61 @property
62 def plugin_name(self):
63 return self._plugin_name
64
65 @property
e8ac1aae
PP
66 def class_name(self):
67 return self._class_name
85dcce24 68
e874da19
PP
69 @property
70 def logging_level(self):
71 return self._logging_level
72
85dcce24
PP
73 @property
74 def params(self):
75 return self._params
76
66964f3f
PP
77 @property
78 def obj(self):
79 return self._obj
80
85dcce24
PP
81
82# datetime.datetime or integral to nanoseconds
83def _get_ns(obj):
84 if obj is None:
85 return
86
87 if isinstance(obj, numbers.Real):
88 # consider that it's already in seconds
89 s = obj
90 elif isinstance(obj, datetime.datetime):
91 # s -> ns
92 s = obj.timestamp()
93 else:
cfbd7cf3
FD
94 raise TypeError(
95 '"{}" is not an integral number or a datetime.datetime object'.format(obj)
96 )
85dcce24
PP
97
98 return int(s * 1e9)
99
100
3d60267b
PP
101class _CompClsType:
102 SOURCE = 0
103 FILTER = 1
104
105
c1859f69
PP
106class _TraceCollectionMessageIteratorProxySink(bt2_component._UserSinkComponent):
107 def __init__(self, params, msg_list):
108 assert type(msg_list) is list
109 self._msg_list = msg_list
110 self._add_input_port('in')
111
112 def _user_graph_is_configured(self):
113 self._msg_iter = self._create_input_port_message_iterator(
114 self._input_ports['in']
115 )
116
117 def _user_consume(self):
118 assert self._msg_list[0] is None
119 self._msg_list[0] = next(self._msg_iter)
120
121
3fb99a22 122class TraceCollectionMessageIterator(bt2_message_iterator._MessageIterator):
cfbd7cf3
FD
123 def __init__(
124 self,
125 source_component_specs,
126 filter_component_specs=None,
127 stream_intersection_mode=False,
128 begin=None,
129 end=None,
130 ):
85dcce24
PP
131 utils._check_bool(stream_intersection_mode)
132 self._stream_intersection_mode = stream_intersection_mode
133 self._begin_ns = _get_ns(begin)
134 self._end_ns = _get_ns(end)
c1859f69 135 self._msg_list = [None]
3d60267b
PP
136
137 if type(source_component_specs) is ComponentSpec:
138 source_component_specs = [source_component_specs]
139
140 if type(filter_component_specs) is ComponentSpec:
141 filter_component_specs = [filter_component_specs]
142 elif filter_component_specs is None:
143 filter_component_specs = []
144
85dcce24 145 self._src_comp_specs = source_component_specs
3d60267b 146 self._flt_comp_specs = filter_component_specs
85dcce24
PP
147 self._next_suffix = 1
148 self._connect_ports = False
149
3d60267b 150 # lists of _ComponentAndSpec
85dcce24 151 self._src_comps_and_specs = []
3d60267b 152 self._flt_comps_and_specs = []
85dcce24 153
3d60267b
PP
154 self._validate_component_specs(source_component_specs)
155 self._validate_component_specs(filter_component_specs)
85dcce24
PP
156 self._build_graph()
157
3d60267b
PP
158 def _validate_component_specs(self, comp_specs):
159 for comp_spec in comp_specs:
160 if type(comp_spec) is not ComponentSpec:
cfbd7cf3
FD
161 raise TypeError(
162 '"{}" object is not a ComponentSpec'.format(type(comp_spec))
163 )
85dcce24
PP
164
165 def __next__(self):
c1859f69
PP
166 assert self._msg_list[0] is None
167 self._graph.run_once()
168 msg = self._msg_list[0]
169 assert msg is not None
170 self._msg_list[0] = None
171 return msg
85dcce24 172
907f2b70 173 def _create_stream_intersection_trimmer(self, component, port):
85dcce24 174 # find the original parameters specified by the user to create
73760435 175 # this port's component to get the `inputs` parameter
85dcce24 176 for src_comp_and_spec in self._src_comps_and_specs:
907f2b70 177 if component == src_comp_and_spec.comp:
85dcce24
PP
178 break
179
180 try:
73760435 181 inputs = src_comp_and_spec.spec.params['inputs']
907f2b70 182 except Exception as e:
ce4923b0 183 raise ValueError(
73760435 184 'all source components must be created with an "inputs" parameter in stream intersection mode'
cfbd7cf3 185 ) from e
85dcce24 186
73760435 187 params = {'inputs': inputs}
85dcce24 188
1a29b831
PP
189 # query the port's component for the `babeltrace.trace-info`
190 # object which contains the stream intersection range for each
191 # exposed trace
3c729b9a 192 query_exec = bt2.QueryExecutor(
1a29b831 193 src_comp_and_spec.comp.cls, 'babeltrace.trace-info', params
cfbd7cf3 194 )
3c729b9a 195 trace_info_res = query_exec.query()
85dcce24
PP
196 begin = None
197 end = None
198
a38d7650 199 # find the trace info for this port's trace
87b768aa
PP
200 try:
201 for trace_info in trace_info_res:
a38d7650
SM
202 for stream in trace_info['streams']:
203 if stream['port-name'] == port.name:
204 range_ns = trace_info['intersection-range-ns']
205 begin = range_ns['begin']
206 end = range_ns['end']
207 break
907f2b70 208 except Exception:
87b768aa 209 pass
85dcce24
PP
210
211 if begin is None or end is None:
ce4923b0 212 raise RuntimeError(
cfbd7cf3
FD
213 'cannot find stream intersection range for port "{}"'.format(port.name)
214 )
85dcce24 215
907f2b70 216 name = 'trimmer-{}-{}'.format(src_comp_and_spec.comp.name, port.name)
85dcce24
PP
217 return self._create_trimmer(begin, end, name)
218
219 def _create_muxer(self):
220 plugin = bt2.find_plugin('utils')
221
222 if plugin is None:
ce4923b0 223 raise RuntimeError('cannot find "utils" plugin (needed for the muxer)')
85dcce24
PP
224
225 if 'muxer' not in plugin.filter_component_classes:
ce4923b0 226 raise RuntimeError(
cfbd7cf3
FD
227 'cannot find "muxer" filter component class in "utils" plugin'
228 )
85dcce24
PP
229
230 comp_cls = plugin.filter_component_classes['muxer']
231 return self._graph.add_component(comp_cls, 'muxer')
232
907f2b70 233 def _create_trimmer(self, begin_ns, end_ns, name):
85dcce24
PP
234 plugin = bt2.find_plugin('utils')
235
236 if plugin is None:
ce4923b0 237 raise RuntimeError('cannot find "utils" plugin (needed for the trimmer)')
85dcce24
PP
238
239 if 'trimmer' not in plugin.filter_component_classes:
ce4923b0 240 raise RuntimeError(
cfbd7cf3
FD
241 'cannot find "trimmer" filter component class in "utils" plugin'
242 )
85dcce24
PP
243
244 params = {}
245
907f2b70
SM
246 def ns_to_string(ns):
247 s_part = ns // 1000000000
248 ns_part = ns % 1000000000
249 return '{}.{:09d}'.format(s_part, ns_part)
85dcce24 250
907f2b70
SM
251 if begin_ns is not None:
252 params['begin'] = ns_to_string(begin_ns)
253
254 if end_ns is not None:
255 params['end'] = ns_to_string(end_ns)
85dcce24
PP
256
257 comp_cls = plugin.filter_component_classes['trimmer']
258 return self._graph.add_component(comp_cls, name, params)
259
3d60267b 260 def _get_unique_comp_name(self, comp_spec):
cfbd7cf3
FD
261 name = '{}-{}'.format(comp_spec.plugin_name, comp_spec.class_name)
262 comps_and_specs = itertools.chain(
263 self._src_comps_and_specs, self._flt_comps_and_specs
264 )
85dcce24 265
3d60267b 266 if name in [comp_and_spec.comp.name for comp_and_spec in comps_and_specs]:
85dcce24
PP
267 name += '-{}'.format(self._next_suffix)
268 self._next_suffix += 1
269
270 return name
271
3d60267b 272 def _create_comp(self, comp_spec, comp_cls_type):
85dcce24
PP
273 plugin = bt2.find_plugin(comp_spec.plugin_name)
274
275 if plugin is None:
ce4923b0 276 raise ValueError('no such plugin: {}'.format(comp_spec.plugin_name))
85dcce24 277
3d60267b
PP
278 if comp_cls_type == _CompClsType.SOURCE:
279 comp_classes = plugin.source_component_classes
280 else:
281 comp_classes = plugin.filter_component_classes
282
e8ac1aae 283 if comp_spec.class_name not in comp_classes:
3d60267b 284 cc_type = 'source' if comp_cls_type == _CompClsType.SOURCE else 'filter'
ce4923b0 285 raise ValueError(
cfbd7cf3
FD
286 'no such {} component class in "{}" plugin: {}'.format(
287 cc_type, comp_spec.plugin_name, comp_spec.class_name
288 )
289 )
85dcce24 290
e8ac1aae 291 comp_cls = comp_classes[comp_spec.class_name]
3d60267b 292 name = self._get_unique_comp_name(comp_spec)
cfbd7cf3 293 comp = self._graph.add_component(
66964f3f 294 comp_cls, name, comp_spec.params, comp_spec.obj, comp_spec.logging_level
cfbd7cf3 295 )
85dcce24
PP
296 return comp
297
298 def _get_free_muxer_input_port(self):
299 for port in self._muxer_comp.input_ports.values():
300 if not port.is_connected:
301 return port
302
907f2b70 303 def _connect_src_comp_port(self, component, port):
85dcce24
PP
304 # if this trace collection iterator is in stream intersection
305 # mode, we need this connection:
306 #
307 # port -> trimmer -> muxer
308 #
309 # otherwise, simply:
310 #
311 # port -> muxer
312 if self._stream_intersection_mode:
907f2b70 313 trimmer_comp = self._create_stream_intersection_trimmer(component, port)
85dcce24
PP
314 self._graph.connect_ports(port, trimmer_comp.input_ports['in'])
315 port_to_muxer = trimmer_comp.output_ports['out']
316 else:
317 port_to_muxer = port
318
319 self._graph.connect_ports(port_to_muxer, self._get_free_muxer_input_port())
320
907f2b70 321 def _graph_port_added(self, component, port):
85dcce24
PP
322 if not self._connect_ports:
323 return
324
3fb99a22 325 if type(port) is bt2_port._InputPort:
85dcce24
PP
326 return
327
907f2b70 328 if component not in [comp.comp for comp in self._src_comps_and_specs]:
85dcce24
PP
329 # do not care about non-source components (muxer, trimmer, etc.)
330 return
331
907f2b70 332 self._connect_src_comp_port(component, port)
85dcce24
PP
333
334 def _build_graph(self):
335 self._graph = bt2.Graph()
907f2b70 336 self._graph.add_port_added_listener(self._graph_port_added)
85dcce24
PP
337 self._muxer_comp = self._create_muxer()
338
339 if self._begin_ns is not None or self._end_ns is not None:
cfbd7cf3
FD
340 trimmer_comp = self._create_trimmer(self._begin_ns, self._end_ns, 'trimmer')
341 self._graph.connect_ports(
342 self._muxer_comp.output_ports['out'], trimmer_comp.input_ports['in']
343 )
c1859f69 344 last_flt_out_port = trimmer_comp.output_ports['out']
85dcce24 345 else:
c1859f69 346 last_flt_out_port = self._muxer_comp.output_ports['out']
85dcce24 347
3d60267b
PP
348 # create extra filter components (chained)
349 for comp_spec in self._flt_comp_specs:
350 comp = self._create_comp(comp_spec, _CompClsType.FILTER)
351 self._flt_comps_and_specs.append(_ComponentAndSpec(comp, comp_spec))
352
353 # connect the extra filter chain
354 for comp_and_spec in self._flt_comps_and_specs:
355 in_port = list(comp_and_spec.comp.input_ports.values())[0]
356 out_port = list(comp_and_spec.comp.output_ports.values())[0]
c1859f69
PP
357 self._graph.connect_ports(last_flt_out_port, in_port)
358 last_flt_out_port = out_port
3d60267b 359
85dcce24
PP
360 # Here we create the components, self._graph_port_added() is
361 # called when they add ports, but the callback returns early
362 # because self._connect_ports is False. This is because the
363 # self._graph_port_added() could not find the associated source
364 # component specification in self._src_comps_and_specs because
365 # it does not exist yet (it needs the created component to
366 # exist).
367 for comp_spec in self._src_comp_specs:
3d60267b
PP
368 comp = self._create_comp(comp_spec, _CompClsType.SOURCE)
369 self._src_comps_and_specs.append(_ComponentAndSpec(comp, comp_spec))
85dcce24
PP
370
371 # Now we connect the ports which exist at this point. We allow
372 # self._graph_port_added() to automatically connect _new_ ports.
373 self._connect_ports = True
374
375 for comp_and_spec in self._src_comps_and_specs:
376 # Keep a separate list because comp_and_spec.output_ports
377 # could change during the connection of one of its ports.
378 # Any new port is handled by self._graph_port_added().
379 out_ports = [port for port in comp_and_spec.comp.output_ports.values()]
380
381 for out_port in out_ports:
907f2b70 382 if out_port.is_connected:
85dcce24
PP
383 continue
384
907f2b70 385 self._connect_src_comp_port(comp_and_spec.comp, out_port)
85dcce24 386
c1859f69
PP
387 # Add the proxy sink, passing our message list to share consumed
388 # messages with this trace collection message iterator.
389 sink = self._graph.add_component(
390 _TraceCollectionMessageIteratorProxySink, 'proxy-sink', obj=self._msg_list
391 )
392 sink_in_port = sink.input_ports['in']
393
394 # connect last filter to proxy sink
395 self._graph.connect_ports(last_flt_out_port, sink_in_port)
This page took 0.071906 seconds and 4 git commands to generate.