Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-17341: [C++] Fix cpu_info.cc build error on musl libc #13819

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions cpp/src/arrow/util/cpu_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -295,35 +295,31 @@ void OsRetrieveCpuInfo(int64_t* hardware_flags, CpuInfo::Vendor* vendor,
//------------------------------ LINUX ------------------------------//
// Get cache size, return 0 on error
int64_t LinuxGetCacheSize(int level) {
const struct {
int sysconf_name;
const char* sysfs_path;
} kCacheSizeEntries[] = {
{
_SC_LEVEL1_DCACHE_SIZE,
"/sys/devices/system/cpu/cpu0/cache/index0/size", // l1d (index1 is l1i)
},
{
_SC_LEVEL2_CACHE_SIZE,
"/sys/devices/system/cpu/cpu0/cache/index2/size", // l2
},
{
_SC_LEVEL3_CACHE_SIZE,
"/sys/devices/system/cpu/cpu0/cache/index3/size", // l3
},
// get cache size by sysconf()
#ifdef _SC_LEVEL1_DCACHE_SIZE
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Musl libc doesn't define these sysconf flags.

const int kCacheSizeConf[] = {
_SC_LEVEL1_DCACHE_SIZE,
_SC_LEVEL2_CACHE_SIZE,
_SC_LEVEL3_CACHE_SIZE,
};
static_assert(sizeof(kCacheSizeEntries) / sizeof(kCacheSizeEntries[0]) == kCacheLevels,
"");
static_assert(sizeof(kCacheSizeConf) / sizeof(kCacheSizeConf[0]) == kCacheLevels, "");

// get cache size by sysconf()
errno = 0;
const int64_t cache_size = sysconf(kCacheSizeEntries[level].sysconf_name);
const int64_t cache_size = sysconf(kCacheSizeConf[level]);
if (errno == 0 && cache_size > 0) {
return cache_size;
}
#endif

// get cache size from sysfs if sysconf() fails or not supported
const char* kCacheSizeSysfs[] = {
"/sys/devices/system/cpu/cpu0/cache/index0/size", // l1d (index1 is l1i)
"/sys/devices/system/cpu/cpu0/cache/index2/size", // l2
"/sys/devices/system/cpu/cpu0/cache/index3/size", // l3
};
static_assert(sizeof(kCacheSizeSysfs) / sizeof(kCacheSizeSysfs[0]) == kCacheLevels, "");

// get cache size from sysfs if sysconf() fails (it does happen on Arm)
std::ifstream cacheinfo(kCacheSizeEntries[level].sysfs_path, std::ios::in);
std::ifstream cacheinfo(kCacheSizeSysfs[level], std::ios::in);
if (!cacheinfo) {
return 0;
}
Expand Down