xref: /linux/tools/perf/arch/x86/util/evlist.c (revision 2da68a77)
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 "util/event.h"
7 #include "util/pmu-hybrid.h"
8 #include "topdown.h"
9 
10 static int ___evlist__add_default_attrs(struct evlist *evlist,
11 					struct perf_event_attr *attrs,
12 					size_t nr_attrs)
13 {
14 	struct perf_cpu_map *cpus;
15 	struct evsel *evsel, *n;
16 	struct perf_pmu *pmu;
17 	LIST_HEAD(head);
18 	size_t i = 0;
19 
20 	for (i = 0; i < nr_attrs; i++)
21 		event_attr_init(attrs + i);
22 
23 	if (!perf_pmu__has_hybrid())
24 		return evlist__add_attrs(evlist, attrs, nr_attrs);
25 
26 	for (i = 0; i < nr_attrs; i++) {
27 		if (attrs[i].type == PERF_TYPE_SOFTWARE) {
28 			evsel = evsel__new(attrs + i);
29 			if (evsel == NULL)
30 				goto out_delete_partial_list;
31 			list_add_tail(&evsel->core.node, &head);
32 			continue;
33 		}
34 
35 		perf_pmu__for_each_hybrid_pmu(pmu) {
36 			evsel = evsel__new(attrs + i);
37 			if (evsel == NULL)
38 				goto out_delete_partial_list;
39 			evsel->core.attr.config |= (__u64)pmu->type << PERF_PMU_TYPE_SHIFT;
40 			cpus = perf_cpu_map__get(pmu->cpus);
41 			evsel->core.cpus = cpus;
42 			evsel->core.own_cpus = perf_cpu_map__get(cpus);
43 			evsel->pmu_name = strdup(pmu->name);
44 			list_add_tail(&evsel->core.node, &head);
45 		}
46 	}
47 
48 	evlist__splice_list_tail(evlist, &head);
49 
50 	return 0;
51 
52 out_delete_partial_list:
53 	__evlist__for_each_entry_safe(&head, n, evsel)
54 		evsel__delete(evsel);
55 	return -1;
56 }
57 
58 int arch_evlist__add_default_attrs(struct evlist *evlist,
59 				   struct perf_event_attr *attrs,
60 				   size_t nr_attrs)
61 {
62 	if (nr_attrs)
63 		return ___evlist__add_default_attrs(evlist, attrs, nr_attrs);
64 
65 	return topdown_parse_events(evlist);
66 }
67 
68 struct evsel *arch_evlist__leader(struct list_head *list)
69 {
70 	struct evsel *evsel, *first, *slots = NULL;
71 	bool has_topdown = false;
72 
73 	first = list_first_entry(list, struct evsel, core.node);
74 
75 	if (!topdown_sys_has_perf_metrics())
76 		return first;
77 
78 	/* If there is a slots event and a topdown event then the slots event comes first. */
79 	__evlist__for_each_entry(list, evsel) {
80 		if (evsel->pmu_name && !strncmp(evsel->pmu_name, "cpu", 3) && evsel->name) {
81 			if (strcasestr(evsel->name, "slots")) {
82 				slots = evsel;
83 				if (slots == first)
84 					return first;
85 			}
86 			if (strcasestr(evsel->name, "topdown"))
87 				has_topdown = true;
88 			if (slots && has_topdown)
89 				return slots;
90 		}
91 	}
92 	return first;
93 }
94