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