2017-06-19 23:01:50 +08:00
|
|
|
/*
|
|
|
|
|
2017-06-22 08:56:10 +08:00
|
|
|
nsjail - CPU affinity
|
2017-06-19 23:01:50 +08:00
|
|
|
-----------------------------------------
|
|
|
|
|
2017-06-22 08:56:10 +08:00
|
|
|
Copyright 2017 Google Inc. All Rights Reserved.
|
2017-06-19 23:01:50 +08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
|
2017-10-18 20:46:17 +08:00
|
|
|
#include <inttypes.h>
|
2017-06-19 23:01:50 +08:00
|
|
|
#include <sched.h>
|
2017-10-18 20:46:17 +08:00
|
|
|
#include <stdint.h>
|
2017-06-19 23:01:50 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-08-05 01:22:33 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2018-02-11 00:49:15 +08:00
|
|
|
#include "logs.h"
|
2018-02-10 01:45:50 +08:00
|
|
|
#include "util.h"
|
|
|
|
|
2018-02-10 00:41:16 +08:00
|
|
|
namespace cpu {
|
2017-06-19 23:01:50 +08:00
|
|
|
|
2022-08-05 14:43:39 +08:00
|
|
|
static std::string listCpusInSet(cpu_set_t* mask) {
|
|
|
|
std::string ret = "[ ";
|
|
|
|
for (size_t i = 0; i < CPU_SETSIZE; i++) {
|
|
|
|
if (CPU_ISSET(i, mask)) {
|
|
|
|
ret.append(std::to_string(i));
|
|
|
|
ret.append(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.append("]");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t getNthOnlineCpu(cpu_set_t* mask, size_t n) {
|
2022-08-05 01:22:33 +08:00
|
|
|
for (size_t i = 0, j = 0; i < CPU_SETSIZE; i++) {
|
2022-08-05 14:43:39 +08:00
|
|
|
if (CPU_ISSET(i, mask)) {
|
2022-08-05 01:22:33 +08:00
|
|
|
if (j == n) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
2017-06-19 23:01:50 +08:00
|
|
|
}
|
2022-08-05 14:43:39 +08:00
|
|
|
LOG_F("No CPU #%zu found, yet there should be %zu left", n, (size_t)CPU_COUNT(mask));
|
2022-08-05 01:22:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-06-19 23:01:50 +08:00
|
|
|
|
2022-08-05 14:43:39 +08:00
|
|
|
static void setRandomCpu(cpu_set_t* original_mask, cpu_set_t* new_mask) {
|
|
|
|
size_t cpus_left = CPU_COUNT(original_mask);
|
2022-08-05 01:22:33 +08:00
|
|
|
if (cpus_left == 0) {
|
2022-08-05 14:43:39 +08:00
|
|
|
LOG_F("There are no more CPUs left to use, and there should be at least 1 left");
|
2017-06-19 23:01:50 +08:00
|
|
|
}
|
2022-08-05 01:22:33 +08:00
|
|
|
|
2022-08-05 14:43:39 +08:00
|
|
|
size_t n = util::rnd64() % cpus_left;
|
|
|
|
n = getNthOnlineCpu(original_mask, n);
|
|
|
|
|
|
|
|
LOG_D(
|
|
|
|
"Setting allowed CPU#:%zu from the set of %s", n, listCpusInSet(original_mask).c_str());
|
|
|
|
|
|
|
|
CPU_SET(n, new_mask);
|
|
|
|
CPU_CLR(n, original_mask);
|
2017-06-19 23:01:50 +08:00
|
|
|
}
|
|
|
|
|
2018-02-10 22:50:12 +08:00
|
|
|
bool initCpu(nsjconf_t* nsjconf) {
|
2022-08-05 01:22:33 +08:00
|
|
|
if (nsjconf->max_cpus == 0) {
|
|
|
|
LOG_D("No max_cpus limit set");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-05 14:43:39 +08:00
|
|
|
std::unique_ptr<cpu_set_t> original_mask(new cpu_set_t);
|
|
|
|
if (!original_mask) {
|
|
|
|
PLOG_W("Failure allocating cpu_set_t");
|
2017-06-19 23:01:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-05 14:43:39 +08:00
|
|
|
if (sched_getaffinity(0, CPU_ALLOC_SIZE(CPU_SETSIZE), original_mask.get()) == -1) {
|
|
|
|
PLOG_W("sched_getaffinity(0, mask_size=%zu)", (size_t)CPU_ALLOC_SIZE(CPU_SETSIZE));
|
2022-08-05 01:22:33 +08:00
|
|
|
return false;
|
2017-06-19 23:01:50 +08:00
|
|
|
}
|
2022-08-05 14:43:39 +08:00
|
|
|
size_t available_cpus = CPU_COUNT(original_mask.get());
|
|
|
|
|
|
|
|
LOG_D("Original CPU set: %s, with %zu allowed CPUs",
|
|
|
|
listCpusInSet(original_mask.get()).c_str(), available_cpus);
|
2022-08-05 01:22:33 +08:00
|
|
|
|
|
|
|
if (nsjconf->max_cpus > available_cpus) {
|
|
|
|
LOG_W(
|
|
|
|
"Number of requested CPUs is bigger than number of available CPUs (%zu > %zu)",
|
|
|
|
nsjconf->max_cpus, available_cpus);
|
2017-06-21 23:57:21 +08:00
|
|
|
return true;
|
|
|
|
}
|
2022-08-05 01:22:33 +08:00
|
|
|
if (nsjconf->max_cpus == available_cpus) {
|
|
|
|
LOG_D("All CPUs requested (%zu of %zu)", nsjconf->max_cpus, available_cpus);
|
2017-06-19 23:01:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-05 14:43:39 +08:00
|
|
|
std::unique_ptr<cpu_set_t> new_mask(new cpu_set_t);
|
|
|
|
if (!new_mask) {
|
|
|
|
PLOG_W("Failure allocating cpu_set_t");
|
2017-06-19 23:01:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-05 14:43:39 +08:00
|
|
|
CPU_ZERO(new_mask.get());
|
2017-06-19 23:01:50 +08:00
|
|
|
|
2017-06-21 23:52:16 +08:00
|
|
|
for (size_t i = 0; i < nsjconf->max_cpus; i++) {
|
2022-08-05 14:43:39 +08:00
|
|
|
setRandomCpu(original_mask.get(), new_mask.get());
|
2017-06-19 23:01:50 +08:00
|
|
|
}
|
|
|
|
|
2022-08-05 14:43:39 +08:00
|
|
|
LOG_D("Setting new CPU mask: %s with %zu allowed CPUs",
|
|
|
|
listCpusInSet(new_mask.get()).c_str(), (size_t)CPU_COUNT(new_mask.get()));
|
|
|
|
|
|
|
|
if (sched_setaffinity(0, CPU_ALLOC_SIZE(CPU_SETSIZE), new_mask.get()) == -1) {
|
|
|
|
PLOG_W("sched_setaffinity(0, mask_size=%zu, max_cpus=%zu) failed",
|
|
|
|
(size_t)CPU_ALLOC_SIZE(CPU_SETSIZE), nsjconf->max_cpus);
|
2017-06-19 23:01:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-10 00:41:16 +08:00
|
|
|
|
|
|
|
} // namespace cpu
|