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