3 # Copyright (c) 2014, 2016, 2019 Intel Corporation
4 # Copyright © 2020 Valve Corporation.
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 # and/or sell copies of the Software, and to permit persons to whom the
11 # Software is furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 # OTHER DEALINGS IN THE SOFTWARE.
24 # SPDX-License-Identifier: MIT
27 """ Base class for dump backends
29 This module provides a base class for replayer dump backend modules.
39 from framework
import core
40 from framework
.replay
.options
import OPTIONS
43 class DumpBackend(metaclass
=abc
.ABCMeta
):
44 """ Base class for dump backends
46 This class provides an basic ancestor for classes implementing dump
47 backends, providing a light public API. The goal of this API is to be "just
48 enough", not a generic writing solution. To that end it provides the
49 method, 'dump'. This method is designed to be just enough to write a
50 backend without needing format specific options.
53 def __init__(self
, trace_path
, output_dir
=None, calls
=None, **kwargs
):
54 """ Generic constructor
56 This method takes keyword arguments that define options for the
57 backends. Options should be prefixed to identify which backends they
58 apply to. For example, an apitrace specific value should be passed as
59 apitrace_*, while a file gfxrecon value should be passed as gfxrecon_*)
63 trace_path -- the path to the trace from which we want to dump calls as
65 output_dir -- the place to write the images to.
66 calls -- an array of the calls in the trace for which we want to
70 self
._trace
_path
= trace_path
71 self
._output
_dir
= output_dir
72 self
._calls
= calls
or []
74 if self
._output
_dir
is None:
75 self
._output
_dir
= path
.join('trace', OPTIONS
.device_name
,
76 path
.dirname(self
._trace
_path
))
79 def log(severity
, msg
, end
='\n'):
80 print('[dump_trace_images] {}: {}'.format(severity
, msg
), flush
=True,
85 print(msg
, flush
=True)
88 def _run_logged_command(cmd
, env
):
89 # Explicitly send the stderr to the fd at sys.stderr in case it was
90 # redirected for the parent process.
92 # https://bugs.python.org/issue44158
93 ret
= subprocess
.run(cmd
, stdout
=subprocess
.PIPE
, stderr
=sys
.stderr
,
95 logoutput
= '[dump_trace_images] Running: {}\n'.format(
96 ' '.join(cmd
)).encode() + ret
.stdout
97 print(logoutput
.decode(errors
='replace'))
100 '[dump_trace_images] Process failed with error code: {}'.format(
104 def _get_last_frame_call(self
):
105 """Get the number of the last frame call from the trace"""
109 """ Dump the calls to images from the trace
111 This method actually dumps the calls from a trace.
116 def dump_handler(func
):
117 """ Decorator function for handling trace dumps.
119 This will handle exceptions and log the result.
123 @functools.wraps(func
)
124 def _inner(self
, *args
, **kwargs
):
126 DumpBackend
.log('Info',
127 'Dumping trace {}'.format(self
._trace
_path
),
129 core
.check_dir(self
._output
_dir
)
130 func(self
, *args
, **kwargs
)
131 DumpBackend
.log_result('OK')
133 except Exception as e
:
134 DumpBackend
.log_result('ERROR')
135 DumpBackend
.log('Debug', '=== Failure log start ===')
137 DumpBackend
.log('Debug', '=== Failure log end ===')