2018-07-04 15:39:44 +01:00
|
|
|
# Copyright 2013-2018 ARM Limited
|
2017-05-12 09:29:35 +01:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=W0613,E1101
|
2018-05-30 13:58:49 +01:00
|
|
|
|
2017-05-12 09:29:35 +01:00
|
|
|
import os
|
|
|
|
|
|
|
|
from devlib import FtraceCollector
|
|
|
|
|
2017-12-04 17:13:38 +00:00
|
|
|
from wa import Instrument, Parameter
|
2017-05-12 09:29:35 +01:00
|
|
|
from wa.framework import signal
|
2018-02-16 14:09:02 +00:00
|
|
|
from wa.framework.instrument import very_slow
|
2017-12-04 17:13:38 +00:00
|
|
|
from wa.framework.exception import InstrumentError
|
2017-05-12 09:29:35 +01:00
|
|
|
from wa.utils.types import list_of_strings
|
|
|
|
from wa.utils.misc import which
|
|
|
|
|
|
|
|
|
|
|
|
OUTPUT_TRACE_FILE = 'trace.dat'
|
|
|
|
OUTPUT_TEXT_FILE = '{}.txt'.format(os.path.splitext(OUTPUT_TRACE_FILE)[0])
|
|
|
|
TIMEOUT = 180
|
|
|
|
|
|
|
|
|
|
|
|
class TraceCmdInstrument(Instrument):
|
|
|
|
|
|
|
|
name = 'trace-cmd'
|
|
|
|
description = """
|
2017-07-10 15:15:21 +01:00
|
|
|
trace-cmd is an instrument which interacts with ftrace Linux kernel internal
|
2017-05-12 09:29:35 +01:00
|
|
|
tracer
|
|
|
|
|
|
|
|
From trace-cmd man page:
|
|
|
|
|
2017-07-10 15:15:21 +01:00
|
|
|
trace-cmd command interacts with the ftrace tracer that is built inside the
|
|
|
|
Linux kernel. It interfaces with the ftrace specific files found in the
|
2017-05-12 09:29:35 +01:00
|
|
|
debugfs file system under the tracing directory.
|
|
|
|
|
|
|
|
trace-cmd reads a list of events it will trace, which can be specified in
|
|
|
|
the config file as follows ::
|
|
|
|
|
|
|
|
trace_events = ['irq*', 'power*']
|
|
|
|
|
2017-07-10 15:15:21 +01:00
|
|
|
If no event is specified, a default set of events that are generally considered useful
|
|
|
|
for debugging/profiling purposes will be enabled.
|
2017-05-12 09:29:35 +01:00
|
|
|
|
|
|
|
The list of available events can be obtained by rooting and running the
|
|
|
|
following command line on the device ::
|
|
|
|
|
|
|
|
trace-cmd list
|
|
|
|
|
|
|
|
You may also specify ``trace_buffer_size`` setting which must be an integer
|
|
|
|
that will be used to set the ftrace buffer size. It will be interpreted as
|
|
|
|
KB::
|
|
|
|
|
|
|
|
trace_cmd_buffer_size = 8000
|
|
|
|
|
|
|
|
The maximum buffer size varies from device to device, but there is a
|
|
|
|
maximum and trying to set buffer size beyond that will fail. If you plan
|
|
|
|
on collecting a lot of trace over long periods of time, the buffer size
|
|
|
|
will not be enough and you will only get trace for the last portion of your
|
|
|
|
run. To deal with this you can set the ``trace_mode`` setting to
|
|
|
|
``'record'`` (the default is ``'start'``)::
|
|
|
|
|
|
|
|
trace_cmd_mode = 'record'
|
|
|
|
|
|
|
|
This will cause trace-cmd to trace into file(s) on disk, rather than the
|
|
|
|
buffer, and so the limit for the max size of the trace is set by the
|
|
|
|
storage available on device. Bear in mind that ``'record'`` mode *is* more
|
|
|
|
intrusive than the default, so if you do not plan on generating a lot of
|
|
|
|
trace, it is best to use the default ``'start'`` mode.
|
|
|
|
|
|
|
|
.. note:: Mode names correspond to the underlying trace-cmd executable's
|
|
|
|
command used to implement them. You can find out more about what
|
|
|
|
is happening in each case from trace-cmd documentation:
|
|
|
|
https://lwn.net/Articles/341902/.
|
|
|
|
|
2017-07-10 15:15:21 +01:00
|
|
|
This instrument comes with an trace-cmd binary that will be copied and used
|
|
|
|
on the device, however post-processing will be, by default, done on-host and you must
|
|
|
|
have trace-cmd installed and in your path. On Ubuntu systems, this may be
|
|
|
|
done with::
|
2017-05-12 09:29:35 +01:00
|
|
|
|
|
|
|
sudo apt-get install trace-cmd
|
|
|
|
|
2017-07-10 15:15:21 +01:00
|
|
|
Alternatively, you may set ``report_on_target`` parameter to ``True`` to enable on-target
|
|
|
|
processing (this is useful when running on non-Linux hosts, but is likely to take longer
|
|
|
|
and may fail on particularly resource-constrained targets).
|
|
|
|
|
2017-05-12 09:29:35 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
parameters = [
|
|
|
|
Parameter('events', kind=list_of_strings,
|
2017-10-02 13:29:10 +01:00
|
|
|
default=['sched*', 'irq*', 'power*', 'thermal*'],
|
2017-05-12 09:29:35 +01:00
|
|
|
global_alias='trace_events',
|
|
|
|
description="""
|
|
|
|
Specifies the list of events to be traced. Each event in the
|
|
|
|
list will be passed to trace-cmd with -e parameter and must
|
|
|
|
be in the format accepted by trace-cmd.
|
|
|
|
"""),
|
|
|
|
Parameter('functions', kind=list_of_strings,
|
|
|
|
global_alias='trace_functions',
|
|
|
|
description="""
|
2017-07-10 15:15:21 +01:00
|
|
|
Specifies the list of functions to be traced.
|
2017-05-12 09:29:35 +01:00
|
|
|
"""),
|
|
|
|
Parameter('buffer_size', kind=int, default=None,
|
|
|
|
global_alias='trace_buffer_size',
|
|
|
|
description="""
|
|
|
|
Attempt to set ftrace buffer size to the specified value (in
|
|
|
|
KB). Default buffer size may need to be increased for
|
|
|
|
long-running workloads, or if a large number of events have
|
|
|
|
been enabled. Note: there is a maximum size that the buffer
|
|
|
|
can be set, and that varies from device to device. Attempting
|
|
|
|
to set buffer size higher than this will fail. In that case,
|
|
|
|
this instrument will set the size to the highest possible
|
|
|
|
value by going down from the specified size in
|
|
|
|
``buffer_size_step`` intervals.
|
|
|
|
"""),
|
|
|
|
Parameter('buffer_size_step', kind=int, default=1000,
|
|
|
|
global_alias='trace_buffer_size_step',
|
|
|
|
description="""
|
|
|
|
Defines the decremental step used if the specified
|
|
|
|
``buffer_size`` could not be set. This will be subtracted
|
|
|
|
form the buffer size until set succeeds or size is reduced to
|
|
|
|
1MB.
|
|
|
|
"""),
|
|
|
|
Parameter('report', kind=bool, default=True,
|
|
|
|
description="""
|
|
|
|
Specifies whether reporting should be performed once the
|
|
|
|
binary trace has been generated.
|
|
|
|
"""),
|
|
|
|
Parameter('no_install', kind=bool, default=False,
|
|
|
|
description="""
|
|
|
|
Do not install the bundled trace-cmd and use the one on the
|
|
|
|
device instead. If there is not already a trace-cmd on the
|
|
|
|
device, an error is raised.
|
|
|
|
"""),
|
|
|
|
Parameter('report_on_target', kind=bool, default=False,
|
|
|
|
description="""
|
|
|
|
When enabled generation of reports will be done host-side
|
|
|
|
because the generated file is very large. If trace-cmd is not
|
|
|
|
available on the host device this setting can be disabled and
|
|
|
|
the report will be generated on the target device.
|
|
|
|
|
|
|
|
.. note:: This requires the latest version of trace-cmd to be
|
|
|
|
installed on the host (the one in your
|
|
|
|
distribution's repos may be too old).
|
|
|
|
"""),
|
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self, target, **kwargs):
|
|
|
|
super(TraceCmdInstrument, self).__init__(target, **kwargs)
|
|
|
|
self.collector = None
|
|
|
|
|
|
|
|
def initialize(self, context):
|
|
|
|
if not self.target.is_rooted:
|
|
|
|
raise InstrumentError('trace-cmd instrument cannot be used on an unrooted device.')
|
|
|
|
collector_params = dict(
|
2018-07-03 14:16:16 +01:00
|
|
|
events=self.events,
|
|
|
|
functions=self.functions,
|
|
|
|
buffer_size=self.buffer_size,
|
|
|
|
buffer_size_step=1000,
|
|
|
|
automark=False,
|
|
|
|
autoreport=True,
|
|
|
|
autoview=False,
|
|
|
|
no_install=self.no_install,
|
|
|
|
strict=False,
|
|
|
|
report_on_target=False,
|
2017-05-12 09:29:35 +01:00
|
|
|
)
|
|
|
|
if self.report and self.report_on_target:
|
|
|
|
collector_params['autoreport'] = True
|
|
|
|
collector_params['report_on_target'] = True
|
|
|
|
else:
|
|
|
|
collector_params['autoreport'] = False
|
|
|
|
collector_params['report_on_target'] = False
|
|
|
|
self.collector = FtraceCollector(self.target, **collector_params)
|
|
|
|
|
|
|
|
# Register ourselves as absolute last event before and
|
|
|
|
# first after so we can mark the trace at the right time
|
|
|
|
signal.connect(self.mark_start, signal.BEFORE_WORKLOAD_EXECUTION, priority=11)
|
|
|
|
signal.connect(self.mark_stop, signal.AFTER_WORKLOAD_EXECUTION, priority=11)
|
|
|
|
|
|
|
|
def setup(self, context):
|
2022-04-26 14:30:04 +01:00
|
|
|
if self.collector:
|
|
|
|
self.collector.reset()
|
2017-05-12 09:29:35 +01:00
|
|
|
|
|
|
|
@very_slow
|
|
|
|
def start(self, context):
|
2022-04-26 14:30:04 +01:00
|
|
|
if self.collector:
|
|
|
|
self.collector.start()
|
2017-05-12 09:29:35 +01:00
|
|
|
|
2017-10-12 17:58:45 +01:00
|
|
|
@very_slow
|
2017-05-12 09:29:35 +01:00
|
|
|
def stop(self, context):
|
2022-04-26 14:30:04 +01:00
|
|
|
if self.collector:
|
|
|
|
self.collector.stop()
|
2017-05-12 09:29:35 +01:00
|
|
|
|
2018-01-10 14:37:13 +00:00
|
|
|
def update_output(self, context): # NOQA pylint: disable=R0912
|
2022-04-26 14:30:04 +01:00
|
|
|
if not self.collector:
|
|
|
|
return
|
2018-05-11 14:41:53 +01:00
|
|
|
self.logger.info('Extracting trace from target...')
|
2017-05-12 09:29:35 +01:00
|
|
|
outfile = os.path.join(context.output_directory, 'trace.dat')
|
2019-12-03 16:46:26 +00:00
|
|
|
self.collector.set_output(outfile)
|
|
|
|
self.collector.get_data()
|
2017-10-02 13:54:18 +01:00
|
|
|
context.add_artifact('trace-cmd-bin', outfile, 'data')
|
|
|
|
if self.report:
|
2018-07-23 17:34:59 +01:00
|
|
|
textfile = os.path.join(context.output_directory, 'trace.txt')
|
2017-10-02 13:54:18 +01:00
|
|
|
if not self.report_on_target:
|
|
|
|
self.collector.report(outfile, textfile)
|
|
|
|
context.add_artifact('trace-cmd-txt', textfile, 'export')
|
2017-05-12 09:29:35 +01:00
|
|
|
|
|
|
|
def teardown(self, context):
|
|
|
|
path = self.target.path.join(self.target.working_directory, OUTPUT_TRACE_FILE)
|
|
|
|
self.target.remove(path)
|
|
|
|
if self.report_on_target:
|
|
|
|
path = self.target.path.join(self.target.working_directory, OUTPUT_TEXT_FILE)
|
|
|
|
self.target.remove(path)
|
|
|
|
|
|
|
|
def validate(self):
|
|
|
|
if self.report and not self.report_on_target and not which('trace-cmd'):
|
|
|
|
raise InstrumentError('trace-cmd is not in PATH; is it installed?')
|
|
|
|
|
|
|
|
def mark_start(self, context):
|
2018-02-01 11:24:13 +00:00
|
|
|
if self.is_enabled:
|
|
|
|
self.collector.mark_start()
|
2017-05-12 09:29:35 +01:00
|
|
|
|
|
|
|
def mark_stop(self, context):
|
2018-02-01 11:24:13 +00:00
|
|
|
if self.is_enabled:
|
|
|
|
self.collector.mark_stop()
|