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

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

106 lines
2.1 KiB
C++
Raw Permalink Normal View History

/* Human readable cpusets.
2018-08-27 11:09:25 +02:00
*
* Author: Steffen Vogel <github@daniel-krebs.net>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-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);
}
2018-08-27 11:09:25 +02:00
}
CpuSet::CpuSet(const std::string &str) : CpuSet() {
size_t endpos, start, end;
2018-08-27 11:09:25 +02:00
for (auto token : tokenize(str, ",")) {
auto sep = token.find('-');
2018-08-27 11:09:25 +02:00
if (sep == std::string::npos) {
start = std::stoi(token, &endpos);
2018-08-27 11:09:25 +02:00
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);
2018-08-27 11:09:25 +02:00
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);
2018-08-27 11:09:25 +02:00
end = std::stoi(token2, &endpos);
2018-08-27 11:09:25 +02:00
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);
}
}
2018-08-27 11:09:25 +02:00
}
CpuSet::CpuSet(const char *str) : CpuSet(std::string(str)) {}
2019-04-15 13:01:01 +02:00
CpuSet::operator uintmax_t() {
uintmax_t iset = 0;
2020-09-13 10:59:19 +02:00
for (size_t i = 0; i < num_cpus; i++) {
if (isSet(i))
iset |= 1ULL << i;
}
2020-09-13 10:59:19 +02:00
return iset;
2020-09-13 10:59:19 +02:00
}
CpuSet::operator std::string() {
std::stringstream ss;
bool first = true;
for (size_t i = 0; i < num_cpus; i++) {
if (isSet(i)) {
size_t run = 0;
for (size_t j = i + 1; j < num_cpus; j++) {
if (!isSet(j))
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();
2018-08-27 11:09:25 +02:00
}
#endif // __linux__