in src/sys_info.cc [99:198]
void SysInfo::init_sys_info(const char * affinity_set) {
// Prevent the (perceived) cpu topology from being changed at runtime
static bool called = false;
if (called) {
return;
}
called = true;
std::string line;
std::ifstream onlinefile("/sys/devices/system/cpu/online");
if (!onlinefile.is_open()) {
fprintf(stderr, "Error opening /sys/devices/system/cpu/online\n");
exit(1);
}
std::getline(onlinefile,line);
onlinefile.close();
online_cpus = str_to_cpu_set(line.c_str());
// Get lowest and highest cpu numbers
cpulo = *online_cpus.begin();
cpuhi = *online_cpus.rbegin();
if (affinity_set) {
affinity_cpus = str_to_cpu_set(affinity_set);
} else {
affinity_cpus = online_cpus;
}
// ************* block device stuff ****************
// all block devices are listed here, including partitions
DIR * c_b_dir = opendir("/sys/class/block");
if (c_b_dir == nullptr) {
fprintf(stderr, "Failed to get list of block devices from /sys/class/block");
perror("opendir");
exit(1);
}
struct dirent *c_b_dirent;
int err;
std::string temp;
std::set<std::string> block_devices;
// loop through the directory stream until we reach the end
while (1) {
// In modern implementations (including the glibc implementation),
// concurrent calls to readdir() that specify different directory
// streams are thread-safe. See "man readdir".
errno = 0;
c_b_dirent = readdir(c_b_dir);
if (errno) { // only error is EBADF for c_b_dir, realllly unlikely
fprintf(stderr, "Error in device info initialization\n");
perror("readdir");
exit(1);
}
if (!c_b_dirent)
break;
temp = std::string(c_b_dirent->d_name);
// ignore hidden files
if (temp[0] == '.') continue;
block_devices.insert(temp);
}
closedir(c_b_dir);
std::string dev_path;
struct stat dev_stat = {0};
// look each partition up in /dev, compare device_id until we find it
for (auto& str : block_devices) {
dev_path = "/dev/" + str;
int err = stat(dev_path.c_str(), &dev_stat);
if (err) {
fprintf(stderr, "Unexpected error '%d: %s' when statting %s!\n", errno, strerror(errno), dev_path.c_str());
perror("stat");
exit(1);
}
//printf("mapping %u,%u to %s\n", major(dev_stat.st_rdev), minor(dev_stat.st_rdev), str.c_str());
// map it for later!
id_to_device[dev_stat.st_rdev] = str;
}
// ************* fua caching ****************
std::ifstream fuafile("/sys/module/libata/parameters/fua");
// if we can't open it, the kernel probably doesn't know the state of fua, so ignore
if (fuafile.is_open()) {
std::getline(fuafile,line);
fuafile.close();
caching_options = "fua="+line;
}
}