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; |
| 88 | int idnum; |
| 89 | const char **ids; |
| 90 | const char *metric_name; |
| 91 | const char *metric_expr; |
Jin Yao | 287f264 | 2019-08-28 13:59:31 +0800 | [diff] [blame] | 92 | const char *metric_unit; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 95 | static struct evsel *find_evsel_group(struct evlist *perf_evlist, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 96 | const char **ids, |
| 97 | int idnum, |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 98 | struct evsel **metric_events, |
| 99 | bool *evlist_used) |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 100 | { |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 101 | struct evsel *ev; |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 102 | int i = 0, j = 0; |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 103 | bool leader_found; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 104 | |
| 105 | evlist__for_each_entry (perf_evlist, ev) { |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 106 | if (evlist_used[j++]) |
| 107 | continue; |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 108 | if (!strcmp(ev->name, ids[i])) { |
| 109 | if (!metric_events[i]) |
| 110 | metric_events[i] = ev; |
Kajol Jain | eb573e7 | 2019-11-20 14:10:59 +0530 | [diff] [blame] | 111 | i++; |
| 112 | if (i == idnum) |
| 113 | break; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 114 | } else { |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 115 | /* Discard the whole match and start again */ |
| 116 | i = 0; |
| 117 | memset(metric_events, 0, |
| 118 | sizeof(struct evsel *) * idnum); |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 119 | |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 120 | if (!strcmp(ev->name, ids[i])) { |
| 121 | if (!metric_events[i]) |
| 122 | metric_events[i] = ev; |
| 123 | i++; |
| 124 | if (i == idnum) |
| 125 | break; |
Andi Kleen | 2f87f33 | 2019-06-24 12:37:10 -0700 | [diff] [blame] | 126 | } |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 129 | |
Kajol Jain | eb573e7 | 2019-11-20 14:10:59 +0530 | [diff] [blame] | 130 | if (i != idnum) { |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 131 | /* Not whole match */ |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | metric_events[idnum] = NULL; |
| 136 | |
| 137 | for (i = 0; i < idnum; i++) { |
| 138 | leader_found = false; |
| 139 | evlist__for_each_entry(perf_evlist, ev) { |
| 140 | if (!leader_found && (ev == metric_events[i])) |
| 141 | leader_found = true; |
| 142 | |
| 143 | if (leader_found && |
| 144 | !strcmp(ev->name, metric_events[i]->name)) { |
| 145 | ev->metric_leader = metric_events[i]; |
| 146 | } |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 147 | j++; |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 148 | } |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 149 | ev = metric_events[i]; |
| 150 | evlist_used[ev->idx] = true; |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return metric_events[0]; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static int metricgroup__setup_events(struct list_head *groups, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 157 | struct evlist *perf_evlist, |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 158 | struct rblist *metric_events_list) |
| 159 | { |
| 160 | struct metric_event *me; |
| 161 | struct metric_expr *expr; |
| 162 | int i = 0; |
| 163 | int ret = 0; |
| 164 | struct egroup *eg; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 165 | struct evsel *evsel; |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 166 | bool *evlist_used; |
| 167 | |
| 168 | evlist_used = calloc(perf_evlist->core.nr_entries, sizeof(bool)); |
| 169 | if (!evlist_used) { |
| 170 | ret = -ENOMEM; |
| 171 | return ret; |
| 172 | } |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 173 | |
| 174 | list_for_each_entry (eg, groups, nd) { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 175 | struct evsel **metric_events; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 176 | |
| 177 | metric_events = calloc(sizeof(void *), eg->idnum + 1); |
| 178 | if (!metric_events) { |
| 179 | ret = -ENOMEM; |
| 180 | break; |
| 181 | } |
Andi Kleen | 2f87f33 | 2019-06-24 12:37:10 -0700 | [diff] [blame] | 182 | evsel = find_evsel_group(perf_evlist, eg->ids, eg->idnum, |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 183 | metric_events, evlist_used); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 184 | if (!evsel) { |
| 185 | pr_debug("Cannot resolve %s: %s\n", |
| 186 | eg->metric_name, eg->metric_expr); |
| 187 | continue; |
| 188 | } |
| 189 | for (i = 0; i < eg->idnum; i++) |
| 190 | metric_events[i]->collect_stat = true; |
| 191 | me = metricgroup__lookup(metric_events_list, evsel, true); |
| 192 | if (!me) { |
| 193 | ret = -ENOMEM; |
| 194 | break; |
| 195 | } |
| 196 | expr = malloc(sizeof(struct metric_expr)); |
| 197 | if (!expr) { |
| 198 | ret = -ENOMEM; |
| 199 | break; |
| 200 | } |
| 201 | expr->metric_expr = eg->metric_expr; |
| 202 | expr->metric_name = eg->metric_name; |
Jin Yao | 287f264 | 2019-08-28 13:59:31 +0800 | [diff] [blame] | 203 | expr->metric_unit = eg->metric_unit; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 204 | expr->metric_events = metric_events; |
| 205 | list_add(&expr->nd, &me->head); |
| 206 | } |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 207 | |
| 208 | free(evlist_used); |
| 209 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static bool match_metric(const char *n, const char *list) |
| 214 | { |
| 215 | int len; |
| 216 | char *m; |
| 217 | |
| 218 | if (!list) |
| 219 | return false; |
| 220 | if (!strcmp(list, "all")) |
| 221 | return true; |
| 222 | if (!n) |
| 223 | return !strcasecmp(list, "No_group"); |
| 224 | len = strlen(list); |
| 225 | m = strcasestr(n, list); |
| 226 | if (!m) |
| 227 | return false; |
| 228 | if ((m == n || m[-1] == ';' || m[-1] == ' ') && |
| 229 | (m[len] == 0 || m[len] == ';')) |
| 230 | return true; |
| 231 | return false; |
| 232 | } |
| 233 | |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 234 | struct mep { |
| 235 | struct rb_node nd; |
| 236 | const char *name; |
| 237 | struct strlist *metrics; |
| 238 | }; |
| 239 | |
| 240 | static int mep_cmp(struct rb_node *rb_node, const void *entry) |
| 241 | { |
| 242 | struct mep *a = container_of(rb_node, struct mep, nd); |
| 243 | struct mep *b = (struct mep *)entry; |
| 244 | |
| 245 | return strcmp(a->name, b->name); |
| 246 | } |
| 247 | |
| 248 | static struct rb_node *mep_new(struct rblist *rl __maybe_unused, |
| 249 | const void *entry) |
| 250 | { |
| 251 | struct mep *me = malloc(sizeof(struct mep)); |
| 252 | |
| 253 | if (!me) |
| 254 | return NULL; |
| 255 | memcpy(me, entry, sizeof(struct mep)); |
| 256 | me->name = strdup(me->name); |
| 257 | if (!me->name) |
| 258 | goto out_me; |
| 259 | me->metrics = strlist__new(NULL, NULL); |
| 260 | if (!me->metrics) |
| 261 | goto out_name; |
| 262 | return &me->nd; |
| 263 | out_name: |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 264 | zfree(&me->name); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 265 | out_me: |
| 266 | free(me); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | static struct mep *mep_lookup(struct rblist *groups, const char *name) |
| 271 | { |
| 272 | struct rb_node *nd; |
| 273 | struct mep me = { |
| 274 | .name = name |
| 275 | }; |
| 276 | nd = rblist__find(groups, &me); |
| 277 | if (nd) |
| 278 | return container_of(nd, struct mep, nd); |
| 279 | rblist__add_node(groups, &me); |
| 280 | nd = rblist__find(groups, &me); |
| 281 | if (nd) |
| 282 | return container_of(nd, struct mep, nd); |
| 283 | return NULL; |
| 284 | } |
| 285 | |
| 286 | static void mep_delete(struct rblist *rl __maybe_unused, |
| 287 | struct rb_node *nd) |
| 288 | { |
| 289 | struct mep *me = container_of(nd, struct mep, nd); |
| 290 | |
| 291 | strlist__delete(me->metrics); |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 292 | zfree(&me->name); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 293 | free(me); |
| 294 | } |
| 295 | |
| 296 | static void metricgroup__print_strlist(struct strlist *metrics, bool raw) |
| 297 | { |
| 298 | struct str_node *sn; |
| 299 | int n = 0; |
| 300 | |
| 301 | strlist__for_each_entry (sn, metrics) { |
| 302 | if (raw) |
| 303 | printf("%s%s", n > 0 ? " " : "", sn->s); |
| 304 | else |
| 305 | printf(" %s\n", sn->s); |
| 306 | n++; |
| 307 | } |
| 308 | if (raw) |
| 309 | putchar('\n'); |
| 310 | } |
| 311 | |
| 312 | void metricgroup__print(bool metrics, bool metricgroups, char *filter, |
Jiri Olsa | 33bbc57 | 2019-02-13 13:32:41 +0100 | [diff] [blame] | 313 | bool raw, bool details) |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 314 | { |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 315 | struct pmu_events_map *map = perf_pmu__find_map(NULL); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 316 | struct pmu_event *pe; |
| 317 | int i; |
| 318 | struct rblist groups; |
| 319 | struct rb_node *node, *next; |
| 320 | struct strlist *metriclist = NULL; |
| 321 | |
| 322 | if (!map) |
| 323 | return; |
| 324 | |
| 325 | if (!metricgroups) { |
| 326 | metriclist = strlist__new(NULL, NULL); |
| 327 | if (!metriclist) |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | rblist__init(&groups); |
| 332 | groups.node_new = mep_new; |
| 333 | groups.node_cmp = mep_cmp; |
| 334 | groups.node_delete = mep_delete; |
| 335 | for (i = 0; ; i++) { |
| 336 | const char *g; |
| 337 | pe = &map->table[i]; |
| 338 | |
| 339 | if (!pe->name && !pe->metric_group && !pe->metric_name) |
| 340 | break; |
| 341 | if (!pe->metric_expr) |
| 342 | continue; |
| 343 | g = pe->metric_group; |
| 344 | if (!g && pe->metric_name) { |
| 345 | if (pe->name) |
| 346 | continue; |
| 347 | g = "No_group"; |
| 348 | } |
| 349 | if (g) { |
| 350 | char *omg; |
| 351 | char *mg = strdup(g); |
| 352 | |
| 353 | if (!mg) |
| 354 | return; |
| 355 | omg = mg; |
| 356 | while ((g = strsep(&mg, ";")) != NULL) { |
| 357 | struct mep *me; |
| 358 | char *s; |
| 359 | |
Arnaldo Carvalho de Melo | 80e9073 | 2019-06-26 11:21:47 -0300 | [diff] [blame] | 360 | g = skip_spaces(g); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 361 | if (*g == 0) |
| 362 | g = "No_group"; |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 363 | if (filter && !strstr(g, filter)) |
| 364 | continue; |
| 365 | if (raw) |
| 366 | s = (char *)pe->metric_name; |
| 367 | else { |
Michael Petlan | 95f0432 | 2018-07-30 17:35:04 -0400 | [diff] [blame] | 368 | if (asprintf(&s, "%s\n%*s%s]", |
| 369 | pe->metric_name, 8, "[", pe->desc) < 0) |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 370 | return; |
Jiri Olsa | 33bbc57 | 2019-02-13 13:32:41 +0100 | [diff] [blame] | 371 | |
| 372 | if (details) { |
| 373 | if (asprintf(&s, "%s\n%*s%s]", |
| 374 | s, 8, "[", pe->metric_expr) < 0) |
| 375 | return; |
| 376 | } |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | if (!s) |
| 380 | continue; |
| 381 | |
| 382 | if (!metricgroups) { |
| 383 | strlist__add(metriclist, s); |
| 384 | } else { |
| 385 | me = mep_lookup(&groups, g); |
| 386 | if (!me) |
| 387 | continue; |
| 388 | strlist__add(me->metrics, s); |
| 389 | } |
| 390 | } |
| 391 | free(omg); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | if (metricgroups && !raw) |
| 396 | printf("\nMetric Groups:\n\n"); |
| 397 | else if (metrics && !raw) |
| 398 | printf("\nMetrics:\n\n"); |
| 399 | |
Davidlohr Bueso | ca22702 | 2018-12-06 11:18:16 -0800 | [diff] [blame] | 400 | for (node = rb_first_cached(&groups.entries); node; node = next) { |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 401 | struct mep *me = container_of(node, struct mep, nd); |
| 402 | |
| 403 | if (metricgroups) |
Andi Kleen | 9c344d1 | 2019-06-28 15:07:36 -0700 | [diff] [blame] | 404 | printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n"); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 405 | if (metrics) |
| 406 | metricgroup__print_strlist(me->metrics, raw); |
| 407 | next = rb_next(node); |
| 408 | rblist__remove_node(&groups, node); |
| 409 | } |
| 410 | if (!metricgroups) |
| 411 | metricgroup__print_strlist(metriclist, raw); |
| 412 | strlist__delete(metriclist); |
| 413 | } |
| 414 | |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 415 | static void metricgroup__add_metric_weak_group(struct strbuf *events, |
| 416 | const char **ids, |
| 417 | int idnum) |
| 418 | { |
| 419 | bool no_group = false; |
| 420 | int i; |
| 421 | |
| 422 | for (i = 0; i < idnum; i++) { |
| 423 | pr_debug("found event %s\n", ids[i]); |
| 424 | /* |
| 425 | * Duration time maps to a software event and can make |
| 426 | * groups not count. Always use it outside a |
| 427 | * group. |
| 428 | */ |
| 429 | if (!strcmp(ids[i], "duration_time")) { |
| 430 | if (i > 0) |
| 431 | strbuf_addf(events, "}:W,"); |
| 432 | strbuf_addf(events, "duration_time"); |
| 433 | no_group = true; |
| 434 | continue; |
| 435 | } |
| 436 | strbuf_addf(events, "%s%s", |
| 437 | i == 0 || no_group ? "{" : ",", |
| 438 | ids[i]); |
| 439 | no_group = false; |
| 440 | } |
| 441 | if (!no_group) |
| 442 | strbuf_addf(events, "}:W"); |
| 443 | } |
| 444 | |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 445 | static void metricgroup__add_metric_non_group(struct strbuf *events, |
| 446 | const char **ids, |
| 447 | int idnum) |
| 448 | { |
| 449 | int i; |
| 450 | |
| 451 | for (i = 0; i < idnum; i++) |
| 452 | strbuf_addf(events, ",%s", ids[i]); |
| 453 | } |
| 454 | |
| 455 | static void metricgroup___watchdog_constraint_hint(const char *name, bool foot) |
| 456 | { |
| 457 | static bool violate_nmi_constraint; |
| 458 | |
| 459 | if (!foot) { |
| 460 | pr_warning("Splitting metric group %s into standalone metrics.\n", name); |
| 461 | violate_nmi_constraint = true; |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | if (!violate_nmi_constraint) |
| 466 | return; |
| 467 | |
| 468 | pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n" |
| 469 | " echo 0 > /proc/sys/kernel/nmi_watchdog\n" |
| 470 | " perf stat ...\n" |
| 471 | " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); |
| 472 | } |
| 473 | |
| 474 | static bool metricgroup__has_constraint(struct pmu_event *pe) |
| 475 | { |
| 476 | if (!pe->metric_constraint) |
| 477 | return false; |
| 478 | |
| 479 | if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") && |
| 480 | sysctl__nmi_watchdog_enabled()) { |
| 481 | metricgroup___watchdog_constraint_hint(pe->metric_name, false); |
| 482 | return true; |
| 483 | } |
| 484 | |
| 485 | return false; |
| 486 | } |
| 487 | |
Kajol Jain | 47352ab | 2020-04-02 02:03:36 +0530 | [diff] [blame] | 488 | static int __metricgroup__add_metric(struct strbuf *events, |
| 489 | struct list_head *group_list, struct pmu_event *pe) |
| 490 | { |
| 491 | |
| 492 | const char **ids; |
| 493 | int idnum; |
| 494 | struct egroup *eg; |
| 495 | |
| 496 | if (expr__find_other(pe->metric_expr, NULL, &ids, &idnum) < 0) |
| 497 | return -EINVAL; |
| 498 | |
| 499 | if (events->len > 0) |
| 500 | strbuf_addf(events, ","); |
| 501 | |
| 502 | if (metricgroup__has_constraint(pe)) |
| 503 | metricgroup__add_metric_non_group(events, ids, idnum); |
| 504 | else |
| 505 | metricgroup__add_metric_weak_group(events, ids, idnum); |
| 506 | |
| 507 | eg = malloc(sizeof(*eg)); |
| 508 | if (!eg) |
| 509 | return -ENOMEM; |
| 510 | |
| 511 | eg->ids = ids; |
| 512 | eg->idnum = idnum; |
| 513 | eg->metric_name = pe->metric_name; |
| 514 | eg->metric_expr = pe->metric_expr; |
| 515 | eg->metric_unit = pe->unit; |
| 516 | list_add_tail(&eg->nd, group_list); |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 521 | static int metricgroup__add_metric(const char *metric, struct strbuf *events, |
| 522 | struct list_head *group_list) |
| 523 | { |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 524 | struct pmu_events_map *map = perf_pmu__find_map(NULL); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 525 | struct pmu_event *pe; |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 526 | int i, ret = -EINVAL; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 527 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 528 | if (!map) |
| 529 | return 0; |
| 530 | |
| 531 | for (i = 0; ; i++) { |
| 532 | pe = &map->table[i]; |
| 533 | |
| 534 | if (!pe->name && !pe->metric_group && !pe->metric_name) |
| 535 | break; |
| 536 | if (!pe->metric_expr) |
| 537 | continue; |
| 538 | if (match_metric(pe->metric_group, metric) || |
| 539 | match_metric(pe->metric_name, metric)) { |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 540 | |
| 541 | pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name); |
| 542 | |
Kajol Jain | 47352ab | 2020-04-02 02:03:36 +0530 | [diff] [blame] | 543 | ret = __metricgroup__add_metric(events, group_list, pe); |
| 544 | if (ret == -ENOMEM) |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 545 | break; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | static int metricgroup__add_metric_list(const char *list, struct strbuf *events, |
| 552 | struct list_head *group_list) |
| 553 | { |
| 554 | char *llist, *nlist, *p; |
| 555 | int ret = -EINVAL; |
| 556 | |
| 557 | nlist = strdup(list); |
| 558 | if (!nlist) |
| 559 | return -ENOMEM; |
| 560 | llist = nlist; |
Andi Kleen | 411bc31 | 2017-09-14 13:57:35 -0700 | [diff] [blame] | 561 | |
| 562 | strbuf_init(events, 100); |
| 563 | strbuf_addf(events, "%s", ""); |
| 564 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 565 | while ((p = strsep(&llist, ",")) != NULL) { |
| 566 | ret = metricgroup__add_metric(p, events, group_list); |
| 567 | if (ret == -EINVAL) { |
| 568 | fprintf(stderr, "Cannot find metric or group `%s'\n", |
| 569 | p); |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | free(nlist); |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 574 | |
| 575 | if (!ret) |
| 576 | metricgroup___watchdog_constraint_hint(NULL, true); |
| 577 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | static void metricgroup__free_egroups(struct list_head *group_list) |
| 582 | { |
| 583 | struct egroup *eg, *egtmp; |
| 584 | int i; |
| 585 | |
| 586 | list_for_each_entry_safe (eg, egtmp, group_list, nd) { |
| 587 | for (i = 0; i < eg->idnum; i++) |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 588 | zfree(&eg->ids[i]); |
| 589 | zfree(&eg->ids); |
Arnaldo Carvalho de Melo | acc7bfb | 2019-07-04 12:20:21 -0300 | [diff] [blame] | 590 | list_del_init(&eg->nd); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 591 | free(eg); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | int metricgroup__parse_groups(const struct option *opt, |
| 596 | const char *str, |
| 597 | struct rblist *metric_events) |
| 598 | { |
| 599 | struct parse_events_error parse_error; |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 600 | struct evlist *perf_evlist = *(struct evlist **)opt->value; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 601 | struct strbuf extra_events; |
| 602 | LIST_HEAD(group_list); |
| 603 | int ret; |
| 604 | |
| 605 | if (metric_events->nr_entries == 0) |
| 606 | metricgroup__rblist_init(metric_events); |
| 607 | ret = metricgroup__add_metric_list(str, &extra_events, &group_list); |
| 608 | if (ret) |
| 609 | return ret; |
| 610 | pr_debug("adding %s\n", extra_events.buf); |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 611 | bzero(&parse_error, sizeof(parse_error)); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 612 | ret = parse_events(perf_evlist, extra_events.buf, &parse_error); |
| 613 | if (ret) { |
Andi Kleen | 333b566 | 2017-09-13 14:50:06 -0700 | [diff] [blame] | 614 | parse_events_print_error(&parse_error, extra_events.buf); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 615 | goto out; |
| 616 | } |
| 617 | strbuf_release(&extra_events); |
| 618 | ret = metricgroup__setup_events(&group_list, perf_evlist, |
| 619 | metric_events); |
| 620 | out: |
| 621 | metricgroup__free_egroups(&group_list); |
| 622 | return ret; |
| 623 | } |
Thomas Richter | 742d92f | 2018-06-26 09:17:01 +0200 | [diff] [blame] | 624 | |
| 625 | bool metricgroup__has_metric(const char *metric) |
| 626 | { |
| 627 | struct pmu_events_map *map = perf_pmu__find_map(NULL); |
| 628 | struct pmu_event *pe; |
| 629 | int i; |
| 630 | |
| 631 | if (!map) |
| 632 | return false; |
| 633 | |
| 634 | for (i = 0; ; i++) { |
| 635 | pe = &map->table[i]; |
| 636 | |
| 637 | if (!pe->name && !pe->metric_group && !pe->metric_name) |
| 638 | break; |
| 639 | if (!pe->metric_expr) |
| 640 | continue; |
| 641 | if (match_metric(pe->metric_name, metric)) |
| 642 | return true; |
| 643 | } |
| 644 | return false; |
| 645 | } |