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