1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/common/lib/cpuset.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

117 lines
2 KiB
C++
Raw Normal View History

2018-08-27 11:09:25 +02:00
/** Human readable cpusets.
*
* @file
* @author Steffen Vogel <github@daniel-krebs.net>
2022-03-15 09:05:42 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-05-19 17:40:10 +02:00
* @license Apache License 2.0
2018-08-27 11:09:25 +02:00
*********************************************************************************/
#include <villas/cpuset.hpp>
#include <villas/utils.hpp>
using namespace villas::utils;
#ifdef __linux__
CpuSet::CpuSet(uintmax_t iset) :
CpuSet()
{
zero();
for (size_t i = 0; i < num_cpus; i++) {
if (iset & (1L << i))
set(i);
}
}
CpuSet::CpuSet(const std::string &str) :
CpuSet()
{
size_t endpos, start, end;
for (auto token : tokenize(str, ",")) {
auto sep = token.find('-');
if (sep == std::string::npos) {
start = std::stoi(token, &endpos);
if (token.begin() + endpos != token.end())
throw std::invalid_argument("Not a valid CPU set");
2018-08-27 11:09:25 +02:00
if (start < num_cpus)
set(start);
}
else {
start = std::stoi(token, &endpos);
if (token.begin() + endpos != token.begin() + sep)
throw std::invalid_argument("Not a valid CPU set");
2018-08-27 11:09:25 +02:00
auto token2 = token.substr(endpos + 1);
end = std::stoi(token2, &endpos);
if (token2.begin() + endpos != token2.end())
throw std::invalid_argument("Not a valid CPU set");
2018-08-27 11:09:25 +02:00
for (size_t i = start; i <= end && i < num_cpus; i++)
set(i);
}
}
}
2019-04-15 13:01:01 +02:00
CpuSet::CpuSet(const char *str)
: CpuSet(std::string(str))
{ }
2020-09-13 10:59:19 +02:00
CpuSet::operator uintmax_t()
{
uintmax_t iset = 0;
for (size_t i = 0; i < num_cpus; i++) {
if (isSet(i))
2020-09-13 10:59:19 +02:00
iset |= 1ULL << i;
}
return iset;
}
2018-08-27 11:09:25 +02:00
CpuSet::operator std::string ()
{
std::stringstream ss;
bool first = true;
for (size_t i = 0; i < num_cpus; i++) {
if (isSet(i)) {
2018-08-27 11:09:25 +02:00
size_t run = 0;
for (size_t j = i + 1; j < num_cpus; j++) {
if (!isSet(j))
2018-08-27 11:09:25 +02:00
break;
run++;
}
if (first)
first = false;
else
ss << ",";
ss << i;
if (run == 1) {
ss << "," << (i + 1);
i++;
}
else if (run > 1) {
ss << "-" << (i + run);
i += run;
}
}
}
return ss.str();
}
#endif // __linux__