File: verilator_profcfunc

package info (click to toggle)
verilator 5.044-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 69,096 kB
  • sloc: cpp: 152,937; python: 22,624; ansic: 11,002; yacc: 6,111; lex: 2,011; makefile: 1,428; sh: 603; perl: 302
file content (192 lines) | stat: -rwxr-xr-x 7,532 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
#!/usr/bin/env python3
# pylint: disable=C0103,C0114,C0116,C0209,R0914,R0912,R0915,eval-used
######################################################################

import argparse
import collections
import re
# from pprint import pprint

######################################################################


def profcfunc(filename: str) -> None:
    funcs = {}

    with open(filename, "r", encoding="utf8") as fh:

        for line in fh:
            #                  %time      cumesec   selfsec     calls     {stuff}   name
            match = re.match(
                r'^\s*([0-9.]+)\s+[0-9.]+\s+([0-9.]+)\s+([0-9.]+)\s+[^a-zA-Z_]*([a-zA-Z_].*)$',
                line)
            if match:
                pct = float(match.group(1))
                sec = float(match.group(2))
                calls = float(match.group(3))
                func = match.group(4)
                if func not in funcs:
                    funcs[func] = {'pct': 0.0, 'sec': 0.0, 'calls': 0}
                funcs[func]['pct'] += pct
                funcs[func]['sec'] += sec
                funcs[func]['calls'] += calls
                continue

            # Older gprofs have no call column for single-call functions
            #                  %time      cumesec   selfsec      {stuff}   name
            match = re.match(r'^\s*([0-9.]+)\s+[0-9.]+\s+([0-9.]+)\s+[^a-zA-Z_]*([a-zA-Z_].*)$',
                             line)
            if match:
                pct = float(match.group(1))
                sec = float(match.group(2))
                calls = 1
                func = match.group(3)
                if func not in funcs:
                    funcs[func] = {'pct': 0, 'sec': 0, 'calls': 0}
                funcs[func]['pct'] += pct
                funcs[func]['sec'] += sec
                funcs[func]['calls'] += calls
                continue

    # Find modules
    verilated_mods = {}
    for func in funcs:
        match = re.search(r'(.*)::eval(_step)?\(', func)
        if match:
            prefix = match.group(1)
            if Args.debug:
                print("-got _eval %s   prefix=%s" % (func, prefix))
            verilated_mods[prefix] = re.compile(r'^' + prefix)
    # pprint(verilated_mods)

    # Sort by Verilog name
    vfuncs = {}
    groups = {}
    groups['type'] = collections.defaultdict(lambda: 0)
    groups['design'] = collections.defaultdict(lambda: 0)
    groups['module'] = collections.defaultdict(lambda: 0)

    for func, func_item in funcs.items():
        pct = func_item['pct']
        vfunc = func

        funcarg = re.sub(r'^.*\(', '', func)

        design = None
        for vde, vde_item in verilated_mods.items():
            if vde_item.match(func) or vde_item.match(funcarg):
                design = vde
                break

        vdesign = "-"

        prof_match = re.search(r'__PROF__([a-zA-Z_0-9]+)__l?([0-9]+)\(', vfunc)
        if design and prof_match:
            linefunc = prof_match.group(1)
            lineno = int(prof_match.group(2))
            vfunc = "VBlock    %s:%d" % (linefunc, lineno)
            vdesign = design
            groups['type']["Verilog Blocks under " + design] += pct
            groups['design'][design] += pct
            groups['module'][linefunc] += pct
        elif design:
            vfunc = "VCommon   " + func
            vdesign = design
            groups['type']["Common code under " + design] += pct
            groups['design'][design] += pct
            groups['module'][design + " common code"] += pct
        elif re.match(r'(VL_[A-Z0-9_]+|_?vl_[a-zA-Z0-9_]+|Verilated)', vfunc):
            vfunc = "VLib      " + func
            groups['type']['VLib'] += pct
            groups['design']['VLib'] += pct
            groups['module']['VLib'] += pct
        elif re.match(r'^_mcount_private', vfunc):
            vfunc = "Prof      " + func
            groups['type']['Prof'] += pct
            groups['design']['Prof'] += pct
            groups['module']['Prof'] += pct
        else:
            vfunc = "C++       " + func
            groups['type']['C++'] += pct
            groups['design']['C++'] += pct
            groups['module']['C++'] += pct

        if vfunc not in vfuncs:
            vfuncs[vfunc] = func_item
            vfuncs[vfunc]['design'] = vdesign
        else:
            vfuncs[vfunc]['pct'] += func_item['pct']
            vfuncs[vfunc]['calls'] += func_item['calls']
            vfuncs[vfunc]['sec'] += func_item['sec']

    for ftype in ['type', 'design', 'module']:
        missing = 100
        for item in groups[ftype].keys():
            missing -= groups[ftype][item]
        groups[ftype]["\377Unaccounted for/rounding error"] = missing

        print("Overall summary by %s:" % ftype)
        print("  %-6s  %s" % ("% time", ftype))
        for what in sorted(groups[ftype].keys()):
            # \377 used to establish sort order
            pwhat = re.sub(r'^\377', '', what)
            print("  %6.2f  %s" % (groups[ftype][what], pwhat))
        print()

    design_width = 1
    for func, func_item in vfuncs.items():
        design_width = max(design_width, len(func_item['design']))

    print("Verilog code profile:")
    print("   These are split into three categories:")
    print("      C++:     Time in non-Verilated C++ code")
    print("      Prof:    Time in profile overhead")
    print("      VBlock:  Time attributable to a block in a" + " Verilog file and line")
    print("      VCommon: Time in a Verilated module," + " due to all parts of the design")
    print("      VLib:    Time in Verilated common libraries," + " called by the Verilated code")
    print()

    print("  %   cumulative   self              ")
    print((" time   seconds   seconds      calls   %-" + str(design_width) +
           "s  type      filename and line number") % "design")

    cume = 0.0
    for func in sorted(vfuncs.keys(), key=lambda f: vfuncs[f]['sec'], reverse=True):
        cume += vfuncs[func]['sec']
        print(("%6.2f %9.2f %8.2f %10d   %-" + str(design_width) + "s  %s") %
              (vfuncs[func]['pct'], cume, vfuncs[func]['sec'], vfuncs[func]['calls'],
               vfuncs[func]['design'], func))


######################################################################
######################################################################

parser = argparse.ArgumentParser(
    allow_abbrev=False,
    formatter_class=argparse.RawDescriptionHelpFormatter,
    description="""Read gprof report created with Verilator --prof-cfuncs

Verilator_profcfunc reads a profile report created by gprof.  The names of
the functions are then transformed, assuming the user used Verilator's
--prof-cfuncs, and a report printed showing the percentage of time, etc,
in each Verilog block.

For documentation see
https://verilator.org/guide/latest/exe_verilator_profcfunc.html""",
    epilog="""Copyright 2002-2026 by Wilson Snyder. This program is free software; you
can redistribute it and/or modify it under the terms of either the GNU
Lesser General Public License Version 3 or the Perl Artistic License
Version 2.0.

SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0""")

parser.add_argument('--debug', action='store_const', const=9, help='enable debug')
parser.add_argument('filename', help='input gprof output to process')

Args = parser.parse_args()
profcfunc(Args.filename)

######################################################################
# Local Variables:
# compile-command: "./verilator_profcfunc ../test_regress/t/t_profcfunc.gprof"
# End: