/
util_impl.cpp
275 lines (233 loc) · 8.51 KB
/
util_impl.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*******************************************************************************
* Copyright 2020-2022 FUJITSU LIMITED
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#include "xbyak_aarch64_util.h"
namespace Xbyak_aarch64 {
namespace util {
void Cpu::setCacheHierarchy() {
/* Cache size of AArch64 CPUs are described in the system registers,
which can't be read from user-space applications.
Linux provides `sysconf` API and `/sys/devices/system/cpu/`
device files to get cache size, but they dosen't always return
correct values. It may depend on Linux kernel version and
support status of CPUs. To avoid this situation, cahche size is
firstly read from `cacheInfoDict`, secondly get by `sysconf`.
`sysconf` example
#include <unistd.h>
int main() {
reutrn sysconf(_SC_LEVEL1_DCACHE_SIZE);
}
*/
const cacheInfo_t *c = nullptr;
for (size_t j = 0; j < sizeof(cacheInfoDict) / sizeof(cacheInfo_t); j++) {
if (cacheInfoDict[j].midr_el1 == midr_el1_) {
c = cacheInfoDict + j;
break;
}
}
if (c != nullptr) {
dataCacheLevel_ = c->dataCacheLevel;
for (uint32_t i = 0; i < maxNumberCacheLevel; i++) {
if (i < c->highestInnerCacheLevel)
dataCacheSize_[i] = c->dataCacheSize[i];
else
dataCacheSize_[i] = c->dataCacheSize[i] / coresSharingDataCache_[i];
}
} else {
/**
* @ToDo Get cache information by `sysconf`
* for the case thd dictionary is unavailable.
*/
// _SC_LEVEL<L>_DCACHE_SIZE macros are not defined on macOS.
#if defined(__APPLE__)
#define GET_CACHE_SIZE(ID) 0
#else
#define GET_CACHE_SIZE(ID) sysconf(ID)
#endif
#define XBYAK_AARCH64_CACHE_SIZE(LEVEL, SIZE, ID, CORES, VAL) \
cache_size = GET_CACHE_SIZE(ID); \
VAL[LEVEL] = cache_size ? (cache_size / (CORES)) : ((SIZE) / (CORES));
uint32_t cache_size;
/* If `sysconf` returns zero as cache sizes, 32KiB, 1MiB, 0 and 0 is set as
1st, 2nd, 3rd and 4th level cache sizes. 2nd cache is assumed as sharing cache. */
XBYAK_AARCH64_CACHE_SIZE(0, 1024 * 32, _SC_LEVEL1_DCACHE_SIZE, 1, coresSharingDataCache_);
XBYAK_AARCH64_CACHE_SIZE(1, 1024 * 1024, _SC_LEVEL2_CACHE_SIZE, 1, coresSharingDataCache_);
XBYAK_AARCH64_CACHE_SIZE(2, 0, _SC_LEVEL3_CACHE_SIZE, 1, coresSharingDataCache_);
XBYAK_AARCH64_CACHE_SIZE(3, 0, _SC_LEVEL4_CACHE_SIZE, 1, coresSharingDataCache_);
XBYAK_AARCH64_CACHE_SIZE(0, 1024 * 32, _SC_LEVEL1_DCACHE_SIZE, 1, dataCacheSize_);
XBYAK_AARCH64_CACHE_SIZE(1, 1024 * 1024, _SC_LEVEL2_CACHE_SIZE, 8, dataCacheSize_);
XBYAK_AARCH64_CACHE_SIZE(2, 0, _SC_LEVEL3_CACHE_SIZE, 1, dataCacheSize_);
XBYAK_AARCH64_CACHE_SIZE(3, 0, _SC_LEVEL4_CACHE_SIZE, 1, dataCacheSize_);
#undef XBYAK_AARCH64_CACHE_SIZE
}
}
void Cpu::setNumCores() {
#ifdef __linux__
/**
* @ToDo There are some methods to get # of cores.
Considering various kernel versions and CPUs, a combination of
multiple methods may be required.
1) sysconf(_SC_NPROCESSORS_ONLN)
2) /sys/devices/system/cpu/online
3) std::thread::hardware_concurrency()
*/
numCores_[0] = numCores_[1] = sysconf(_SC_NPROCESSORS_ONLN);
coresSharingDataCache_[0] = 1;
/* # of numa nodes: /sys/devices/system/node/node[0-9]+
# of cores for each numa node: /sys/devices/system/node/node[0-9]+/cpu[0-9]+
It is assumed L2 cache is shared by each numa node. */
const int nodes = getFilePathMaxTailNumPlus1(XBYAK_AARCH64_PATH_NODES);
int cores = 1;
if (nodes > 0) {
cores = getFilePathMaxTailNumPlus1(XBYAK_AARCH64_PATH_CORES);
coresSharingDataCache_[1] = (cores > 0) ? cores : 1;
} else {
coresSharingDataCache_[1] = 1;
}
#else
numCores_[0] = numCores_[1] = 1;
for (unsigned int i = 0; i < maxNumberCacheLevel; i++)
coresSharingDataCache_[i] = 1;
coresSharingDataCache_[1] = 8; // Set possible value.
#endif
}
void Cpu::setSysRegVal() {
#ifdef __linux__
XBYAK_AARCH64_READ_SYSREG(midr_el1_, MIDR_EL1);
#endif
}
/**
* Return directory path
* @param[in] path ex. /sys/devices/system/node/node
* @param[out] buf ex. /sys/devices/system/node
*/
int Cpu::getRegEx(char *buf, const char *path, const char *regex) {
regex_t regexBuf;
regmatch_t match[1];
if (regcomp(®exBuf, regex, REG_EXTENDED) != 0)
throw ERR_INTERNAL;
const int retVal = regexec(®exBuf, path, 1, match, 0);
regfree(®exBuf);
if (retVal != 0)
return -1;
const int startIdx = match[0].rm_so;
const int endIdx = match[0].rm_eo;
/* Something wrong (multiple match or not match) */
if (startIdx == -1 || endIdx == -1 || (endIdx - startIdx - 1) < 1)
return -1;
strncpy(buf, path + startIdx, endIdx - startIdx);
buf[endIdx - startIdx] = '\0';
return 0;
}
int Cpu::getFilePathMaxTailNumPlus1(const char *path) {
#ifdef __linux__
char dir_path[max_path_len];
char file_pattern[max_path_len];
int retVal = 0;
getRegEx(dir_path, path, "/([^/]+/)+");
/* Remove last '/'. */
dir_path[strlen(dir_path) - 1] = '\0';
getRegEx(file_pattern, path, "[^/]+$");
strncat(file_pattern, "[0-9]+", 16);
fflush(stdout);
DIR *dir = opendir(dir_path);
struct dirent *dp;
dp = readdir(dir);
while (dp != NULL) {
if (getRegEx(dir_path, dp->d_name, file_pattern) == 0)
retVal++;
dp = readdir(dir);
}
if (dir != NULL)
closedir(dir);
return retVal;
#else
return 0;
#endif
}
Cpu::Cpu() : type_(tNONE), sveLen_(SVE_NONE) {
#ifdef __linux__
unsigned long hwcap = getauxval(AT_HWCAP);
if (hwcap & HWCAP_ATOMICS) {
type_ |= tATOMIC;
}
if (hwcap & HWCAP_FP) {
type_ |= tFP;
}
if (hwcap & HWCAP_ASIMD) {
type_ |= tADVSIMD;
}
#ifdef HWCAP_SVE
/* Some old <sys/auxv.h> may not define HWCAP_SVE.
In that case, SVE is treated as if it were not supported. */
if (hwcap & HWCAP_SVE) {
type_ |= tSVE;
// svcntb(); if arm_sve.h is available
sveLen_ = (sveLen_t)prctl(51); // PR_SVE_GET_VL
}
#endif
#elif defined(__APPLE__)
size_t val = 0;
size_t len = sizeof(val);
if (sysctlbyname(hw_opt_atomics, &val, &len, NULL, 0) != 0)
throw Error(ERR_INTERNAL);
else
type_ |= (val == 1) ? tATOMIC : 0;
if (sysctlbyname(hw_opt_fp, &val, &len, NULL, 0) != 0)
throw Error(ERR_INTERNAL);
else
type_ |= (val == 1) ? tFP : 0;
if (sysctlbyname(hw_opt_neon, &val, &len, NULL, 0) != 0)
throw Error(ERR_INTERNAL);
else
type_ |= (val == 1) ? tADVSIMD : 0;
#endif
setSysRegVal();
setNumCores();
setCacheHierarchy();
}
Type Cpu::getType() const { return type_; }
bool Cpu::has(Type type) const { return (type & type_) != 0; }
uint64_t Cpu::getSveLen() const { return sveLen_; }
bool Cpu::isAtomicSupported() const { return type_ & tATOMIC; }
const char *Cpu::getImplementer() const {
uint64_t implementer = (midr_el1_ >> 24) & 0xff;
for (size_t i = 0; i < sizeof(implementers) / sizeof(implementer_t); i++) {
if (implementers[i].id == implementer)
return implementers[i].implementer;
}
return nullptr;
}
uint32_t Cpu::getCoresSharingDataCache(uint32_t i) const {
if (i >= dataCacheLevel_)
throw Error(ERR_BAD_PARAMETER);
return coresSharingDataCache_[i];
}
uint32_t Cpu::getDataCacheLevels() const { return dataCacheLevel_; }
uint32_t Cpu::getDataCacheSize(uint32_t i) const {
if (i >= dataCacheLevel_)
throw Error(ERR_BAD_PARAMETER);
return dataCacheSize_[i];
}
uint32_t Cpu::getNumCores(Arm64CpuTopologyLevel level) const {
switch (level) {
case CoreLevel:
return numCores_[level - 1];
default:
throw Error(ERR_BAD_PARAMETER);
}
}
} // namespace util
} // namespace Xbyak_aarch64