@@ -296,35 +296,31 @@ void OsRetrieveCpuInfo(int64_t* hardware_flags, CpuInfo::Vendor* vendor,
296296// ------------------------------ LINUX ------------------------------//
297297// Get cache size, return 0 on error
298298int64_t LinuxGetCacheSize (int level) {
299- const struct {
300- int sysconf_name;
301- const char * sysfs_path;
302- } kCacheSizeEntries [] = {
303- {
304- _SC_LEVEL1_DCACHE_SIZE,
305- " /sys/devices/system/cpu/cpu0/cache/index0/size" , // l1d (index1 is l1i)
306- },
307- {
308- _SC_LEVEL2_CACHE_SIZE,
309- " /sys/devices/system/cpu/cpu0/cache/index2/size" , // l2
310- },
311- {
312- _SC_LEVEL3_CACHE_SIZE,
313- " /sys/devices/system/cpu/cpu0/cache/index3/size" , // l3
314- },
299+ // get cache size by sysconf()
300+ #ifdef _SC_LEVEL1_DCACHE_SIZE
301+ const int kCacheSizeConf [] = {
302+ _SC_LEVEL1_DCACHE_SIZE,
303+ _SC_LEVEL2_CACHE_SIZE,
304+ _SC_LEVEL3_CACHE_SIZE,
315305 };
316- static_assert (sizeof (kCacheSizeEntries ) / sizeof (kCacheSizeEntries [0 ]) == kCacheLevels ,
317- " " );
306+ static_assert (sizeof (kCacheSizeConf ) / sizeof (kCacheSizeConf [0 ]) == kCacheLevels , " " );
318307
319- // get cache size by sysconf()
320308 errno = 0 ;
321- const int64_t cache_size = sysconf (kCacheSizeEntries [level]. sysconf_name );
309+ const int64_t cache_size = sysconf (kCacheSizeConf [level]);
322310 if (errno == 0 && cache_size > 0 ) {
323311 return cache_size;
324312 }
313+ #endif
314+
315+ // get cache size from sysfs if sysconf() fails or not supported
316+ const char * kCacheSizeSysfs [] = {
317+ " /sys/devices/system/cpu/cpu0/cache/index0/size" , // l1d (index1 is l1i)
318+ " /sys/devices/system/cpu/cpu0/cache/index2/size" , // l2
319+ " /sys/devices/system/cpu/cpu0/cache/index3/size" , // l3
320+ };
321+ static_assert (sizeof (kCacheSizeSysfs ) / sizeof (kCacheSizeSysfs [0 ]) == kCacheLevels , " " );
325322
326- // get cache size from sysfs if sysconf() fails (it does happen on Arm)
327- std::ifstream cacheinfo (kCacheSizeEntries [level].sysfs_path , std::ios::in);
323+ std::ifstream cacheinfo (kCacheSizeSysfs [level], std::ios::in);
328324 if (!cacheinfo) {
329325 return 0 ;
330326 }
0 commit comments