162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_cifrom __future__ import print_function
462306a36Sopenharmony_ci
562306a36Sopenharmony_cidata    = {}
662306a36Sopenharmony_citimes   = []
762306a36Sopenharmony_cithreads = []
862306a36Sopenharmony_cicpus    = []
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cidef get_key(time, event, cpu, thread):
1162306a36Sopenharmony_ci    return "%d-%s-%d-%d" % (time, event, cpu, thread)
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cidef store_key(time, cpu, thread):
1462306a36Sopenharmony_ci    if (time not in times):
1562306a36Sopenharmony_ci        times.append(time)
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci    if (cpu not in cpus):
1862306a36Sopenharmony_ci        cpus.append(cpu)
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci    if (thread not in threads):
2162306a36Sopenharmony_ci        threads.append(thread)
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_cidef store(time, event, cpu, thread, val, ena, run):
2462306a36Sopenharmony_ci    #print("event %s cpu %d, thread %d, time %d, val %d, ena %d, run %d" %
2562306a36Sopenharmony_ci    #      (event, cpu, thread, time, val, ena, run))
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci    store_key(time, cpu, thread)
2862306a36Sopenharmony_ci    key = get_key(time, event, cpu, thread)
2962306a36Sopenharmony_ci    data[key] = [ val, ena, run]
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cidef get(time, event, cpu, thread):
3262306a36Sopenharmony_ci    key = get_key(time, event, cpu, thread)
3362306a36Sopenharmony_ci    return data[key][0]
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cidef stat__cycles_k(cpu, thread, time, val, ena, run):
3662306a36Sopenharmony_ci    store(time, "cycles", cpu, thread, val, ena, run);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cidef stat__instructions_k(cpu, thread, time, val, ena, run):
3962306a36Sopenharmony_ci    store(time, "instructions", cpu, thread, val, ena, run);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cidef stat__cycles_u(cpu, thread, time, val, ena, run):
4262306a36Sopenharmony_ci    store(time, "cycles", cpu, thread, val, ena, run);
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cidef stat__instructions_u(cpu, thread, time, val, ena, run):
4562306a36Sopenharmony_ci    store(time, "instructions", cpu, thread, val, ena, run);
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cidef stat__cycles(cpu, thread, time, val, ena, run):
4862306a36Sopenharmony_ci    store(time, "cycles", cpu, thread, val, ena, run);
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cidef stat__instructions(cpu, thread, time, val, ena, run):
5162306a36Sopenharmony_ci    store(time, "instructions", cpu, thread, val, ena, run);
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cidef stat__interval(time):
5462306a36Sopenharmony_ci    for cpu in cpus:
5562306a36Sopenharmony_ci        for thread in threads:
5662306a36Sopenharmony_ci            cyc = get(time, "cycles", cpu, thread)
5762306a36Sopenharmony_ci            ins = get(time, "instructions", cpu, thread)
5862306a36Sopenharmony_ci            cpi = 0
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci            if ins != 0:
6162306a36Sopenharmony_ci                cpi = cyc/float(ins)
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci            print("%15f: cpu %d, thread %d -> cpi %f (%d/%d)" % (time/(float(1000000000)), cpu, thread, cpi, cyc, ins))
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cidef trace_end():
6662306a36Sopenharmony_ci    pass
6762306a36Sopenharmony_ci# XXX trace_end callback could be used as an alternative place
6862306a36Sopenharmony_ci#     to compute same values as in the script above:
6962306a36Sopenharmony_ci#
7062306a36Sopenharmony_ci#    for time in times:
7162306a36Sopenharmony_ci#        for cpu in cpus:
7262306a36Sopenharmony_ci#            for thread in threads:
7362306a36Sopenharmony_ci#                cyc = get(time, "cycles", cpu, thread)
7462306a36Sopenharmony_ci#                ins = get(time, "instructions", cpu, thread)
7562306a36Sopenharmony_ci#
7662306a36Sopenharmony_ci#                if ins != 0:
7762306a36Sopenharmony_ci#                    cpi = cyc/float(ins)
7862306a36Sopenharmony_ci#
7962306a36Sopenharmony_ci#                print("time %.9f, cpu %d, thread %d -> cpi %f" % (time/(float(1000000000)), cpu, thread, cpi))
80