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