Files
perfect/include/perfect/detail/os/linux.hpp
Carl Pearson f51ef904fb initial commit
2019-09-19 10:59:28 -05:00

51 lines
1.1 KiB
C++

#pragma once
#include <string>
#include <fstream>
#include <cassert>
#include <vector>
#include <sched.h>
#include <sys/types.h>
#include <unistd.h>
#include "perfect/result.hpp"
namespace perfect {
std::vector<int> cpus() {
std::vector<int> result;
cpu_set_t mask;
if (sched_getaffinity(0 /*caller*/, sizeof(cpu_set_t), &mask)) {
assert(0 && "failed sched_getaffinity");
}
for (int i = 0; i < CPU_SETSIZE; ++i) {
if (CPU_ISSET(i, &mask)) {
result.push_back(i);
}
}
}
Result get_governor(std::string &result, const int cpu) {
std::string path("/sys/devices/system/cpu/cpu");
path += std::to_string(cpu);
path += "/cpufreq/scaling_governor";
std::ifstream ifs(path, std::ifstream::in);
std::getline(ifs, result);
return Result::SUCCESS;
}
Result set_governor(const int cpu, const std::string &governor) {
std::string path("/sys/devices/system/cpu/cpu");
path += std::to_string(cpu);
path += "/cpufreq/scaling_governor";
std::ofstream ofs(path, std::ofstream::out);
ofs << governor;
ofs.close();
if (ofs.fail()) {
return Result::NO_PERMISSION;
}
return Result::SUCCESS;
}
}