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
11static 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
56out_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
66int 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
76int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs)
77{
78	if (topdown_sys_has_perf_metrics() &&
79	    (arch_evsel__must_be_in_group(lhs) || arch_evsel__must_be_in_group(rhs))) {
80		/* Ensure the topdown slots comes first. */
81		if (strcasestr(lhs->name, "slots") && !strcasestr(lhs->name, "uops_retired.slots"))
82			return -1;
83		if (strcasestr(rhs->name, "slots") && !strcasestr(rhs->name, "uops_retired.slots"))
84			return 1;
85		/* Followed by topdown events. */
86		if (strcasestr(lhs->name, "topdown") && !strcasestr(rhs->name, "topdown"))
87			return -1;
88		if (!strcasestr(lhs->name, "topdown") && strcasestr(rhs->name, "topdown"))
89			return 1;
90	}
91
92	/* Default ordering by insertion index. */
93	return lhs->core.idx - rhs->core.idx;
94}
95