File: keyframes.py

package info (click to toggle)
gfxreconstruct 0.9.18%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,636 kB
  • sloc: cpp: 328,961; ansic: 25,454; python: 18,156; xml: 255; sh: 128; makefile: 6
file content (332 lines) | stat: -rwxr-xr-x 12,876 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python3
"""Given a file with a set of (ASCII) commands, identify "interesting"
keyframes based on distinct maximal function sets used in a frame.  Keyframes
selected in this way tend to exercise more distinct parts of the trace,
as many frames will use the same "footprint" of functions as other frames;
by collapsing to maximal function sets, keyframes can be selected that more
thoroughly exercise the graphics API.

The selected keyframes can be further refined through the use of filter
operations.

To use on a trace captured by gfxreconstruct, the trace must be converted
to an ASCII command file containing one function or method name per line, e.g.:

    ...
    vkDestroyImageView
    vkBeginCommandBuffer
    vkCmdPipelineBarrier
    vkCmdWriteTimestamp
    vkAcquireNextImageKHR
    vkCmdPipelineBarrier
    vkCmdBeginRenderPass
    vkCmdBindPipeline
    vkCmdBindDescriptorSets
    vkCmdSetViewport
    vkCmdBindIndexBuffer
    vkCmdDrawIndexed
    vkCmdPipelineBarrier
    vkCmdWriteTimestamp
    vkCmdEndRenderPass
    vkCmdResetQueryPool
    vkEndCommandBuffer
    vkFlushMappedMemoryRanges
    vkResetFences
    vkQueueSubmit
    vkQueuePresentKHR
    ...

The command file must include frame marker functions (e.g. vkQueuePresentKHR
for Vulkan, or Present for DX12); if it does not, no frame boundaries can
be established, and keyframes cannot be calculated.

The gfxrecon-toascii tool can be used to create such a file from a trace
file, using an editor or a script to extract only the function or method
name associated with each captured call.

Then using this script will yield the keyframes, e.g.:

    $ python3 keyframes.py commandfile.txt
    INFO: processing the command file commandfile.txt...
    INFO: sorting 70 function sets by cardinality...
    INFO: collapsing into maximal distinct sets...
    INFO: filtering 10 keyframes from 22 maximal sets...
    INFO: Keyframes in priority order: 0,1908,1051,1910,2086,4280,5329,1616,2088,4281
    INFO: Keyframes in sorted order:   0,1051,1616,1908,1910,2086,2088,4280,4281,5329
"""

import argparse
import logging
import sys

log = logging.getLogger(__name__)

class KeyframeException(Exception):
    """Exception class for this module."""

##################################################################
# Filter functions.  Each of these provides a condition that can
# be used to disqualify frames from selection.

class Filter:
    """This represents a keyframe filter that can be used to limit the
    choices of keyframes."""

    # The filter name is used by the '--filters' switch.
    name = None
    description = None

    def __init__(self):
        """Filter-specific initialization."""

    def preArgumentParse(self, parser):
        """Add any command-line arguments used by this filter to the
        argument parser."""

    def postArgumentParse(self, args):
        """Parse any command-line arguments needed by this filter."""

    def allowed(self, frame, keyframes):
        """Return True if the given frame can be added to the given
        list of existing keyframes.  Return False if it cannot."""
        raise NotImplementedError

def getFilterByName(name):
    """Return an instance of a keyframe filter that has a class.name field
    that matches the given "name"."""
    for filter_subclass in Filter.__subclasses__():
        if filter_subclass.name == name:
            return filter_subclass()
    raise KeyframeException(f'no keyframe filter named "{name}" is available')

class FilterDistance(Filter):
    """Require that two keyframes be a minimum number of frames apart."""
    name = 'distance'
    description = 'require keyframes be a minimum number of frames apart'

    def __init__(self):
        super().__init__()
        self.distance = None

    def preArgumentParse(self, parser):
        """Add the distance filter arguments."""
        distance_group = parser.add_argument_group(f'{self.name} filter arguments')
        distance_group.add_argument('-d', '--distance', type=int, default=30,
                                    help='the minimum distance between two keyframes [%(default)s]')

    def postARgumentParse(self, args):
        """Process the distance filter arguments."""
        self.distance = args.distance

    def allowed(self, frame, keyframes):
        """Return False if the new candidate frame is too close to any of the existing
        keyframes."""
        for keyframe in keyframes:
            if keyframe - self.distance < frame < keyframe + self.distance:
                return False
        return True

##################################################################
# Utility functions.

def Ranges(values):
    """Given a sorted list of unique integers, convert them into a list
    of lists of consecutive items, e.g. [1,2,3, 7,8,9,  15, 17,18] =>
    [[1,2,3], [7,8,9], [15], [17,18]]."""

    # There is a Python recipe using itertools.groupby().  It's hard
    # to understand, so I'm using a more straightforward generator.
    first = last = values[0]
    for value in values[1:]:
        if value == last + 1:
            # Consecutive - keep searching
            last = value
        else:
            # Next value is not consecutive - return the last group
            yield first, last
            # and when we come back, advance to the next
            first = last = value
    # Yield the last group, then we're done
    yield first, last

# The set of commands that identify where frames end.
FRAME_END_FUNCTIONS = {
    # Vulkan
    'vkQueuePresentKHR',
    # DX12
    'Present',
}

