aboutsummaryrefslogtreecommitdiff
path: root/scripts/enable_tracing.py
blob: 842c58edf188cd45e0adb9a4184e5f69cf0e4e4c (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
#
# Copyright (c) 2020 Arm Limited.
#
# SPDX-License-Identifier: MIT
#
# 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.
#
#
#!/usr/bin/env python
import re
import os
import sys
import argparse
import fnmatch
import logging

import json
import glob

logger = logging.getLogger("acl_tracing")

# Returns the files matching the given pattern
def find(path, pattern):
    matches = []
    for root, dirnames, filenames, in os.walk(path):
        for filename in fnmatch.filter(filenames, pattern):
            matches.append(os.path.join(root,filename))
    return matches

# Returns the class name (Core or Runtime) and arguments of the given function
def get_class_and_args(function):
    decl = " ".join(function_signature)
    m = re.match("void ([^:]+)::configure\(([^)]*)\)", decl)
    if m:
            assert m, "Can't parse '%s'" % line
            class_name = m.group(1)
            args = m.group(2)
            #Remove comments:
            args = re.sub("\/\*.*?\*\/","",args)
            #Remove templates
            args = re.sub("<.*?>","",args)
            logger.debug(args)
            arg_names = []
            for arg in args.split(","):
                m = re.match(".*?([^ &*]+)$", arg.strip())
                arg_names.append(m.group(1))
                logger.debug("  %s" % m.group(1))
            return (class_name, arg_names)
    else:
        return ('','')

# Adds the tracepoints to the source file for the given function
def do_insert_tracing(source, function, fd):
    logger.debug("Full signature = %s" % " ".join(function_signature))
    class_name, arg_names = get_class_and_args(function)
    if len(arg_names):
        assert len(arg_names), "No argument to configure for %s ?" % class_name
        spaces = re.match("([ ]*)void", function[0]).group(1)
        fd.write("%s    CREATE_TRACEPOINT(%s, \"%s\", this, TracePoint::Args()" % (spaces, source, class_name))
        for arg in arg_names:
            fd.write("<<%s" % arg)
        fd.write(");\n")
    else:
        print('Failed to get class name in %s ' % " ".join(function_signature))


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
            formatter_class=argparse.RawDescriptionHelpFormatter,
            description="Post process JSON benchmark files",
    )

    parser.add_argument("-D", "--debug", action='store_true', help="Enable script debugging output")
    args = parser.parse_args()
    logging_level = logging.INFO
    if args.debug:
        logging_level = logging.DEBUG
    logging.basicConfig(level=logging_level)
    logger.debug("Arguments passed: %s" % str(args.__dict__))
    for f in find("src","*.cpp"):
        logger.debug(f)
        fd = open(f,'r+')
        lines = fd.readlines()
        contains_configure = False
        for line in lines:
            if re.search(r"void.*::configure\(",line):
                contains_configure = True
                break
        if not contains_configure:
            continue
        fd.seek(0)
        fd.truncate()
        function_signature = None
        insert_tracing = False
        start = True
        for line in lines:
            write = True
            if start:
                if not (line.startswith("/*") or line.startswith(" *") or line.startswith("#") or len(line.strip()) == 0):
                    start = False
                    fd.write("#include \"arm_compute/core/TracePoint.h\"\n")
            elif not function_signature:
                if re.search(r"void.*::configure\(",line):
                    function_signature = [ line.rstrip() ]
            else:
                if re.search("[ ]*{$", line):
                    insert_tracing = True
                else:
                    function_signature.append(line.rstrip())
            if write:
                fd.write(line)
            if insert_tracing:
                if "/core/" in f:
                    source = "TracePoint::Layer::CORE"
                elif "/runtime/" in f:
                    source = "TracePoint::Layer::RUNTIME"
                else:
                    assert "Can't find layer for file %s" %f
                do_insert_tracing(source, function_signature, fd)
                insert_tracing = False
                function_signature = None