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> |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 18 | #include "strlist.h" |
| 19 | #include <assert.h> |
Arnaldo Carvalho de Melo | bd9860b | 2019-06-25 21:13:51 -0300 | [diff] [blame] | 20 | #include <linux/ctype.h> |
Arnaldo Carvalho de Melo | b420902 | 2019-08-29 15:56:40 -0300 | [diff] [blame] | 21 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 22 | #include <linux/zalloc.h> |
Arnaldo Carvalho de Melo | 0b8026e | 2019-08-21 10:54:14 -0300 | [diff] [blame] | 23 | #include <subcmd/parse-options.h> |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 24 | #include <api/fs/fs.h> |
| 25 | #include "util.h" |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 26 | #include <asm/bug.h> |
Namhyung Kim | b214ba8 | 2020-09-24 21:44:53 +0900 | [diff] [blame] | 27 | #include "cgroup.h" |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 28 | |
| 29 | struct metric_event *metricgroup__lookup(struct rblist *metric_events, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 30 | struct evsel *evsel, |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 31 | bool create) |
| 32 | { |
| 33 | struct rb_node *nd; |
| 34 | struct metric_event me = { |
| 35 | .evsel = evsel |
| 36 | }; |
Andi Kleen | 4bd1bef | 2017-11-17 13:43:00 -0800 | [diff] [blame] | 37 | |
| 38 | if (!metric_events) |
| 39 | return NULL; |
| 40 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 41 | nd = rblist__find(metric_events, &me); |
| 42 | if (nd) |
| 43 | return container_of(nd, struct metric_event, nd); |
| 44 | if (create) { |
| 45 | rblist__add_node(metric_events, &me); |
| 46 | nd = rblist__find(metric_events, &me); |
| 47 | if (nd) |
| 48 | return container_of(nd, struct metric_event, nd); |
| 49 | } |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | static int metric_event_cmp(struct rb_node *rb_node, const void *entry) |
| 54 | { |
| 55 | struct metric_event *a = container_of(rb_node, |
| 56 | struct metric_event, |
| 57 | nd); |
| 58 | const struct metric_event *b = entry; |
| 59 | |
| 60 | if (a->evsel == b->evsel) |
| 61 | return 0; |
| 62 | if ((char *)a->evsel < (char *)b->evsel) |
| 63 | return -1; |
| 64 | return +1; |
| 65 | } |
| 66 | |
| 67 | static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused, |
| 68 | const void *entry) |
| 69 | { |
| 70 | struct metric_event *me = malloc(sizeof(struct metric_event)); |
| 71 | |
| 72 | if (!me) |
| 73 | return NULL; |
| 74 | memcpy(me, entry, sizeof(struct metric_event)); |
| 75 | me->evsel = ((struct metric_event *)entry)->evsel; |
| 76 | INIT_LIST_HEAD(&me->head); |
| 77 | return &me->nd; |
| 78 | } |
| 79 | |
Jiri Olsa | 9afe565 | 2020-06-02 23:47:38 +0200 | [diff] [blame] | 80 | static void metric_event_delete(struct rblist *rblist __maybe_unused, |
| 81 | struct rb_node *rb_node) |
| 82 | { |
| 83 | struct metric_event *me = container_of(rb_node, struct metric_event, nd); |
| 84 | struct metric_expr *expr, *tmp; |
| 85 | |
| 86 | list_for_each_entry_safe(expr, tmp, &me->head, nd) { |
Jiri Olsa | 4ea2896 | 2020-07-19 20:13:10 +0200 | [diff] [blame] | 87 | free(expr->metric_refs); |
Namhyung Kim | b033ab1 | 2020-09-15 12:18:10 +0900 | [diff] [blame] | 88 | free(expr->metric_events); |
Jiri Olsa | 9afe565 | 2020-06-02 23:47:38 +0200 | [diff] [blame] | 89 | free(expr); |
| 90 | } |
| 91 | |
| 92 | free(me); |
| 93 | } |
| 94 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 95 | static void metricgroup__rblist_init(struct rblist *metric_events) |
| 96 | { |
| 97 | rblist__init(metric_events); |
| 98 | metric_events->node_cmp = metric_event_cmp; |
| 99 | metric_events->node_new = metric_event_new; |
Jiri Olsa | 9afe565 | 2020-06-02 23:47:38 +0200 | [diff] [blame] | 100 | metric_events->node_delete = metric_event_delete; |
| 101 | } |
| 102 | |
| 103 | void metricgroup__rblist_exit(struct rblist *metric_events) |
| 104 | { |
| 105 | rblist__exit(metric_events); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 108 | /* |
| 109 | * A node in the list of referenced metrics. metric_expr |
| 110 | * is held as a convenience to avoid a search through the |
| 111 | * metric list. |
| 112 | */ |
| 113 | struct metric_ref_node { |
| 114 | const char *metric_name; |
| 115 | const char *metric_expr; |
| 116 | struct list_head list; |
| 117 | }; |
| 118 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 119 | struct metric { |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 120 | struct list_head nd; |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 121 | struct expr_parse_ctx pctx; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 122 | const char *metric_name; |
| 123 | const char *metric_expr; |
Jin Yao | 287f264 | 2019-08-28 13:59:31 +0800 | [diff] [blame] | 124 | const char *metric_unit; |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 125 | struct list_head metric_refs; |
| 126 | int metric_refs_cnt; |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 127 | int runtime; |
Ian Rogers | 7f9eca5 | 2020-05-20 11:20:07 -0700 | [diff] [blame] | 128 | bool has_constraint; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 131 | #define RECURSION_ID_MAX 1000 |
| 132 | |
| 133 | struct expr_ids { |
| 134 | struct expr_id id[RECURSION_ID_MAX]; |
| 135 | int cnt; |
| 136 | }; |
| 137 | |
| 138 | static struct expr_id *expr_ids__alloc(struct expr_ids *ids) |
| 139 | { |
| 140 | if (ids->cnt >= RECURSION_ID_MAX) |
| 141 | return NULL; |
| 142 | return &ids->id[ids->cnt++]; |
| 143 | } |
| 144 | |
| 145 | static void expr_ids__exit(struct expr_ids *ids) |
| 146 | { |
| 147 | int i; |
| 148 | |
| 149 | for (i = 0; i < ids->cnt; i++) |
| 150 | free(ids->id[i].id); |
| 151 | } |
| 152 | |
Ian Rogers | dcc81be | 2020-09-17 13:18:07 -0700 | [diff] [blame] | 153 | static bool contains_event(struct evsel **metric_events, int num_events, |
| 154 | const char *event_name) |
| 155 | { |
| 156 | int i; |
| 157 | |
| 158 | for (i = 0; i < num_events; i++) { |
| 159 | if (!strcmp(metric_events[i]->name, event_name)) |
| 160 | return true; |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 165 | /** |
Ian Rogers | 9e34c1c | 2020-09-09 20:26:31 -0700 | [diff] [blame] | 166 | * Find a group of events in perf_evlist that correspond to those from a parsed |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 167 | * metric expression. Note, as find_evsel_group is called in the same order as |
| 168 | * perf_evlist was constructed, metric_no_merge doesn't need to test for |
| 169 | * underfilling a group. |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 170 | * @perf_evlist: a list of events something like: {metric1 leader, metric1 |
| 171 | * sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling, |
| 172 | * metric2 sibling}:W,duration_time |
| 173 | * @pctx: the parse context for the metric expression. |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 174 | * @metric_no_merge: don't attempt to share events for the metric with other |
| 175 | * metrics. |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 176 | * @has_constraint: is there a contraint on the group of events? In which case |
| 177 | * the events won't be grouped. |
| 178 | * @metric_events: out argument, null terminated array of evsel's associated |
| 179 | * with the metric. |
| 180 | * @evlist_used: in/out argument, bitmap tracking which evlist events are used. |
| 181 | * @return the first metric event or NULL on failure. |
| 182 | */ |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 183 | static struct evsel *find_evsel_group(struct evlist *perf_evlist, |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 184 | struct expr_parse_ctx *pctx, |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 185 | bool metric_no_merge, |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 186 | bool has_constraint, |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 187 | struct evsel **metric_events, |
Ian Rogers | 45db55f | 2020-05-20 00:28:08 -0700 | [diff] [blame] | 188 | unsigned long *evlist_used) |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 189 | { |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 190 | struct evsel *ev, *current_leader = NULL; |
Jiri Olsa | 070b3b5 | 2020-07-12 15:26:18 +0200 | [diff] [blame] | 191 | struct expr_id_data *val_ptr; |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 192 | int i = 0, matched_events = 0, events_to_match; |
| 193 | const int idnum = (int)hashmap__size(&pctx->ids); |
| 194 | |
Ian Rogers | dcc81be | 2020-09-17 13:18:07 -0700 | [diff] [blame] | 195 | /* |
| 196 | * duration_time is always grouped separately, when events are grouped |
| 197 | * (ie has_constraint is false) then ignore it in the matching loop and |
| 198 | * add it to metric_events at the end. |
| 199 | */ |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 200 | if (!has_constraint && |
| 201 | hashmap__find(&pctx->ids, "duration_time", (void **)&val_ptr)) |
| 202 | events_to_match = idnum - 1; |
| 203 | else |
| 204 | events_to_match = idnum; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 205 | |
| 206 | evlist__for_each_entry (perf_evlist, ev) { |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 207 | /* |
| 208 | * Events with a constraint aren't grouped and match the first |
| 209 | * events available. |
| 210 | */ |
| 211 | if (has_constraint && ev->weak_group) |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 212 | continue; |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 213 | /* Ignore event if already used and merging is disabled. */ |
| 214 | if (metric_no_merge && test_bit(ev->idx, evlist_used)) |
| 215 | continue; |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 216 | if (!has_constraint && ev->leader != current_leader) { |
| 217 | /* |
| 218 | * Start of a new group, discard the whole match and |
| 219 | * start again. |
| 220 | */ |
| 221 | matched_events = 0; |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 222 | memset(metric_events, 0, |
| 223 | sizeof(struct evsel *) * idnum); |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 224 | current_leader = ev->leader; |
| 225 | } |
Ian Rogers | dcc81be | 2020-09-17 13:18:07 -0700 | [diff] [blame] | 226 | /* |
| 227 | * Check for duplicate events with the same name. For example, |
| 228 | * uncore_imc/cas_count_read/ will turn into 6 events per socket |
| 229 | * on skylakex. Only the first such event is placed in |
| 230 | * metric_events. If events aren't grouped then this also |
| 231 | * ensures that the same event in different sibling groups |
| 232 | * aren't both added to metric_events. |
| 233 | */ |
| 234 | if (contains_event(metric_events, matched_events, ev->name)) |
| 235 | continue; |
| 236 | /* Does this event belong to the parse context? */ |
| 237 | if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 238 | metric_events[matched_events++] = ev; |
Ian Rogers | dcc81be | 2020-09-17 13:18:07 -0700 | [diff] [blame] | 239 | |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 240 | if (matched_events == events_to_match) |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | if (events_to_match != idnum) { |
| 245 | /* Add the first duration_time. */ |
| 246 | evlist__for_each_entry(perf_evlist, ev) { |
| 247 | if (!strcmp(ev->name, "duration_time")) { |
| 248 | metric_events[matched_events++] = ev; |
| 249 | break; |
| 250 | } |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 253 | |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 254 | if (matched_events != idnum) { |
Ian Rogers | dcc81be | 2020-09-17 13:18:07 -0700 | [diff] [blame] | 255 | /* Not a whole match */ |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | metric_events[idnum] = NULL; |
| 260 | |
| 261 | for (i = 0; i < idnum; i++) { |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 262 | ev = metric_events[i]; |
Ian Rogers | dcc81be | 2020-09-17 13:18:07 -0700 | [diff] [blame] | 263 | /* Don't free the used events. */ |
Ian Rogers | 45db55f | 2020-05-20 00:28:08 -0700 | [diff] [blame] | 264 | set_bit(ev->idx, evlist_used); |
Ian Rogers | dcc81be | 2020-09-17 13:18:07 -0700 | [diff] [blame] | 265 | /* |
| 266 | * The metric leader points to the identically named event in |
| 267 | * metric_events. |
| 268 | */ |
| 269 | ev->metric_leader = ev; |
| 270 | /* |
| 271 | * Mark two events with identical names in the same group (or |
| 272 | * globally) as being in use as uncore events may be duplicated |
| 273 | * for each pmu. Set the metric leader of such events to be the |
| 274 | * event that appears in metric_events. |
| 275 | */ |
| 276 | evlist__for_each_entry_continue(perf_evlist, ev) { |
| 277 | /* |
| 278 | * If events are grouped then the search can terminate |
| 279 | * when then group is left. |
| 280 | */ |
| 281 | if (!has_constraint && |
John Garry | c2337d6 | 2020-12-04 19:10:12 +0800 | [diff] [blame^] | 282 | ev->leader != metric_events[i]->leader && |
| 283 | !strcmp(ev->leader->pmu_name, |
| 284 | metric_events[i]->leader->pmu_name)) |
Ian Rogers | dcc81be | 2020-09-17 13:18:07 -0700 | [diff] [blame] | 285 | break; |
| 286 | if (!strcmp(metric_events[i]->name, ev->name)) { |
| 287 | set_bit(ev->idx, evlist_used); |
| 288 | ev->metric_leader = metric_events[i]; |
| 289 | } |
| 290 | } |
Jin Yao | f01642e | 2019-08-28 13:59:32 +0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | return metric_events[0]; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static int metricgroup__setup_events(struct list_head *groups, |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 297 | bool metric_no_merge, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 298 | struct evlist *perf_evlist, |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 299 | struct rblist *metric_events_list) |
| 300 | { |
| 301 | struct metric_event *me; |
| 302 | struct metric_expr *expr; |
| 303 | int i = 0; |
| 304 | int ret = 0; |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 305 | struct metric *m; |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 306 | struct evsel *evsel, *tmp; |
Ian Rogers | 45db55f | 2020-05-20 00:28:08 -0700 | [diff] [blame] | 307 | unsigned long *evlist_used; |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 308 | |
Ian Rogers | 45db55f | 2020-05-20 00:28:08 -0700 | [diff] [blame] | 309 | evlist_used = bitmap_alloc(perf_evlist->core.nr_entries); |
| 310 | if (!evlist_used) |
| 311 | return -ENOMEM; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 312 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 313 | list_for_each_entry (m, groups, nd) { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 314 | struct evsel **metric_events; |
Jiri Olsa | 4ea2896 | 2020-07-19 20:13:10 +0200 | [diff] [blame] | 315 | struct metric_ref *metric_refs = NULL; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 316 | |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 317 | metric_events = calloc(sizeof(void *), |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 318 | hashmap__size(&m->pctx.ids) + 1); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 319 | if (!metric_events) { |
| 320 | ret = -ENOMEM; |
| 321 | break; |
| 322 | } |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 323 | evsel = find_evsel_group(perf_evlist, &m->pctx, |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 324 | metric_no_merge, |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 325 | m->has_constraint, metric_events, |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 326 | evlist_used); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 327 | if (!evsel) { |
| 328 | pr_debug("Cannot resolve %s: %s\n", |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 329 | m->metric_name, m->metric_expr); |
Ian Rogers | a159e2f | 2020-05-07 22:36:24 -0700 | [diff] [blame] | 330 | free(metric_events); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 331 | continue; |
| 332 | } |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 333 | for (i = 0; metric_events[i]; i++) |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 334 | metric_events[i]->collect_stat = true; |
| 335 | me = metricgroup__lookup(metric_events_list, evsel, true); |
| 336 | if (!me) { |
| 337 | ret = -ENOMEM; |
Ian Rogers | a159e2f | 2020-05-07 22:36:24 -0700 | [diff] [blame] | 338 | free(metric_events); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | expr = malloc(sizeof(struct metric_expr)); |
| 342 | if (!expr) { |
| 343 | ret = -ENOMEM; |
Ian Rogers | a159e2f | 2020-05-07 22:36:24 -0700 | [diff] [blame] | 344 | free(metric_events); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 345 | break; |
| 346 | } |
Jiri Olsa | 4ea2896 | 2020-07-19 20:13:10 +0200 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * Collect and store collected nested expressions |
| 350 | * for metric processing. |
| 351 | */ |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 352 | if (m->metric_refs_cnt) { |
Jiri Olsa | 4ea2896 | 2020-07-19 20:13:10 +0200 | [diff] [blame] | 353 | struct metric_ref_node *ref; |
| 354 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 355 | metric_refs = zalloc(sizeof(struct metric_ref) * (m->metric_refs_cnt + 1)); |
Jiri Olsa | 4ea2896 | 2020-07-19 20:13:10 +0200 | [diff] [blame] | 356 | if (!metric_refs) { |
| 357 | ret = -ENOMEM; |
| 358 | free(metric_events); |
Namhyung Kim | b033ab1 | 2020-09-15 12:18:10 +0900 | [diff] [blame] | 359 | free(expr); |
Jiri Olsa | 4ea2896 | 2020-07-19 20:13:10 +0200 | [diff] [blame] | 360 | break; |
| 361 | } |
| 362 | |
| 363 | i = 0; |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 364 | list_for_each_entry(ref, &m->metric_refs, list) { |
Jiri Olsa | 4ea2896 | 2020-07-19 20:13:10 +0200 | [diff] [blame] | 365 | /* |
| 366 | * Intentionally passing just const char pointers, |
| 367 | * originally from 'struct pmu_event' object. |
| 368 | * We don't need to change them, so there's no |
| 369 | * need to create our own copy. |
| 370 | */ |
| 371 | metric_refs[i].metric_name = ref->metric_name; |
| 372 | metric_refs[i].metric_expr = ref->metric_expr; |
| 373 | i++; |
| 374 | } |
| 375 | }; |
| 376 | |
| 377 | expr->metric_refs = metric_refs; |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 378 | expr->metric_expr = m->metric_expr; |
| 379 | expr->metric_name = m->metric_name; |
| 380 | expr->metric_unit = m->metric_unit; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 381 | expr->metric_events = metric_events; |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 382 | expr->runtime = m->runtime; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 383 | list_add(&expr->nd, &me->head); |
| 384 | } |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 385 | |
Ian Rogers | 2440689 | 2020-05-20 11:20:09 -0700 | [diff] [blame] | 386 | evlist__for_each_entry_safe(perf_evlist, tmp, evsel) { |
| 387 | if (!test_bit(evsel->idx, evlist_used)) { |
| 388 | evlist__remove(perf_evlist, evsel); |
| 389 | evsel__delete(evsel); |
| 390 | } |
| 391 | } |
Ian Rogers | 45db55f | 2020-05-20 00:28:08 -0700 | [diff] [blame] | 392 | bitmap_free(evlist_used); |
Kajol Jain | 58fc90f | 2020-02-21 15:41:21 +0530 | [diff] [blame] | 393 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | static bool match_metric(const char *n, const char *list) |
| 398 | { |
| 399 | int len; |
| 400 | char *m; |
| 401 | |
| 402 | if (!list) |
| 403 | return false; |
| 404 | if (!strcmp(list, "all")) |
| 405 | return true; |
| 406 | if (!n) |
| 407 | return !strcasecmp(list, "No_group"); |
| 408 | len = strlen(list); |
| 409 | m = strcasestr(n, list); |
| 410 | if (!m) |
| 411 | return false; |
| 412 | if ((m == n || m[-1] == ';' || m[-1] == ' ') && |
| 413 | (m[len] == 0 || m[len] == ';')) |
| 414 | return true; |
| 415 | return false; |
| 416 | } |
| 417 | |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 418 | struct mep { |
| 419 | struct rb_node nd; |
| 420 | const char *name; |
| 421 | struct strlist *metrics; |
| 422 | }; |
| 423 | |
| 424 | static int mep_cmp(struct rb_node *rb_node, const void *entry) |
| 425 | { |
| 426 | struct mep *a = container_of(rb_node, struct mep, nd); |
| 427 | struct mep *b = (struct mep *)entry; |
| 428 | |
| 429 | return strcmp(a->name, b->name); |
| 430 | } |
| 431 | |
| 432 | static struct rb_node *mep_new(struct rblist *rl __maybe_unused, |
| 433 | const void *entry) |
| 434 | { |
| 435 | struct mep *me = malloc(sizeof(struct mep)); |
| 436 | |
| 437 | if (!me) |
| 438 | return NULL; |
| 439 | memcpy(me, entry, sizeof(struct mep)); |
| 440 | me->name = strdup(me->name); |
| 441 | if (!me->name) |
| 442 | goto out_me; |
| 443 | me->metrics = strlist__new(NULL, NULL); |
| 444 | if (!me->metrics) |
| 445 | goto out_name; |
| 446 | return &me->nd; |
| 447 | out_name: |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 448 | zfree(&me->name); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 449 | out_me: |
| 450 | free(me); |
| 451 | return NULL; |
| 452 | } |
| 453 | |
| 454 | static struct mep *mep_lookup(struct rblist *groups, const char *name) |
| 455 | { |
| 456 | struct rb_node *nd; |
| 457 | struct mep me = { |
| 458 | .name = name |
| 459 | }; |
| 460 | nd = rblist__find(groups, &me); |
| 461 | if (nd) |
| 462 | return container_of(nd, struct mep, nd); |
| 463 | rblist__add_node(groups, &me); |
| 464 | nd = rblist__find(groups, &me); |
| 465 | if (nd) |
| 466 | return container_of(nd, struct mep, nd); |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | static void mep_delete(struct rblist *rl __maybe_unused, |
| 471 | struct rb_node *nd) |
| 472 | { |
| 473 | struct mep *me = container_of(nd, struct mep, nd); |
| 474 | |
| 475 | strlist__delete(me->metrics); |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 476 | zfree(&me->name); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 477 | free(me); |
| 478 | } |
| 479 | |
| 480 | static void metricgroup__print_strlist(struct strlist *metrics, bool raw) |
| 481 | { |
| 482 | struct str_node *sn; |
| 483 | int n = 0; |
| 484 | |
| 485 | strlist__for_each_entry (sn, metrics) { |
| 486 | if (raw) |
| 487 | printf("%s%s", n > 0 ? " " : "", sn->s); |
| 488 | else |
| 489 | printf(" %s\n", sn->s); |
| 490 | n++; |
| 491 | } |
| 492 | if (raw) |
| 493 | putchar('\n'); |
| 494 | } |
| 495 | |
| 496 | void metricgroup__print(bool metrics, bool metricgroups, char *filter, |
Jiri Olsa | 33bbc57 | 2019-02-13 13:32:41 +0100 | [diff] [blame] | 497 | bool raw, bool details) |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 498 | { |
Ganapatrao Kulkarni | 54e32dc | 2017-10-17 00:02:18 +0530 | [diff] [blame] | 499 | struct pmu_events_map *map = perf_pmu__find_map(NULL); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 500 | struct pmu_event *pe; |
| 501 | int i; |
| 502 | struct rblist groups; |
| 503 | struct rb_node *node, *next; |
| 504 | struct strlist *metriclist = NULL; |
| 505 | |
| 506 | if (!map) |
| 507 | return; |
| 508 | |
| 509 | if (!metricgroups) { |
| 510 | metriclist = strlist__new(NULL, NULL); |
| 511 | if (!metriclist) |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | rblist__init(&groups); |
| 516 | groups.node_new = mep_new; |
| 517 | groups.node_cmp = mep_cmp; |
| 518 | groups.node_delete = mep_delete; |
| 519 | for (i = 0; ; i++) { |
| 520 | const char *g; |
| 521 | pe = &map->table[i]; |
| 522 | |
| 523 | if (!pe->name && !pe->metric_group && !pe->metric_name) |
| 524 | break; |
| 525 | if (!pe->metric_expr) |
| 526 | continue; |
| 527 | g = pe->metric_group; |
| 528 | if (!g && pe->metric_name) { |
| 529 | if (pe->name) |
| 530 | continue; |
| 531 | g = "No_group"; |
| 532 | } |
| 533 | if (g) { |
| 534 | char *omg; |
| 535 | char *mg = strdup(g); |
| 536 | |
| 537 | if (!mg) |
| 538 | return; |
| 539 | omg = mg; |
| 540 | while ((g = strsep(&mg, ";")) != NULL) { |
| 541 | struct mep *me; |
| 542 | char *s; |
| 543 | |
Arnaldo Carvalho de Melo | 80e9073 | 2019-06-26 11:21:47 -0300 | [diff] [blame] | 544 | g = skip_spaces(g); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 545 | if (*g == 0) |
| 546 | g = "No_group"; |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 547 | if (filter && !strstr(g, filter)) |
| 548 | continue; |
| 549 | if (raw) |
| 550 | s = (char *)pe->metric_name; |
| 551 | else { |
Michael Petlan | 95f0432 | 2018-07-30 17:35:04 -0400 | [diff] [blame] | 552 | if (asprintf(&s, "%s\n%*s%s]", |
| 553 | pe->metric_name, 8, "[", pe->desc) < 0) |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 554 | return; |
Jiri Olsa | 33bbc57 | 2019-02-13 13:32:41 +0100 | [diff] [blame] | 555 | |
| 556 | if (details) { |
| 557 | if (asprintf(&s, "%s\n%*s%s]", |
| 558 | s, 8, "[", pe->metric_expr) < 0) |
| 559 | return; |
| 560 | } |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | if (!s) |
| 564 | continue; |
| 565 | |
| 566 | if (!metricgroups) { |
| 567 | strlist__add(metriclist, s); |
| 568 | } else { |
| 569 | me = mep_lookup(&groups, g); |
| 570 | if (!me) |
| 571 | continue; |
| 572 | strlist__add(me->metrics, s); |
| 573 | } |
Namhyung Kim | 4f57a1e | 2020-09-15 12:18:09 +0900 | [diff] [blame] | 574 | |
| 575 | if (!raw) |
| 576 | free(s); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 577 | } |
| 578 | free(omg); |
| 579 | } |
| 580 | } |
| 581 | |
Namhyung Kim | fac49a3 | 2020-09-09 14:58:48 +0900 | [diff] [blame] | 582 | if (!filter || !rblist__empty(&groups)) { |
| 583 | if (metricgroups && !raw) |
| 584 | printf("\nMetric Groups:\n\n"); |
| 585 | else if (metrics && !raw) |
| 586 | printf("\nMetrics:\n\n"); |
| 587 | } |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 588 | |
Davidlohr Bueso | ca22702 | 2018-12-06 11:18:16 -0800 | [diff] [blame] | 589 | for (node = rb_first_cached(&groups.entries); node; node = next) { |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 590 | struct mep *me = container_of(node, struct mep, nd); |
| 591 | |
| 592 | if (metricgroups) |
Andi Kleen | 9c344d1 | 2019-06-28 15:07:36 -0700 | [diff] [blame] | 593 | printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n"); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 594 | if (metrics) |
| 595 | metricgroup__print_strlist(me->metrics, raw); |
| 596 | next = rb_next(node); |
| 597 | rblist__remove_node(&groups, node); |
| 598 | } |
| 599 | if (!metricgroups) |
| 600 | metricgroup__print_strlist(metriclist, raw); |
| 601 | strlist__delete(metriclist); |
| 602 | } |
| 603 | |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 604 | static void metricgroup__add_metric_weak_group(struct strbuf *events, |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 605 | struct expr_parse_ctx *ctx) |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 606 | { |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 607 | struct hashmap_entry *cur; |
Ian Rogers | 4e21c13 | 2020-05-20 11:20:05 -0700 | [diff] [blame] | 608 | size_t bkt; |
| 609 | bool no_group = true, has_duration = false; |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 610 | |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 611 | hashmap__for_each_entry((&ctx->ids), cur, bkt) { |
| 612 | pr_debug("found event %s\n", (const char *)cur->key); |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 613 | /* |
| 614 | * Duration time maps to a software event and can make |
| 615 | * groups not count. Always use it outside a |
| 616 | * group. |
| 617 | */ |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 618 | if (!strcmp(cur->key, "duration_time")) { |
Ian Rogers | 4e21c13 | 2020-05-20 11:20:05 -0700 | [diff] [blame] | 619 | has_duration = true; |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 620 | continue; |
| 621 | } |
| 622 | strbuf_addf(events, "%s%s", |
Ian Rogers | 4e21c13 | 2020-05-20 11:20:05 -0700 | [diff] [blame] | 623 | no_group ? "{" : ",", |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 624 | (const char *)cur->key); |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 625 | no_group = false; |
| 626 | } |
Ian Rogers | 4e21c13 | 2020-05-20 11:20:05 -0700 | [diff] [blame] | 627 | if (!no_group) { |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 628 | strbuf_addf(events, "}:W"); |
Ian Rogers | 4e21c13 | 2020-05-20 11:20:05 -0700 | [diff] [blame] | 629 | if (has_duration) |
| 630 | strbuf_addf(events, ",duration_time"); |
| 631 | } else if (has_duration) |
| 632 | strbuf_addf(events, "duration_time"); |
Kan Liang | f742634 | 2020-02-24 13:59:21 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 635 | static void metricgroup__add_metric_non_group(struct strbuf *events, |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 636 | struct expr_parse_ctx *ctx) |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 637 | { |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 638 | struct hashmap_entry *cur; |
| 639 | size_t bkt; |
Ian Rogers | e2ce105 | 2020-05-20 11:20:11 -0700 | [diff] [blame] | 640 | bool first = true; |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 641 | |
Ian Rogers | e2ce105 | 2020-05-20 11:20:11 -0700 | [diff] [blame] | 642 | hashmap__for_each_entry((&ctx->ids), cur, bkt) { |
| 643 | if (!first) |
| 644 | strbuf_addf(events, ","); |
| 645 | strbuf_addf(events, "%s", (const char *)cur->key); |
| 646 | first = false; |
| 647 | } |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static void metricgroup___watchdog_constraint_hint(const char *name, bool foot) |
| 651 | { |
| 652 | static bool violate_nmi_constraint; |
| 653 | |
| 654 | if (!foot) { |
| 655 | pr_warning("Splitting metric group %s into standalone metrics.\n", name); |
| 656 | violate_nmi_constraint = true; |
| 657 | return; |
| 658 | } |
| 659 | |
| 660 | if (!violate_nmi_constraint) |
| 661 | return; |
| 662 | |
| 663 | pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n" |
| 664 | " echo 0 > /proc/sys/kernel/nmi_watchdog\n" |
| 665 | " perf stat ...\n" |
| 666 | " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); |
| 667 | } |
| 668 | |
| 669 | static bool metricgroup__has_constraint(struct pmu_event *pe) |
| 670 | { |
| 671 | if (!pe->metric_constraint) |
| 672 | return false; |
| 673 | |
| 674 | if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") && |
| 675 | sysctl__nmi_watchdog_enabled()) { |
| 676 | metricgroup___watchdog_constraint_hint(pe->metric_name, false); |
| 677 | return true; |
| 678 | } |
| 679 | |
| 680 | return false; |
| 681 | } |
| 682 | |
Kajol Jain | f5a489d | 2020-09-07 12:11:32 +0530 | [diff] [blame] | 683 | int __weak arch_get_runtimeparam(struct pmu_event *pe __maybe_unused) |
Kajol Jain | 1e1a873 | 2020-04-02 02:03:37 +0530 | [diff] [blame] | 684 | { |
| 685 | return 1; |
| 686 | } |
| 687 | |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 688 | static int __add_metric(struct list_head *metric_list, |
Jiri Olsa | e7e1badd | 2020-07-19 20:13:08 +0200 | [diff] [blame] | 689 | struct pmu_event *pe, |
| 690 | bool metric_no_group, |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 691 | int runtime, |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 692 | struct metric **mp, |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 693 | struct expr_id *parent, |
| 694 | struct expr_ids *ids) |
Kajol Jain | 47352ab | 2020-04-02 02:03:36 +0530 | [diff] [blame] | 695 | { |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 696 | struct metric_ref_node *ref; |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 697 | struct metric *m; |
Kajol Jain | 47352ab | 2020-04-02 02:03:36 +0530 | [diff] [blame] | 698 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 699 | if (*mp == NULL) { |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 700 | /* |
| 701 | * We got in here for the parent group, |
| 702 | * allocate it and put it on the list. |
| 703 | */ |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 704 | m = zalloc(sizeof(*m)); |
| 705 | if (!m) |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 706 | return -ENOMEM; |
Kajol Jain | 47352ab | 2020-04-02 02:03:36 +0530 | [diff] [blame] | 707 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 708 | expr__ctx_init(&m->pctx); |
| 709 | m->metric_name = pe->metric_name; |
| 710 | m->metric_expr = pe->metric_expr; |
| 711 | m->metric_unit = pe->unit; |
| 712 | m->runtime = runtime; |
| 713 | m->has_constraint = metric_no_group || metricgroup__has_constraint(pe); |
| 714 | INIT_LIST_HEAD(&m->metric_refs); |
| 715 | m->metric_refs_cnt = 0; |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 716 | |
| 717 | parent = expr_ids__alloc(ids); |
| 718 | if (!parent) { |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 719 | free(m); |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 720 | return -EINVAL; |
| 721 | } |
| 722 | |
| 723 | parent->id = strdup(pe->metric_name); |
| 724 | if (!parent->id) { |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 725 | free(m); |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 726 | return -ENOMEM; |
| 727 | } |
Namhyung Kim | 6f47ed6 | 2020-09-15 12:18:17 +0900 | [diff] [blame] | 728 | *mp = m; |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 729 | } else { |
| 730 | /* |
| 731 | * We got here for the referenced metric, via the |
| 732 | * recursive metricgroup__add_metric call, add |
| 733 | * it to the parent group. |
| 734 | */ |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 735 | m = *mp; |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 736 | |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 737 | ref = malloc(sizeof(*ref)); |
| 738 | if (!ref) |
| 739 | return -ENOMEM; |
| 740 | |
| 741 | /* |
| 742 | * Intentionally passing just const char pointers, |
| 743 | * from 'pe' object, so they never go away. We don't |
| 744 | * need to change them, so there's no need to create |
| 745 | * our own copy. |
| 746 | */ |
| 747 | ref->metric_name = pe->metric_name; |
| 748 | ref->metric_expr = pe->metric_expr; |
| 749 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 750 | list_add(&ref->list, &m->metric_refs); |
| 751 | m->metric_refs_cnt++; |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 752 | } |
| 753 | |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 754 | /* Force all found IDs in metric to have us as parent ID. */ |
| 755 | WARN_ON_ONCE(!parent); |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 756 | m->pctx.parent = parent; |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 757 | |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 758 | /* |
| 759 | * For both the parent and referenced metrics, we parse |
| 760 | * all the metric's IDs and add it to the parent context. |
| 761 | */ |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 762 | if (expr__find_other(pe->metric_expr, NULL, &m->pctx, runtime) < 0) { |
Namhyung Kim | 6f47ed6 | 2020-09-15 12:18:17 +0900 | [diff] [blame] | 763 | if (m->metric_refs_cnt == 0) { |
| 764 | expr__ctx_clear(&m->pctx); |
| 765 | free(m); |
| 766 | *mp = NULL; |
| 767 | } |
Ian Rogers | ded80bd | 2020-05-15 15:17:32 -0700 | [diff] [blame] | 768 | return -EINVAL; |
| 769 | } |
| 770 | |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 771 | /* |
| 772 | * We add new group only in the 'parent' call, |
| 773 | * so bail out for referenced metric case. |
| 774 | */ |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 775 | if (m->metric_refs_cnt) |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 776 | return 0; |
| 777 | |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 778 | if (list_empty(metric_list)) |
| 779 | list_add(&m->nd, metric_list); |
Ian Rogers | 6bf2102 | 2020-05-20 11:20:08 -0700 | [diff] [blame] | 780 | else { |
| 781 | struct list_head *pos; |
| 782 | |
| 783 | /* Place the largest groups at the front. */ |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 784 | list_for_each_prev(pos, metric_list) { |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 785 | struct metric *old = list_entry(pos, struct metric, nd); |
Ian Rogers | 6bf2102 | 2020-05-20 11:20:08 -0700 | [diff] [blame] | 786 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 787 | if (hashmap__size(&m->pctx.ids) <= |
Ian Rogers | 6bf2102 | 2020-05-20 11:20:08 -0700 | [diff] [blame] | 788 | hashmap__size(&old->pctx.ids)) |
| 789 | break; |
| 790 | } |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 791 | list_add(&m->nd, pos); |
Ian Rogers | 6bf2102 | 2020-05-20 11:20:08 -0700 | [diff] [blame] | 792 | } |
Kajol Jain | 47352ab | 2020-04-02 02:03:36 +0530 | [diff] [blame] | 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | |
Jiri Olsa | ce39194 | 2020-07-19 20:13:06 +0200 | [diff] [blame] | 797 | #define map_for_each_event(__pe, __idx, __map) \ |
| 798 | for (__idx = 0, __pe = &__map->table[__idx]; \ |
| 799 | __pe->name || __pe->metric_group || __pe->metric_name; \ |
| 800 | __pe = &__map->table[++__idx]) |
| 801 | |
| 802 | #define map_for_each_metric(__pe, __idx, __map, __metric) \ |
| 803 | map_for_each_event(__pe, __idx, __map) \ |
| 804 | if (__pe->metric_expr && \ |
| 805 | (match_metric(__pe->metric_group, __metric) || \ |
| 806 | match_metric(__pe->metric_name, __metric))) |
| 807 | |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 808 | static struct pmu_event *find_metric(const char *metric, struct pmu_events_map *map) |
| 809 | { |
| 810 | struct pmu_event *pe; |
| 811 | int i; |
| 812 | |
| 813 | map_for_each_event(pe, i, map) { |
| 814 | if (match_metric(pe->metric_name, metric)) |
| 815 | return pe; |
| 816 | } |
| 817 | |
| 818 | return NULL; |
| 819 | } |
| 820 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 821 | static int recursion_check(struct metric *m, const char *id, struct expr_id **parent, |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 822 | struct expr_ids *ids) |
| 823 | { |
| 824 | struct expr_id_data *data; |
| 825 | struct expr_id *p; |
| 826 | int ret; |
| 827 | |
| 828 | /* |
| 829 | * We get the parent referenced by 'id' argument and |
| 830 | * traverse through all the parent object IDs to check |
| 831 | * if we already processed 'id', if we did, it's recursion |
| 832 | * and we fail. |
| 833 | */ |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 834 | ret = expr__get_id(&m->pctx, id, &data); |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 835 | if (ret) |
| 836 | return ret; |
| 837 | |
Ian Rogers | 29396cd | 2020-08-26 08:30:55 -0700 | [diff] [blame] | 838 | p = expr_id_data__parent(data); |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 839 | |
| 840 | while (p->parent) { |
| 841 | if (!strcmp(p->id, id)) { |
| 842 | pr_err("failed: recursion detected for %s\n", id); |
| 843 | return -1; |
| 844 | } |
| 845 | p = p->parent; |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | * If we are over the limit of static entris, the metric |
| 850 | * is too difficult/nested to process, fail as well. |
| 851 | */ |
| 852 | p = expr_ids__alloc(ids); |
| 853 | if (!p) { |
| 854 | pr_err("failed: too many nested metrics\n"); |
| 855 | return -EINVAL; |
| 856 | } |
| 857 | |
| 858 | p->id = strdup(id); |
Ian Rogers | 29396cd | 2020-08-26 08:30:55 -0700 | [diff] [blame] | 859 | p->parent = expr_id_data__parent(data); |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 860 | *parent = p; |
| 861 | |
| 862 | return p->id ? 0 : -ENOMEM; |
| 863 | } |
| 864 | |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 865 | static int add_metric(struct list_head *metric_list, |
Jiri Olsa | a29c164 | 2020-07-19 20:13:07 +0200 | [diff] [blame] | 866 | struct pmu_event *pe, |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 867 | bool metric_no_group, |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 868 | struct metric **mp, |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 869 | struct expr_id *parent, |
| 870 | struct expr_ids *ids); |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 871 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 872 | static int __resolve_metric(struct metric *m, |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 873 | bool metric_no_group, |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 874 | struct list_head *metric_list, |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 875 | struct pmu_events_map *map, |
| 876 | struct expr_ids *ids) |
Jiri Olsa | a29c164 | 2020-07-19 20:13:07 +0200 | [diff] [blame] | 877 | { |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 878 | struct hashmap_entry *cur; |
| 879 | size_t bkt; |
| 880 | bool all; |
| 881 | int ret; |
| 882 | |
| 883 | /* |
| 884 | * Iterate all the parsed IDs and if there's metric, |
| 885 | * add it to the context. |
| 886 | */ |
| 887 | do { |
| 888 | all = true; |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 889 | hashmap__for_each_entry((&m->pctx.ids), cur, bkt) { |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 890 | struct expr_id *parent; |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 891 | struct pmu_event *pe; |
| 892 | |
| 893 | pe = find_metric(cur->key, map); |
| 894 | if (!pe) |
| 895 | continue; |
| 896 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 897 | ret = recursion_check(m, cur->key, &parent, ids); |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 898 | if (ret) |
| 899 | return ret; |
| 900 | |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 901 | all = false; |
| 902 | /* The metric key itself needs to go out.. */ |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 903 | expr__del_id(&m->pctx, cur->key); |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 904 | |
| 905 | /* ... and it gets resolved to the parent context. */ |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 906 | ret = add_metric(metric_list, pe, metric_no_group, &m, parent, ids); |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 907 | if (ret) |
| 908 | return ret; |
| 909 | |
| 910 | /* |
| 911 | * We added new metric to hashmap, so we need |
| 912 | * to break the iteration and start over. |
| 913 | */ |
| 914 | break; |
| 915 | } |
| 916 | } while (!all); |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | static int resolve_metric(bool metric_no_group, |
| 922 | struct list_head *metric_list, |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 923 | struct pmu_events_map *map, |
| 924 | struct expr_ids *ids) |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 925 | { |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 926 | struct metric *m; |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 927 | int err; |
| 928 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 929 | list_for_each_entry(m, metric_list, nd) { |
| 930 | err = __resolve_metric(m, metric_no_group, metric_list, map, ids); |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 931 | if (err) |
| 932 | return err; |
| 933 | } |
| 934 | return 0; |
| 935 | } |
| 936 | |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 937 | static int add_metric(struct list_head *metric_list, |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 938 | struct pmu_event *pe, |
| 939 | bool metric_no_group, |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 940 | struct metric **m, |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 941 | struct expr_id *parent, |
| 942 | struct expr_ids *ids) |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 943 | { |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 944 | struct metric *orig = *m; |
Jiri Olsa | a29c164 | 2020-07-19 20:13:07 +0200 | [diff] [blame] | 945 | int ret = 0; |
| 946 | |
| 947 | pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name); |
| 948 | |
| 949 | if (!strstr(pe->metric_expr, "?")) { |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 950 | ret = __add_metric(metric_list, pe, metric_no_group, 1, m, parent, ids); |
Jiri Olsa | a29c164 | 2020-07-19 20:13:07 +0200 | [diff] [blame] | 951 | } else { |
| 952 | int j, count; |
| 953 | |
Kajol Jain | f5a489d | 2020-09-07 12:11:32 +0530 | [diff] [blame] | 954 | count = arch_get_runtimeparam(pe); |
Jiri Olsa | a29c164 | 2020-07-19 20:13:07 +0200 | [diff] [blame] | 955 | |
| 956 | /* This loop is added to create multiple |
| 957 | * events depend on count value and add |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 958 | * those events to metric_list. |
Jiri Olsa | a29c164 | 2020-07-19 20:13:07 +0200 | [diff] [blame] | 959 | */ |
| 960 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 961 | for (j = 0; j < count && !ret; j++, *m = orig) |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 962 | ret = __add_metric(metric_list, pe, metric_no_group, j, m, parent, ids); |
Jiri Olsa | a29c164 | 2020-07-19 20:13:07 +0200 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | return ret; |
| 966 | } |
| 967 | |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 968 | static int metricgroup__add_metric(const char *metric, bool metric_no_group, |
| 969 | struct strbuf *events, |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 970 | struct list_head *metric_list, |
Jiri Olsa | 1381396 | 2020-06-09 12:57:47 -0300 | [diff] [blame] | 971 | struct pmu_events_map *map) |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 972 | { |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 973 | struct expr_ids ids = { .cnt = 0, }; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 974 | struct pmu_event *pe; |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 975 | struct metric *m; |
Jiri Olsa | 98461d9 | 2020-07-19 20:13:13 +0200 | [diff] [blame] | 976 | LIST_HEAD(list); |
Ian Rogers | 9081039 | 2020-05-20 11:20:06 -0700 | [diff] [blame] | 977 | int i, ret; |
| 978 | bool has_match = false; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 979 | |
Jiri Olsa | ce39194 | 2020-07-19 20:13:06 +0200 | [diff] [blame] | 980 | map_for_each_metric(pe, i, map, metric) { |
Jiri Olsa | ce39194 | 2020-07-19 20:13:06 +0200 | [diff] [blame] | 981 | has_match = true; |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 982 | m = NULL; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 983 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 984 | ret = add_metric(&list, pe, metric_no_group, &m, NULL, &ids); |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 985 | if (ret) |
Namhyung Kim | 27adafc | 2020-09-15 12:18:16 +0900 | [diff] [blame] | 986 | goto out; |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 987 | |
| 988 | /* |
| 989 | * Process any possible referenced metrics |
| 990 | * included in the expression. |
| 991 | */ |
| 992 | ret = resolve_metric(metric_no_group, |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 993 | &list, map, &ids); |
Jiri Olsa | a29c164 | 2020-07-19 20:13:07 +0200 | [diff] [blame] | 994 | if (ret) |
Namhyung Kim | 27adafc | 2020-09-15 12:18:16 +0900 | [diff] [blame] | 995 | goto out; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 996 | } |
Jiri Olsa | ce39194 | 2020-07-19 20:13:06 +0200 | [diff] [blame] | 997 | |
| 998 | /* End of pmu events. */ |
Namhyung Kim | 27adafc | 2020-09-15 12:18:16 +0900 | [diff] [blame] | 999 | if (!has_match) { |
| 1000 | ret = -EINVAL; |
| 1001 | goto out; |
| 1002 | } |
Jiri Olsa | ce39194 | 2020-07-19 20:13:06 +0200 | [diff] [blame] | 1003 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 1004 | list_for_each_entry(m, &list, nd) { |
Ian Rogers | 7f9eca5 | 2020-05-20 11:20:07 -0700 | [diff] [blame] | 1005 | if (events->len > 0) |
| 1006 | strbuf_addf(events, ","); |
| 1007 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 1008 | if (m->has_constraint) { |
Ian Rogers | 7f9eca5 | 2020-05-20 11:20:07 -0700 | [diff] [blame] | 1009 | metricgroup__add_metric_non_group(events, |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 1010 | &m->pctx); |
Ian Rogers | 7f9eca5 | 2020-05-20 11:20:07 -0700 | [diff] [blame] | 1011 | } else { |
| 1012 | metricgroup__add_metric_weak_group(events, |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 1013 | &m->pctx); |
Ian Rogers | 7f9eca5 | 2020-05-20 11:20:07 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
Jiri Olsa | 98461d9 | 2020-07-19 20:13:13 +0200 | [diff] [blame] | 1016 | |
Namhyung Kim | 27adafc | 2020-09-15 12:18:16 +0900 | [diff] [blame] | 1017 | out: |
| 1018 | /* |
| 1019 | * add to metric_list so that they can be released |
| 1020 | * even if it's failed |
| 1021 | */ |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 1022 | list_splice(&list, metric_list); |
Jiri Olsa | f6fb096 | 2020-07-19 20:13:16 +0200 | [diff] [blame] | 1023 | expr_ids__exit(&ids); |
Namhyung Kim | 27adafc | 2020-09-15 12:18:16 +0900 | [diff] [blame] | 1024 | return ret; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 1027 | static int metricgroup__add_metric_list(const char *list, bool metric_no_group, |
| 1028 | struct strbuf *events, |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 1029 | struct list_head *metric_list, |
Jiri Olsa | 1381396 | 2020-06-09 12:57:47 -0300 | [diff] [blame] | 1030 | struct pmu_events_map *map) |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1031 | { |
| 1032 | char *llist, *nlist, *p; |
| 1033 | int ret = -EINVAL; |
| 1034 | |
| 1035 | nlist = strdup(list); |
| 1036 | if (!nlist) |
| 1037 | return -ENOMEM; |
| 1038 | llist = nlist; |
Andi Kleen | 411bc31 | 2017-09-14 13:57:35 -0700 | [diff] [blame] | 1039 | |
| 1040 | strbuf_init(events, 100); |
| 1041 | strbuf_addf(events, "%s", ""); |
| 1042 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1043 | while ((p = strsep(&llist, ",")) != NULL) { |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 1044 | ret = metricgroup__add_metric(p, metric_no_group, events, |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 1045 | metric_list, map); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1046 | if (ret == -EINVAL) { |
| 1047 | fprintf(stderr, "Cannot find metric or group `%s'\n", |
| 1048 | p); |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | free(nlist); |
Kan Liang | ab483d8 | 2020-02-24 13:59:23 -0800 | [diff] [blame] | 1053 | |
| 1054 | if (!ret) |
| 1055 | metricgroup___watchdog_constraint_hint(NULL, true); |
| 1056 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1057 | return ret; |
| 1058 | } |
| 1059 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 1060 | static void metric__free_refs(struct metric *metric) |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 1061 | { |
| 1062 | struct metric_ref_node *ref, *tmp; |
| 1063 | |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 1064 | list_for_each_entry_safe(ref, tmp, &metric->metric_refs, list) { |
Jiri Olsa | 83de0b7 | 2020-07-19 20:13:09 +0200 | [diff] [blame] | 1065 | list_del(&ref->list); |
| 1066 | free(ref); |
| 1067 | } |
| 1068 | } |
| 1069 | |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 1070 | static void metricgroup__free_metrics(struct list_head *metric_list) |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1071 | { |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 1072 | struct metric *m, *tmp; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1073 | |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 1074 | list_for_each_entry_safe (m, tmp, metric_list, nd) { |
Jiri Olsa | a0c05b3 | 2020-07-19 20:13:19 +0200 | [diff] [blame] | 1075 | metric__free_refs(m); |
| 1076 | expr__ctx_clear(&m->pctx); |
| 1077 | list_del_init(&m->nd); |
| 1078 | free(m); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
Jiri Olsa | 8b4468a | 2020-06-02 23:47:33 +0200 | [diff] [blame] | 1082 | static int parse_groups(struct evlist *perf_evlist, const char *str, |
| 1083 | bool metric_no_group, |
| 1084 | bool metric_no_merge, |
Jiri Olsa | 68173bd | 2020-06-09 12:50:42 -0300 | [diff] [blame] | 1085 | struct perf_pmu *fake_pmu, |
Jiri Olsa | 1381396 | 2020-06-09 12:57:47 -0300 | [diff] [blame] | 1086 | struct rblist *metric_events, |
| 1087 | struct pmu_events_map *map) |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1088 | { |
| 1089 | struct parse_events_error parse_error; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1090 | struct strbuf extra_events; |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 1091 | LIST_HEAD(metric_list); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1092 | int ret; |
| 1093 | |
| 1094 | if (metric_events->nr_entries == 0) |
| 1095 | metricgroup__rblist_init(metric_events); |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 1096 | ret = metricgroup__add_metric_list(str, metric_no_group, |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 1097 | &extra_events, &metric_list, map); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1098 | if (ret) |
Namhyung Kim | 4f57a1e | 2020-09-15 12:18:09 +0900 | [diff] [blame] | 1099 | goto out; |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1100 | pr_debug("adding %s\n", extra_events.buf); |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 1101 | bzero(&parse_error, sizeof(parse_error)); |
Jiri Olsa | 68173bd | 2020-06-09 12:50:42 -0300 | [diff] [blame] | 1102 | ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1103 | if (ret) { |
Andi Kleen | 333b566 | 2017-09-13 14:50:06 -0700 | [diff] [blame] | 1104 | parse_events_print_error(&parse_error, extra_events.buf); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1105 | goto out; |
| 1106 | } |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 1107 | ret = metricgroup__setup_events(&metric_list, metric_no_merge, |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 1108 | perf_evlist, metric_events); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1109 | out: |
Jiri Olsa | 119e521 | 2020-07-19 20:13:20 +0200 | [diff] [blame] | 1110 | metricgroup__free_metrics(&metric_list); |
Namhyung Kim | 4f57a1e | 2020-09-15 12:18:09 +0900 | [diff] [blame] | 1111 | strbuf_release(&extra_events); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1112 | return ret; |
| 1113 | } |
Thomas Richter | 742d92f | 2018-06-26 09:17:01 +0200 | [diff] [blame] | 1114 | |
Jiri Olsa | 8b4468a | 2020-06-02 23:47:33 +0200 | [diff] [blame] | 1115 | int metricgroup__parse_groups(const struct option *opt, |
| 1116 | const char *str, |
| 1117 | bool metric_no_group, |
| 1118 | bool metric_no_merge, |
| 1119 | struct rblist *metric_events) |
| 1120 | { |
| 1121 | struct evlist *perf_evlist = *(struct evlist **)opt->value; |
Jiri Olsa | 1381396 | 2020-06-09 12:57:47 -0300 | [diff] [blame] | 1122 | struct pmu_events_map *map = perf_pmu__find_map(NULL); |
| 1123 | |
| 1124 | if (!map) |
| 1125 | return 0; |
Jiri Olsa | 8b4468a | 2020-06-02 23:47:33 +0200 | [diff] [blame] | 1126 | |
| 1127 | return parse_groups(perf_evlist, str, metric_no_group, |
Jiri Olsa | 1381396 | 2020-06-09 12:57:47 -0300 | [diff] [blame] | 1128 | metric_no_merge, NULL, metric_events, map); |
Jiri Olsa | 8b4468a | 2020-06-02 23:47:33 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
Jiri Olsa | f78ac00 | 2020-06-02 23:47:36 +0200 | [diff] [blame] | 1131 | int metricgroup__parse_groups_test(struct evlist *evlist, |
| 1132 | struct pmu_events_map *map, |
| 1133 | const char *str, |
| 1134 | bool metric_no_group, |
| 1135 | bool metric_no_merge, |
| 1136 | struct rblist *metric_events) |
| 1137 | { |
| 1138 | return parse_groups(evlist, str, metric_no_group, |
| 1139 | metric_no_merge, &perf_pmu__fake, metric_events, map); |
| 1140 | } |
| 1141 | |
Thomas Richter | 742d92f | 2018-06-26 09:17:01 +0200 | [diff] [blame] | 1142 | bool metricgroup__has_metric(const char *metric) |
| 1143 | { |
| 1144 | struct pmu_events_map *map = perf_pmu__find_map(NULL); |
| 1145 | struct pmu_event *pe; |
| 1146 | int i; |
| 1147 | |
| 1148 | if (!map) |
| 1149 | return false; |
| 1150 | |
| 1151 | for (i = 0; ; i++) { |
| 1152 | pe = &map->table[i]; |
| 1153 | |
| 1154 | if (!pe->name && !pe->metric_group && !pe->metric_name) |
| 1155 | break; |
| 1156 | if (!pe->metric_expr) |
| 1157 | continue; |
| 1158 | if (match_metric(pe->metric_name, metric)) |
| 1159 | return true; |
| 1160 | } |
| 1161 | return false; |
| 1162 | } |
Namhyung Kim | b214ba8 | 2020-09-24 21:44:53 +0900 | [diff] [blame] | 1163 | |
| 1164 | int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp, |
| 1165 | struct rblist *new_metric_events, |
| 1166 | struct rblist *old_metric_events) |
| 1167 | { |
| 1168 | unsigned i; |
| 1169 | |
| 1170 | for (i = 0; i < rblist__nr_entries(old_metric_events); i++) { |
| 1171 | struct rb_node *nd; |
| 1172 | struct metric_event *old_me, *new_me; |
| 1173 | struct metric_expr *old_expr, *new_expr; |
| 1174 | struct evsel *evsel; |
| 1175 | size_t alloc_size; |
| 1176 | int idx, nr; |
| 1177 | |
| 1178 | nd = rblist__entry(old_metric_events, i); |
| 1179 | old_me = container_of(nd, struct metric_event, nd); |
| 1180 | |
| 1181 | evsel = evlist__find_evsel(evlist, old_me->evsel->idx); |
| 1182 | if (!evsel) |
| 1183 | return -EINVAL; |
| 1184 | new_me = metricgroup__lookup(new_metric_events, evsel, true); |
| 1185 | if (!new_me) |
| 1186 | return -ENOMEM; |
| 1187 | |
| 1188 | pr_debug("copying metric event for cgroup '%s': %s (idx=%d)\n", |
| 1189 | cgrp ? cgrp->name : "root", evsel->name, evsel->idx); |
| 1190 | |
| 1191 | list_for_each_entry(old_expr, &old_me->head, nd) { |
| 1192 | new_expr = malloc(sizeof(*new_expr)); |
| 1193 | if (!new_expr) |
| 1194 | return -ENOMEM; |
| 1195 | |
| 1196 | new_expr->metric_expr = old_expr->metric_expr; |
| 1197 | new_expr->metric_name = old_expr->metric_name; |
| 1198 | new_expr->metric_unit = old_expr->metric_unit; |
| 1199 | new_expr->runtime = old_expr->runtime; |
| 1200 | |
| 1201 | if (old_expr->metric_refs) { |
| 1202 | /* calculate number of metric_events */ |
| 1203 | for (nr = 0; old_expr->metric_refs[nr].metric_name; nr++) |
| 1204 | continue; |
| 1205 | alloc_size = sizeof(*new_expr->metric_refs); |
| 1206 | new_expr->metric_refs = calloc(nr + 1, alloc_size); |
| 1207 | if (!new_expr->metric_refs) { |
| 1208 | free(new_expr); |
| 1209 | return -ENOMEM; |
| 1210 | } |
| 1211 | |
| 1212 | memcpy(new_expr->metric_refs, old_expr->metric_refs, |
| 1213 | nr * alloc_size); |
| 1214 | } else { |
| 1215 | new_expr->metric_refs = NULL; |
| 1216 | } |
| 1217 | |
| 1218 | /* calculate number of metric_events */ |
| 1219 | for (nr = 0; old_expr->metric_events[nr]; nr++) |
| 1220 | continue; |
| 1221 | alloc_size = sizeof(*new_expr->metric_events); |
| 1222 | new_expr->metric_events = calloc(nr + 1, alloc_size); |
| 1223 | if (!new_expr->metric_events) { |
| 1224 | free(new_expr->metric_refs); |
| 1225 | free(new_expr); |
| 1226 | return -ENOMEM; |
| 1227 | } |
| 1228 | |
| 1229 | /* copy evsel in the same position */ |
| 1230 | for (idx = 0; idx < nr; idx++) { |
| 1231 | evsel = old_expr->metric_events[idx]; |
| 1232 | evsel = evlist__find_evsel(evlist, evsel->idx); |
| 1233 | if (evsel == NULL) { |
| 1234 | free(new_expr->metric_events); |
| 1235 | free(new_expr->metric_refs); |
| 1236 | free(new_expr); |
| 1237 | return -EINVAL; |
| 1238 | } |
| 1239 | new_expr->metric_events[idx] = evsel; |
| 1240 | } |
| 1241 | |
| 1242 | list_add(&new_expr->nd, &new_me->head); |
| 1243 | } |
| 1244 | } |
| 1245 | return 0; |
| 1246 | } |