Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_physical_cpu_count api family #4302

Merged
merged 12 commits into from Oct 31, 2022
28 changes: 28 additions & 0 deletions src/cpu.cpp
Expand Up @@ -1362,6 +1362,27 @@ static int get_max_freq_khz(int cpuid)
return max_freq_khz;
}

static int get_thread_siblings_list_count(int cpuid)
{
char path[256];
sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpuid);

FILE* fp = fopen(path, "rb");
if (!fp)
return 1;

int count = 1;
while (!feof(fp))
{
if (fgetc(fp) == ',')
nihui marked this conversation as resolved.
Show resolved Hide resolved
count++;
}

fclose(fp);

return count;
}

static int set_sched_affinity(const CpuSet& thread_affinity_mask)
{
// set affinity for thread
Expand Down Expand Up @@ -1480,6 +1501,13 @@ static int setup_thread_affinity_masks()

for (int i = 0; i < g_cpucount; i++)
{
if (get_thread_siblings_list_count(i) > 1)
{
// always treat smt core as big core
g_thread_affinity_mask_big.enable(i);
continue;
}

if (cpu_max_freq_khz[i] < max_freq_khz_medium)
g_thread_affinity_mask_little.enable(i);
else
Expand Down