summaryrefslogtreecommitdiff
path: root/framework/replay/backends/abstract.py
blob: 937d3a8d14b0396c3c6ad73825a48e40283bb138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# coding=utf-8
#
# Copyright (c) 2014, 2016, 2019 Intel Corporation
# Copyright © 2020 Valve Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# SPDX-License-Identifier: MIT


""" Base class for dump backends

This module provides a base class for replayer dump backend modules.

"""

import abc
import functools
import subprocess
import sys

from os import path

from framework import core
from framework.replay.options import OPTIONS


class DumpBackend(metaclass=abc.ABCMeta):
    """ Base class for dump backends

    This class provides an basic ancestor for classes implementing dump
    backends, providing a light public API. The goal of this API is to be "just
    enough", not a generic writing solution. To that end it provides the
    method, 'dump'. This method is designed to be just enough to write a
    backend without needing format specific options.

    """
    def __init__(self, trace_path, output_dir=None, calls=None, **kwargs):
        """ Generic constructor

        This method takes keyword arguments that define options for the
        backends. Options should be prefixed to identify which backends they
        apply to. For example, an apitrace specific value should be passed as
        apitrace_*, while a file gfxrecon value should be passed as gfxrecon_*)

        Arguments:

        trace_path -- the path to the trace from which we want to dump calls as
                      images.
        output_dir -- the place to write the images to.
        calls      -- an array of the calls in the trace for which we want to
                      dump images.

        """
        self._trace_path = trace_path
        self._output_dir = output_dir
        self._calls = calls or []

        if self._output_dir is None:
            self._output_dir = path.join('trace', OPTIONS.device_name,
                                         path.dirname(self._trace_path))


    @staticmethod
    def log(severity, msg, end='\n'):
        print('[dump_trace_images] {}: {}'.format(severity, msg), flush=True,
              end=end)


    @staticmethod
    def log_result(msg):
        print(msg, flush=True)


    @staticmethod
    def _run_logged_command(cmd, env):
        # Explicitly send the stderr to the fd at sys.stderr in case it was
        # redirected for the parent process.
        # See:
        # https://bugs.python.org/issue44158
        ret = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=sys.stderr,
                             env=env)
        logoutput = '[dump_trace_images] Running: {}\n'.format(
            ' '.join(cmd)).encode() + ret.stdout
        print(logoutput.decode(errors='replace'))
        if ret.returncode:
            raise RuntimeError(
                '[dump_trace_images] Process failed with error code: {}'.format(
                    ret.returncode))


    @abc.abstractmethod
    def _get_last_frame_call(self):
        """Get the number of the last frame call from the trace"""


    @abc.abstractmethod
    def dump(self):
        """ Dump the calls to images from the trace

        This method actually dumps the calls from a trace.

        """


def dump_handler(func):
    """ Decorator function for handling trace dumps.

    This will handle exceptions and log the result.

    """

    @functools.wraps(func)
    def _inner(self, *args, **kwargs):
        try:
            DumpBackend.log('Info',
                            'Dumping trace {}'.format(self._trace_path),
                            end='...\n')
            core.check_dir(self._output_dir)
            func(self, *args, **kwargs)
            DumpBackend.log_result('OK')
            return True
        except Exception as e:
            DumpBackend.log_result('ERROR')
            DumpBackend.log('Debug', '=== Failure log start ===')
            print(e)
            DumpBackend.log('Debug', '=== Failure log end ===')
            return False

    return _inner