nv-report.py 4.26 KB
Newer Older
1 2 3 4 5
#!/usr/bin/python

"""
We're matching lines like

6 7
5.shader_test - type: 1, local: 0, shared: 0, gpr: 4, inst: 7, bytes: 56
11.shader_test - type: 1, local: 0, shared: 0, gpr: 4, inst: 1, bytes: 8
8 9 10

although the order of the fields after the dash does not matter, and all
fields, except for the type, are optional.
11 12 13 14 15
"""

import re
import sys

16 17 18 19 20 21 22

def getgroupvalue(m, groupname):
    if not m[groupname]:
        return 0
    else:
        return int(m[groupname].group(1), 10)

23 24 25 26
class Stat(object):

    def __init__(self, m=None):
        if m:
27
            self.local = getgroupvalue(m, "local")
28
            self.shared = getgroupvalue(m, "shared")
29 30 31
            self.gpr = getgroupvalue(m, "gpr")
            self.inst = getgroupvalue(m, "inst")
            self.bytes = getgroupvalue(m, "bytes")
32 33
        else:
            self.local = 0
34
            self.shared = 0
35 36 37 38 39 40
            self.gpr = 0
            self.inst = 0
            self.bytes = 0

    def __eq__(self, other):
        return (self.local == other.local and
41
                self.shared == other.shared and
42 43 44 45 46 47 48 49 50
                self.gpr == other.gpr and
                self.inst == other.inst and
                self.bytes == other.bytes)

class Stats(object):

    def __init__(self):
        self.stats = {}
        self.local = 0
51
        self.shared = 0
52 53 54 55 56 57 58
        self.gpr = 0
        self.inst = 0
        self.bytes = 0

    def record(self, name, stat):
        assert name not in self.stats, name
        self.stats[name] = stat
59
        for attr in ("local", "shared", "gpr", "inst", "bytes"):
60 61
            setattr(self, attr, getattr(self, attr) + getattr(stat, attr))

62 63 64 65
RE = {
        "name":   re.compile(r"^(.*) - "),
        "type":   re.compile(r"type: (\d+)"),
        "local":  re.compile(r"local: (\d+)"),
66
        "shared": re.compile(r"shared: (\d+)"),
67 68 69 70
        "gpr":    re.compile(r"gpr: (\d+)"),
        "inst":   re.compile(r"inst: (\d+)"),
        "bytes":  re.compile(r"bytes: (\d+)")
}
71 72 73 74 75 76 77

def analyze(fname):
    stats = Stats()
    with open(fname, "r") as f:
        for line in f.xreadlines():
            if line.startswith("Thread "):
                continue
78
            m = {}
79
            for attr in ("name", "type", "local", "shared", "gpr", "inst", "bytes"):
80 81 82 83
                m[attr] = RE[attr].search(line)
            assert m["name"], line
            assert m["type"], line
            stats.record(m["name"].group(1) + " - " + m["type"].group(1), Stat(m))
84 85 86 87

    return stats

def diff(a, b):
88 89 90 91 92 93
    percentage = 0.
    if a != 0.:
        percentage = b * 100. / a - 100.
    elif b != 0.:
        percentage = float('inf')
    return "%d -> %d (%.2f%%)" % (a, b, percentage)
94 95 96 97 98 99

def main(argv):
    # Count up each of the metrics in the before and after, and
    # produce hurt/helped comparisons.
    before = analyze(argv[1])
    after = analyze(argv[2])
100
    keys = set(before.stats.keys()) | set(after.stats.keys())
101 102 103 104

    helped = Stat()
    hurt = Stat()
    for key in keys:
105 106 107
        if key not in after.stats or key not in before.stats:
            print "Missing", key
            continue
108 109 110
        a = after.stats[key]
        b = before.stats[key]
        if a != b:
111
            for attr in ("local", "shared", "gpr", "inst", "bytes"):
112 113 114 115 116 117 118 119 120 121 122 123 124
                aa = getattr(a, attr)
                ba = getattr(b, attr)
                if aa == ba:
                    continue
                if aa < ba:
                    setattr(helped, attr,
                            getattr(helped, attr) + 1)
                else:
                    setattr(hurt, attr,
                            getattr(hurt, attr) + 1)

    print "total instructions in shared programs :", diff(before.inst, after.inst)
    print "total gprs used in shared programs    :", diff(before.gpr, after.gpr)
125
    print "total shared used in shared programs  :", diff(before.shared, after.shared)
126 127
    print "total local used in shared programs   :", diff(before.local, after.local)
    print
128
    print "%10s %10s %10s %10s %10s %10s " % ("", "local", "shared", "gpr", "inst", "bytes")
129
    print "%10s " % "helped",
130
    for attr in ("local", "shared", "gpr", "inst", "bytes"):
131 132 133
        print "%10d " % getattr(helped, attr),
    print
    print "%10s " % "hurt",
134
    for attr in ("local", "shared", "gpr", "inst", "bytes"):
135 136 137 138 139
        print "%10d " % getattr(hurt, attr),


if __name__ == "__main__":
    main(sys.argv)