1*b8a1962dSJiri Olsa#!/usr/bin/env python
2*b8a1962dSJiri Olsa
3*b8a1962dSJiri Olsadata    = {}
4*b8a1962dSJiri Olsatimes   = []
5*b8a1962dSJiri Olsathreads = []
6*b8a1962dSJiri Olsacpus    = []
7*b8a1962dSJiri Olsa
8*b8a1962dSJiri Olsadef get_key(time, event, cpu, thread):
9*b8a1962dSJiri Olsa    return "%d-%s-%d-%d" % (time, event, cpu, thread)
10*b8a1962dSJiri Olsa
11*b8a1962dSJiri Olsadef store_key(time, cpu, thread):
12*b8a1962dSJiri Olsa    if (time not in times):
13*b8a1962dSJiri Olsa        times.append(time)
14*b8a1962dSJiri Olsa
15*b8a1962dSJiri Olsa    if (cpu not in cpus):
16*b8a1962dSJiri Olsa        cpus.append(cpu)
17*b8a1962dSJiri Olsa
18*b8a1962dSJiri Olsa    if (thread not in threads):
19*b8a1962dSJiri Olsa        threads.append(thread)
20*b8a1962dSJiri Olsa
21*b8a1962dSJiri Olsadef store(time, event, cpu, thread, val, ena, run):
22*b8a1962dSJiri Olsa    #print "event %s cpu %d, thread %d, time %d, val %d, ena %d, run %d" % \
23*b8a1962dSJiri Olsa    #      (event, cpu, thread, time, val, ena, run)
24*b8a1962dSJiri Olsa
25*b8a1962dSJiri Olsa    store_key(time, cpu, thread)
26*b8a1962dSJiri Olsa    key = get_key(time, event, cpu, thread)
27*b8a1962dSJiri Olsa    data[key] = [ val, ena, run]
28*b8a1962dSJiri Olsa
29*b8a1962dSJiri Olsadef get(time, event, cpu, thread):
30*b8a1962dSJiri Olsa    key = get_key(time, event, cpu, thread)
31*b8a1962dSJiri Olsa    return data[key][0]
32*b8a1962dSJiri Olsa
33*b8a1962dSJiri Olsadef stat__cycles_k(cpu, thread, time, val, ena, run):
34*b8a1962dSJiri Olsa    store(time, "cycles", cpu, thread, val, ena, run);
35*b8a1962dSJiri Olsa
36*b8a1962dSJiri Olsadef stat__instructions_k(cpu, thread, time, val, ena, run):
37*b8a1962dSJiri Olsa    store(time, "instructions", cpu, thread, val, ena, run);
38*b8a1962dSJiri Olsa
39*b8a1962dSJiri Olsadef stat__cycles_u(cpu, thread, time, val, ena, run):
40*b8a1962dSJiri Olsa    store(time, "cycles", cpu, thread, val, ena, run);
41*b8a1962dSJiri Olsa
42*b8a1962dSJiri Olsadef stat__instructions_u(cpu, thread, time, val, ena, run):
43*b8a1962dSJiri Olsa    store(time, "instructions", cpu, thread, val, ena, run);
44*b8a1962dSJiri Olsa
45*b8a1962dSJiri Olsadef stat__cycles(cpu, thread, time, val, ena, run):
46*b8a1962dSJiri Olsa    store(time, "cycles", cpu, thread, val, ena, run);
47*b8a1962dSJiri Olsa
48*b8a1962dSJiri Olsadef stat__instructions(cpu, thread, time, val, ena, run):
49*b8a1962dSJiri Olsa    store(time, "instructions", cpu, thread, val, ena, run);
50*b8a1962dSJiri Olsa
51*b8a1962dSJiri Olsadef stat__interval(time):
52*b8a1962dSJiri Olsa    for cpu in cpus:
53*b8a1962dSJiri Olsa        for thread in threads:
54*b8a1962dSJiri Olsa            cyc = get(time, "cycles", cpu, thread)
55*b8a1962dSJiri Olsa            ins = get(time, "instructions", cpu, thread)
56*b8a1962dSJiri Olsa            cpi = 0
57*b8a1962dSJiri Olsa
58*b8a1962dSJiri Olsa            if ins != 0:
59*b8a1962dSJiri Olsa                cpi = cyc/float(ins)
60*b8a1962dSJiri Olsa
61*b8a1962dSJiri Olsa            print "%15f: cpu %d, thread %d -> cpi %f (%d/%d)" % (time/(float(1000000000)), cpu, thread, cpi, cyc, ins)
62*b8a1962dSJiri Olsa
63*b8a1962dSJiri Olsadef trace_end():
64*b8a1962dSJiri Olsa    pass
65*b8a1962dSJiri Olsa# XXX trace_end callback could be used as an alternative place
66*b8a1962dSJiri Olsa#     to compute same values as in the script above:
67*b8a1962dSJiri Olsa#
68*b8a1962dSJiri Olsa#    for time in times:
69*b8a1962dSJiri Olsa#        for cpu in cpus:
70*b8a1962dSJiri Olsa#            for thread in threads:
71*b8a1962dSJiri Olsa#                cyc = get(time, "cycles", cpu, thread)
72*b8a1962dSJiri Olsa#                ins = get(time, "instructions", cpu, thread)
73*b8a1962dSJiri Olsa#
74*b8a1962dSJiri Olsa#                if ins != 0:
75*b8a1962dSJiri Olsa#                    cpi = cyc/float(ins)
76*b8a1962dSJiri Olsa#
77*b8a1962dSJiri Olsa#                print "time %.9f, cpu %d, thread %d -> cpi %f" % (time/(float(1000000000)), cpu, thread, cpi)
78