def getCommandFileFootprintsToFrames(commandfile):
    """Given the name of a commandfile, returns a dictionary indexed
    by frame "footprints" (i.e. sets of API commands that were used
    in a frame), each of which is associated with a list of frame
    numbers that have that same "footprint"."""

    # gfxreconstruct numbers frames starting from 1.
    frame_number = 1
    footprints_to_frames = {}
    frame_footprint = set()

    log.info("processing the command file %s...", commandfile)
    with open(commandfile, "rt") as f:
        for line in f:
            # Right now command lines are just function names.
            function_name = line.strip()

            # If it's a normal function, just save it and keep going.
            if function_name not in FRAME_END_FUNCTIONS:
                frame_footprint.add(function_name)
                continue

            # Here, we're done with the frame.  Freeze the set to make it
            # hashable, so we can use it in hashmaps.
            log.debug('    finishing frame %s...', frame_number)

            frozen_footprint = frozenset(frame_footprint)
            try:
                footprints_to_frames[frozen_footprint].append(frame_number)
            except KeyError:
                footprints_to_frames[frozen_footprint] = [frame_number]

            # Get ready for the next set.
            frame_footprint.clear()
            frame_number += 1

        # end for line in f
    # end with open

    # Note that it's possible that there are functions after the last complete
    # frame.  We're generally not interested in these because they are never
    # presented, so we can ignore them safely.

    # If there are no sets, something is terribly wrong.
    if not footprints_to_frames:
        raise KeyframeException(f'no frame separators found in command file {commandfile}')

    return footprints_to_frames

def getMaximalSets(sets):
    """Given a list of sets, return a list of maximal sets from the list, sorted
    by set cardinality (the number of elements in the set).  A "maximal set" is
    one that is not a subset of any other set in the list."""

    # Start with the largest sets and work our way down.
    log.info('sorting %s function sets by cardinality...', len(sets))
    sets_by_cardinality = sorted(sets, key=len, reverse=True)

    # The largest set is certainly maximal, as it cannot be a subset of any other.
    maximal_sets = [sets_by_cardinality.pop(0)]

    # For each remaining set, discard it if it is a proper subset of
    # any of the current maximal sets.
    for next_set in sets_by_cardinality:
        for maximal_set in maximal_sets:
            if next_set < maximal_set:
                break
        # "else" on a for loop is triggered only if the loop falls through
        # without a break.  In this case, that means that the set was
        # *not* a subset of any existing maximal set, which means it's our
        # next entry.
        else:
            maximal_sets.append(next_set)

    return maximal_sets

def getKeyframes(maximal_footprints, footprints_to_frames, max_keyframes, filters):
    """Given a sorted list of frame footprints and a mapping of those footprints to lists
    of frames, select at most one frame from each footprint, up to the
    "max_keyframes" given, and return the list of keyframes (in order of discovery,
    i.e. the frame from the most maximal footprint comes first).  If filters are
    provided, they can disqualify a frame from consideration, which can result in
    no frames being selected from a particular footprint."""

    log.info('filtering %s keyframes from %s maximal footprints...',
             max_keyframes, len(maximal_footprints))
    keyframes = []
    for footprint in maximal_footprints:
        # Done if we've selected all we need.
        if len(keyframes) >= max_keyframes:
            break

        # Get a list of candidate frames from this footprint.
        candidate_frames = footprints_to_frames[footprint]

        # Select a frame; attempt all the filters on the frame.
        for first_frame, last_frame in Ranges(candidate_frames):
            frame = int((first_frame + last_frame)/2)
            for f in filters:
                if not f.allowed(frame, keyframes):
                    # Break means this frame failed the filter.
                    break
            else:
                # Here, we got through all the filters, so this frame
                # is a good candidate.
                keyframes.append(frame)
                # break here means we've found a good frame and can exit the "for frame" loop.
                break
        # end for each frame range
    # end for each footprint

    return keyframes


##################################################################
# Main program.
def main():
    """Main program function."""
    LOG_LEVELS = ['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG']
    LOG_FORMAT = '%(levelname)s: %(message)s'

    parser = argparse.ArgumentParser(description='identify interesting keyframes')
    parser.add_argument('--log-level', type=str.upper, default='INFO', choices=LOG_LEVELS,
                        help='the desired logging level [%(default)s]')
    common = parser.add_argument_group('common arguments')
    common.add_argument('-n', type=int, default=10,
                        help="how many keyframes to return [%(default)s]")

    filter_descriptions = [f'{x.name} ({x.description})' for x in Filter.__subclasses__()]
    filter_help = ('comma-separated list of keyframe filters to use [none]; available filters are: '
                   + '; '.join(filter_descriptions))
    common.add_argument('-f', '--filters', help=filter_help)
    common.add_argument('commandfile',
                        help="The command file to read")

    # Add command-line switches for all filters.
    for filter_class in Filter.__subclasses__():
        f = filter_class()
        f.preArgumentParse(parser)

    # Parse arguments.
    args = parser.parse_args()

    # Set up logging.
    logging.basicConfig(stream=sys.stdout, format=LOG_FORMAT, level=args.log_level)

    # Prepare a list of filters that we're actually going to use.
    filters = []
    if args.filters is not None:
        for filter_name in args.filters.split(','):
            f = getFilterByName(filter_name)
            filters.append(f)
            # Let active filters manage their arguments.
            f.postArgumentParse(args)

    # Read frames from the command file and convert to footprints.
    footprints_to_frames = getCommandFileFootprintsToFrames(args.commandfile)

    # Extract the maximal footprints from the dictionary.
    maximal_footprints = getMaximalSets(footprints_to_frames.keys())

    # Select a frame from each of our maximal footprints, until we
    # have the count we want or until we run out of footprints.
    keyframes = getKeyframes(maximal_footprints, footprints_to_frames, args.n, filters)

    # Give our output.
    log.info("Keyframes in priority order: %s", ",".join([str(x) for x in keyframes]))
    keyframes.sort()
    log.info("Keyframes in sorted order:   %s", ",".join([str(x) for x in keyframes]))

##################################################################
if __name__ == '__main__':
    main()