1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include "util/pmu.h"
4 #include "util/evlist.h"
5 #include "util/parse-events.h"
6 #include "topdown.h"
7 
8 #define TOPDOWN_L1_EVENTS	"{slots,topdown-retiring,topdown-bad-spec,topdown-fe-bound,topdown-be-bound}"
9 #define TOPDOWN_L2_EVENTS	"{slots,topdown-retiring,topdown-bad-spec,topdown-fe-bound,topdown-be-bound,topdown-heavy-ops,topdown-br-mispredict,topdown-fetch-lat,topdown-mem-bound}"
10 
arch_evlist__add_default_attrs(struct evlist * evlist,struct perf_event_attr * attrs,size_t nr_attrs)11 int arch_evlist__add_default_attrs(struct evlist *evlist,
12 				   struct perf_event_attr *attrs,
13 				   size_t nr_attrs)
14 {
15 	if (nr_attrs)
16 		return __evlist__add_default_attrs(evlist, attrs, nr_attrs);
17 
18 	if (!pmu_have_event("cpu", "slots"))
19 		return 0;
20 
21 	if (pmu_have_event("cpu", "topdown-heavy-ops"))
22 		return parse_events(evlist, TOPDOWN_L2_EVENTS, NULL);
23 	else
24 		return parse_events(evlist, TOPDOWN_L1_EVENTS, NULL);
25 }
26 
arch_evlist__leader(struct list_head * list)27 struct evsel *arch_evlist__leader(struct list_head *list)
28 {
29 	struct evsel *evsel, *first, *slots = NULL;
30 	bool has_topdown = false;
31 
32 	first = list_first_entry(list, struct evsel, core.node);
33 
34 	if (!topdown_sys_has_perf_metrics())
35 		return first;
36 
37 	/* If there is a slots event and a topdown event then the slots event comes first. */
38 	__evlist__for_each_entry(list, evsel) {
39 		if (evsel->pmu_name && !strncmp(evsel->pmu_name, "cpu", 3) && evsel->name) {
40 			if (strcasestr(evsel->name, "slots")) {
41 				slots = evsel;
42 				if (slots == first)
43 					return first;
44 			}
45 			if (strcasestr(evsel->name, "topdown"))
46 				has_topdown = true;
47 			if (slots && has_topdown)
48 				return slots;
49 		}
50 	}
51 	return first;
52 }
53