blob: 4389c86fc38715f56225e8fb746a96b6e882409d [file] [log] [blame]
Damjan Marion47d165e2019-01-28 13:27:31 +01001#!/usr/bin/env python3
2
3import json, argparse
4
5p = argparse.ArgumentParser()
6
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02007p.add_argument(
8 "-i", "--input", action="store", help="input JSON file name", required=True
9)
Damjan Marion47d165e2019-01-28 13:27:31 +010010
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020011p.add_argument(
12 "-o", "--output", action="store", help="output C file name", required=True
13)
Damjan Marion47d165e2019-01-28 13:27:31 +010014
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020015p.add_argument(
16 "-m",
17 "--model",
18 action="append",
19 help="CPU model in format: model[,stepping0]",
20 required=True,
21)
Damjan Marion47d165e2019-01-28 13:27:31 +010022
23r = p.parse_args()
24
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020025with open(r.input, "r") as fp:
Damjan Marion47d165e2019-01-28 13:27:31 +010026 objects = json.load(fp)
27
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020028c = open(r.output, "w")
Damjan Marion47d165e2019-01-28 13:27:31 +010029
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020030c.write(
31 """
Damjan Marion47d165e2019-01-28 13:27:31 +010032#include <perfmon/perfmon_intel.h>
33
34static perfmon_intel_pmc_cpu_model_t cpu_model_table[] = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020035"""
36)
Damjan Marion47d165e2019-01-28 13:27:31 +010037
38for v in r.model:
39 if "," in v:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020040 (m, s) = v.split(",")
Damjan Marion47d165e2019-01-28 13:27:31 +010041 m = int(m, 0)
42 s = int(s, 0)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020043 c.write(" {}0x{:02X}, 0x{:02X}, 1{},\n".format("{", m, s, "}"))
Damjan Marion47d165e2019-01-28 13:27:31 +010044 else:
45 m = int(v, 0)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020046 c.write(" {}0x{:02X}, 0x00, 0{},\n".format("{", m, "}"))
47c.write(
48 """
Damjan Marion47d165e2019-01-28 13:27:31 +010049};
50
51static perfmon_intel_pmc_event_t event_table[] = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020052"""
53)
Damjan Marion47d165e2019-01-28 13:27:31 +010054
55for obj in objects:
56 MSRIndex = obj["MSRIndex"]
57 if MSRIndex != "0":
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020058 continue
Damjan Marion47d165e2019-01-28 13:27:31 +010059
60 EventCode = obj["EventCode"]
61 UMask = obj["UMask"]
62 EventName = obj["EventName"].lower()
63 if "," in EventCode:
64 continue
65
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020066 c.write(" {\n")
67 c.write(" .event_code = {}{}{},\n".format("{", EventCode, "}"))
68 c.write(" .umask = {},\n".format(UMask))
69 c.write(' .event_name = "{}",\n'.format(EventName))
70 c.write(" },\n")
Damjan Marion47d165e2019-01-28 13:27:31 +010071
72
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020073c.write(
74 """ {
Damjan Marion47d165e2019-01-28 13:27:31 +010075 .event_name = 0,
76 },
77};
78
79PERFMON_REGISTER_INTEL_PMC (cpu_model_table, event_table);
80
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020081"""
82)
Damjan Marion47d165e2019-01-28 13:27:31 +010083
84c.close()