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