Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017, Intel Corporation. |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* Manage metrics and groups of metrics from JSON files */ |
| 7 | |
| 8 | #include "metricgroup.h" |
Arnaldo Carvalho de Melo | b420902 | 2019-08-29 15:56:40 -0300 | [diff] [blame] | 9 | #include "debug.h" |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 10 | #include "evlist.h" |
Arnaldo Carvalho de Melo | 0b8026e | 2019-08-21 10:54:14 -0300 | [diff] [blame] | 11 | #include "evsel.h" |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 12 | #include "strbuf.h" |
| 13 | #include "pmu.h" |
| 14 | #include "expr.h" |
| 15 | #include "rblist.h" |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 16 | #include <string.h> |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include "pmu-events/pmu-events.h" |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 19 | #include "strlist.h" |
| 20 | #include <assert.h> |
Arnaldo Carvalho de Melo | bd9860b | 2019-06-25 21:13:51 -0300 | [diff] [blame] | 21 | #include <linux/ctype.h> |
Arnaldo Carvalho de Melo | b420902 | 2019-08-29 15:56:40 -0300 | [diff] [blame] | 22 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 23 | #include <linux/zalloc.h> |
Arnaldo Carvalho de Melo | 0b8026e | 2019-08-21 10:54:14 -0300 | [diff] [blame] | 24 | #include <subcmd/parse-options.h> |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 25 | #include <api/fs/fs.h> |
| 26 | #include "util.h" |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 27 | |
| 28 | struct metric_event *metricgroup__lookup(struct rblist *metric_events, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 29 | struct evsel *evsel, |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 30 | bool create) |
| 31 | { |
| 32 | struct rb_node *nd; |
| 33 | struct metric_event me = { |
| 34 | .evsel = evsel |
| 35 | }; |
Andi Kleen | 4bd1bef | 2017-11-17 13:43:00 -0800 | [diff] [blame] | 36 | |
| 37 | if (!metric_events) |
| 38 | return NULL; |
| 39 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 40 | nd = rblist__find(metric_events, &me); |
| 41 | if (nd) |
| 42 | return container_of(nd, struct metric_event, nd); |
| 43 | if (create) { |
| 44 | rblist__add_node(metric_events, &me); |
| 45 | nd = rblist__find(metric_events, &me); |
| 46 | if (nd) |
| 47 | return container_of(nd, struct metric_event, nd); |
| 48 | } |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | static int metric_event_cmp(struct rb_node *rb_node, const void *entry) |
| 53 | { |
| 54 | struct metric_event *a = container_of(rb_node, |
| 55 | struct metric_event, |
| 56 | nd); |
| 57 | const struct metric_event *b = entry; |
| 58 | |
| 59 | if (a->evsel == b->evsel) |
| 60 | return 0; |
| 61 | if ((char *)a->evsel < (char *)b->evsel) |
| 62 | return -1; |
| 63 | return +1; |
| 64 | } |
| 65 | |
| 66 | static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused, |
| 67 | const void *entry) |
| 68 | { |
| 69 | struct metric_event *me = malloc(sizeof(struct metric_event)); |
| 70 | |
| 71 | if (!me) |
| 72 | return NULL; |
| 73 | memcpy(me, entry, sizeof(struct metric_event)); |
| 74 | me->evsel = ((struct metric_event *)entry)->evsel; |
| 75 | INIT_LIST_HEAD(&me->head); |
| 76 | return &me->nd; |
| 77 | } |
| 78 | |
| 79 | static void metricgroup__rblist_init(struct rblist *metric_events) |
| 80 | { |
| 81 | rblist__init(metric_events); |
| 82 | metric_events->node_cmp = metric_event_cmp; |
| 83 | metric_events->node_new = metric_event_new; |
| 84 | } |
| 85 | |
| 86 | struct egroup { |
| 87 | struct list_head nd; |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 88 | struct expr_parse_ctx pctx; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 89 | const char *metric_name; |
| 90 | const char *metric_expr; |
Jin Yao | 287f264 | 2019-08-28 13:59:31 +0800 | [diff] [blame] | 91 | const char *metric_unit; |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 92 | int runtime; |
Ian Rogers | 7f9eca5 | 2020-05-20 11:20:07 -0700 | [diff] [blame] | 93 | bool has_constraint; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Find a group of events in perf_evlist that correpond to those from a parsed |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 98 | * metric expression. Note, as find_evsel_group is called in the same order as |
| 99 | * perf_evlist was constructed, metric_no_merge doesn't need to test for |
| 100 | * underfilling a group. |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 101 | * @perf_evlist: a list of events something like: {metric1 leader, metric1 |
| 102 | * sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling, |
| 103 | * metric2 sibling}:W,duration_time |
| 104 | * @pctx: the parse context for the metric expression. |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 105 | * @metric_no_merge: don't attempt to share events for the metric with other |
| 106 | * metrics. |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 107 | * @has_constraint: is there a contraint on the group of events? In which case |
| 108 | * the events won't be grouped. |
| 109 | * @metric_events: out argument, null terminated array of evsel's associated |
| 110 | * with the metric. |
| 111 | * @evlist_used: in/out argument, bitmap tracking which evlist events are used. |
| 112 | * @return the first metric event or NULL on failure. |
| 113 | */ |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 114 | static struct evsel *find_evsel_group(struct evlist *perf_evlist, |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 115 | struct expr_parse_ctx *pctx, |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 116 | bool metric_no_merge, |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 117 | bool has_constraint, |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 118 | struct evsel **metric_events, |
Ian Rogers | 45db55f | 2020-05-20 00:28:08 -0700 | [diff] [blame] | 119 | unsigned long *evlist_used) |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 120 | { |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 121 | struct evsel *ev, *current_leader = NULL; |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 122 | double *val_ptr; |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 123 | int i = 0, matched_events = 0, events_to_match; |
| 124 | const int idnum = (int)hashmap__size(&pctx->ids); |
| 125 | |
| 126 | /* duration_time is grouped separately. */ |
| 127 | if (!has_constraint && |
| 128 | hashmap__find(&pctx->ids, "duration_time", (void **)&val_ptr)) |
| 129 | events_to_match = idnum - 1; |
| 130 | else |
| 131 | events_to_match = idnum; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 132 | |
| 133 | evlist__for_each_entry (perf_evlist, ev) { |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 134 | /* |
| 135 | * Events with a constraint aren't grouped and match the first |
| 136 | * events available. |
| 137 | */ |
| 138 | if (has_constraint && ev->weak_group) |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 139 | continue; |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 140 | /* Ignore event if already used and merging is disabled. */ |
| 141 | if (metric_no_merge && test_bit(ev->idx, evlist_used)) |
| 142 | continue; |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 143 | if (!has_constraint && ev->leader != current_leader) { |
| 144 | /* |
| 145 | * Start of a new group, discard the whole match and |
| 146 | * start again. |
| 147 | */ |
| 148 | matched_events = 0; |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 149 | memset(metric_events, 0, |
| 150 | sizeof(struct evsel *) * idnum); |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 151 | current_leader = ev->leader; |
| 152 | } |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 153 | if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) { |
| 154 | if (has_constraint) { |
| 155 | /* |
| 156 | * Events aren't grouped, ensure the same event |
| 157 | * isn't matched from two groups. |
| 158 | */ |
| 159 | for (i = 0; i < matched_events; i++) { |
| 160 | if (!strcmp(ev->name, |
| 161 | metric_events[i]->name)) { |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | if (i != matched_events) |
| 166 | continue; |
| 167 | } |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 168 | metric_events[matched_events++] = ev; |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 169 | } |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 170 | if (matched_events == events_to_match) |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | if (events_to_match != idnum) { |
| 175 | /* Add the first duration_time. */ |
| 176 | evlist__for_each_entry(perf_evlist, ev) { |
| 177 | if (!strcmp(ev->name, "duration_time")) { |
| 178 | metric_events[matched_events++] = ev; |
| 179 | break; |
| 180 | } |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 183 | |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 184 | if (matched_events != idnum) { |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 185 | /* Not whole match */ |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | metric_events[idnum] = NULL; |
| 190 | |
| 191 | for (i = 0; i < idnum; i++) { |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 192 | ev = metric_events[i]; |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 193 | ev->metric_leader = ev; |
Ian Rogers | 45db55f | 2020-05-20 00:28:08 -0700 | [diff] [blame] | 194 | set_bit(ev->idx, evlist_used); |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return metric_events[0]; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static int metricgroup__setup_events(struct list_head *groups, |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 201 | bool metric_no_merge, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 202 | struct evlist *perf_evlist, |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 203 | struct rblist *metric_events_list) |
| 204 | { |
| 205 | struct metric_event *me; |
| 206 | struct metric_expr *expr; |
| 207 | int i = 0; |
| 208 | int ret = 0; |
| 209 | struct egroup *eg; |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 210 | struct evsel *evsel, *tmp; |
Ian Rogers | 45db55f | 2020-05-20 00:28:08 -0700 | [diff] [blame] | 211 | unsigned long *evlist_used; |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 212 | |
Ian Rogers | 45db55f | 2020-05-20 00:28:08 -0700 | [diff] [blame] | 213 | evlist_used = bitmap_alloc(perf_evlist->core.nr_entries); |
| 214 | if (!evlist_used) |
| 215 | return -ENOMEM; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 216 | |
| 217 | list_for_each_entry (eg, groups, nd) { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 218 | struct evsel **metric_events; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 219 | |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 220 | metric_events = calloc(sizeof(void *), |
| 221 | hashmap__size(&eg->pctx.ids) + 1); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 222 | if (!metric_events) { |
| 223 | ret = -ENOMEM; |
| 224 | break; |
| 225 | } |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 226 | evsel = find_evsel_group(perf_evlist, &eg->pctx, |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 227 | metric_no_merge, |
| 228 | eg->has_constraint, metric_events, |
| 229 | evlist_used); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 230 | if (!evsel) { |
| 231 | pr_debug("Cannot resolve %s: %s\n", |
| 232 | eg->metric_name, eg->metric_expr); |
Ian Rogers | a159e2f | 2020-05-07 22:36:24 -0700 | [diff] [blame] | 233 | free(metric_events); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 234 | continue; |
| 235 | } |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 236 | for (i = 0; metric_events[i]; i++) |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 237 | metric_events[i]->collect_stat = true; |
| 238 | me = metricgroup__lookup(metric_events_list, evsel, true); |
| 239 | if (!me) { |
| 240 | ret = -ENOMEM; |
Ian Rogers | a159e2f | 2020-05-07 22:36:24 -0700 | [diff] [blame] | 241 | free(metric_events); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 242 | break; |
| 243 | } |
| 244 | expr = malloc(sizeof(struct metric_expr)); |
| 245 | if (!expr) { |
| 246 | ret = -ENOMEM; |
Ian Rogers | a159e2f | 2020-05-07 22:36:24 -0700 | [diff] [blame] | 247 | free(metric_events); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 248 | break; |
| 249 | } |
| 250 | expr->metric_expr = eg->metric_expr; |
| 251 | expr->metric_name = eg->metric_name; |
Jin Yao | 287f264 | 2019-08-28 13:59:31 +0800 | [diff] [blame] | 252 | expr->metric_unit = eg->metric_unit; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 253 | expr->metric_events = metric_events; |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 254 | expr->runtime = eg->runtime; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 255 | list_add(&expr->nd, &me->head); |
| 256 | } |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 257 | |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 258 | evlist__for_each_entry_safe(perf_evlist, tmp, evsel) { |
| 259 | if (!test_bit(evsel->idx, evlist_used)) { |
| 260 | evlist__remove(perf_evlist, evsel); |
| 261 | evsel__delete(evsel); |
| 262 | } |
| 263 | } |
Ian Rogers | 45db55f | 2020-05-20 00:28:08 -0700 | [diff] [blame] | 264 | bitmap_free(evlist_used); |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 265 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static bool match_metric(const char *n, const char *list) |
| 270 | { |
| 271 | int len; |
| 272 | char *m; |
| 273 | |
| 274 | if (!list) |
| 275 | return false; |
| 276 | if (!strcmp(list, "all")) |
| 277 | return true; |
| 278 | if (!n) |
| 279 | return !strcasecmp(list, "No_group"); |
| 280 | len = strlen(list); |
| 281 | m = strcasestr(n, list); |
| 282 | if (!m) |
| 283 | return false; |
| 284 | if ((m == n || m[-1] == ';' || m[-1] == ' ') && |
| 285 | (m[len] == 0 || m[len] == ';')) |
| 286 | return true; |
| 287 | return false; |
| 288 | } |
| 289 | |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 290 | struct mep { |
| 291 | struct rb_node nd; |
| 292 | const char *name; |
| 293 | struct strlist *metrics; |
| 294 | }; |
| 295 | |
| 296 | static int mep_cmp(struct rb_node *rb_node, const void *entry) |
| 297 | { |
| 298 | struct mep *a = container_of(rb_node, struct mep, nd); |
| 299 | struct mep *b = (struct mep *)entry; |
| 300 | |
| 301 | return strcmp(a->name, b->name); |
| 302 | } |
| 303 | |
| 304 | static struct rb_node *mep_new(struct rblist *rl __maybe_unused, |
| 305 | const void *entry) |
| 306 | { |
| 307 | struct mep *me = malloc(sizeof(struct mep)); |
| 308 | |
| 309 | if (!me) |
| 310 | return NULL; |
| 311 | memcpy(me, entry, sizeof(struct mep)); |
| 312 | me->name = strdup(me->name); |
| 313 | if (!me->name) |
| 314 | goto out_me; |
| 315 | me->metrics = strlist__new(NULL, NULL); |
| 316 | if (!me->metrics) |
| 317 | goto out_name; |
| 318 | return &me->nd; |
| 319 | out_name: |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 320 | zfree(&me->name); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 321 | out_me: |
| 322 | free(me); |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | static struct mep *mep_lookup(struct rblist *groups, const char *name) |
| 327 | { |
| 328 | struct rb_node *nd; |
| 329 | struct mep me = { |
| 330 | .name = name |
| 331 | }; |
| 332 | nd = rblist__find(groups, &me); |
| 333 | if (nd) |
| 334 | return container_of(nd, struct mep, nd); |
| 335 | rblist__add_node(groups, &me); |
| 336 | nd = rblist__find(groups, &me); |
| 337 | if (nd) |
| 338 | return container_of(nd, struct mep, nd); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | static void mep_delete(struct rblist *rl __maybe_unused, |
| 343 | struct rb_node *nd) |
| 344 | { |
| 345 | struct mep *me = container_of(nd, struct mep, nd); |
| 346 | |
| 347 | strlist__delete(me->metrics); |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 348 | zfree(&me->name); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 349 | free(me); |
| 350 | } |
| 351 | |
| 352 | static void metricgroup__print_strlist(struct strlist *metrics, bool raw) |
| 353 | { |
| 354 | struct str_node *sn; |
| 355 | int n = 0; |
| 356 | |
| 357 | strlist__for_each_entry (sn, metrics) { |
| 358 | if (raw) |
| 359 | printf("%s%s", n > 0 ? " " : "", sn->s); |
| 360 | else |
| 361 | printf(" %s\n", sn->s); |
| 362 | n++; |
| 363 | } |
| 364 | if (raw) |
| 365 | putchar('\n'); |
| 366 | } |
| 367 | |
| 368 | void metricgroup__print(bool metrics, bool metricgroups, char *filter, |
Jiri Olsa | 33bbc57 | 2019-02-13 13:32:41 +0100 | [diff] [blame] | 369 | bool raw, bool details) |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 370 | { |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 371 | struct pmu_events_map *map = perf_pmu__find_map(NULL); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 372 | struct pmu_event *pe; |
| 373 | int i; |
| 374 | struct rblist groups; |
| 375 | struct rb_node *node, *next; |
| 376 | struct strlist *metriclist = NULL; |
| 377 | |
| 378 | if (!map) |
| 379 | return; |
| 380 | |
| 381 | if (!metricgroups) { |
| 382 | metriclist = strlist__new(NULL, NULL); |
| 383 | if (!metriclist) |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | rblist__init(&groups); |
| 388 | groups.node_new = mep_new; |
| 389 | groups.node_cmp = mep_cmp; |
| 390 | groups.node_delete = mep_delete; |
| 391 | for (i = 0; ; i++) { |
| 392 | const char *g; |
| 393 | pe = &map->table[i]; |
| 394 | |
| 395 | if (!pe->name && !pe->metric_group && !pe->metric_name) |
| 396 | break; |
| 397 | if (!pe->metric_expr) |
| 398 | continue; |
| 399 | g = pe->metric_group; |
| 400 | if (!g && pe->metric_name) { |
| 401 | if (pe->name) |
| 402 | continue; |
| 403 | g = "No_group"; |
| 404 | } |
| 405 | if (g) { |
| 406 | char *omg; |
| 407 | char *mg = strdup(g); |
| 408 | |
| 409 | if (!mg) |
| 410 | return; |
| 411 | omg = mg; |
| 412 | while ((g = strsep(&mg, ";")) != NULL) { |
| 413 | struct mep *me; |
| 414 | char *s; |
| 415 | |
Arnaldo Carvalho de Melo | 80e9073 | 2019-06-26 11:21:47 -0300 | [diff] [blame] | 416 | g = skip_spaces(g); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 417 | if (*g == 0) |
| 418 | g = "No_group"; |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 419 | if (filter && !strstr(g, filter)) |
| 420 | continue; |
| 421 | if (raw) |
| 422 | s = (char *)pe->metric_name; |
| 423 | else { |
Michael Petlan | 95f0432 | 2018-07-30 17:35:04 -0400 | [diff] [blame] | 424 | if (asprintf(&s, "%s\n%*s%s]", |
| 425 | pe->metric_name, 8, "[", pe->desc) < 0) |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 426 | return; |
Jiri Olsa | 33bbc57 | 2019-02-13 13:32:41 +0100 | [diff] [blame] | 427 | |
| 428 | if (details) { |
| 429 | if (asprintf(&s, "%s\n%*s%s]", |
| 430 | s, 8, "[", pe->metric_expr) < 0) |
| 431 | return; |
| 432 | } |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | if (!s) |
| 436 | continue; |
| 437 | |
| 438 | if (!metricgroups) { |
| 439 | strlist__add(metriclist, s); |
| 440 | } else { |
| 441 | me = mep_lookup(&groups, g); |
| 442 | if (!me) |
| 443 | continue; |
| 444 | strlist__add(me->metrics, s); |
| 445 | } |
| 446 | } |
| 447 | free(omg); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | if (metricgroups && !raw) |
| 452 | printf("\nMetric Groups:\n\n"); |
| 453 | else if (metrics && !raw) |
| 454 | printf("\nMetrics:\n\n"); |
| 455 | |
Davidlohr Bueso | ca22702 | 2018-12-06 11:18:16 -0800 | [diff] [blame] | 456 | for (node = rb_first_cached(&groups.entries); node; node = next) { |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 457 | struct mep *me = container_of(node, struct mep, nd); |
| 458 | |
| 459 | if (metricgroups) |
Andi Kleen | 9c344d1 | 2019-06-28 15:07:36 -0700 | [diff] [blame] | 460 | printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n"); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 461 | if (metrics) |
| 462 | metricgroup__print_strlist(me->metrics, raw); |
| 463 | next = rb_next(node); |
| 464 | rblist__remove_node(&groups, node); |
| 465 | } |
| 466 | if (!metricgroups) |
| 467 | metricgroup__print_strlist(metriclist, raw); |
| 468 | strlist__delete(metriclist); |
| 469 | } |
| 470 | |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 471 | static void metricgroup__add_metric_weak_group(struct strbuf *events, |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 472 | struct expr_parse_ctx *ctx) |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 473 | { |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 474 | struct hashmap_entry *cur; |
Ian Rogers | 4e21c13 | 2020-05-20 11:20:05 -0700 | [diff] [blame] | 475 | size_t bkt; |
| 476 | bool no_group = true, has_duration = false; |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 477 | |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 478 | hashmap__for_each_entry((&ctx->ids), cur, bkt) { |
| 479 | pr_debug("found event %s\n", (const char *)cur->key); |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 480 | /* |
| 481 | * Duration time maps to a software event and can make |
| 482 | * groups not count. Always use it outside a |
| 483 | * group. |
| 484 | */ |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 485 | if (!strcmp(cur->key, "duration_time")) { |
Ian Rogers | 4e21c13 | 2020-05-20 11:20:05 -0700 | [diff] [blame] | 486 | has_duration = true; |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 487 | continue; |
| 488 | } |
| 489 | strbuf_addf(events, "%s%s", |
Ian Rogers | 4e21c13 | 2020-05-20 11:20:05 -0700 | [diff] [blame] | 490 | no_group ? "{" : ",", |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 491 | (const char *)cur->key); |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 492 | no_group = false; |
| 493 | } |
Ian Rogers | 4e21c13 | 2020-05-20 11:20:05 -0700 | [diff] [blame] | 494 | if (!no_group) { |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 495 | strbuf_addf(events, "}:W"); |
Ian Rogers | 4e21c13 | 2020-05-20 11:20:05 -0700 | [diff] [blame] | 496 | if (has_duration) |
| 497 | strbuf_addf(events, ",duration_time"); |
| 498 | } else if (has_duration) |
| 499 | strbuf_addf(events, "duration_time"); |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 502 | static void metricgroup__add_metric_non_group(struct strbuf *events, |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 503 | struct expr_parse_ctx *ctx) |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 504 | { |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 505 | struct hashmap_entry *cur; |
| 506 | size_t bkt; |
Ian Rogers | e2ce105 | 2020-05-20 11:20:11 -0700 | [diff] [blame] | 507 | bool first = true; |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 508 | |
Ian Rogers | e2ce105 | 2020-05-20 11:20:11 -0700 | [diff] [blame] | 509 | hashmap__for_each_entry((&ctx->ids), cur, bkt) { |
| 510 | if (!first) |
| 511 | strbuf_addf(events, ","); |
| 512 | strbuf_addf(events, "%s", (const char *)cur->key); |
| 513 | first = false; |
| 514 | } |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static void metricgroup___watchdog_constraint_hint(const char *name, bool foot) |
| 518 | { |
| 519 | static bool violate_nmi_constraint; |
| 520 | |
| 521 | if (!foot) { |
| 522 | pr_warning("Splitting metric group %s into standalone metrics.\n", name); |
| 523 | violate_nmi_constraint = true; |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | if (!violate_nmi_constraint) |
| 528 | return; |
| 529 | |
| 530 | pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n" |
| 531 | " echo 0 > /proc/sys/kernel/nmi_watchdog\n" |
| 532 | " perf stat ...\n" |
| 533 | " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); |
| 534 | } |
| 535 | |
| 536 | static bool metricgroup__has_constraint(struct pmu_event *pe) |
| 537 | { |
| 538 | if (!pe->metric_constraint) |
| 539 | return false; |
| 540 | |
| 541 | if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") && |
| 542 | sysctl__nmi_watchdog_enabled()) { |
| 543 | metricgroup___watchdog_constraint_hint(pe->metric_name, false); |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | return false; |
| 548 | } |
| 549 | |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 550 | int __weak arch_get_runtimeparam(void) |
| 551 | { |
| 552 | return 1; |
| 553 | } |
| 554 | |
Ian Rogers | 7f9eca5 | 2020-05-20 11:20:07 -0700 | [diff] [blame] | 555 | static int __metricgroup__add_metric(struct list_head *group_list, |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 556 | struct pmu_event *pe, |
| 557 | bool metric_no_group, |
| 558 | int runtime) |
Kajol Jain | 47352ab | 2020-04-02 02:03:36 +0530 | [diff] [blame] | 559 | { |
Kajol Jain | 47352ab | 2020-04-02 02:03:36 +0530 | [diff] [blame] | 560 | struct egroup *eg; |
| 561 | |
Kajol Jain | 47352ab | 2020-04-02 02:03:36 +0530 | [diff] [blame] | 562 | eg = malloc(sizeof(*eg)); |
| 563 | if (!eg) |
| 564 | return -ENOMEM; |
| 565 | |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 566 | expr__ctx_init(&eg->pctx); |
Kajol Jain | 47352ab | 2020-04-02 02:03:36 +0530 | [diff] [blame] | 567 | eg->metric_name = pe->metric_name; |
| 568 | eg->metric_expr = pe->metric_expr; |
| 569 | eg->metric_unit = pe->unit; |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 570 | eg->runtime = runtime; |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 571 | eg->has_constraint = metric_no_group || metricgroup__has_constraint(pe); |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 572 | |
| 573 | if (expr__find_other(pe->metric_expr, NULL, &eg->pctx, runtime) < 0) { |
| 574 | expr__ctx_clear(&eg->pctx); |
| 575 | free(eg); |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | |
Ian Rogers | 6bf2102 | 2020-05-20 11:20:08 -0700 | [diff] [blame] | 579 | if (list_empty(group_list)) |
| 580 | list_add(&eg->nd, group_list); |
| 581 | else { |
| 582 | struct list_head *pos; |
| 583 | |
| 584 | /* Place the largest groups at the front. */ |
| 585 | list_for_each_prev(pos, group_list) { |
| 586 | struct egroup *old = list_entry(pos, struct egroup, nd); |
| 587 | |
| 588 | if (hashmap__size(&eg->pctx.ids) <= |
| 589 | hashmap__size(&old->pctx.ids)) |
| 590 | break; |
| 591 | } |
| 592 | list_add(&eg->nd, pos); |
| 593 | } |
Kajol Jain | 47352ab | 2020-04-02 02:03:36 +0530 | [diff] [blame] | 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 598 | static int metricgroup__add_metric(const char *metric, bool metric_no_group, |
| 599 | struct strbuf *events, |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 600 | struct list_head *group_list) |
| 601 | { |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 602 | struct pmu_events_map *map = perf_pmu__find_map(NULL); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 603 | struct pmu_event *pe; |
Ian Rogers | 7f9eca5 | 2020-05-20 11:20:07 -0700 | [diff] [blame] | 604 | struct egroup *eg; |
Ian Rogers | 9081039 | 2020-05-20 11:20:06 -0700 | [diff] [blame] | 605 | int i, ret; |
| 606 | bool has_match = false; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 607 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 608 | if (!map) |
| 609 | return 0; |
| 610 | |
| 611 | for (i = 0; ; i++) { |
| 612 | pe = &map->table[i]; |
| 613 | |
Ian Rogers | 9081039 | 2020-05-20 11:20:06 -0700 | [diff] [blame] | 614 | if (!pe->name && !pe->metric_group && !pe->metric_name) { |
| 615 | /* End of pmu events. */ |
| 616 | if (!has_match) |
| 617 | return -EINVAL; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 618 | break; |
Ian Rogers | 9081039 | 2020-05-20 11:20:06 -0700 | [diff] [blame] | 619 | } |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 620 | if (!pe->metric_expr) |
| 621 | continue; |
| 622 | if (match_metric(pe->metric_group, metric) || |
| 623 | match_metric(pe->metric_name, metric)) { |
Ian Rogers | 9081039 | 2020-05-20 11:20:06 -0700 | [diff] [blame] | 624 | has_match = true; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 625 | pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name); |
| 626 | |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 627 | if (!strstr(pe->metric_expr, "?")) { |
Ian Rogers | 7f9eca5 | 2020-05-20 11:20:07 -0700 | [diff] [blame] | 628 | ret = __metricgroup__add_metric(group_list, |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 629 | pe, |
| 630 | metric_no_group, |
| 631 | 1); |
Ian Rogers | 9081039 | 2020-05-20 11:20:06 -0700 | [diff] [blame] | 632 | if (ret) |
| 633 | return ret; |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 634 | } else { |
| 635 | int j, count; |
| 636 | |
| 637 | count = arch_get_runtimeparam(); |
| 638 | |
| 639 | /* This loop is added to create multiple |
| 640 | * events depend on count value and add |
| 641 | * those events to group_list. |
| 642 | */ |
| 643 | |
Ian Rogers | 9081039 | 2020-05-20 11:20:06 -0700 | [diff] [blame] | 644 | for (j = 0; j < count; j++) { |
Ian Rogers | 7f9eca5 | 2020-05-20 11:20:07 -0700 | [diff] [blame] | 645 | ret = __metricgroup__add_metric( |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 646 | group_list, pe, |
| 647 | metric_no_group, j); |
Ian Rogers | 9081039 | 2020-05-20 11:20:06 -0700 | [diff] [blame] | 648 | if (ret) |
| 649 | return ret; |
| 650 | } |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 651 | } |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
Ian Rogers | 7f9eca5 | 2020-05-20 11:20:07 -0700 | [diff] [blame] | 654 | list_for_each_entry(eg, group_list, nd) { |
| 655 | if (events->len > 0) |
| 656 | strbuf_addf(events, ","); |
| 657 | |
| 658 | if (eg->has_constraint) { |
| 659 | metricgroup__add_metric_non_group(events, |
| 660 | &eg->pctx); |
| 661 | } else { |
| 662 | metricgroup__add_metric_weak_group(events, |
| 663 | &eg->pctx); |
| 664 | } |
| 665 | } |
Ian Rogers | 9081039 | 2020-05-20 11:20:06 -0700 | [diff] [blame] | 666 | return 0; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 669 | static int metricgroup__add_metric_list(const char *list, bool metric_no_group, |
| 670 | struct strbuf *events, |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 671 | struct list_head *group_list) |
| 672 | { |
| 673 | char *llist, *nlist, *p; |
| 674 | int ret = -EINVAL; |
| 675 | |
| 676 | nlist = strdup(list); |
| 677 | if (!nlist) |
| 678 | return -ENOMEM; |
| 679 | llist = nlist; |
Andi Kleen | 411bc31 | 2017-09-14 13:57:35 -0700 | [diff] [blame] | 680 | |
| 681 | strbuf_init(events, 100); |
| 682 | strbuf_addf(events, "%s", ""); |
| 683 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 684 | while ((p = strsep(&llist, ",")) != NULL) { |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 685 | ret = metricgroup__add_metric(p, metric_no_group, events, |
| 686 | group_list); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 687 | if (ret == -EINVAL) { |
| 688 | fprintf(stderr, "Cannot find metric or group `%s'\n", |
| 689 | p); |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | free(nlist); |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 694 | |
| 695 | if (!ret) |
| 696 | metricgroup___watchdog_constraint_hint(NULL, true); |
| 697 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | static void metricgroup__free_egroups(struct list_head *group_list) |
| 702 | { |
| 703 | struct egroup *eg, *egtmp; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 704 | |
| 705 | list_for_each_entry_safe (eg, egtmp, group_list, nd) { |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 706 | expr__ctx_clear(&eg->pctx); |
Arnaldo Carvalho de Melo | acc7bfb | 2019-07-04 12:20:21 -0300 | [diff] [blame] | 707 | list_del_init(&eg->nd); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 708 | free(eg); |
| 709 | } |
| 710 | } |
| 711 | |
Jiri Olsa | 8b4468a | 2020-06-02 23:47:33 +0200 | [diff] [blame] | 712 | static int parse_groups(struct evlist *perf_evlist, const char *str, |
| 713 | bool metric_no_group, |
| 714 | bool metric_no_merge, |
Jiri Olsa | 68173bd | 2020-06-09 12:50:42 -0300 | [diff] [blame^] | 715 | struct perf_pmu *fake_pmu, |
Jiri Olsa | 8b4468a | 2020-06-02 23:47:33 +0200 | [diff] [blame] | 716 | struct rblist *metric_events) |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 717 | { |
| 718 | struct parse_events_error parse_error; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 719 | struct strbuf extra_events; |
| 720 | LIST_HEAD(group_list); |
| 721 | int ret; |
| 722 | |
| 723 | if (metric_events->nr_entries == 0) |
| 724 | metricgroup__rblist_init(metric_events); |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 725 | ret = metricgroup__add_metric_list(str, metric_no_group, |
| 726 | &extra_events, &group_list); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 727 | if (ret) |
| 728 | return ret; |
| 729 | pr_debug("adding %s\n", extra_events.buf); |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 730 | bzero(&parse_error, sizeof(parse_error)); |
Jiri Olsa | 68173bd | 2020-06-09 12:50:42 -0300 | [diff] [blame^] | 731 | ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 732 | if (ret) { |
Andi Kleen | 333b566 | 2017-09-13 14:50:06 -0700 | [diff] [blame] | 733 | parse_events_print_error(&parse_error, extra_events.buf); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 734 | goto out; |
| 735 | } |
| 736 | strbuf_release(&extra_events); |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 737 | ret = metricgroup__setup_events(&group_list, metric_no_merge, |
| 738 | perf_evlist, metric_events); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 739 | out: |
| 740 | metricgroup__free_egroups(&group_list); |
| 741 | return ret; |
| 742 | } |
Thomas Richter | 742d92f | 2018-06-26 09:17:01 +0200 | [diff] [blame] | 743 | |
Jiri Olsa | 8b4468a | 2020-06-02 23:47:33 +0200 | [diff] [blame] | 744 | int metricgroup__parse_groups(const struct option *opt, |
| 745 | const char *str, |
| 746 | bool metric_no_group, |
| 747 | bool metric_no_merge, |
| 748 | struct rblist *metric_events) |
| 749 | { |
| 750 | struct evlist *perf_evlist = *(struct evlist **)opt->value; |
| 751 | |
| 752 | return parse_groups(perf_evlist, str, metric_no_group, |
Jiri Olsa | 68173bd | 2020-06-09 12:50:42 -0300 | [diff] [blame^] | 753 | metric_no_merge, NULL, metric_events); |
Jiri Olsa | 8b4468a | 2020-06-02 23:47:33 +0200 | [diff] [blame] | 754 | } |
| 755 | |
Thomas Richter | 742d92f | 2018-06-26 09:17:01 +0200 | [diff] [blame] | 756 | bool metricgroup__has_metric(const char *metric) |
| 757 | { |
| 758 | struct pmu_events_map *map = perf_pmu__find_map(NULL); |
| 759 | struct pmu_event *pe; |
| 760 | int i; |
| 761 | |
| 762 | if (!map) |
| 763 | return false; |
| 764 | |
| 765 | for (i = 0; ; i++) { |
| 766 | pe = &map->table[i]; |
| 767 | |
| 768 | if (!pe->name && !pe->metric_group && !pe->metric_name) |
| 769 | break; |
| 770 | if (!pe->metric_expr) |
| 771 | continue; |
| 772 | if (match_metric(pe->metric_name, metric)) |
| 773 | return true; |
| 774 | } |
| 775 | return false; |
| 776 | } |