4e7361e7ee877e4df02b1e5e005f20c74df3cc25
1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
26 from bt2
import message_iterator
as bt2_message_iterator
27 from bt2
import logging
as bt2_logging
28 from bt2
import port
as bt2_port
30 from collections
import namedtuple
34 # a pair of component and ComponentSpec
35 _ComponentAndSpec
= namedtuple('_ComponentAndSpec', ['comp', 'spec'])
45 logging_level
=bt2_logging
.LoggingLevel
.NONE
,
47 utils
._check
_str
(plugin_name
)
48 utils
._check
_str
(class_name
)
49 utils
._check
_log
_level
(logging_level
)
50 self
._plugin
_name
= plugin_name
51 self
._class
_name
= class_name
52 self
._logging
_level
= logging_level
55 if type(params
) is str:
56 self
._params
= bt2
.create_value({'inputs': [params
]})
58 self
._params
= bt2
.create_value(params
)
61 def plugin_name(self
):
62 return self
._plugin
_name
66 return self
._class
_name
69 def logging_level(self
):
70 return self
._logging
_level
81 # datetime.datetime or integral to nanoseconds
86 if isinstance(obj
, numbers
.Real
):
87 # consider that it's already in seconds
89 elif isinstance(obj
, datetime
.datetime
):
94 '"{}" is not an integral number or a datetime.datetime object'.format(obj
)
105 class TraceCollectionMessageIterator(bt2_message_iterator
._MessageIterator
):
108 source_component_specs
,
109 filter_component_specs
=None,
110 stream_intersection_mode
=False,
114 utils
._check
_bool
(stream_intersection_mode
)
115 self
._stream
_intersection
_mode
= stream_intersection_mode
116 self
._begin
_ns
= _get_ns(begin
)
117 self
._end
_ns
= _get_ns(end
)
119 if type(source_component_specs
) is ComponentSpec
:
120 source_component_specs
= [source_component_specs
]
122 if type(filter_component_specs
) is ComponentSpec
:
123 filter_component_specs
= [filter_component_specs
]
124 elif filter_component_specs
is None:
125 filter_component_specs
= []
127 self
._src
_comp
_specs
= source_component_specs
128 self
._flt
_comp
_specs
= filter_component_specs
129 self
._next
_suffix
= 1
130 self
._connect
_ports
= False
132 # lists of _ComponentAndSpec
133 self
._src
_comps
_and
_specs
= []
134 self
._flt
_comps
_and
_specs
= []
136 self
._validate
_component
_specs
(source_component_specs
)
137 self
._validate
_component
_specs
(filter_component_specs
)
140 def _validate_component_specs(self
, comp_specs
):
141 for comp_spec
in comp_specs
:
142 if type(comp_spec
) is not ComponentSpec
:
144 '"{}" object is not a ComponentSpec'.format(type(comp_spec
))
148 return next(self
._msg
_iter
)
150 def _create_stream_intersection_trimmer(self
, component
, port
):
151 # find the original parameters specified by the user to create
152 # this port's component to get the `inputs` parameter
153 for src_comp_and_spec
in self
._src
_comps
_and
_specs
:
154 if component
== src_comp_and_spec
.comp
:
158 inputs
= src_comp_and_spec
.spec
.params
['inputs']
159 except Exception as e
:
161 'all source components must be created with an "inputs" parameter in stream intersection mode'
164 params
= {'inputs': inputs
}
166 # query the port's component for the `babeltrace.trace-info`
167 # object which contains the stream intersection range for each
169 query_exec
= bt2
.QueryExecutor(
170 src_comp_and_spec
.comp
.cls
, 'babeltrace.trace-info', params
172 trace_info_res
= query_exec
.query()
176 # find the trace info for this port's trace
178 for trace_info
in trace_info_res
:
179 for stream
in trace_info
['streams']:
180 if stream
['port-name'] == port
.name
:
181 range_ns
= trace_info
['intersection-range-ns']
182 begin
= range_ns
['begin']
183 end
= range_ns
['end']
188 if begin
is None or end
is None:
190 'cannot find stream intersection range for port "{}"'.format(port
.name
)
193 name
= 'trimmer-{}-{}'.format(src_comp_and_spec
.comp
.name
, port
.name
)
194 return self
._create
_trimmer
(begin
, end
, name
)
196 def _create_muxer(self
):
197 plugin
= bt2
.find_plugin('utils')
200 raise RuntimeError('cannot find "utils" plugin (needed for the muxer)')
202 if 'muxer' not in plugin
.filter_component_classes
:
204 'cannot find "muxer" filter component class in "utils" plugin'
207 comp_cls
= plugin
.filter_component_classes
['muxer']
208 return self
._graph
.add_component(comp_cls
, 'muxer')
210 def _create_trimmer(self
, begin_ns
, end_ns
, name
):
211 plugin
= bt2
.find_plugin('utils')
214 raise RuntimeError('cannot find "utils" plugin (needed for the trimmer)')
216 if 'trimmer' not in plugin
.filter_component_classes
:
218 'cannot find "trimmer" filter component class in "utils" plugin'
223 def ns_to_string(ns
):
224 s_part
= ns
// 1000000000
225 ns_part
= ns
% 1000000000
226 return '{}.{:09d}'.format(s_part
, ns_part
)
228 if begin_ns
is not None:
229 params
['begin'] = ns_to_string(begin_ns
)
231 if end_ns
is not None:
232 params
['end'] = ns_to_string(end_ns
)
234 comp_cls
= plugin
.filter_component_classes
['trimmer']
235 return self
._graph
.add_component(comp_cls
, name
, params
)
237 def _get_unique_comp_name(self
, comp_spec
):
238 name
= '{}-{}'.format(comp_spec
.plugin_name
, comp_spec
.class_name
)
239 comps_and_specs
= itertools
.chain(
240 self
._src
_comps
_and
_specs
, self
._flt
_comps
_and
_specs
243 if name
in [comp_and_spec
.comp
.name
for comp_and_spec
in comps_and_specs
]:
244 name
+= '-{}'.format(self
._next
_suffix
)
245 self
._next
_suffix
+= 1
249 def _create_comp(self
, comp_spec
, comp_cls_type
):
250 plugin
= bt2
.find_plugin(comp_spec
.plugin_name
)
253 raise ValueError('no such plugin: {}'.format(comp_spec
.plugin_name
))
255 if comp_cls_type
== _CompClsType
.SOURCE
:
256 comp_classes
= plugin
.source_component_classes
258 comp_classes
= plugin
.filter_component_classes
260 if comp_spec
.class_name
not in comp_classes
:
261 cc_type
= 'source' if comp_cls_type
== _CompClsType
.SOURCE
else 'filter'
263 'no such {} component class in "{}" plugin: {}'.format(
264 cc_type
, comp_spec
.plugin_name
, comp_spec
.class_name
268 comp_cls
= comp_classes
[comp_spec
.class_name
]
269 name
= self
._get
_unique
_comp
_name
(comp_spec
)
270 comp
= self
._graph
.add_component(
271 comp_cls
, name
, comp_spec
.params
, comp_spec
.obj
, comp_spec
.logging_level
275 def _get_free_muxer_input_port(self
):
276 for port
in self
._muxer
_comp
.input_ports
.values():
277 if not port
.is_connected
:
280 def _connect_src_comp_port(self
, component
, port
):
281 # if this trace collection iterator is in stream intersection
282 # mode, we need this connection:
284 # port -> trimmer -> muxer
289 if self
._stream
_intersection
_mode
:
290 trimmer_comp
= self
._create
_stream
_intersection
_trimmer
(component
, port
)
291 self
._graph
.connect_ports(port
, trimmer_comp
.input_ports
['in'])
292 port_to_muxer
= trimmer_comp
.output_ports
['out']
296 self
._graph
.connect_ports(port_to_muxer
, self
._get
_free
_muxer
_input
_port
())
298 def _graph_port_added(self
, component
, port
):
299 if not self
._connect
_ports
:
302 if type(port
) is bt2_port
._InputPort
:
305 if component
not in [comp
.comp
for comp
in self
._src
_comps
_and
_specs
]:
306 # do not care about non-source components (muxer, trimmer, etc.)
309 self
._connect
_src
_comp
_port
(component
, port
)
311 def _build_graph(self
):
312 self
._graph
= bt2
.Graph()
313 self
._graph
.add_port_added_listener(self
._graph
_port
_added
)
314 self
._muxer
_comp
= self
._create
_muxer
()
316 if self
._begin
_ns
is not None or self
._end
_ns
is not None:
317 trimmer_comp
= self
._create
_trimmer
(self
._begin
_ns
, self
._end
_ns
, 'trimmer')
318 self
._graph
.connect_ports(
319 self
._muxer
_comp
.output_ports
['out'], trimmer_comp
.input_ports
['in']
321 msg_iter_port
= trimmer_comp
.output_ports
['out']
323 msg_iter_port
= self
._muxer
_comp
.output_ports
['out']
325 # create extra filter components (chained)
326 for comp_spec
in self
._flt
_comp
_specs
:
327 comp
= self
._create
_comp
(comp_spec
, _CompClsType
.FILTER
)
328 self
._flt
_comps
_and
_specs
.append(_ComponentAndSpec(comp
, comp_spec
))
330 # connect the extra filter chain
331 for comp_and_spec
in self
._flt
_comps
_and
_specs
:
332 in_port
= list(comp_and_spec
.comp
.input_ports
.values())[0]
333 out_port
= list(comp_and_spec
.comp
.output_ports
.values())[0]
334 self
._graph
.connect_ports(msg_iter_port
, in_port
)
335 msg_iter_port
= out_port
337 # Here we create the components, self._graph_port_added() is
338 # called when they add ports, but the callback returns early
339 # because self._connect_ports is False. This is because the
340 # self._graph_port_added() could not find the associated source
341 # component specification in self._src_comps_and_specs because
342 # it does not exist yet (it needs the created component to
344 for comp_spec
in self
._src
_comp
_specs
:
345 comp
= self
._create
_comp
(comp_spec
, _CompClsType
.SOURCE
)
346 self
._src
_comps
_and
_specs
.append(_ComponentAndSpec(comp
, comp_spec
))
348 # Now we connect the ports which exist at this point. We allow
349 # self._graph_port_added() to automatically connect _new_ ports.
350 self
._connect
_ports
= True
352 for comp_and_spec
in self
._src
_comps
_and
_specs
:
353 # Keep a separate list because comp_and_spec.output_ports
354 # could change during the connection of one of its ports.
355 # Any new port is handled by self._graph_port_added().
356 out_ports
= [port
for port
in comp_and_spec
.comp
.output_ports
.values()]
358 for out_port
in out_ports
:
359 if out_port
.is_connected
:
362 self
._connect
_src
_comp
_port
(comp_and_spec
.comp
, out_port
)
364 # create this trace collection iterator's message iterator
365 self
._msg
_iter
= self
._graph
.create_output_port_message_iterator(msg_iter_port
)
This page took 0.037678 seconds and 4 git commands to generate.