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-09 06:03:20 +08:00
|
|
|
static const std::string listCpusInSet(cpu_set_t* mask) {
|
|
|
|
std::string ret;
|
2022-08-05 14:43:39 +08:00
|
|
|
for (size_t i = 0; i < CPU_SETSIZE; i++) {
|
|
|
|
if (CPU_ISSET(i, mask)) {
|
2022-08-09 06:03:20 +08:00
|
|
|
if (!ret.empty()) {
|
2022-08-06 01:15:18 +08:00
|
|
|
ret.append(",");
|
|
|
|
}
|
2022-08-05 14:43:39 +08:00
|
|
|
ret.append(std::to_string(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-09 06:03:20 +08:00
|
|
|
LOG_F("No CPU #%zu found, yet there should be %zu left in the mask [%s]", n,
|
2022-08-06 01:15:18 +08:00
|
|
|
(size_t)CPU_COUNT(mask), listCpusInSet(mask).c_str());
|
2022-08-05 01:22:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-06-19 23:01:50 +08:00
|
|
|
|
2022-08-09 22:13:03 +08:00
|
|
|
static void setRandomCpu(cpu_set_t* orig_mask, cpu_set_t* new_mask, size_t available_cpus) {
|
|
|
|
if (available_cpus == 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-09 22:13:03 +08:00
|
|
|
size_t n = util::rnd64() % available_cpus;
|
2022-08-06 01:15:18 +08:00
|
|
|
n = getNthOnlineCpu(orig_mask, n);
|
2022-08-05 14:43:39 +08:00
|
|
|
|
|
|
|
CPU_SET(n, new_mask);
|
2022-08-09 06:03:20 +08:00
|
|
|
LOG_D("Add CPU #%zu from the original set of [%s] (size=%zu), new mask [%s] (size=%zu)", n,
|
2022-08-06 01:15:18 +08:00
|
|
|
listCpusInSet(orig_mask).c_str(), (size_t)CPU_COUNT(orig_mask),
|
|
|
|
listCpusInSet(new_mask).c_str(), (size_t)CPU_COUNT(new_mask));
|
|
|
|
CPU_CLR(n, orig_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-06 01:15:18 +08:00
|
|
|
std::unique_ptr<cpu_set_t> orig_mask(new cpu_set_t);
|
|
|
|
if (!orig_mask) {
|
2022-08-05 14:43:39 +08:00
|
|
|
PLOG_W("Failure allocating cpu_set_t");
|
2017-06-19 23:01:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-06 01:15:18 +08:00
|
|
|
if (sched_getaffinity(0, CPU_ALLOC_SIZE(CPU_SETSIZE), orig_mask.get()) == -1) {
|
2022-08-05 14:43:39 +08:00
|
|
|
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-06 01:15:18 +08:00
|
|
|
size_t available_cpus = CPU_COUNT(orig_mask.get());
|
2022-08-05 14:43:39 +08:00
|
|
|
|
2022-08-09 06:03:20 +08:00
|
|
|
LOG_D("Original CPU set: [%s], with %zu allowed CPUs",
|
|
|
|
listCpusInSet(orig_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-09 22:13:03 +08:00
|
|
|
setRandomCpu(orig_mask.get(), new_mask.get(), available_cpus);
|
|
|
|
available_cpus--;
|
2017-06-19 23:01:50 +08:00
|
|
|
}
|
|
|
|
|
2022-08-09 22:13:03 +08:00
|
|
|
LOG_D(
|
|
|
|
"Setting new CPU mask: [%s] with %zu allowed CPUs (max_cpus=%zu), %zu (CPU_COUNT=%zu) "
|
|
|
|
"left",
|
|
|
|
listCpusInSet(new_mask.get()).c_str(), nsjconf->max_cpus,
|
|
|
|
(size_t)CPU_COUNT(new_mask.get()), available_cpus, (size_t)CPU_COUNT(orig_mask.get()));
|
2022-08-05 14:43:39 +08:00
|
|
|
|
|
|
|
if (sched_setaffinity(0, CPU_ALLOC_SIZE(CPU_SETSIZE), new_mask.get()) == -1) {
|
2022-08-09 22:13:03 +08:00
|
|
|
PLOG_W("sched_setaffinity(0, mask_size=%zu, max_cpus=%zu (CPU_COUNT=%zu)) failed",
|
|
|
|
(size_t)CPU_ALLOC_SIZE(CPU_SETSIZE), nsjconf->max_cpus,
|
|
|
|
(size_t)CPU_COUNT(new_mask.get()));
|
2017-06-19 23:01:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-10 00:41:16 +08:00
|
|
|
|
|
|
|
} // namespace cpu
|