cl: Fix missing OpenCL 3.0 definition
[piglit.git] / framework / replay / backends / abstract.py
blob966fad669eccc59bfb3d3f42d3c3b025d5764693
1 # coding=utf-8
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.
31 """
33 import abc
34 import functools
35 import subprocess
36 import sys
37 from os import path
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.
52 """
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_*)
61 Arguments:
63 trace_path -- the path to the trace from which we want to dump calls as
64 images.
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
67 dump images.
69 """
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))
78 @staticmethod
79 def log(severity, msg, end='\n'):
80 print('[dump_trace_images] {}: {}'.format(severity, msg), flush=True,
81 end=end)
83 @staticmethod
84 def log_result(msg):
85 print(msg, flush=True)
87 @staticmethod
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.
91 # See:
92 # https://bugs.python.org/issue44158
93 ret = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=sys.stderr,
94 env=env)
95 logoutput = '[dump_trace_images] Running: {}\n'.format(
96 ' '.join(cmd)).encode() + ret.stdout
97 print(logoutput.decode(errors='replace'))
98 if ret.returncode:
99 raise RuntimeError(
100 '[dump_trace_images] Process failed with error code: {}'.format(
101 ret.returncode))
103 @abc.abstractmethod
104 def _get_last_frame_call(self):
105 """Get the number of the last frame call from the trace"""
107 @abc.abstractmethod
108 def dump(self):
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):
125 try:
126 DumpBackend.log('Info',
127 'Dumping trace {}'.format(self._trace_path),
128 end='...\n')
129 core.check_dir(self._output_dir)
130 func(self, *args, **kwargs)
131 DumpBackend.log_result('OK')
132 return True
133 except Exception as e:
134 DumpBackend.log_result('ERROR')
135 DumpBackend.log('Debug', '=== Failure log start ===')
136 print(e)
137 DumpBackend.log('Debug', '=== Failure log end ===')
138 return False
140 return _inner