int get_nprocs() { int cpu_count = 1; FILE* fp = fopen("/sys/devices/system/cpu/online", "re"); if (fp != nullptr) { char* line = nullptr; size_t len = 0; if (getline(&line, &len, fp) != -1) { cpu_count = GetCpuCountFromString(line); free(line); } fclose(fp); } return cpu_count; }
TEST(UNISTD_TEST, get_cpu_count_from_string) { ASSERT_EQ(0, GetCpuCountFromString(" ")); ASSERT_EQ(1, GetCpuCountFromString("0")); ASSERT_EQ(40, GetCpuCountFromString("0-39")); ASSERT_EQ(4, GetCpuCountFromString("0, 1-2, 4\n")); }