1bf215546Sopenharmony_ci#!/usr/bin/env python3
2bf215546Sopenharmony_ci# Copyright © Microsoft Corporation
3bf215546Sopenharmony_ci#
4bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci# copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci# to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci# and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci# Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci#
11bf215546Sopenharmony_ci# The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci# paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci# Software.
14bf215546Sopenharmony_ci#
15bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci# IN THE SOFTWARE.
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ciimport argparse
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci# Take a log file produced by GALLIUM_REFCNT_LOG, filter it to the objects that
26bf215546Sopenharmony_ci# weren't destroyed by the end of the log, and write the results out sorted.
27bf215546Sopenharmony_ci# Strips stacks by default to prevent OOM. Could probably be rewritten to walk
28bf215546Sopenharmony_ci# the file twice to preserve stacks without OOM, but this was the easy way.
29bf215546Sopenharmony_cidef main():
30bf215546Sopenharmony_ci    parser = argparse.ArgumentParser()
31bf215546Sopenharmony_ci    parser.add_argument('--input',
32bf215546Sopenharmony_ci                        action='store',
33bf215546Sopenharmony_ci                        required=True,
34bf215546Sopenharmony_ci                        help='path to file containing refcount log')
35bf215546Sopenharmony_ci    parser.add_argument('--output',
36bf215546Sopenharmony_ci                        action='store',
37bf215546Sopenharmony_ci                        required=True,
38bf215546Sopenharmony_ci                        help='path to trimmed log')
39bf215546Sopenharmony_ci    parser.add_argument('--filter',
40bf215546Sopenharmony_ci                        help='object type filter')
41bf215546Sopenharmony_ci    parser.add_argument('--keep-stacks',
42bf215546Sopenharmony_ci                        help='keep stacks, otherwise only headers')
43bf215546Sopenharmony_ci    args = parser.parse_args()
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci    objects = {}
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci    with open(args.input) as in_file:
48bf215546Sopenharmony_ci        stack = []
49bf215546Sopenharmony_ci        cur_object = ''
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci        for line in in_file:
52bf215546Sopenharmony_ci            if line[0] == '<':
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci                parts = line.split(' ')
55bf215546Sopenharmony_ci                prev_object = cur_object
56bf215546Sopenharmony_ci                cur_object = parts[1]
57bf215546Sopenharmony_ci                if parts[3].strip() == 'Destroy':
58bf215546Sopenharmony_ci                    if cur_object in objects:
59bf215546Sopenharmony_ci                        del objects[cur_object]
60bf215546Sopenharmony_ci                else:
61bf215546Sopenharmony_ci                    if parts[3].strip() == 'Create':
62bf215546Sopenharmony_ci                        if (not args.filter) or (args.filter in parts[0]):
63bf215546Sopenharmony_ci                            objects[cur_object] = []
64bf215546Sopenharmony_ci                    if prev_object in objects:
65bf215546Sopenharmony_ci                        objects[prev_object] += stack
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci                stack = [line]
68bf215546Sopenharmony_ci            elif args.keep_stacks:
69bf215546Sopenharmony_ci                stack += line
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci    with open(args.output, 'wt') as out_file:
72bf215546Sopenharmony_ci        for stack in objects.values():
73bf215546Sopenharmony_ci            for stack_line in stack:
74bf215546Sopenharmony_ci                out_file.write(stack_line)
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ciif __name__ == '__main__':
78bf215546Sopenharmony_ci    main()
79