Skip to content

Commit

Permalink
[thread_pool] topology: Use sched_getaffinity on Android
Browse files Browse the repository at this point in the history
Previously, this was done with syscall(__NR_sched_getaffinity, ...) which
doesn't get the return value right.  (I saw 8 (num cpus) instead of 0.)

There doesn't seem to be a reason to use syscall(), so treat Android like
generic Linux.

PiperOrigin-RevId: 730373336
  • Loading branch information
jmr authored and copybara-github committed Feb 24, 2025
1 parent 4d6ad10 commit 173e82d
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions hwy/contrib/thread_pool/topology.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,20 @@ using CpuSet = cpuset_t;
// Helper functions reduce the number of #if in GetThreadAffinity.
int GetAffinity(CpuSet* set) {
// To specify the current thread, pass 0 on Linux/Android and -1 on FreeBSD.
#ifdef __ANDROID__
return syscall(__NR_sched_getaffinity, 0, sizeof(CpuSet), set);
#elif HWY_OS_FREEBSD
#if HWY_OS_FREEBSD
return cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(CpuSet),
set);
#else // normal Linux
#else // Linux
return sched_getaffinity(0, sizeof(CpuSet), set);
#endif
}

int SetAffinity(CpuSet* set) {
// To specify the current thread, pass 0 on Linux/Android and -1 on FreeBSD.
#ifdef __ANDROID__
return syscall(__NR_sched_setaffinity, 0, sizeof(CpuSet), set);
#elif HWY_OS_FREEBSD
#if HWY_OS_FREEBSD
return cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(CpuSet),
set);
#else // normal Linux
#else // Linux
return sched_setaffinity(0, sizeof(CpuSet), set);
#endif
}
Expand Down

0 comments on commit 173e82d

Please sign in to comment.