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