hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright 2017, Michael Ellerman, IBM Corp.
 */
 
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <link.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
 
#include "utils.h"
 
#ifndef AT_L1I_CACHESIZE
#define AT_L1I_CACHESIZE    40
#define AT_L1I_CACHEGEOMETRY    41
#define AT_L1D_CACHESIZE    42
#define AT_L1D_CACHEGEOMETRY    43
#define AT_L2_CACHESIZE        44
#define AT_L2_CACHEGEOMETRY    45
#define AT_L3_CACHESIZE        46
#define AT_L3_CACHEGEOMETRY    47
#endif
 
static void print_size(const char *label, uint32_t val)
{
   printf("%s cache size: %#10x %10dB %10dK\n", label, val, val, val / 1024);
}
 
static void print_geo(const char *label, uint32_t val)
{
   uint16_t assoc;
 
   printf("%s line size:  %#10x       ", label, val & 0xFFFF);
 
   assoc = val >> 16;
   if (assoc)
       printf("%u-way", assoc);
   else
       printf("fully");
 
   printf(" associative\n");
}
 
static int test_cache_shape()
{
   static char buffer[4096];
   ElfW(auxv_t) *p;
   int found;
 
   FAIL_IF(read_auxv(buffer, sizeof(buffer)));
 
   found = 0;
 
   p = find_auxv_entry(AT_L1I_CACHESIZE, buffer);
   if (p) {
       found++;
       print_size("L1I ", (uint32_t)p->a_un.a_val);
   }
 
   p = find_auxv_entry(AT_L1I_CACHEGEOMETRY, buffer);
   if (p) {
       found++;
       print_geo("L1I ", (uint32_t)p->a_un.a_val);
   }
 
   p = find_auxv_entry(AT_L1D_CACHESIZE, buffer);
   if (p) {
       found++;
       print_size("L1D ", (uint32_t)p->a_un.a_val);
   }
 
   p = find_auxv_entry(AT_L1D_CACHEGEOMETRY, buffer);
   if (p) {
       found++;
       print_geo("L1D ", (uint32_t)p->a_un.a_val);
   }
 
   p = find_auxv_entry(AT_L2_CACHESIZE, buffer);
   if (p) {
       found++;
       print_size("L2  ", (uint32_t)p->a_un.a_val);
   }
 
   p = find_auxv_entry(AT_L2_CACHEGEOMETRY, buffer);
   if (p) {
       found++;
       print_geo("L2  ", (uint32_t)p->a_un.a_val);
   }
 
   p = find_auxv_entry(AT_L3_CACHESIZE, buffer);
   if (p) {
       found++;
       print_size("L3  ", (uint32_t)p->a_un.a_val);
   }
 
   p = find_auxv_entry(AT_L3_CACHEGEOMETRY, buffer);
   if (p) {
       found++;
       print_geo("L3  ", (uint32_t)p->a_un.a_val);
   }
 
   /* If we found none we're probably on a system where they don't exist */
   SKIP_IF(found == 0);
 
   /* But if we found any, we expect to find them all */
   FAIL_IF(found != 8);
 
   return 0;
}
 
int main(void)
{
   return test_harness(test_cache_shape, "cache_shape");
}