xref: /openbmc/linux/tools/perf/arch/x86/util/evlist.c (revision 05a57605)
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__num_core_pmus() == 1)
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_core(pmu)) != NULL) {
37 			struct perf_cpu_map *cpus;
38 			struct evsel *evsel;
39 
40 			evsel = evsel__new(attrs + i);
41 			if (evsel == NULL)
42 				goto out_delete_partial_list;
43 			evsel->core.attr.config |= (__u64)pmu->type << PERF_PMU_TYPE_SHIFT;
44 			cpus = perf_cpu_map__get(pmu->cpus);
45 			evsel->core.cpus = cpus;
46 			evsel->core.own_cpus = perf_cpu_map__get(cpus);
47 			evsel->pmu_name = strdup(pmu->name);
48 			list_add_tail(&evsel->core.node, &head);
49 		}
50 	}
51 
52 	evlist__splice_list_tail(evlist, &head);
53 
54 	return 0;
55 
56 out_delete_partial_list:
57 	{
58 		struct evsel *evsel, *n;
59 
60 		__evlist__for_each_entry_safe(&head, n, evsel)
61 			evsel__delete(evsel);
62 	}
63 	return -1;
64 }
65 
66 int arch_evlist__add_default_attrs(struct evlist *evlist,
67 				   struct perf_event_attr *attrs,
68 				   size_t nr_attrs)
69 {
70 	if (!nr_attrs)
71 		return 0;
72 
73 	return ___evlist__add_default_attrs(evlist, attrs, nr_attrs);
74 }
75 
76 int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs)
77 {
78 	if (topdown_sys_has_perf_metrics() && evsel__sys_has_perf_metrics(lhs)) {
79 		/* Ensure the topdown slots comes first. */
80 		if (strcasestr(lhs->name, "slots"))
81 			return -1;
82 		if (strcasestr(rhs->name, "slots"))
83 			return 1;
84 		/* Followed by topdown events. */
85 		if (strcasestr(lhs->name, "topdown") && !strcasestr(rhs->name, "topdown"))
86 			return -1;
87 		if (!strcasestr(lhs->name, "topdown") && strcasestr(rhs->name, "topdown"))
88 			return 1;
89 	}
90 
91 	/* Default ordering by insertion index. */
92 	return lhs->core.idx - rhs->core.idx;
93 }
94