aboutsummaryrefslogtreecommitdiff
path: root/tools/cache-info.c
blob: cbe9e6eeabd50bb30797e09c3f57647d5be1f5b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <inttypes.h>

#include <cpuinfo.h>


void report_cache(
	uint32_t count, const struct cpuinfo_cache cache[restrict static 1],
	uint32_t level, const char* nonunified_type)
{
	const char* type = (cache->flags & CPUINFO_CACHE_UNIFIED) ? "unified" : nonunified_type;
	printf("L%"PRIu32" %s cache: ", level, type);

	uint32_t size = cache->size;
	const char* units = "bytes";
	if (size % UINT32_C(1048576) == 0) {
		size /= UINT32_C(1048576);
		units = "MB";
	} else if (size % UINT32_C(1024) == 0) {
		size /= UINT32_C(1024);
		units = "KB";
	}
	if (count != 1) {
		printf("%"PRIu32" x ", count);
	}
	if (level == 1) {
		printf("%"PRIu32" %s, ", size, units);
	} else {
		printf("%"PRIu32" %s (%s), ", size, units, (cache->flags & CPUINFO_CACHE_INCLUSIVE) ? "inclusive" : "exclusive");
	}

	if (cache->associativity * cache->line_size == cache->size) {
		printf("fully associative");
	} else {
		printf("%"PRIu32"-way set associative", cache->associativity);
	}
	if (cache->sets != 0) {
		printf(" (%"PRIu32" sets", cache->sets);
		if (cache->partitions != 1) {
			printf(", %"PRIu32" partitions", cache->partitions);
		}
		if (cache->flags & CPUINFO_CACHE_COMPLEX_INDEXING) {
			printf(", complex indexing), ");
		} else {
			printf("), ");
		}
	}

	printf("%"PRIu32" byte lines", cache->line_size);
	if (cache->processor_count != 0) {
		printf(", shared by %"PRIu32" processors\n", cache->processor_count);
	} else {
		printf("\n");
	}
}

int main(int argc, char** argv) {
	cpuinfo_initialize();
	if (cpuinfo_get_l1i_caches_count() != 0 && (cpuinfo_get_l1i_cache(0)->flags & CPUINFO_CACHE_UNIFIED) == 0) {
		report_cache(cpuinfo_get_l1i_caches_count(), cpuinfo_get_l1i_cache(0), 1, "instruction");
	}
	if (cpuinfo_get_l1d_caches_count() != 0) {
		report_cache(cpuinfo_get_l1d_caches_count(), cpuinfo_get_l1d_cache(0), 1, "data");
	}
	if (cpuinfo_get_l2_caches_count() != 0) {
		report_cache(cpuinfo_get_l2_caches_count(), cpuinfo_get_l2_cache(0), 2, "data");
	}
	if (cpuinfo_get_l3_caches_count() != 0) {
		report_cache(cpuinfo_get_l3_caches_count(), cpuinfo_get_l3_cache(0), 3, "data");
	}
	if (cpuinfo_get_l4_caches_count() != 0) {
		report_cache(cpuinfo_get_l4_caches_count(), cpuinfo_get_l4_cache(0), 4, "data");
	}
}