getpapi.py 2.18 KB
Newer Older
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
import os, sys, json, shutil, glob
from subprocess import Popen, PIPE

outdir = "papi_hl_output"

def run(cmds):
    proc = Popen(cmds, stdout=PIPE, stderr=PIPE, shell=True)
    outs, errs = proc.communicate()
    return proc.poll()

data = {
    "cpu in mhz": None,
    "region_count": None,
    "cycles": [],
    "perf::TASK-CLOCK": [],
}


def unpack(event, d):

    clock_speed = int(d["cpu in mhz"])

    if data["cpu in mhz"] is None:
        data["cpu in mhz"] = clock_speed

    elif data["cpu in mhz"] != clock_speed:
        raise Exception("Clock speed mismatch: %d vs. %d" % (data["cpu in mhz"], clock_speed))

    if "threads" not in d:
        print("Warning: %s event did not collected." % event)
        return

    for key, value in d["threads"][0]["regions"][0]["main"].items():
        num = int(value)

        if key == "region_count":
            if data["region_count"] is None:
                data["region_count"] = num

            elif data["region_count"] != num:
                raise Exception("Region count mismatch: %d vs. %d" % (data["region_count"], num))

        elif key == "cycles":
            cycles = num
            if data["cycles"] is None:
                data["cycles"] = num

            else:
                data["cycles"].append(num)

        elif key == "perf::TASK-CLOCK":
            cycles = num
            if data["perf::TASK-CLOCK"] is None:
                data["perf::TASK-CLOCK"] = num

            else:
                data["perf::TASK-CLOCK"].append(num)

        else:
            data[key] = num

with open(sys.argv[1]) as f:
    for line in f:
        event = line.strip()
65
        cmds = "export PAPI_EVENTS=%s; %s" % (event, sys.argv[2])
66
67
68
69
70

        if os.path.exists(outdir):
            shutil.rmtree(outdir)

        run(cmds)
71
72
73
74
75
76
77
78
79

        if os.path.isdir(outdir):
            for path in os.listdir(outdir):
                if path.startswith("rank"):
                    with open(os.path.join(outdir, path)) as j:
                        _d = json.load(j)
                        unpack(event, _d)
        else:
            print("Warning: kernel execution for papi %s is failed." % event)
80

81
    with open("kernel_papi.json", "w") as kp:
82
        json.dump(data, kp)