xref: /linux/tools/perf/util/cpumap.c (revision 1bbeaf83dd7b5e3628b98bec66ff8fe2646e14aa)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2cd0cfad7SBorislav Petkov #include <api/fs/fs.h>
3a12b51c4SPaul Mackerras #include "cpumap.h"
45e51b0bbSArnaldo Carvalho de Melo #include "debug.h"
55e51b0bbSArnaldo Carvalho de Melo #include "event.h"
6a12b51c4SPaul Mackerras #include <assert.h>
776b31a29SArnaldo Carvalho de Melo #include <dirent.h>
8a12b51c4SPaul Mackerras #include <stdio.h>
986ee6e18SStephane Eranian #include <stdlib.h>
10f77b57adSJiri Olsa #include <linux/bitmap.h>
11f30a79b0SJiri Olsa #include "asm/bug.h"
12a12b51c4SPaul Mackerras 
133052ba56SArnaldo Carvalho de Melo #include <linux/ctype.h>
147f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
15c4d9d95fSIan Rogers #include <internal/cpumap.h>
163d689ed6SArnaldo Carvalho de Melo 
176d18804bSIan Rogers static struct perf_cpu max_cpu_num;
186d18804bSIan Rogers static struct perf_cpu max_present_cpu_num;
195ac76283SArnaldo Carvalho de Melo static int max_node_num;
20194a3a20SIan Rogers /**
21194a3a20SIan Rogers  * The numa node X as read from /sys/devices/system/node/nodeX indexed by the
22194a3a20SIan Rogers  * CPU number.
23194a3a20SIan Rogers  */
245ac76283SArnaldo Carvalho de Melo static int *cpunode_map;
255ac76283SArnaldo Carvalho de Melo 
perf_record_cpu_map_data__test_bit(int i,const struct perf_record_cpu_map_data * data)26b2f10cd4SIan Rogers bool perf_record_cpu_map_data__test_bit(int i,
27b2f10cd4SIan Rogers 					const struct perf_record_cpu_map_data *data)
28b2f10cd4SIan Rogers {
29b2f10cd4SIan Rogers 	int bit_word32 = i / 32;
30b2f10cd4SIan Rogers 	__u32 bit_mask32 = 1U << (i & 31);
31b2f10cd4SIan Rogers 	int bit_word64 = i / 64;
32b2f10cd4SIan Rogers 	__u64 bit_mask64 = ((__u64)1) << (i & 63);
33b2f10cd4SIan Rogers 
34b2f10cd4SIan Rogers 	return (data->mask32_data.long_size == 4)
35b2f10cd4SIan Rogers 		? (bit_word32 < data->mask32_data.nr) &&
36b2f10cd4SIan Rogers 		(data->mask32_data.mask[bit_word32] & bit_mask32) != 0
37b2f10cd4SIan Rogers 		: (bit_word64 < data->mask64_data.nr) &&
38b2f10cd4SIan Rogers 		(data->mask64_data.mask[bit_word64] & bit_mask64) != 0;
39b2f10cd4SIan Rogers }
40b2f10cd4SIan Rogers 
41b2f10cd4SIan Rogers /* Read ith mask value from data into the given 64-bit sized bitmap */
perf_record_cpu_map_data__read_one_mask(const struct perf_record_cpu_map_data * data,int i,unsigned long * bitmap)42b2f10cd4SIan Rogers static void perf_record_cpu_map_data__read_one_mask(const struct perf_record_cpu_map_data *data,
43b2f10cd4SIan Rogers 						    int i, unsigned long *bitmap)
44b2f10cd4SIan Rogers {
45b2f10cd4SIan Rogers #if __SIZEOF_LONG__ == 8
46b2f10cd4SIan Rogers 	if (data->mask32_data.long_size == 4)
47b2f10cd4SIan Rogers 		bitmap[0] = data->mask32_data.mask[i];
48b2f10cd4SIan Rogers 	else
49b2f10cd4SIan Rogers 		bitmap[0] = data->mask64_data.mask[i];
50b2f10cd4SIan Rogers #else
51b2f10cd4SIan Rogers 	if (data->mask32_data.long_size == 4) {
52b2f10cd4SIan Rogers 		bitmap[0] = data->mask32_data.mask[i];
53b2f10cd4SIan Rogers 		bitmap[1] = 0;
54b2f10cd4SIan Rogers 	} else {
55b2f10cd4SIan Rogers #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
56b2f10cd4SIan Rogers 		bitmap[0] = (unsigned long)(data->mask64_data.mask[i] >> 32);
57b2f10cd4SIan Rogers 		bitmap[1] = (unsigned long)data->mask64_data.mask[i];
58b2f10cd4SIan Rogers #else
59b2f10cd4SIan Rogers 		bitmap[0] = (unsigned long)data->mask64_data.mask[i];
60b2f10cd4SIan Rogers 		bitmap[1] = (unsigned long)(data->mask64_data.mask[i] >> 32);
61b2f10cd4SIan Rogers #endif
62b2f10cd4SIan Rogers 	}
63b2f10cd4SIan Rogers #endif
64b2f10cd4SIan Rogers }
cpu_map__from_entries(const struct perf_record_cpu_map_data * data)65b2f10cd4SIan Rogers static struct perf_cpu_map *cpu_map__from_entries(const struct perf_record_cpu_map_data *data)
66f77b57adSJiri Olsa {
67f854839bSJiri Olsa 	struct perf_cpu_map *map;
68f77b57adSJiri Olsa 
69b2f10cd4SIan Rogers 	map = perf_cpu_map__empty_new(data->cpus_data.nr);
70f77b57adSJiri Olsa 	if (!map)
71f77b57adSJiri Olsa 		return NULL;
72f77b57adSJiri Olsa 
73b2f10cd4SIan Rogers 	for (unsigned int i = 0; i < data->cpus_data.nr; i++) {
7415d2b995SJiri Olsa 		/*
7515d2b995SJiri Olsa 		 * Special treatment for -1, which is not real cpu number,
7615d2b995SJiri Olsa 		 * and we need to use (int) -1 to initialize map[i],
7715d2b995SJiri Olsa 		 * otherwise it would become 65535.
7815d2b995SJiri Olsa 		 */
79b2f10cd4SIan Rogers 		if (data->cpus_data.cpu[i] == (u16) -1) {
80da885a0eSIan Rogers 			RC_CHK_ACCESS(map)->map[i].cpu = -1;
8115d2b995SJiri Olsa 		} else if (data->cpus_data.cpu[i] < INT16_MAX) {
82da885a0eSIan Rogers 			RC_CHK_ACCESS(map)->map[i].cpu = (int16_t) data->cpus_data.cpu[i];
83f77b57adSJiri Olsa 		} else {
8415d2b995SJiri Olsa 			pr_err("Invalid cpumap entry %u\n", data->cpus_data.cpu[i]);
85f77b57adSJiri Olsa 			perf_cpu_map__put(map);
86f77b57adSJiri Olsa 			return NULL;
87f77b57adSJiri Olsa 		}
88f77b57adSJiri Olsa 	}
89b2f10cd4SIan Rogers 
90f77b57adSJiri Olsa 	return map;
91b2f10cd4SIan Rogers }
92b2f10cd4SIan Rogers 
cpu_map__from_mask(const struct perf_record_cpu_map_data * data)93f854839bSJiri Olsa static struct perf_cpu_map *cpu_map__from_mask(const struct perf_record_cpu_map_data *data)
94f77b57adSJiri Olsa {
95b2f10cd4SIan Rogers 	DECLARE_BITMAP(local_copy, 64);
96b2f10cd4SIan Rogers 	int weight = 0, mask_nr = data->mask32_data.nr;
97b2f10cd4SIan Rogers 	struct perf_cpu_map *map;
98b2f10cd4SIan Rogers 
99f77b57adSJiri Olsa 	for (int i = 0; i < mask_nr; i++) {
100b2f10cd4SIan Rogers 		perf_record_cpu_map_data__read_one_mask(data, i, local_copy);
101b2f10cd4SIan Rogers 		weight += bitmap_weight(local_copy, 64);
102b2f10cd4SIan Rogers 	}
103f77b57adSJiri Olsa 
104b2f10cd4SIan Rogers 	map = perf_cpu_map__empty_new(weight);
105b2f10cd4SIan Rogers 	if (!map)
106b2f10cd4SIan Rogers 		return NULL;
107b2f10cd4SIan Rogers 
108b2f10cd4SIan Rogers 	for (int i = 0, j = 0; i < mask_nr; i++) {
109b2f10cd4SIan Rogers 		int cpus_per_i = (i * data->mask32_data.long_size  * BITS_PER_BYTE);
110da885a0eSIan Rogers 		int cpu;
111f77b57adSJiri Olsa 
112f77b57adSJiri Olsa 		perf_record_cpu_map_data__read_one_mask(data, i, local_copy);
113f77b57adSJiri Olsa 		for_each_set_bit(cpu, local_copy, 64) {
114f77b57adSJiri Olsa 			if (cpu + cpus_per_i < INT16_MAX) {
115f77b57adSJiri Olsa 				RC_CHK_ACCESS(map)->map[j++].cpu = cpu + cpus_per_i;
116c7202d20SIan Rogers 			} else {
117c7202d20SIan Rogers 				pr_err("Invalid cpumap entry %d\n", cpu + cpus_per_i);
118c7202d20SIan Rogers 				perf_cpu_map__put(map);
119c7202d20SIan Rogers 				return NULL;
120c7202d20SIan Rogers 			}
121c7202d20SIan Rogers 		}
122c7202d20SIan Rogers 	}
123c7202d20SIan Rogers 	return map;
124c7202d20SIan Rogers 
125c7202d20SIan Rogers }
126c7202d20SIan Rogers 
cpu_map__from_range(const struct perf_record_cpu_map_data * data)127da885a0eSIan Rogers static struct perf_cpu_map *cpu_map__from_range(const struct perf_record_cpu_map_data *data)
128c7202d20SIan Rogers {
129c7202d20SIan Rogers 	struct perf_cpu_map *map;
130c7202d20SIan Rogers 	unsigned int i = 0;
131da885a0eSIan Rogers 
132c7202d20SIan Rogers 	map = perf_cpu_map__empty_new(data->range_cpu_data.end_cpu -
133c7202d20SIan Rogers 				data->range_cpu_data.start_cpu + 1 + data->range_cpu_data.any_cpu);
134c7202d20SIan Rogers 	if (!map)
135c7202d20SIan Rogers 		return NULL;
136b2f10cd4SIan Rogers 
137f77b57adSJiri Olsa 	if (data->range_cpu_data.any_cpu)
138c7202d20SIan Rogers 		RC_CHK_ACCESS(map)->map[i++].cpu = -1;
139c7202d20SIan Rogers 
140b2f10cd4SIan Rogers 	for (int cpu = data->range_cpu_data.start_cpu; cpu <= data->range_cpu_data.end_cpu;
141c7202d20SIan Rogers 	     i++, cpu++) {
142b2f10cd4SIan Rogers 		if (cpu < INT16_MAX) {
143c7202d20SIan Rogers 			RC_CHK_ACCESS(map)->map[i].cpu = cpu;
144c7202d20SIan Rogers 		} else {
145c7202d20SIan Rogers 			pr_err("Invalid cpumap entry %d\n", cpu);
146c7202d20SIan Rogers 			perf_cpu_map__put(map);
147c7202d20SIan Rogers 			return NULL;
148c7202d20SIan Rogers 		}
149f77b57adSJiri Olsa 	}
150f77b57adSJiri Olsa 
151f854839bSJiri Olsa 	return map;
1529ae7d335SArnaldo Carvalho de Melo }
153a24020e6SJiri Olsa 
cpu_map__new_data(const struct perf_record_cpu_map_data * data)154a24020e6SJiri Olsa struct perf_cpu_map *cpu_map__new_data(const struct perf_record_cpu_map_data *data)
1559ae7d335SArnaldo Carvalho de Melo {
156a24020e6SJiri Olsa 	switch (data->type) {
157a24020e6SJiri Olsa 	case PERF_CPU_MAP__CPUS:
158a24020e6SJiri Olsa 		return cpu_map__from_entries(data);
1599ae7d335SArnaldo Carvalho de Melo 	case PERF_CPU_MAP__MASK:
1609ae7d335SArnaldo Carvalho de Melo 		return cpu_map__from_mask(data);
161315c0a1fSJiri Olsa 	case PERF_CPU_MAP__RANGE_CPUS:
1622322f573SJiri Olsa 		return cpu_map__from_range(data);
1631f94479eSIan Rogers 	default:
1642322f573SJiri Olsa 		pr_err("cpu_map__new_data unknown type %d\n", data->type);
1652322f573SJiri Olsa 		return NULL;
166d57fd492SArnaldo Carvalho de Melo 	}
167da885a0eSIan Rogers }
1682322f573SJiri Olsa 
cpu_map__fprintf(struct perf_cpu_map * map,FILE * fp)1692322f573SJiri Olsa size_t cpu_map__fprintf(struct perf_cpu_map *map, FILE *fp)
1702322f573SJiri Olsa {
1712322f573SJiri Olsa #define BUFSIZE 1024
1722322f573SJiri Olsa 	char buf[BUFSIZE];
173cea6575fSJames Clark 
174cea6575fSJames Clark 	cpu_map__snprint(map, buf, sizeof(buf));
175ff523295SJames Clark 	return fprintf(fp, "%s\n", buf);
176cea6575fSJames Clark #undef BUFSIZE
177cea6575fSJames Clark }
178cea6575fSJames Clark 
perf_cpu_map__empty_new(int nr)179cea6575fSJames Clark struct perf_cpu_map *perf_cpu_map__empty_new(int nr)
180cea6575fSJames Clark {
181cea6575fSJames Clark 	struct perf_cpu_map *cpus = perf_cpu_map__alloc(nr);
18251b826faSIan Rogers 
183cea6575fSJames Clark 	if (cpus != NULL) {
184cea6575fSJames Clark 		for (int i = 0; i < nr; i++)
185cea6575fSJames Clark 			RC_CHK_ACCESS(cpus)->map[i].cpu = -1;
186cea6575fSJames Clark 	}
187cea6575fSJames Clark 
188cea6575fSJames Clark 	return cpus;
189cea6575fSJames Clark }
1905d8cf721SArnaldo Carvalho de Melo 
cpu_aggr_map__empty_new(int nr)1915ac59a8aSStephane Eranian struct cpu_aggr_map *cpu_aggr_map__empty_new(int nr)
1925ac59a8aSStephane Eranian {
1935ac59a8aSStephane Eranian 	struct cpu_aggr_map *cpus = malloc(sizeof(*cpus) + sizeof(struct aggr_cpu_id) * nr);
19486ee6e18SStephane Eranian 
1955d8cf721SArnaldo Carvalho de Melo 	if (cpus != NULL) {
1965ac59a8aSStephane Eranian 		int i;
1975d8cf721SArnaldo Carvalho de Melo 
1985d8cf721SArnaldo Carvalho de Melo 		cpus->nr = nr;
199193b6bd3SKan Liang 		for (i = 0; i < nr; i++)
2006d18804bSIan Rogers 			cpus->map[i] = aggr_cpu_id__empty();
2015d8cf721SArnaldo Carvalho de Melo 	}
2026d18804bSIan Rogers 
2035d8cf721SArnaldo Carvalho de Melo 	return cpus;
204193b6bd3SKan Liang }
205193b6bd3SKan Liang 
cpu__get_topology_int(int cpu,const char * name,int * value)2066d18804bSIan Rogers static int cpu__get_topology_int(int cpu, const char *name, int *value)
207193b6bd3SKan Liang {
20851b826faSIan Rogers 	char path[PATH_MAX];
209193b6bd3SKan Liang 
2104e90e5ccSIan Rogers 	snprintf(path, PATH_MAX,
2112760f5a1SJames Clark 		"devices/system/cpu/cpu%d/topology/%s", cpu, name);
2125ac59a8aSStephane Eranian 
2135ac59a8aSStephane Eranian 	return sysfs__read_int(path, value);
2145f50e15cSIan Rogers }
2155ac59a8aSStephane Eranian 
cpu__get_socket_id(struct perf_cpu cpu)2162760f5a1SJames Clark int cpu__get_socket_id(struct perf_cpu cpu)
2172760f5a1SJames Clark {
2182760f5a1SJames Clark 	int value, ret = cpu__get_topology_int(cpu.cpu, "physical_package_id", &value);
2198d4852b4SJames Clark 	return ret ?: value;
220fcd83a35SJames Clark }
221ba2ee166SJames Clark 
aggr_cpu_id__socket(struct perf_cpu cpu,void * data __maybe_unused)2221a270cb6SJames Clark struct aggr_cpu_id aggr_cpu_id__socket(struct perf_cpu cpu, void *data __maybe_unused)
223b9933817SJames Clark {
224ba2ee166SJames Clark 	struct aggr_cpu_id id = aggr_cpu_id__empty();
225*cbc917a1SYicong Yang 
226*cbc917a1SYicong Yang 	id.socket = cpu__get_socket_id(cpu);
227995ed074SK Prateek Nayak 	return id;
228995ed074SK Prateek Nayak }
229995ed074SK Prateek Nayak 
aggr_cpu_id__cmp(const void * a_pointer,const void * b_pointer)230995ed074SK Prateek Nayak static int aggr_cpu_id__cmp(const void *a_pointer, const void *b_pointer)
2318d4852b4SJames Clark {
232b9933817SJames Clark 	struct aggr_cpu_id *a = (struct aggr_cpu_id *)a_pointer;
2338d4852b4SJames Clark 	struct aggr_cpu_id *b = (struct aggr_cpu_id *)b_pointer;
234fa2edc07SNamhyung Kim 
23586ee6e18SStephane Eranian 	if (a->node != b->node)
23686ee6e18SStephane Eranian 		return a->node - b->node;
2375f50e15cSIan Rogers 	else if (a->socket != b->socket)
2385f50e15cSIan Rogers 		return a->socket - b->socket;
239505ac48bSNamhyung Kim 	else if (a->die != b->die)
24086ee6e18SStephane Eranian 		return a->die - b->die;
2416d18804bSIan Rogers 	else if (a->cluster != b->cluster)
2426d18804bSIan Rogers 		return a->cluster - b->cluster;
2437bb1d048SIan Rogers 	else if (a->cache_lvl != b->cache_lvl)
2445ac59a8aSStephane Eranian 		return a->cache_lvl - b->cache_lvl;
24586ee6e18SStephane Eranian 	else if (a->cache != b->cache)
2465f50e15cSIan Rogers 		return a->cache - b->cache;
2475ac59a8aSStephane Eranian 	else if (a->core != b->core)
24891585846SJames Clark 		return a->core - b->core;
24991585846SJames Clark 	else
25091585846SJames Clark 		return a->thread_idx - b->thread_idx;
2515f50e15cSIan Rogers }
2525f50e15cSIan Rogers 
cpu_aggr_map__new(const struct perf_cpu_map * cpus,aggr_cpu_id_get_t get_id,void * data,bool needs_sort)2535f50e15cSIan Rogers struct cpu_aggr_map *cpu_aggr_map__new(const struct perf_cpu_map *cpus,
2545f50e15cSIan Rogers 				       aggr_cpu_id_get_t get_id,
2555f50e15cSIan Rogers 				       void *data, bool needs_sort)
2565f50e15cSIan Rogers {
2575f50e15cSIan Rogers 	int idx;
2585ac59a8aSStephane Eranian 	struct perf_cpu cpu;
2595ac59a8aSStephane Eranian 	struct cpu_aggr_map *c = cpu_aggr_map__empty_new(perf_cpu_map__nr(cpus));
2605f50e15cSIan Rogers 
2615f50e15cSIan Rogers 	if (!c)
2625f50e15cSIan Rogers 		return NULL;
26386ee6e18SStephane Eranian 
2645ac59a8aSStephane Eranian 	/* Reset size as it may only be partially filled */
2655ac59a8aSStephane Eranian 	c->nr = 0;
266bd26bddfSIan Rogers 
2677bb1d048SIan Rogers 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
268bd26bddfSIan Rogers 		bool duplicate = false;
269bd26bddfSIan Rogers 		struct aggr_cpu_id cpu_id = get_id(cpu, data);
270bd26bddfSIan Rogers 
27186ee6e18SStephane Eranian 		for (int j = 0; j < c->nr; j++) {
272bd26bddfSIan Rogers 			if (aggr_cpu_id__equal(&cpu_id, &c->map[j])) {
273bd26bddfSIan Rogers 				duplicate = true;
274bd26bddfSIan Rogers 				break;
275505ac48bSNamhyung Kim 			}
2765f50e15cSIan Rogers 		}
277505ac48bSNamhyung Kim 		if (!duplicate) {
2785f50e15cSIan Rogers 			c->map[c->nr] = cpu_id;
2795f50e15cSIan Rogers 			c->nr++;
2805f50e15cSIan Rogers 		}
2815f50e15cSIan Rogers 	}
2825ac59a8aSStephane Eranian 	/* Trim. */
28386ee6e18SStephane Eranian 	if (c->nr != perf_cpu_map__nr(cpus)) {
2846d18804bSIan Rogers 		struct cpu_aggr_map *trimmed_c =
285b74d8686SKan Liang 			realloc(c,
2866d18804bSIan Rogers 				sizeof(struct cpu_aggr_map) + sizeof(struct aggr_cpu_id) * c->nr);
287b74d8686SKan Liang 
288b74d8686SKan Liang 		if (trimmed_c)
289b74d8686SKan Liang 			c = trimmed_c;
290b74d8686SKan Liang 	}
2916d18804bSIan Rogers 
292db5742b6SKan Liang 	/* ensure we process id in increasing order */
293ca2c9b76SIan Rogers 	if (needs_sort)
294ca2c9b76SIan Rogers 		qsort(c->map, c->nr, sizeof(struct aggr_cpu_id), aggr_cpu_id__cmp);
295db5742b6SKan Liang 
2964e90e5ccSIan Rogers 	return c;
297db5742b6SKan Liang 
2981a270cb6SJames Clark }
2991a270cb6SJames Clark 
cpu__get_die_id(struct perf_cpu cpu)300db5742b6SKan Liang int cpu__get_die_id(struct perf_cpu cpu)
301db5742b6SKan Liang {
3021a270cb6SJames Clark 	int value, ret = cpu__get_topology_int(cpu.cpu, "die_id", &value);
3031a270cb6SJames Clark 
3041a270cb6SJames Clark 	return ret ?: value;
305db5742b6SKan Liang }
306973aeb3cSIan Rogers 
aggr_cpu_id__die(struct perf_cpu cpu,void * data)30751b826faSIan Rogers struct aggr_cpu_id aggr_cpu_id__die(struct perf_cpu cpu, void *data)
3081a270cb6SJames Clark {
3091a270cb6SJames Clark 	struct aggr_cpu_id id;
310ba2ee166SJames Clark 	int die;
3112760f5a1SJames Clark 
312db5742b6SKan Liang 	die = cpu__get_die_id(cpu);
313db5742b6SKan Liang 	/* There is no die_id on legacy system. */
314*cbc917a1SYicong Yang 	if (die < 0)
315*cbc917a1SYicong Yang 		die = 0;
316*cbc917a1SYicong Yang 
317*cbc917a1SYicong Yang 	/*
318*cbc917a1SYicong Yang 	 * die_id is relative to socket, so start
319*cbc917a1SYicong Yang 	 * with the socket ID and then add die to
320*cbc917a1SYicong Yang 	 * make a unique ID.
321*cbc917a1SYicong Yang 	 */
322*cbc917a1SYicong Yang 	id = aggr_cpu_id__socket(cpu, data);
323*cbc917a1SYicong Yang 	if (aggr_cpu_id__is_empty(&id))
324*cbc917a1SYicong Yang 		return id;
325*cbc917a1SYicong Yang 
326*cbc917a1SYicong Yang 	id.die = die;
327*cbc917a1SYicong Yang 	return id;
328*cbc917a1SYicong Yang }
329*cbc917a1SYicong Yang 
cpu__get_cluster_id(struct perf_cpu cpu)330*cbc917a1SYicong Yang int cpu__get_cluster_id(struct perf_cpu cpu)
331*cbc917a1SYicong Yang {
332*cbc917a1SYicong Yang 	int value, ret = cpu__get_topology_int(cpu.cpu, "cluster_id", &value);
333*cbc917a1SYicong Yang 
334*cbc917a1SYicong Yang 	return ret ?: value;
335*cbc917a1SYicong Yang }
336*cbc917a1SYicong Yang 
aggr_cpu_id__cluster(struct perf_cpu cpu,void * data)337*cbc917a1SYicong Yang struct aggr_cpu_id aggr_cpu_id__cluster(struct perf_cpu cpu, void *data)
3386d18804bSIan Rogers {
33912c08a9fSStephane Eranian 	int cluster = cpu__get_cluster_id(cpu);
3406d18804bSIan Rogers 	struct aggr_cpu_id id;
3415d8cf721SArnaldo Carvalho de Melo 
342193b6bd3SKan Liang 	/* There is no cluster_id on legacy system. */
343193b6bd3SKan Liang 	if (cluster < 0)
3446d18804bSIan Rogers 		cluster = 0;
345193b6bd3SKan Liang 
346ca2c9b76SIan Rogers 	id = aggr_cpu_id__die(cpu, data);
3474e90e5ccSIan Rogers 	if (aggr_cpu_id__is_empty(&id))
348193b6bd3SKan Liang 		return id;
349*cbc917a1SYicong Yang 
350*cbc917a1SYicong Yang 	id.cluster = cluster;
35151b826faSIan Rogers 	return id;
3522760f5a1SJames Clark }
35312c08a9fSStephane Eranian 
cpu__get_core_id(struct perf_cpu cpu)35412c08a9fSStephane Eranian int cpu__get_core_id(struct perf_cpu cpu)
355ba2ee166SJames Clark {
356ba2ee166SJames Clark 	int value, ret = cpu__get_topology_int(cpu.cpu, "core_id", &value);
35712c08a9fSStephane Eranian 	return ret ?: value;
358ca2c9b76SIan Rogers }
3592760f5a1SJames Clark 
aggr_cpu_id__core(struct perf_cpu cpu,void * data)360ca2c9b76SIan Rogers struct aggr_cpu_id aggr_cpu_id__core(struct perf_cpu cpu, void *data)
36112c08a9fSStephane Eranian {
36212c08a9fSStephane Eranian 	struct aggr_cpu_id id;
3636d18804bSIan Rogers 	int core = cpu__get_core_id(cpu);
36434794913SIan Rogers 
36534794913SIan Rogers 	/* aggr_cpu_id__die returns a struct with socket die, and cluster set. */
36634794913SIan Rogers 	id = aggr_cpu_id__cluster(cpu, data);
36734794913SIan Rogers 	if (aggr_cpu_id__is_empty(&id))
36834794913SIan Rogers 		return id;
36934794913SIan Rogers 
37034794913SIan Rogers 	/*
37134794913SIan Rogers 	 * core_id is relative to socket and die, we need a global id.
37234794913SIan Rogers 	 * So we combine the result from cpu_map__get_die with the core id
37334794913SIan Rogers 	 */
37434794913SIan Rogers 	id.core = core;
37534794913SIan Rogers 	return id;
37634794913SIan Rogers 
3776d18804bSIan Rogers }
37886895b48SJiri Olsa 
aggr_cpu_id__cpu(struct perf_cpu cpu,void * data)37951b826faSIan Rogers struct aggr_cpu_id aggr_cpu_id__cpu(struct perf_cpu cpu, void *data)
38086895b48SJiri Olsa {
381194a3a20SIan Rogers 	struct aggr_cpu_id id;
3822760f5a1SJames Clark 
383ca2c9b76SIan Rogers 	/* aggr_cpu_id__core returns a struct with socket, die and core set. */
3842760f5a1SJames Clark 	id = aggr_cpu_id__core(cpu, data);
385375369abSNamhyung Kim 	if (aggr_cpu_id__is_empty(&id))
386375369abSNamhyung Kim 		return id;
387375369abSNamhyung Kim 
388375369abSNamhyung Kim 	id.cpu = cpu;
389375369abSNamhyung Kim 	return id;
390375369abSNamhyung Kim 
391375369abSNamhyung Kim }
392375369abSNamhyung Kim 
aggr_cpu_id__node(struct perf_cpu cpu,void * data __maybe_unused)393375369abSNamhyung Kim struct aggr_cpu_id aggr_cpu_id__node(struct perf_cpu cpu, void *data __maybe_unused)
394375369abSNamhyung Kim {
3957780c25bSDon Zickus 	struct aggr_cpu_id id = aggr_cpu_id__empty();
3967780c25bSDon Zickus 
3977780c25bSDon Zickus 	id.node = cpu__get_node(cpu);
3987780c25bSDon Zickus 	return id;
3997780c25bSDon Zickus }
4007780c25bSDon Zickus 
aggr_cpu_id__global(struct perf_cpu cpu,void * data __maybe_unused)4017780c25bSDon Zickus struct aggr_cpu_id aggr_cpu_id__global(struct perf_cpu cpu, void *data __maybe_unused)
4027780c25bSDon Zickus {
4037780c25bSDon Zickus 	struct aggr_cpu_id id = aggr_cpu_id__empty();
4047780c25bSDon Zickus 
4057780c25bSDon Zickus 	/* it always aggregates to the cpu 0 */
4067780c25bSDon Zickus 	cpu.cpu = 0;
4077780c25bSDon Zickus 	id.cpu = cpu;
4087780c25bSDon Zickus 	return id;
4097780c25bSDon Zickus }
4107780c25bSDon Zickus 
4117780c25bSDon Zickus /* setup simple routines to easily access node numbers given a cpu number */
get_max_num(char * path,int * max)4127780c25bSDon Zickus static int get_max_num(char *path, int *max)
4137780c25bSDon Zickus {
4147780c25bSDon Zickus 	size_t num;
4157780c25bSDon Zickus 	char *buf;
4167780c25bSDon Zickus 	int err = 0;
4177780c25bSDon Zickus 
4187780c25bSDon Zickus 	if (filename__read_str(path, &buf, &num))
4197780c25bSDon Zickus 		return -1;
4207780c25bSDon Zickus 
4217780c25bSDon Zickus 	buf[num] = '\0';
4227780c25bSDon Zickus 
4237780c25bSDon Zickus 	/* start on the right, to find highest node num */
4247780c25bSDon Zickus 	while (--num) {
4257780c25bSDon Zickus 		if ((buf[num] == ',') || (buf[num] == '-')) {
4267780c25bSDon Zickus 			num++;
4277780c25bSDon Zickus 			break;
4287780c25bSDon Zickus 		}
4297780c25bSDon Zickus 	}
4307780c25bSDon Zickus 	if (sscanf(&buf[num], "%d", max) < 1) {
4317780c25bSDon Zickus 		err = -1;
4327780c25bSDon Zickus 		goto out;
4337780c25bSDon Zickus 	}
4347780c25bSDon Zickus 
4356d18804bSIan Rogers 	/* convert from 0-based to 1-based */
4366d18804bSIan Rogers 	(*max)++;
4377780c25bSDon Zickus 
4387780c25bSDon Zickus out:
4397780c25bSDon Zickus 	free(buf);
4407780c25bSDon Zickus 	return err;
4417780c25bSDon Zickus }
4427780c25bSDon Zickus 
443f5b1f4e4SDon Zickus /* Determine highest possible cpu in the system for sparse allocation */
set_max_cpu_num(void)444d74b181aSChristophe JAILLET static void set_max_cpu_num(void)
4457780c25bSDon Zickus {
4467780c25bSDon Zickus 	const char *mnt;
4477780c25bSDon Zickus 	char path[PATH_MAX];
4487780c25bSDon Zickus 	int max, ret = -1;
4496d18804bSIan Rogers 
45092a7e127SJan Stancek 	/* set up default */
45192a7e127SJan Stancek 	max_cpu_num.cpu = 4096;
45292a7e127SJan Stancek 	max_present_cpu_num.cpu = 4096;
45392a7e127SJan Stancek 
45492a7e127SJan Stancek 	mnt = sysfs__mountpoint();
455d74b181aSChristophe JAILLET 	if (!mnt)
45692a7e127SJan Stancek 		goto out;
45792a7e127SJan Stancek 
45892a7e127SJan Stancek 	/* get the highest possible cpu number for a sparse allocation */
45992a7e127SJan Stancek 	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
4606d18804bSIan Rogers 	if (ret >= PATH_MAX) {
4617780c25bSDon Zickus 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
4627780c25bSDon Zickus 		goto out;
4637780c25bSDon Zickus 	}
4646d18804bSIan Rogers 
4657780c25bSDon Zickus 	ret = get_max_num(path, &max);
4667780c25bSDon Zickus 	if (ret)
4677780c25bSDon Zickus 		goto out;
4687780c25bSDon Zickus 
4697780c25bSDon Zickus 	max_cpu_num.cpu = max;
4707780c25bSDon Zickus 
4717780c25bSDon Zickus 	/* get the highest present cpu number for a sparse allocation */
4727780c25bSDon Zickus 	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
4737780c25bSDon Zickus 	if (ret >= PATH_MAX) {
4747780c25bSDon Zickus 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
4757780c25bSDon Zickus 		goto out;
4767780c25bSDon Zickus 	}
4777780c25bSDon Zickus 
4787780c25bSDon Zickus 	ret = get_max_num(path, &max);
4797780c25bSDon Zickus 
4807780c25bSDon Zickus 	if (!ret && max > INT16_MAX) {
4817780c25bSDon Zickus 		pr_err("Read out of bounds max cpus of %d\n", max);
4827780c25bSDon Zickus 		ret = -1;
483d74b181aSChristophe JAILLET 	}
4847780c25bSDon Zickus 	if (!ret)
4857780c25bSDon Zickus 		max_present_cpu_num.cpu = (int16_t)max;
4867780c25bSDon Zickus out:
4877780c25bSDon Zickus 	if (ret)
4887780c25bSDon Zickus 		pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num.cpu);
4897780c25bSDon Zickus }
4907780c25bSDon Zickus 
4917780c25bSDon Zickus /* Determine highest possible node in the system for sparse allocation */
set_max_node_num(void)4927780c25bSDon Zickus static void set_max_node_num(void)
4937780c25bSDon Zickus {
4947780c25bSDon Zickus 	const char *mnt;
4955ac76283SArnaldo Carvalho de Melo 	char path[PATH_MAX];
4965ac76283SArnaldo Carvalho de Melo 	int ret = -1;
4975ac76283SArnaldo Carvalho de Melo 
4985ac76283SArnaldo Carvalho de Melo 	/* set up default */
4995ac76283SArnaldo Carvalho de Melo 	max_node_num = 8;
5005ac76283SArnaldo Carvalho de Melo 
5015ac76283SArnaldo Carvalho de Melo 	mnt = sysfs__mountpoint();
5025ac76283SArnaldo Carvalho de Melo 	if (!mnt)
5036d18804bSIan Rogers 		goto out;
5045ac76283SArnaldo Carvalho de Melo 
5056d18804bSIan Rogers 	/* get the highest possible cpu number for a sparse allocation */
5065ac76283SArnaldo Carvalho de Melo 	ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
5075ac76283SArnaldo Carvalho de Melo 	if (ret >= PATH_MAX) {
5085ac76283SArnaldo Carvalho de Melo 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
5095ac76283SArnaldo Carvalho de Melo 		goto out;
5105ac76283SArnaldo Carvalho de Melo 	}
5116d18804bSIan Rogers 
51292a7e127SJan Stancek 	ret = get_max_num(path, &max_node_num);
5136d18804bSIan Rogers 
51492a7e127SJan Stancek out:
51592a7e127SJan Stancek 	if (ret)
51692a7e127SJan Stancek 		pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
51792a7e127SJan Stancek }
51892a7e127SJan Stancek 
cpu__max_node(void)51992a7e127SJan Stancek int cpu__max_node(void)
5206d18804bSIan Rogers {
5215ac76283SArnaldo Carvalho de Melo 	if (unlikely(!max_node_num))
5225ac76283SArnaldo Carvalho de Melo 		set_max_node_num();
5235ac76283SArnaldo Carvalho de Melo 
5245ac76283SArnaldo Carvalho de Melo 	return max_node_num;
5255ac76283SArnaldo Carvalho de Melo }
5265ac76283SArnaldo Carvalho de Melo 
cpu__max_cpu(void)5276d18804bSIan Rogers struct perf_cpu cpu__max_cpu(void)
5285ac76283SArnaldo Carvalho de Melo {
5295ac76283SArnaldo Carvalho de Melo 	if (unlikely(!max_cpu_num.cpu))
5307780c25bSDon Zickus 		set_max_cpu_num();
5317780c25bSDon Zickus 
5327780c25bSDon Zickus 	return max_cpu_num;
5337780c25bSDon Zickus }
5347780c25bSDon Zickus 
cpu__max_present_cpu(void)5357780c25bSDon Zickus struct perf_cpu cpu__max_present_cpu(void)
5367780c25bSDon Zickus {
5376d18804bSIan Rogers 	if (unlikely(!max_present_cpu_num.cpu))
5387780c25bSDon Zickus 		set_max_cpu_num();
5397780c25bSDon Zickus 
5407780c25bSDon Zickus 	return max_present_cpu_num;
5417780c25bSDon Zickus }
5427780c25bSDon Zickus 
5436d18804bSIan Rogers 
cpu__get_node(struct perf_cpu cpu)5447780c25bSDon Zickus int cpu__get_node(struct perf_cpu cpu)
5457780c25bSDon Zickus {
5467780c25bSDon Zickus 	if (unlikely(cpunode_map == NULL)) {
5477780c25bSDon Zickus 		pr_debug("cpu_map not initialized\n");
5487780c25bSDon Zickus 		return -1;
5497780c25bSDon Zickus 	}
5507780c25bSDon Zickus 
5517780c25bSDon Zickus 	return cpunode_map[cpu.cpu];
5527780c25bSDon Zickus }
5537780c25bSDon Zickus 
init_cpunode_map(void)5547780c25bSDon Zickus static int init_cpunode_map(void)
5557780c25bSDon Zickus {
5567780c25bSDon Zickus 	int i;
5577780c25bSDon Zickus 
5587780c25bSDon Zickus 	set_max_cpu_num();
5597780c25bSDon Zickus 	set_max_node_num();
5607780c25bSDon Zickus 
5617780c25bSDon Zickus 	cpunode_map = calloc(max_cpu_num.cpu, sizeof(int));
5627780c25bSDon Zickus 	if (!cpunode_map) {
5637780c25bSDon Zickus 		pr_err("%s: calloc failed\n", __func__);
5647780c25bSDon Zickus 		return -1;
5657780c25bSDon Zickus 	}
5667780c25bSDon Zickus 
5677780c25bSDon Zickus 	for (i = 0; i < max_cpu_num.cpu; i++)
568d74b181aSChristophe JAILLET 		cpunode_map[i] = -1;
5697780c25bSDon Zickus 
5707780c25bSDon Zickus 	return 0;
5717780c25bSDon Zickus }
5727780c25bSDon Zickus 
cpu__setup_cpunode_map(void)5737780c25bSDon Zickus int cpu__setup_cpunode_map(void)
5747780c25bSDon Zickus {
5757780c25bSDon Zickus 	struct dirent *dent1, *dent2;
5767780c25bSDon Zickus 	DIR *dir1, *dir2;
5777780c25bSDon Zickus 	unsigned int cpu, mem;
5787780c25bSDon Zickus 	char buf[PATH_MAX];
5797780c25bSDon Zickus 	char path[PATH_MAX];
5807780c25bSDon Zickus 	const char *mnt;
5817780c25bSDon Zickus 	int n;
5827780c25bSDon Zickus 
583d74b181aSChristophe JAILLET 	/* initialize globals */
5847780c25bSDon Zickus 	if (init_cpunode_map())
5857780c25bSDon Zickus 		return -1;
5867780c25bSDon Zickus 
5877780c25bSDon Zickus 	mnt = sysfs__mountpoint();
5887780c25bSDon Zickus 	if (!mnt)
5897780c25bSDon Zickus 		return 0;
5907780c25bSDon Zickus 
5917780c25bSDon Zickus 	n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
5927780c25bSDon Zickus 	if (n >= PATH_MAX) {
5937780c25bSDon Zickus 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
5947780c25bSDon Zickus 		return -1;
5957780c25bSDon Zickus 	}
5967780c25bSDon Zickus 
5977780c25bSDon Zickus 	dir1 = opendir(path);
5987780c25bSDon Zickus 	if (!dir1)
5997780c25bSDon Zickus 		return 0;
6007780c25bSDon Zickus 
601e632aa69SJiri Olsa 	/* walk tree and setup map */
602f854839bSJiri Olsa 	while ((dent1 = readdir(dir1)) != NULL) {
603a24020e6SJiri Olsa 		if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
6046d18804bSIan Rogers 			continue;
605a24020e6SJiri Olsa 
606a24020e6SJiri Olsa 		n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
607a24020e6SJiri Olsa 		if (n >= PATH_MAX) {
608a24020e6SJiri Olsa 			pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
609a24020e6SJiri Olsa 			continue;
6107bb1d048SIan Rogers 		}
6116d18804bSIan Rogers 
6127bb1d048SIan Rogers 		dir2 = opendir(buf);
613a24020e6SJiri Olsa 		if (!dir2)
6146d18804bSIan Rogers 			continue;
615491b13c4SIan Rogers 		while ((dent2 = readdir(dir2)) != NULL) {
616a24020e6SJiri Olsa 			if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
617a24020e6SJiri Olsa 				continue;
618a24020e6SJiri Olsa 			cpunode_map[cpu] = mem;
619a24020e6SJiri Olsa 		}
620a24020e6SJiri Olsa 		closedir(dir2);
621a24020e6SJiri Olsa 	}
622491b13c4SIan Rogers 	closedir(dir1);
623a24020e6SJiri Olsa 	return 0;
624491b13c4SIan Rogers }
625a24020e6SJiri Olsa 
cpu_map__snprint(struct perf_cpu_map * map,char * buf,size_t size)626a24020e6SJiri Olsa size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)
627a24020e6SJiri Olsa {
628a24020e6SJiri Olsa 	int i, start = -1;
629a24020e6SJiri Olsa 	bool first = true;
630491b13c4SIan Rogers 	size_t ret = 0;
631a24020e6SJiri Olsa 
632a24020e6SJiri Olsa #define COMMA first ? "" : ","
633a24020e6SJiri Olsa 
634491b13c4SIan Rogers 	for (i = 0; i < perf_cpu_map__nr(map) + 1; i++) {
635a24020e6SJiri Olsa 		struct perf_cpu cpu = { .cpu = INT16_MAX };
636a24020e6SJiri Olsa 		bool last = i == perf_cpu_map__nr(map);
637a24020e6SJiri Olsa 
638a24020e6SJiri Olsa 		if (!last)
639a24020e6SJiri Olsa 			cpu = perf_cpu_map__cpu(map, i);
640a24020e6SJiri Olsa 
641a24020e6SJiri Olsa 		if (start == -1) {
642a24020e6SJiri Olsa 			start = i;
643deb83da1SJiri Olsa 			if (last) {
644a24020e6SJiri Olsa 				ret += snprintf(buf + ret, size - ret,
645a24020e6SJiri Olsa 						"%s%d", COMMA,
6464400ac8aSNamhyung Kim 						perf_cpu_map__cpu(map, i).cpu);
6474400ac8aSNamhyung Kim 			}
6484400ac8aSNamhyung Kim 		} else if (((i - start) != (cpu.cpu - perf_cpu_map__cpu(map, start).cpu)) || last) {
6494400ac8aSNamhyung Kim 			int end = i - 1;
6504400ac8aSNamhyung Kim 
6514400ac8aSNamhyung Kim 			if (start == end) {
6524400ac8aSNamhyung Kim 				ret += snprintf(buf + ret, size - ret,
6534400ac8aSNamhyung Kim 						"%s%d", COMMA,
6544400ac8aSNamhyung Kim 						perf_cpu_map__cpu(map, start).cpu);
6554400ac8aSNamhyung Kim 			} else {
656f854839bSJiri Olsa 				ret += snprintf(buf + ret, size - ret,
6574400ac8aSNamhyung Kim 						"%s%d-%d", COMMA,
6584400ac8aSNamhyung Kim 						perf_cpu_map__cpu(map, start).cpu, perf_cpu_map__cpu(map, end).cpu);
6594400ac8aSNamhyung Kim 			}
6604400ac8aSNamhyung Kim 			first = false;
6617bb1d048SIan Rogers 			start = i;
6624400ac8aSNamhyung Kim 		}
6635f5e25f1SHe Zhe 	}
6645f5e25f1SHe Zhe 
6655f5e25f1SHe Zhe #undef COMMA
6666d18804bSIan Rogers 
6674400ac8aSNamhyung Kim 	pr_debug2("cpumask list: %s\n", buf);
6684400ac8aSNamhyung Kim 	return ret;
6694400ac8aSNamhyung Kim }
6704400ac8aSNamhyung Kim 
hex_char(unsigned char val)6714400ac8aSNamhyung Kim static char hex_char(unsigned char val)
6727bb1d048SIan Rogers {
6736d18804bSIan Rogers 	if (val < 10)
6744400ac8aSNamhyung Kim 		return val + '0';
6754400ac8aSNamhyung Kim 	if (val < 16)
6764400ac8aSNamhyung Kim 		return val - 10 + 'a';
6776d18804bSIan Rogers 	return '?';
6784400ac8aSNamhyung Kim }
6794400ac8aSNamhyung Kim 
cpu_map__snprint_mask(struct perf_cpu_map * map,char * buf,size_t size)6804400ac8aSNamhyung Kim size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size)
6814400ac8aSNamhyung Kim {
6824400ac8aSNamhyung Kim 	int idx;
6834400ac8aSNamhyung Kim 	char *ptr = buf;
6844400ac8aSNamhyung Kim 	unsigned char *bitmap;
6854400ac8aSNamhyung Kim 	struct perf_cpu c, last_cpu = perf_cpu_map__max(map);
6864400ac8aSNamhyung Kim 
6874400ac8aSNamhyung Kim 	if (buf == NULL)
6884400ac8aSNamhyung Kim 		return 0;
6894400ac8aSNamhyung Kim 
6904400ac8aSNamhyung Kim 	bitmap = zalloc(last_cpu.cpu / 8 + 1);
6914400ac8aSNamhyung Kim 	if (bitmap == NULL) {
6924400ac8aSNamhyung Kim 		buf[0] = '\0';
6934400ac8aSNamhyung Kim 		return 0;
6944400ac8aSNamhyung Kim 	}
695f13de660SAlexey Budankov 
696a0c41caeSIan Rogers 	perf_cpu_map__for_each_cpu(c, idx, map)
697f13de660SAlexey Budankov 		bitmap[c.cpu / 8] |= 1 << (c.cpu % 8);
698a0c41caeSIan Rogers 
699f13de660SAlexey Budankov 	for (int cpu = last_cpu.cpu / 4 * 4; cpu >= 0; cpu -= 4) {
700f13de660SAlexey Budankov 		unsigned char bits = bitmap[cpu / 8];
701effe957cSIan Rogers 
702f13de660SAlexey Budankov 		if (cpu % 8)
703f13de660SAlexey Budankov 			bits >>= 4;
704f13de660SAlexey Budankov 		else
705fa265e59SJames Clark 			bits &= 0xf;
7063ac23d19SIan Rogers 
707fa265e59SJames Clark 		*ptr++ = hex_char(bits);
708fa2edc07SNamhyung Kim 		if ((cpu % 32) == 0 && cpu > 0)
7093ac23d19SIan Rogers 			*ptr++ = ',';
7103ac23d19SIan Rogers 	}
7113ac23d19SIan Rogers 	*ptr = '\0';
712*cbc917a1SYicong Yang 	free(bitmap);
713995ed074SK Prateek Nayak 
714995ed074SK Prateek Nayak 	buf[size - 1] = '\0';
71534794913SIan Rogers 	return ptr - buf;
7166d18804bSIan Rogers }
717fa265e59SJames Clark 
cpu_map__online(void)718fa265e59SJames Clark struct perf_cpu_map *cpu_map__online(void) /* thread unsafe */
71951b826faSIan Rogers {
720fa265e59SJames Clark 	static struct perf_cpu_map *online;
721fa2edc07SNamhyung Kim 
72251b826faSIan Rogers 	if (!online)
72351b826faSIan Rogers 		online = perf_cpu_map__new_online_cpus(); /* from /sys/devices/system/cpu/online */
72451b826faSIan Rogers 
725*cbc917a1SYicong Yang 	return perf_cpu_map__get(online);
726995ed074SK Prateek Nayak }
727995ed074SK Prateek Nayak 
aggr_cpu_id__equal(const struct aggr_cpu_id * a,const struct aggr_cpu_id * b)72834794913SIan Rogers bool aggr_cpu_id__equal(const struct aggr_cpu_id *a, const struct aggr_cpu_id *b)
7296d18804bSIan Rogers {
730fa265e59SJames Clark 	return a->thread_idx == b->thread_idx &&
731fa265e59SJames Clark 		a->node == b->node &&
73251b826faSIan Rogers 		a->socket == b->socket &&
733fa265e59SJames Clark 		a->die == b->die &&
734fa265e59SJames Clark 		a->cluster == b->cluster &&
735fa2edc07SNamhyung Kim 		a->cache_lvl == b->cache_lvl &&
7361a270cb6SJames Clark 		a->cache == b->cache &&
737ba2ee166SJames Clark 		a->core == b->core &&
738b9933817SJames Clark 		a->cpu.cpu == b->cpu.cpu;
739*cbc917a1SYicong Yang }
740995ed074SK Prateek Nayak 
aggr_cpu_id__is_empty(const struct aggr_cpu_id * a)741995ed074SK Prateek Nayak bool aggr_cpu_id__is_empty(const struct aggr_cpu_id *a)
74234794913SIan Rogers {
7436d18804bSIan Rogers 	return a->thread_idx == -1 &&
744fa265e59SJames Clark 		a->node == -1 &&
745fa265e59SJames Clark 		a->socket == -1 &&
746fa265e59SJames Clark 		a->die == -1 &&
747 		a->cluster == -1 &&
748 		a->cache_lvl == -1 &&
749 		a->cache == -1 &&
750 		a->core == -1 &&
751 		a->cpu.cpu == -1;
752 }
753 
aggr_cpu_id__empty(void)754 struct aggr_cpu_id aggr_cpu_id__empty(void)
755 {
756 	struct aggr_cpu_id ret = {
757 		.thread_idx = -1,
758 		.node = -1,
759 		.socket = -1,
760 		.die = -1,
761 		.cluster = -1,
762 		.cache_lvl = -1,
763 		.cache = -1,
764 		.core = -1,
765 		.cpu = (struct perf_cpu){ .cpu = -1 },
766 	};
767 	return ret;
768 }
769