tests: replace pycodestyle with black
[vpp.git] / extras / deprecated / perfmon / intel_json_to_c.py
1 #!/usr/bin/env python3
2
3 import json, argparse
4
5 p = argparse.ArgumentParser()
6
7 p.add_argument(
8     "-i", "--input", action="store", help="input JSON file name", required=True
9 )
10
11 p.add_argument(
12     "-o", "--output", action="store", help="output C file name", required=True
13 )
14
15 p.add_argument(
16     "-m",
17     "--model",
18     action="append",
19     help="CPU model in format: model[,stepping0]",
20     required=True,
21 )
22
23 r = p.parse_args()
24
25 with open(r.input, "r") as fp:
26     objects = json.load(fp)
27
28 c = open(r.output, "w")
29
30 c.write(
31     """
32 #include <perfmon/perfmon_intel.h>
33
34 static perfmon_intel_pmc_cpu_model_t cpu_model_table[] = {
35 """
36 )
37
38 for v in r.model:
39     if "," in v:
40         (m, s) = v.split(",")
41         m = int(m, 0)
42         s = int(s, 0)
43         c.write("  {}0x{:02X}, 0x{:02X}, 1{},\n".format("{", m, s, "}"))
44     else:
45         m = int(v, 0)
46         c.write("  {}0x{:02X}, 0x00, 0{},\n".format("{", m, "}"))
47 c.write(
48     """
49 };
50
51 static perfmon_intel_pmc_event_t event_table[] = {
52 """
53 )
54
55 for obj in objects:
56     MSRIndex = obj["MSRIndex"]
57     if MSRIndex != "0":
58         continue
59
60     EventCode = obj["EventCode"]
61     UMask = obj["UMask"]
62     EventName = obj["EventName"].lower()
63     if "," in EventCode:
64         continue
65
66     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")
71
72
73 c.write(
74     """  {
75    .event_name = 0,
76    },
77 };
78
79 PERFMON_REGISTER_INTEL_PMC (cpu_model_table, event_table);
80
81 """
82 )
83
84 c.close()