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'])
44 logging_level
=bt2_logging
.LoggingLevel
.NONE
,
46 utils
._check
_str
(plugin_name
)
47 utils
._check
_str
(class_name
)
48 utils
._check
_log
_level
(logging_level
)
49 self
._plugin
_name
= plugin_name
50 self
._class
_name
= class_name
51 self
._logging
_level
= logging_level
53 if type(params
) is str:
54 self
._params
= bt2
.create_value({'inputs': [params
]})
56 self
._params
= bt2
.create_value(params
)
59 def plugin_name(self
):
60 return self
._plugin
_name
64 return self
._class
_name
67 def logging_level(self
):
68 return self
._logging
_level
75 # datetime.datetime or integral to nanoseconds
80 if isinstance(obj
, numbers
.Real
):
81 # consider that it's already in seconds
83 elif isinstance(obj
, datetime
.datetime
):
88 '"{}" is not an integral number or a datetime.datetime object'.format(obj
)
99 class TraceCollectionMessageIterator(bt2_message_iterator
._MessageIterator
):
102 source_component_specs
,
103 filter_component_specs
=None,
104 stream_intersection_mode
=False,
108 utils
._check
_bool
(stream_intersection_mode
)
109 self
._stream
_intersection
_mode
= stream_intersection_mode
110 self
._begin
_ns
= _get_ns(begin
)
111 self
._end
_ns
= _get_ns(end
)
113 if type(source_component_specs
) is ComponentSpec
:
114 source_component_specs
= [source_component_specs
]
116 if type(filter_component_specs
) is ComponentSpec
:
117 filter_component_specs
= [filter_component_specs
]
118 elif filter_component_specs
is None:
119 filter_component_specs
= []
121 self
._src
_comp
_specs
= source_component_specs
122 self
._flt
_comp
_specs
= filter_component_specs
123 self
._next
_suffix
= 1
124 self
._connect
_ports
= False
126 # lists of _ComponentAndSpec
127 self
._src
_comps
_and
_specs
= []
128 self
._flt
_comps
_and
_specs
= []
130 self
._validate
_component
_specs
(source_component_specs
)
131 self
._validate
_component
_specs
(filter_component_specs
)
134 def _validate_component_specs(self
, comp_specs
):
135 for comp_spec
in comp_specs
:
136 if type(comp_spec
) is not ComponentSpec
:
138 '"{}" object is not a ComponentSpec'.format(type(comp_spec
))
142 return next(self
._msg
_iter
)
144 def _create_stream_intersection_trimmer(self
, component
, port
):
145 # find the original parameters specified by the user to create
146 # this port's component to get the `inputs` parameter
147 for src_comp_and_spec
in self
._src
_comps
_and
_specs
:
148 if component
== src_comp_and_spec
.comp
:
152 inputs
= src_comp_and_spec
.spec
.params
['inputs']
153 except Exception as e
:
155 'all source components must be created with an "inputs" parameter in stream intersection mode'
158 params
= {'inputs': inputs
}
160 # query the port's component for the `babeltrace.trace-info`
161 # object which contains the stream intersection range for each
163 query_exec
= bt2
.QueryExecutor()
164 trace_info_res
= query_exec
.query(
165 src_comp_and_spec
.comp
.cls
, 'babeltrace.trace-info', params
170 # find the trace info for this port's trace
172 for trace_info
in trace_info_res
:
173 for stream
in trace_info
['streams']:
174 if stream
['port-name'] == port
.name
:
175 range_ns
= trace_info
['intersection-range-ns']
176 begin
= range_ns
['begin']
177 end
= range_ns
['end']
182 if begin
is None or end
is None:
184 'cannot find stream intersection range for port "{}"'.format(port
.name
)
187 name
= 'trimmer-{}-{}'.format(src_comp_and_spec
.comp
.name
, port
.name
)
188 return self
._create
_trimmer
(begin
, end
, name
)
190 def _create_muxer(self
):
191 plugin
= bt2
.find_plugin('utils')
194 raise RuntimeError('cannot find "utils" plugin (needed for the muxer)')
196 if 'muxer' not in plugin
.filter_component_classes
:
198 'cannot find "muxer" filter component class in "utils" plugin'
201 comp_cls
= plugin
.filter_component_classes
['muxer']
202 return self
._graph
.add_component(comp_cls
, 'muxer')
204 def _create_trimmer(self
, begin_ns
, end_ns
, name
):
205 plugin
= bt2
.find_plugin('utils')
208 raise RuntimeError('cannot find "utils" plugin (needed for the trimmer)')
210 if 'trimmer' not in plugin
.filter_component_classes
:
212 'cannot find "trimmer" filter component class in "utils" plugin'
217 def ns_to_string(ns
):
218 s_part
= ns
// 1000000000
219 ns_part
= ns
% 1000000000
220 return '{}.{:09d}'.format(s_part
, ns_part
)
222 if begin_ns
is not None:
223 params
['begin'] = ns_to_string(begin_ns
)
225 if end_ns
is not None:
226 params
['end'] = ns_to_string(end_ns
)
228 comp_cls
= plugin
.filter_component_classes
['trimmer']
229 return self
._graph
.add_component(comp_cls
, name
, params
)
231 def _get_unique_comp_name(self
, comp_spec
):
232 name
= '{}-{}'.format(comp_spec
.plugin_name
, comp_spec
.class_name
)
233 comps_and_specs
= itertools
.chain(
234 self
._src
_comps
_and
_specs
, self
._flt
_comps
_and
_specs
237 if name
in [comp_and_spec
.comp
.name
for comp_and_spec
in comps_and_specs
]:
238 name
+= '-{}'.format(self
._next
_suffix
)
239 self
._next
_suffix
+= 1
243 def _create_comp(self
, comp_spec
, comp_cls_type
):
244 plugin
= bt2
.find_plugin(comp_spec
.plugin_name
)
247 raise ValueError('no such plugin: {}'.format(comp_spec
.plugin_name
))
249 if comp_cls_type
== _CompClsType
.SOURCE
:
250 comp_classes
= plugin
.source_component_classes
252 comp_classes
= plugin
.filter_component_classes
254 if comp_spec
.class_name
not in comp_classes
:
255 cc_type
= 'source' if comp_cls_type
== _CompClsType
.SOURCE
else 'filter'
257 'no such {} component class in "{}" plugin: {}'.format(
258 cc_type
, comp_spec
.plugin_name
, comp_spec
.class_name
262 comp_cls
= comp_classes
[comp_spec
.class_name
]
263 name
= self
._get
_unique
_comp
_name
(comp_spec
)
264 comp
= self
._graph
.add_component(
265 comp_cls
, name
, comp_spec
.params
, comp_spec
.logging_level
269 def _get_free_muxer_input_port(self
):
270 for port
in self
._muxer
_comp
.input_ports
.values():
271 if not port
.is_connected
:
274 def _connect_src_comp_port(self
, component
, port
):
275 # if this trace collection iterator is in stream intersection
276 # mode, we need this connection:
278 # port -> trimmer -> muxer
283 if self
._stream
_intersection
_mode
:
284 trimmer_comp
= self
._create
_stream
_intersection
_trimmer
(component
, port
)
285 self
._graph
.connect_ports(port
, trimmer_comp
.input_ports
['in'])
286 port_to_muxer
= trimmer_comp
.output_ports
['out']
290 self
._graph
.connect_ports(port_to_muxer
, self
._get
_free
_muxer
_input
_port
())
292 def _graph_port_added(self
, component
, port
):
293 if not self
._connect
_ports
:
296 if type(port
) is bt2_port
._InputPort
:
299 if component
not in [comp
.comp
for comp
in self
._src
_comps
_and
_specs
]:
300 # do not care about non-source components (muxer, trimmer, etc.)
303 self
._connect
_src
_comp
_port
(component
, port
)
305 def _build_graph(self
):
306 self
._graph
= bt2
.Graph()
307 self
._graph
.add_port_added_listener(self
._graph
_port
_added
)
308 self
._muxer
_comp
= self
._create
_muxer
()
310 if self
._begin
_ns
is not None or self
._end
_ns
is not None:
311 trimmer_comp
= self
._create
_trimmer
(self
._begin
_ns
, self
._end
_ns
, 'trimmer')
312 self
._graph
.connect_ports(
313 self
._muxer
_comp
.output_ports
['out'], trimmer_comp
.input_ports
['in']
315 msg_iter_port
= trimmer_comp
.output_ports
['out']
317 msg_iter_port
= self
._muxer
_comp
.output_ports
['out']
319 # create extra filter components (chained)
320 for comp_spec
in self
._flt
_comp
_specs
:
321 comp
= self
._create
_comp
(comp_spec
, _CompClsType
.FILTER
)
322 self
._flt
_comps
_and
_specs
.append(_ComponentAndSpec(comp
, comp_spec
))
324 # connect the extra filter chain
325 for comp_and_spec
in self
._flt
_comps
_and
_specs
:
326 in_port
= list(comp_and_spec
.comp
.input_ports
.values())[0]
327 out_port
= list(comp_and_spec
.comp
.output_ports
.values())[0]
328 self
._graph
.connect_ports(msg_iter_port
, in_port
)
329 msg_iter_port
= out_port
331 # Here we create the components, self._graph_port_added() is
332 # called when they add ports, but the callback returns early
333 # because self._connect_ports is False. This is because the
334 # self._graph_port_added() could not find the associated source
335 # component specification in self._src_comps_and_specs because
336 # it does not exist yet (it needs the created component to
338 for comp_spec
in self
._src
_comp
_specs
:
339 comp
= self
._create
_comp
(comp_spec
, _CompClsType
.SOURCE
)
340 self
._src
_comps
_and
_specs
.append(_ComponentAndSpec(comp
, comp_spec
))
342 # Now we connect the ports which exist at this point. We allow
343 # self._graph_port_added() to automatically connect _new_ ports.
344 self
._connect
_ports
= True
346 for comp_and_spec
in self
._src
_comps
_and
_specs
:
347 # Keep a separate list because comp_and_spec.output_ports
348 # could change during the connection of one of its ports.
349 # Any new port is handled by self._graph_port_added().
350 out_ports
= [port
for port
in comp_and_spec
.comp
.output_ports
.values()]
352 for out_port
in out_ports
:
353 if out_port
.is_connected
:
356 self
._connect
_src
_comp
_port
(comp_and_spec
.comp
, out_port
)
358 # create this trace collection iterator's message iterator
359 self
._msg
_iter
= self
._graph
.create_output_port_message_iterator(msg_iter_port
)
This page took 0.042984 seconds and 5 git commands to generate.