Damjan Marion | 47d165e | 2019-01-28 13:27:31 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import json, argparse |
| 4 | |
| 5 | p = argparse.ArgumentParser() |
| 6 | |
| 7 | p.add_argument('-i', '--input', action="store", |
| 8 | help="input JSON file name", required = True) |
| 9 | |
| 10 | p.add_argument('-o', '--output', action="store", |
| 11 | help="output C file name", required = True) |
| 12 | |
| 13 | p.add_argument('-m', '--model', action="append", |
| 14 | help="CPU model in format: model[,stepping0]", |
| 15 | required = True) |
| 16 | |
| 17 | r = p.parse_args() |
| 18 | |
| 19 | with open(r.input, 'r') as fp: |
| 20 | objects = json.load(fp) |
| 21 | |
| 22 | c = open(r.output, 'w') |
| 23 | |
| 24 | c.write (""" |
| 25 | #include <perfmon/perfmon_intel.h> |
| 26 | |
| 27 | static perfmon_intel_pmc_cpu_model_t cpu_model_table[] = { |
| 28 | """) |
| 29 | |
| 30 | for v in r.model: |
| 31 | if "," in v: |
| 32 | (m, s) = v.split(",") |
| 33 | m = int(m, 0) |
| 34 | s = int(s, 0) |
| 35 | c.write (" {}0x{:02X}, 0x{:02X}, 1{},\n".format("{", m, s, "}")) |
| 36 | else: |
| 37 | m = int(v, 0) |
| 38 | c.write (" {}0x{:02X}, 0x00, 0{},\n".format("{", m, "}")) |
| 39 | c.write (""" |
| 40 | }; |
| 41 | |
| 42 | static perfmon_intel_pmc_event_t event_table[] = { |
| 43 | """) |
| 44 | |
| 45 | for obj in objects: |
| 46 | MSRIndex = obj["MSRIndex"] |
| 47 | if MSRIndex != "0": |
| 48 | continue |
| 49 | |
| 50 | EventCode = obj["EventCode"] |
| 51 | UMask = obj["UMask"] |
| 52 | EventName = obj["EventName"].lower() |
| 53 | if "," in EventCode: |
| 54 | continue |
| 55 | |
| 56 | c.write (" {\n") |
| 57 | c.write (" .event_code = {}{}{},\n".format("{", EventCode, "}")) |
| 58 | c.write (" .umask = {},\n".format(UMask)) |
| 59 | c.write (" .event_name = \"{}\",\n".format(EventName)) |
| 60 | c.write (" },\n") |
| 61 | |
| 62 | |
| 63 | c.write (""" { |
| 64 | .event_name = 0, |
| 65 | }, |
| 66 | }; |
| 67 | |
| 68 | PERFMON_REGISTER_INTEL_PMC (cpu_model_table, event_table); |
| 69 | |
| 70 | """) |
| 71 | |
| 72 | c.close() |