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 01:22:33 +08:00
|
|
|
static size_t getNthCpu(cpu_set_t* mask, size_t mask_size, size_t n, size_t cpus_left) {
|
|
|
|
for (size_t i = 0, j = 0; i < CPU_SETSIZE; i++) {
|
|
|
|
if (CPU_ISSET_S(i, mask_size, mask)) {
|
|
|
|
if (j == n) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
2017-06-19 23:01:50 +08:00
|
|
|
}
|
2022-08-05 01:22:33 +08:00
|
|
|
LOG_F("No CPU #%zu found, yet there should be %zu left", n,
|
|
|
|
(size_t)CPU_COUNT_S(mask_size, mask));
|
|
|
|
return 0;
|
|
|
|
}
|
2017-06-19 23:01:50 +08:00
|
|
|
|
2022-08-05 01:22:33 +08:00
|
|
|
static void setRandomCpu(cpu_set_t* original_mask, cpu_set_t* new_mask, size_t mask_size) {
|
|
|
|
size_t cpus_left = CPU_COUNT_S(mask_size, original_mask);
|
|
|
|
if (cpus_left == 0) {
|
|
|
|
LOG_F("There are no more CPUs left to use");
|
2017-06-19 23:01:50 +08:00
|
|
|
}
|
2022-08-05 01:22:33 +08:00
|
|
|
|
|
|
|
size_t n = getNthCpu(original_mask, mask_size, util::rnd64() % cpus_left, cpus_left);
|
|
|
|
LOG_D("Setting allowed CPU#:%" PRIu64, n);
|
|
|
|
CPU_SET_S(n, mask_size, new_mask);
|
|
|
|
CPU_CLR_S(n, mask_size, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t mask_size = CPU_ALLOC_SIZE(CPU_SETSIZE);
|
|
|
|
std::unique_ptr<cpu_set_t> original_mask(CPU_ALLOC(CPU_SETSIZE));
|
|
|
|
if (original_mask.get() == NULL) {
|
|
|
|
PLOG_W("Failure allocating cpu_set_t for %d CPUs", CPU_SETSIZE);
|
2017-06-19 23:01:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-05 01:22:33 +08:00
|
|
|
if (sched_getaffinity(0, mask_size, original_mask.get()) == -1) {
|
|
|
|
PLOG_W("sched_getaffinity(0, mask_size=%zu)", mask_size);
|
|
|
|
return false;
|
2017-06-19 23:01:50 +08:00
|
|
|
}
|
2022-08-05 01:22:33 +08:00
|
|
|
size_t available_cpus = CPU_COUNT_S(mask_size, original_mask.get());
|
|
|
|
|
|
|
|
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 01:22:33 +08:00
|
|
|
std::unique_ptr<cpu_set_t> new_mask(CPU_ALLOC(CPU_SETSIZE));
|
|
|
|
if (new_mask.get() == NULL) {
|
|
|
|
PLOG_W("Failure allocating cpu_set_t for %d CPUs", CPU_SETSIZE);
|
2017-06-19 23:01:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-05 01:22:33 +08:00
|
|
|
CPU_ZERO_S(mask_size, 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 01:22:33 +08:00
|
|
|
setRandomCpu(original_mask.get(), new_mask.get(), mask_size);
|
2017-06-19 23:01:50 +08:00
|
|
|
}
|
|
|
|
|
2022-08-05 01:22:33 +08:00
|
|
|
if (sched_setaffinity(0, mask_size, new_mask.get()) == -1) {
|
2017-06-21 23:52:16 +08:00
|
|
|
PLOG_W("sched_setaffinity(max_cpus=%zu) failed", 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
|