OSDN Git Service

Implement simpleperf list subcommand.
[android-x86/system-extras.git] / simpleperf / generate_event_type_table.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2015 The Android Open Source Project
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18
19 def gen_event_type_entry_str(event_type_name, event_type, event_config):
20     """
21     return string like:
22     {"cpu-cycles", PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES},
23     """
24     return '{"%s", %s, %s},\n' % (event_type_name, event_type, event_config)
25
26
27 def gen_hardware_events():
28     hardware_configs = ["cpu-cycles",
29                         "instructions",
30                         "cache-references",
31                         "cache-misses",
32                         "branch-instructions",
33                         "branch-misses",
34                         "bus-cycles",
35                         "stalled-cycles-frontend",
36                         "stalled-cycles-backend",
37                         ]
38     generated_str = ""
39     for config in hardware_configs:
40         event_type_name = config
41         event_config = "PERF_COUNT_HW_" + config.replace('-', '_').upper()
42
43         generated_str += gen_event_type_entry_str(
44             event_type_name, "PERF_TYPE_HARDWARE", event_config)
45
46     return generated_str
47
48
49 def gen_software_events():
50     software_configs = ["cpu-clock",
51                         "task-clock",
52                         "page-faults",
53                         "context-switches",
54                         "cpu-migrations",
55                         ["minor-faults", "PERF_COUNT_SW_PAGE_FAULTS_MIN"],
56                         ["major-faults", "PERF_COUNT_SW_PAGE_FAULTS_MAJ"],
57                         "alignment-faults",
58                         "emulation-faults",
59                         ]
60     generated_str = ""
61     for config in software_configs:
62         if type(config) is list:
63             event_type_name = config[0]
64             event_config = config[1]
65         else:
66             event_type_name = config
67             event_config = "PERF_COUNT_SW_" + config.replace('-', '_').upper()
68
69         generated_str += gen_event_type_entry_str(
70             event_type_name, "PERF_TYPE_SOFTWARE", event_config)
71
72     return generated_str
73
74
75 def gen_hw_cache_events():
76     hw_cache_types = [["L1-dcache", "PERF_COUNT_HW_CACHE_L1D"],
77                       ["L1-icache", "PERF_COUNT_HW_CACHE_L1I"],
78                       ["LLC", "PERF_COUNT_HW_CACHE_LL"],
79                       ["dTLB", "PERF_COUNT_HW_CACHE_DTLB"],
80                       ["iTLB", "PERF_COUNT_HW_CACHE_ITLB"],
81                       ["branch", "PERF_COUNT_HW_CACHE_BPU"],
82                       ["node", "PERF_COUNT_HW_CACHE_NODE"],
83                       ]
84     hw_cache_ops = [["loades", "load", "PERF_COUNT_HW_CACHE_OP_READ"],
85                     ["stores", "store", "PERF_COUNT_HW_CACHE_OP_WRITE"],
86                     ["prefetches", "prefetch",
87                      "PERF_COUNT_HW_CACHE_OP_PREFETCH"],
88                     ]
89     hw_cache_op_results = [["accesses", "PERF_COUNT_HW_CACHE_RESULT_ACCESS"],
90                            ["misses", "PERF_COUNT_HW_CACHE_RESULT_MISS"],
91                            ]
92     generated_str = ""
93     for (type_name, type_config) in hw_cache_types:
94         for (op_name_access, op_name_miss, op_config) in hw_cache_ops:
95             for (result_name, result_config) in hw_cache_op_results:
96                 if result_name == "accesses":
97                     event_type_name = type_name + '-' + op_name_access
98                 else:
99                     event_type_name = type_name + '-' + \
100                         op_name_miss + '-' + result_name
101                 event_config = "((%s) | (%s << 8) | (%s << 16))" % (
102                     type_config, op_config, result_config)
103                 generated_str += gen_event_type_entry_str(
104                     event_type_name, "PERF_TYPE_HW_CACHE", event_config)
105
106     return generated_str
107
108
109 def gen_events():
110     generated_str = "// This file is auto-generated by generate-event_table.py.\n\n"
111     generated_str += gen_hardware_events() + '\n'
112     generated_str += gen_software_events() + '\n'
113     generated_str += gen_hw_cache_events() + '\n'
114     return generated_str
115
116 generated_str = gen_events()
117 fh = open('event_type_table.h', 'w')
118 fh.write(generated_str)
119 fh.close()