|
|
@ -46,23 +46,31 @@ namespace testing { |
|
|
|
#define _GNU_SOURCE |
|
|
|
#define _GNU_SOURCE |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
#include <sched.h> |
|
|
|
#include <sched.h> |
|
|
|
int LimitCores(std::vector<int> cores) { |
|
|
|
int LimitCores(const int *cores, int cores_size) { |
|
|
|
size_t num_cores = static_cast<size_t>(gpr_cpu_num_cores()); |
|
|
|
int num_cores = gpr_cpu_num_cores(); |
|
|
|
if (num_cores > cores.size()) { |
|
|
|
int cores_set = 0; |
|
|
|
cpu_set_t *cpup = CPU_ALLOC(num_cores); |
|
|
|
|
|
|
|
GPR_ASSERT(cpup); |
|
|
|
|
|
|
|
size_t size = CPU_ALLOC_SIZE(num_cores); |
|
|
|
|
|
|
|
CPU_ZERO_S(size, cpup); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < cores.size(); i++) { |
|
|
|
cpu_set_t *cpup = CPU_ALLOC(num_cores); |
|
|
|
CPU_SET_S(cores[i], size, cpup); |
|
|
|
GPR_ASSERT(cpup); |
|
|
|
|
|
|
|
size_t size = CPU_ALLOC_SIZE(num_cores); |
|
|
|
|
|
|
|
CPU_ZERO_S(size, cpup); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (cores_size > 0) { |
|
|
|
|
|
|
|
for (int i = 0; i < cores_size; i++) { |
|
|
|
|
|
|
|
if (cores[i] < num_cores) { |
|
|
|
|
|
|
|
CPU_SET_S(cores[i], size, cpup); |
|
|
|
|
|
|
|
cores_set++; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
GPR_ASSERT(sched_setaffinity(0, size, cpup) == 0); |
|
|
|
|
|
|
|
CPU_FREE(cpup); |
|
|
|
|
|
|
|
return cores.size(); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return num_cores; |
|
|
|
for (int i = 0; i < num_cores; i++) { |
|
|
|
|
|
|
|
CPU_SET_S(i, size, cpup); |
|
|
|
|
|
|
|
cores_set++; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
GPR_ASSERT(sched_setaffinity(0, size, cpup) == 0); |
|
|
|
|
|
|
|
CPU_FREE(cpup); |
|
|
|
|
|
|
|
return cores_set; |
|
|
|
} |
|
|
|
} |
|
|
|
#else |
|
|
|
#else |
|
|
|
// LimitCores is not currently supported for non-Linux platforms
|
|
|
|
// LimitCores is not currently supported for non-Linux platforms
|
|
|
|