MCPcopy Create free account
hub / github.com/apache/arrow / LinuxGetCacheSize

Function LinuxGetCacheSize

cpp/src/arrow/util/cpu_info.cc:207–250  ·  view source on GitHub ↗

Get cache size, return 0 on error

Source from the content-addressed store, hash-verified

205
206// Get cache size, return 0 on error
207int64_t LinuxGetCacheSize(int level) {
208 // get cache size by sysconf()
209# ifdef _SC_LEVEL1_DCACHE_SIZE
210 constexpr auto kCacheSizeConf = std::array<int, 3>{
211 _SC_LEVEL1_DCACHE_SIZE,
212 _SC_LEVEL2_CACHE_SIZE,
213 _SC_LEVEL3_CACHE_SIZE,
214 };
215
216 errno = 0;
217 DCHECK(0 <= level && static_cast<std::size_t>(level) < kCacheSizeConf.size());
218 const int64_t cache_size = sysconf(kCacheSizeConf[level]);
219 if (errno == 0 && cache_size > 0) {
220 return cache_size;
221 }
222# endif
223
224 // get cache size from sysfs if sysconf() fails or not supported
225 constexpr auto kCacheSizeSysfs = std::array<const char*, 3>{
226 "/sys/devices/system/cpu/cpu0/cache/index0/size", // l1d (index1 is l1i)
227 "/sys/devices/system/cpu/cpu0/cache/index2/size", // l2
228 "/sys/devices/system/cpu/cpu0/cache/index3/size", // l3
229 };
230
231 DCHECK(0 <= level && static_cast<std::size_t>(level) < kCacheSizeSysfs.size());
232 std::ifstream cacheinfo(kCacheSizeSysfs[level], std::ios::in);
233 if (!cacheinfo) {
234 return 0;
235 }
236 // cacheinfo is one line like: 65536, 64K, 1M, etc.
237 uint64_t size = 0;
238 char unit = '\0';
239 cacheinfo >> size >> unit;
240 if (unit == 'K') {
241 size <<= 10;
242 } else if (unit == 'M') {
243 size <<= 20;
244 } else if (unit == 'G') {
245 size <<= 30;
246 } else if (unit != '\0') {
247 return 0;
248 }
249 return static_cast<int64_t>(size);
250}
251
252template <std::size_t N>
253void OsRetrieveCacheSize(std::array<int64_t, N>* cache_sizes) {

Callers 1

OsRetrieveCacheSizeFunction · 0.85

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected