Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include <linux/err.h> |
| 3 | #include <linux/zalloc.h> |
| 4 | #include <errno.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/param.h> |
| 9 | #include "evlist.h" |
| 10 | #include "evsel.h" |
| 11 | #include "parse-events.h" |
| 12 | #include "parse-events-hybrid.h" |
| 13 | #include "debug.h" |
| 14 | #include "pmu.h" |
| 15 | #include "pmu-hybrid.h" |
| 16 | #include "perf.h" |
| 17 | |
| 18 | static void config_hybrid_attr(struct perf_event_attr *attr, |
| 19 | int type, int pmu_type) |
| 20 | { |
| 21 | /* |
| 22 | * attr.config layout for type PERF_TYPE_HARDWARE and |
| 23 | * PERF_TYPE_HW_CACHE |
| 24 | * |
| 25 | * PERF_TYPE_HARDWARE: 0xEEEEEEEE000000AA |
| 26 | * AA: hardware event ID |
| 27 | * EEEEEEEE: PMU type ID |
| 28 | * PERF_TYPE_HW_CACHE: 0xEEEEEEEE00DDCCBB |
| 29 | * BB: hardware cache ID |
| 30 | * CC: hardware cache op ID |
| 31 | * DD: hardware cache op result ID |
| 32 | * EEEEEEEE: PMU type ID |
| 33 | * If the PMU type ID is 0, the PERF_TYPE_RAW will be applied. |
| 34 | */ |
| 35 | attr->type = type; |
| 36 | attr->config = attr->config | ((__u64)pmu_type << PERF_PMU_TYPE_SHIFT); |
| 37 | } |
| 38 | |
| 39 | static int create_event_hybrid(__u32 config_type, int *idx, |
| 40 | struct list_head *list, |
Ian Rogers | 8e8bbfb | 2021-10-15 10:21:24 -0700 | [diff] [blame] | 41 | struct perf_event_attr *attr, const char *name, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 42 | const char *metric_id, |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 43 | struct list_head *config_terms, |
| 44 | struct perf_pmu *pmu) |
| 45 | { |
| 46 | struct evsel *evsel; |
| 47 | __u32 type = attr->type; |
| 48 | __u64 config = attr->config; |
| 49 | |
| 50 | config_hybrid_attr(attr, config_type, pmu->type); |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 51 | evsel = parse_events__add_event_hybrid(list, idx, attr, name, metric_id, |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 52 | pmu, config_terms); |
| 53 | if (evsel) |
| 54 | evsel->pmu_name = strdup(pmu->name); |
| 55 | else |
| 56 | return -ENOMEM; |
| 57 | |
| 58 | attr->type = type; |
| 59 | attr->config = config; |
| 60 | return 0; |
| 61 | } |
| 62 | |
Jin Yao | c93afad | 2021-04-27 15:01:24 +0800 | [diff] [blame] | 63 | static int pmu_cmp(struct parse_events_state *parse_state, |
| 64 | struct perf_pmu *pmu) |
| 65 | { |
| 66 | if (!parse_state->hybrid_pmu_name) |
| 67 | return 0; |
| 68 | |
| 69 | return strcmp(parse_state->hybrid_pmu_name, pmu->name); |
| 70 | } |
| 71 | |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 72 | static int add_hw_hybrid(struct parse_events_state *parse_state, |
| 73 | struct list_head *list, struct perf_event_attr *attr, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 74 | const char *name, const char *metric_id, |
| 75 | struct list_head *config_terms) |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 76 | { |
| 77 | struct perf_pmu *pmu; |
| 78 | int ret; |
| 79 | |
| 80 | perf_pmu__for_each_hybrid_pmu(pmu) { |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 81 | LIST_HEAD(terms); |
| 82 | |
Jin Yao | c93afad | 2021-04-27 15:01:24 +0800 | [diff] [blame] | 83 | if (pmu_cmp(parse_state, pmu)) |
| 84 | continue; |
| 85 | |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 86 | copy_config_terms(&terms, config_terms); |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 87 | ret = create_event_hybrid(PERF_TYPE_HARDWARE, |
| 88 | &parse_state->idx, list, attr, name, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 89 | metric_id, &terms, pmu); |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 90 | free_config_terms(&terms); |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 91 | if (ret) |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
Jin Yao | 94da591 | 2021-04-27 15:01:23 +0800 | [diff] [blame] | 98 | static int create_raw_event_hybrid(int *idx, struct list_head *list, |
Ian Rogers | 8e8bbfb | 2021-10-15 10:21:24 -0700 | [diff] [blame] | 99 | struct perf_event_attr *attr, |
| 100 | const char *name, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 101 | const char *metric_id, |
Jin Yao | 94da591 | 2021-04-27 15:01:23 +0800 | [diff] [blame] | 102 | struct list_head *config_terms, |
| 103 | struct perf_pmu *pmu) |
| 104 | { |
| 105 | struct evsel *evsel; |
| 106 | |
| 107 | attr->type = pmu->type; |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 108 | evsel = parse_events__add_event_hybrid(list, idx, attr, name, metric_id, |
Jin Yao | 94da591 | 2021-04-27 15:01:23 +0800 | [diff] [blame] | 109 | pmu, config_terms); |
| 110 | if (evsel) |
| 111 | evsel->pmu_name = strdup(pmu->name); |
| 112 | else |
| 113 | return -ENOMEM; |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int add_raw_hybrid(struct parse_events_state *parse_state, |
| 119 | struct list_head *list, struct perf_event_attr *attr, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 120 | const char *name, const char *metric_id, |
| 121 | struct list_head *config_terms) |
Jin Yao | 94da591 | 2021-04-27 15:01:23 +0800 | [diff] [blame] | 122 | { |
| 123 | struct perf_pmu *pmu; |
| 124 | int ret; |
| 125 | |
| 126 | perf_pmu__for_each_hybrid_pmu(pmu) { |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 127 | LIST_HEAD(terms); |
| 128 | |
Jin Yao | c93afad | 2021-04-27 15:01:24 +0800 | [diff] [blame] | 129 | if (pmu_cmp(parse_state, pmu)) |
| 130 | continue; |
| 131 | |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 132 | copy_config_terms(&terms, config_terms); |
Jin Yao | 94da591 | 2021-04-27 15:01:23 +0800 | [diff] [blame] | 133 | ret = create_raw_event_hybrid(&parse_state->idx, list, attr, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 134 | name, metric_id, &terms, pmu); |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 135 | free_config_terms(&terms); |
Jin Yao | 94da591 | 2021-04-27 15:01:23 +0800 | [diff] [blame] | 136 | if (ret) |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 143 | int parse_events__add_numeric_hybrid(struct parse_events_state *parse_state, |
| 144 | struct list_head *list, |
| 145 | struct perf_event_attr *attr, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 146 | const char *name, const char *metric_id, |
Ian Rogers | 8e8bbfb | 2021-10-15 10:21:24 -0700 | [diff] [blame] | 147 | struct list_head *config_terms, |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 148 | bool *hybrid) |
| 149 | { |
| 150 | *hybrid = false; |
| 151 | if (attr->type == PERF_TYPE_SOFTWARE) |
| 152 | return 0; |
| 153 | |
| 154 | if (!perf_pmu__has_hybrid()) |
| 155 | return 0; |
| 156 | |
| 157 | *hybrid = true; |
| 158 | if (attr->type != PERF_TYPE_RAW) { |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 159 | return add_hw_hybrid(parse_state, list, attr, name, metric_id, |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 160 | config_terms); |
| 161 | } |
| 162 | |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 163 | return add_raw_hybrid(parse_state, list, attr, name, metric_id, |
Jin Yao | 94da591 | 2021-04-27 15:01:23 +0800 | [diff] [blame] | 164 | config_terms); |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 165 | } |
Jin Yao | 30def61 | 2021-04-27 15:01:22 +0800 | [diff] [blame] | 166 | |
| 167 | int parse_events__add_cache_hybrid(struct list_head *list, int *idx, |
Ian Rogers | 8e8bbfb | 2021-10-15 10:21:24 -0700 | [diff] [blame] | 168 | struct perf_event_attr *attr, |
| 169 | const char *name, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 170 | const char *metric_id, |
Jin Yao | 30def61 | 2021-04-27 15:01:22 +0800 | [diff] [blame] | 171 | struct list_head *config_terms, |
Jin Yao | c93afad | 2021-04-27 15:01:24 +0800 | [diff] [blame] | 172 | bool *hybrid, |
| 173 | struct parse_events_state *parse_state) |
Jin Yao | 30def61 | 2021-04-27 15:01:22 +0800 | [diff] [blame] | 174 | { |
| 175 | struct perf_pmu *pmu; |
| 176 | int ret; |
| 177 | |
| 178 | *hybrid = false; |
| 179 | if (!perf_pmu__has_hybrid()) |
| 180 | return 0; |
| 181 | |
| 182 | *hybrid = true; |
| 183 | perf_pmu__for_each_hybrid_pmu(pmu) { |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 184 | LIST_HEAD(terms); |
| 185 | |
Jin Yao | c93afad | 2021-04-27 15:01:24 +0800 | [diff] [blame] | 186 | if (pmu_cmp(parse_state, pmu)) |
| 187 | continue; |
| 188 | |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 189 | copy_config_terms(&terms, config_terms); |
Jin Yao | 30def61 | 2021-04-27 15:01:22 +0800 | [diff] [blame] | 190 | ret = create_event_hybrid(PERF_TYPE_HW_CACHE, idx, list, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 191 | attr, name, metric_id, &terms, pmu); |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 192 | free_config_terms(&terms); |
Jin Yao | 30def61 | 2021-04-27 15:01:22 +0800 | [diff] [blame] | 193 | if (ret) |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |