xref: /linux/tools/perf/pmu-events/make_legacy_cache.py (revision 9e906a9dead17d81d6c2687f65e159231d0e3286)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
3import json
4
5hw_cache_id = [
6    (0, # PERF_COUNT_HW_CACHE_L1D
7     ["L1-dcache",  "l1-d",     "l1d",      "L1-data",],
8     [0, 1, 2,], # read, write, prefetch
9     "Level 1 data cache",
10     ),
11    (1, # PERF_COUNT_HW_CACHE_L1I
12     ["L1-icache",  "l1-i",     "l1i",      "L1-instruction",],
13     [0, 2,], # read, prefetch
14     "Level 1 instruction cache",
15     ),
16    (2, # PERF_COUNT_HW_CACHE_LL
17     ["LLC", "L2"],
18     [0, 1, 2,], # read, write, prefetch
19     "Last level cache",
20     ),
21    (3, # PERF_COUNT_HW_CACHE_DTLB
22     ["dTLB",   "d-tlb",    "Data-TLB",],
23     [0, 1, 2,], # read, write, prefetch
24     "Data TLB",
25     ),
26    (4, # PERF_COUNT_HW_CACHE_ITLB
27     ["iTLB",   "i-tlb",    "Instruction-TLB",],
28     [0,], # read
29     "Instruction TLB",
30     ),
31    (5, # PERF_COUNT_HW_CACHE_BPU
32     ["branch", "branches", "bpu",      "btb",      "bpc",],
33     [0,], # read
34     "Branch prediction unit",
35     ),
36    (6, # PERF_COUNT_HW_CACHE_NODE
37     ["node",],
38     [0, 1, 2,], # read, write, prefetch
39     "Local memory",
40     ),
41]
42
43hw_cache_op = [
44    (0, # PERF_COUNT_HW_CACHE_OP_READ
45     ["load",   "loads",    "read",],
46     "read"),
47    (1, # PERF_COUNT_HW_CACHE_OP_WRITE
48     ["store",  "stores",   "write",],
49     "write"),
50    (2, # PERF_COUNT_HW_CACHE_OP_PREFETCH
51     ["prefetch",   "prefetches",   "speculative-read", "speculative-load",],
52     "prefetch"),
53]
54
55hw_cache_result = [
56    (0, # PERF_COUNT_HW_CACHE_RESULT_ACCESS
57     ["refs",   "Reference",    "ops",      "access",],
58     "accesses"),
59    (1, # PERF_COUNT_HW_CACHE_RESULT_MISS
60     ["misses", "miss",],
61     "misses"),
62]
63
64events = []
65def add_event(name: str,
66              cache_id: int, cache_op: int, cache_result: int,
67              desc: str,
68              deprecated: bool) -> None:
69    # Avoid conflicts with PERF_TYPE_HARDWARE events which are higher priority.
70    if name in ["branch-misses", "branches"]:
71        return
72
73    # Tweak and deprecate L2 named events.
74    if name.startswith("L2"):
75        desc = desc.replace("Last level cache", "Level 2 (or higher) last level cache")
76        deprecated = True
77
78    event = {
79        "EventName": name,
80        "BriefDescription": desc,
81        "LegacyCacheCode": f"0x{cache_id | (cache_op << 8) | (cache_result << 16):06x}",
82    }
83
84    # Deprecate events with the name starting L2 as it is actively
85    # confusing as on many machines it actually means the L3 cache.
86    if deprecated:
87        event["Deprecated"] = "1"
88    events.append(event)
89
90for (cache_id, names, ops, cache_desc) in hw_cache_id:
91    for name in names:
92        add_event(name,
93                  cache_id,
94                  0, # PERF_COUNT_HW_CACHE_OP_READ
95                  0, # PERF_COUNT_HW_CACHE_RESULT_ACCESS
96                  f"{cache_desc} read accesses.",
97                  deprecated=True)
98
99        for (op, op_names, op_desc) in hw_cache_op:
100            if op not in ops:
101                continue
102            for op_name in op_names:
103                deprecated = (names[0] != name or op_names[1] != op_name)
104                add_event(f"{name}-{op_name}",
105                          cache_id,
106                          op,
107                          0, # PERF_COUNT_HW_CACHE_RESULT_ACCESS
108                          f"{cache_desc} {op_desc} accesses.",
109                          deprecated)
110
111                for (result,  result_names, result_desc) in hw_cache_result:
112                    for result_name in result_names:
113                        deprecated = ((names[0] != name or op_names[0] != op_name) or
114                                      (result == 0) or (result_names[0] != result_name))
115                        add_event(f"{name}-{op_name}-{result_name}",
116                                  cache_id, op, result,
117                                  f"{cache_desc} {op_desc} {result_desc}.",
118                                  deprecated)
119
120        for (result,  result_names, result_desc) in hw_cache_result:
121            for result_name in result_names:
122                add_event(f"{name}-{result_name}",
123                          cache_id,
124                          0, # PERF_COUNT_HW_CACHE_OP_READ
125                          result,
126                          f"{cache_desc} read {result_desc}.",
127                          deprecated=True)
128
129print(json.dumps(events, indent=2))
130