xref: /linux/tools/perf/arch/x86/util/evlist.c (revision 983034cd0d212b23a63efb48ecc47d55d70ee301)
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 #include "evsel.h"
10 
11 static int ___evlist__add_default_attrs(struct evlist *evlist,
12 					struct perf_event_attr *attrs,
13 					size_t nr_attrs)
14 {
15 	struct perf_cpu_map *cpus;
16 	struct evsel *evsel, *n;
17 	struct perf_pmu *pmu;
18 	LIST_HEAD(head);
19 	size_t i = 0;
20 
21 	for (i = 0; i < nr_attrs; i++)
22 		event_attr_init(attrs + i);
23 
24 	if (!perf_pmu__has_hybrid())
25 		return evlist__add_attrs(evlist, attrs, nr_attrs);
26 
27 	for (i = 0; i < nr_attrs; i++) {
28 		if (attrs[i].type == PERF_TYPE_SOFTWARE) {
29 			evsel = evsel__new(attrs + i);
30 			if (evsel == NULL)
31 				goto out_delete_partial_list;
32 			list_add_tail(&evsel->core.node, &head);
33 			continue;
34 		}
35 
36 		perf_pmu__for_each_hybrid_pmu(pmu) {
37 			evsel = evsel__new(attrs + i);
38 			if (evsel == NULL)
39 				goto out_delete_partial_list;
40 			evsel->core.attr.config |= (__u64)pmu->type << PERF_PMU_TYPE_SHIFT;
41 			cpus = perf_cpu_map__get(pmu->cpus);
42 			evsel->core.cpus = cpus;
43 			evsel->core.own_cpus = perf_cpu_map__get(cpus);
44 			evsel->pmu_name = strdup(pmu->name);
45 			list_add_tail(&evsel->core.node, &head);
46 		}
47 	}
48 
49 	evlist__splice_list_tail(evlist, &head);
50 
51 	return 0;
52 
53 out_delete_partial_list:
54 	__evlist__for_each_entry_safe(&head, n, evsel)
55 		evsel__delete(evsel);
56 	return -1;
57 }
58 
59 int arch_evlist__add_default_attrs(struct evlist *evlist,
60 				   struct perf_event_attr *attrs,
61 				   size_t nr_attrs)
62 {
63 	if (!nr_attrs)
64 		return 0;
65 
66 	return ___evlist__add_default_attrs(evlist, attrs, nr_attrs);
67 }
68 
69 int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs)
70 {
71 	if (topdown_sys_has_perf_metrics() && evsel__sys_has_perf_metrics(lhs)) {
72 		/* Ensure the topdown slots comes first. */
73 		if (strcasestr(lhs->name, "slots"))
74 			return -1;
75 		if (strcasestr(rhs->name, "slots"))
76 			return 1;
77 		/* Followed by topdown events. */
78 		if (strcasestr(lhs->name, "topdown") && !strcasestr(rhs->name, "topdown"))
79 			return -1;
80 		if (!strcasestr(lhs->name, "topdown") && strcasestr(rhs->name, "topdown"))
81 			return 1;
82 	}
83 
84 	/* Default ordering by insertion index. */
85 	return lhs->core.idx - rhs->core.idx;
86 }
87