1 #include <stddef.h>
2
3 #include <cpuinfo.h>
4 #include <cpuinfo/internal-api.h>
5 #include <cpuinfo/log.h>
6
7
8 bool cpuinfo_is_initialized = false;
9
10 struct cpuinfo_processor* cpuinfo_processors = NULL;
11 struct cpuinfo_core* cpuinfo_cores = NULL;
12 struct cpuinfo_cluster* cpuinfo_clusters = NULL;
13 struct cpuinfo_package* cpuinfo_packages = NULL;
14 struct cpuinfo_cache* cpuinfo_cache[cpuinfo_cache_level_max] = { NULL };
15
16 uint32_t cpuinfo_processors_count = 0;
17 uint32_t cpuinfo_cores_count = 0;
18 uint32_t cpuinfo_clusters_count = 0;
19 uint32_t cpuinfo_packages_count = 0;
20 uint32_t cpuinfo_cache_count[cpuinfo_cache_level_max] = { 0 };
21 uint32_t cpuinfo_max_cache_size = 0;
22
23
cpuinfo_get_processors(void)24 const struct cpuinfo_processor* cpuinfo_get_processors(void) {
25 if (!cpuinfo_is_initialized) {
26 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "processors");
27 }
28 return cpuinfo_processors;
29 }
30
cpuinfo_get_cores(void)31 const struct cpuinfo_core* cpuinfo_get_cores(void) {
32 if (!cpuinfo_is_initialized) {
33 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "core");
34 }
35 return cpuinfo_cores;
36 }
37
cpuinfo_get_clusters(void)38 const struct cpuinfo_cluster* cpuinfo_get_clusters(void) {
39 if (!cpuinfo_is_initialized) {
40 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "clusters");
41 }
42 return cpuinfo_clusters;
43 }
44
cpuinfo_get_packages(void)45 const struct cpuinfo_package* cpuinfo_get_packages(void) {
46 if (!cpuinfo_is_initialized) {
47 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "packages");
48 }
49 return cpuinfo_packages;
50 }
51
cpuinfo_get_processor(uint32_t index)52 const struct cpuinfo_processor* cpuinfo_get_processor(uint32_t index) {
53 if (!cpuinfo_is_initialized) {
54 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "processor");
55 }
56 if (index < cpuinfo_processors_count) {
57 return cpuinfo_processors + index;
58 } else {
59 return NULL;
60 }
61 }
62
cpuinfo_get_core(uint32_t index)63 const struct cpuinfo_core* cpuinfo_get_core(uint32_t index) {
64 if (!cpuinfo_is_initialized) {
65 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "core");
66 }
67 if (index < cpuinfo_cores_count) {
68 return cpuinfo_cores + index;
69 } else {
70 return NULL;
71 }
72 }
73
cpuinfo_get_cluster(uint32_t index)74 const struct cpuinfo_cluster* cpuinfo_get_cluster(uint32_t index) {
75 if (!cpuinfo_is_initialized) {
76 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "cluster");
77 }
78 if (index < cpuinfo_clusters_count) {
79 return cpuinfo_clusters + index;
80 } else {
81 return NULL;
82 }
83 }
84
cpuinfo_get_package(uint32_t index)85 const struct cpuinfo_package* cpuinfo_get_package(uint32_t index) {
86 if (!cpuinfo_is_initialized) {
87 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "package");
88 }
89 if (index < cpuinfo_packages_count) {
90 return cpuinfo_packages + index;
91 } else {
92 return NULL;
93 }
94 }
95
cpuinfo_get_processors_count(void)96 uint32_t cpuinfo_get_processors_count(void) {
97 if (!cpuinfo_is_initialized) {
98 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "processors_count");
99 }
100 return cpuinfo_processors_count;
101 }
102
cpuinfo_get_cores_count(void)103 uint32_t cpuinfo_get_cores_count(void) {
104 if (!cpuinfo_is_initialized) {
105 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "cores_count");
106 }
107 return cpuinfo_cores_count;
108 }
109
cpuinfo_get_clusters_count(void)110 uint32_t cpuinfo_get_clusters_count(void) {
111 if (!cpuinfo_is_initialized) {
112 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "clusters_count");
113 }
114 return cpuinfo_clusters_count;
115 }
116
cpuinfo_get_packages_count(void)117 uint32_t cpuinfo_get_packages_count(void) {
118 if (!cpuinfo_is_initialized) {
119 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "packages_count");
120 }
121 return cpuinfo_packages_count;
122 }
123
cpuinfo_get_l1i_caches(void)124 const struct cpuinfo_cache* CPUINFO_ABI cpuinfo_get_l1i_caches(void) {
125 if (!cpuinfo_is_initialized) {
126 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l1i_caches");
127 }
128 return cpuinfo_cache[cpuinfo_cache_level_1i];
129 }
130
cpuinfo_get_l1d_caches(void)131 const struct cpuinfo_cache* CPUINFO_ABI cpuinfo_get_l1d_caches(void) {
132 if (!cpuinfo_is_initialized) {
133 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l1d_caches");
134 }
135 return cpuinfo_cache[cpuinfo_cache_level_1d];
136 }
137
cpuinfo_get_l2_caches(void)138 const struct cpuinfo_cache* CPUINFO_ABI cpuinfo_get_l2_caches(void) {
139 if (!cpuinfo_is_initialized) {
140 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l2_caches");
141 }
142 return cpuinfo_cache[cpuinfo_cache_level_2];
143 }
144
cpuinfo_get_l3_caches(void)145 const struct cpuinfo_cache* CPUINFO_ABI cpuinfo_get_l3_caches(void) {
146 if (!cpuinfo_is_initialized) {
147 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l3_caches");
148 }
149 return cpuinfo_cache[cpuinfo_cache_level_3];
150 }
151
cpuinfo_get_l4_caches(void)152 const struct cpuinfo_cache* CPUINFO_ABI cpuinfo_get_l4_caches(void) {
153 if (!cpuinfo_is_initialized) {
154 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l4_caches");
155 }
156 return cpuinfo_cache[cpuinfo_cache_level_4];
157 }
158
cpuinfo_get_l1i_cache(uint32_t index)159 const struct cpuinfo_cache* CPUINFO_ABI cpuinfo_get_l1i_cache(uint32_t index) {
160 if (!cpuinfo_is_initialized) {
161 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l1i_cache");
162 }
163 if (index < cpuinfo_cache_count[cpuinfo_cache_level_1i]) {
164 return cpuinfo_cache[cpuinfo_cache_level_1i] + index;
165 } else {
166 return NULL;
167 }
168 }
169
cpuinfo_get_l1d_cache(uint32_t index)170 const struct cpuinfo_cache* CPUINFO_ABI cpuinfo_get_l1d_cache(uint32_t index) {
171 if (!cpuinfo_is_initialized) {
172 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l1d_cache");
173 }
174 if (index < cpuinfo_cache_count[cpuinfo_cache_level_1d]) {
175 return cpuinfo_cache[cpuinfo_cache_level_1d] + index;
176 } else {
177 return NULL;
178 }
179 }
180
cpuinfo_get_l2_cache(uint32_t index)181 const struct cpuinfo_cache* CPUINFO_ABI cpuinfo_get_l2_cache(uint32_t index) {
182 if (!cpuinfo_is_initialized) {
183 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l2_cache");
184 }
185 if (index < cpuinfo_cache_count[cpuinfo_cache_level_2]) {
186 return cpuinfo_cache[cpuinfo_cache_level_2] + index;
187 } else {
188 return NULL;
189 }
190 }
191
cpuinfo_get_l3_cache(uint32_t index)192 const struct cpuinfo_cache* CPUINFO_ABI cpuinfo_get_l3_cache(uint32_t index) {
193 if (!cpuinfo_is_initialized) {
194 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l3_cache");
195 }
196 if (index < cpuinfo_cache_count[cpuinfo_cache_level_3]) {
197 return cpuinfo_cache[cpuinfo_cache_level_3] + index;
198 } else {
199 return NULL;
200 }
201 }
202
cpuinfo_get_l4_cache(uint32_t index)203 const struct cpuinfo_cache* CPUINFO_ABI cpuinfo_get_l4_cache(uint32_t index) {
204 if (!cpuinfo_is_initialized) {
205 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l4_cache");
206 }
207 if (index < cpuinfo_cache_count[cpuinfo_cache_level_4]) {
208 return cpuinfo_cache[cpuinfo_cache_level_4] + index;
209 } else {
210 return NULL;
211 }
212 }
213
cpuinfo_get_l1i_caches_count(void)214 uint32_t CPUINFO_ABI cpuinfo_get_l1i_caches_count(void) {
215 if (!cpuinfo_is_initialized) {
216 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l1i_caches_count");
217 }
218 return cpuinfo_cache_count[cpuinfo_cache_level_1i];
219 }
220
cpuinfo_get_l1d_caches_count(void)221 uint32_t CPUINFO_ABI cpuinfo_get_l1d_caches_count(void) {
222 if (!cpuinfo_is_initialized) {
223 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l1d_caches_count");
224 }
225 return cpuinfo_cache_count[cpuinfo_cache_level_1d];
226 }
227
cpuinfo_get_l2_caches_count(void)228 uint32_t CPUINFO_ABI cpuinfo_get_l2_caches_count(void) {
229 if (!cpuinfo_is_initialized) {
230 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l2_caches_count");
231 }
232 return cpuinfo_cache_count[cpuinfo_cache_level_2];
233 }
234
cpuinfo_get_l3_caches_count(void)235 uint32_t CPUINFO_ABI cpuinfo_get_l3_caches_count(void) {
236 if (!cpuinfo_is_initialized) {
237 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l3_caches_count");
238 }
239 return cpuinfo_cache_count[cpuinfo_cache_level_3];
240 }
241
cpuinfo_get_l4_caches_count(void)242 uint32_t CPUINFO_ABI cpuinfo_get_l4_caches_count(void) {
243 if (!cpuinfo_is_initialized) {
244 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "l4_caches_count");
245 }
246 return cpuinfo_cache_count[cpuinfo_cache_level_4];
247 }
248
cpuinfo_get_max_cache_size(void)249 uint32_t CPUINFO_ABI cpuinfo_get_max_cache_size(void) {
250 if (!cpuinfo_is_initialized) {
251 cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "max_cache_size");
252 }
253 return cpuinfo_max_cache_size;
254 }
255