summaryrefslogtreecommitdiff
path: root/graphics/asymptote/profile.py
blob: bfdd52015bc77ef86e9dc7f1ac234d78942d0510 (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
#!/usr/bin/env python3

import sys
from pprint import pprint

# Unused line numbers required by kcachegrind.
POS = '1'

def nameFromNode(tree):
    name = tree['name']
    pos = tree['pos']
    if pos.endswith(": "):
        pos = pos[:-2]
    return (name, pos)

def addFuncNames(tree, s):
    s.add(nameFromNode(tree))
    for child in tree['children']:
        addFuncNames(child, s)

def funcNames(tree):
    s = set()
    addFuncNames(tree, s)
    return s

def computeTotals(tree):
    for child in tree['children']:
        computeTotals(child)
    tree['instTotal'] = (tree['instructions']
                     + sum(child['instTotal'] for child in tree['children']))
    tree['nsecsTotal'] = (tree['nsecs']
                     + sum(child['nsecsTotal'] for child in tree['children']))

def printName(name, prefix=''):
    print (prefix+"fl=", name[1])
    print (prefix+"fn=", name[0])

class Arc:
    def __init__(self):
        self.calls = 0
        self.instTotal = 0
        self.nsecsTotal = 0

    def add(self, tree):
        self.calls += tree['calls']
        self.instTotal += tree['instTotal']
        self.nsecsTotal += tree['nsecsTotal']

class Func:
    def __init__(self):
        self.instructions = 0
        self.nsecs = 0
        self.arcs = {}

    def addChildTime(self, tree):
        arc = self.arcs.setdefault(nameFromNode(tree), Arc())
        arc.add(tree)

    def analyse(self, tree):
        self.instructions += tree['instructions']
        self.nsecs += tree['nsecs']
        for child in tree['children']:
            self.addChildTime(child)

    def dump(self):
        print (POS, self.instructions, self.nsecs)
        for name in self.arcs:
            printName(name, prefix='c')
            arc = self.arcs[name]
            print ("calls="+str(arc.calls), POS)
            print (POS, arc.instTotal, arc.nsecsTotal)
        print ()

def analyse(funcs, tree):
    funcs[nameFromNode(tree)].analyse(tree)
    for child in tree['children']:
        analyse(funcs, child)

def dump(funcs):
    print ("events: Instructions Nanoseconds")
    for name in funcs:
        printName(name)
        funcs[name].dump()

rawdata = __import__("asyprof")
profile = rawdata.profile

computeTotals(profile)
names = funcNames(profile)

funcs = {}
for name in names:
    funcs[name] = Func()

analyse(funcs, profile)
dump(funcs)

#pprint(names)