2017-06-19 23:01:50 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
nsjail - CLONE_NEWUTS routines
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
Copyright 2014 Google Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void cpuSetRandomCpu(cpu_set_t * mask, size_t mask_size, size_t cpu_num)
|
|
|
|
{
|
|
|
|
if ((size_t) CPU_COUNT_S(mask_size, mask) >= cpu_num) {
|
|
|
|
LOG_F
|
|
|
|
("Number of CPUs in the mask '%d' is bigger than number of available CPUs '%zu'",
|
|
|
|
CPU_COUNT(mask), cpu_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
uint64_t n = utilRnd64() % cpu_num;
|
|
|
|
if (!CPU_ISSET_S(n, mask_size, mask)) {
|
|
|
|
CPU_SET_S(n, mask_size, mask);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cpuInit(struct nsjconf_t *nsjconf)
|
|
|
|
{
|
|
|
|
long all_cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
if (all_cpus < 0) {
|
|
|
|
PLOG_W("sysconf(_SC_NPROCESSORS_ONLN) returned %ld", all_cpus);
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-21 23:52:16 +08:00
|
|
|
if (nsjconf->max_cpus >= (size_t) all_cpus) {
|
2017-06-20 01:11:53 +08:00
|
|
|
LOG_W("Requested number of CPUs:%zu is bigger than CPUs online:%ld",
|
2017-06-21 23:52:16 +08:00
|
|
|
nsjconf->max_cpus, all_cpus);
|
2017-06-19 23:01:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
2017-06-21 23:52:16 +08:00
|
|
|
if (nsjconf->max_cpus == 0) {
|
|
|
|
LOG_D("No max_cpus limit set");
|
2017-06-19 23:01:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu_set_t *mask = CPU_ALLOC(all_cpus);
|
|
|
|
if (mask == NULL) {
|
|
|
|
PLOG_W("Failure allocating cpu_set_t for %ld CPUs", all_cpus);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t mask_size = CPU_ALLOC_SIZE(all_cpus);
|
|
|
|
CPU_ZERO_S(mask_size, mask);
|
|
|
|
|
2017-06-21 23:52:16 +08:00
|
|
|
for (size_t i = 0; i < nsjconf->max_cpus; i++) {
|
2017-06-19 23:01:50 +08:00
|
|
|
cpuSetRandomCpu(mask, mask_size, all_cpus);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sched_setaffinity(0, mask_size, mask) == -1) {
|
2017-06-21 23:52:16 +08:00
|
|
|
PLOG_W("sched_setaffinity(max_cpus=%zu) failed", nsjconf->max_cpus);
|
2017-06-19 23:07:50 +08:00
|
|
|
CPU_FREE(mask);
|
2017-06-19 23:01:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
2017-06-19 23:07:50 +08:00
|
|
|
CPU_FREE(mask);
|
2017-06-19 23:01:50 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|