-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix getting affinity set on MUSL on Jetson TX2 #206
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
The code in PAL_GetCurrentThreadAffinitySet relied on the fact that the number of processors reported as configured in the system is always larger than the maximum CPU index. However, it turns out that it is not true on some devices / distros. The Jetson TX2 reports CPUs 0, 3, 4 and 5 in the affinity mask and the 1 and 2 are never reported. GLIBC reports 6 as the number of configured CPUs, however MUSL reports just 4. The PAL_GetCurrentThreadAffinitySet was using the number of CPUs reported as configured as the upper bound for scanning affinity set, so on Jetson TX2, the affinity mask returned had just two bits set while there were 4 CPUs. That triggered an assert in the GCToOSInterface::Initialize. This change fixes that by reading the maximum CPU index from the /proc/cpuinfo. It falls back to using the number of processors configured when the /proc/cpuinfo is not available (on macOS, FreeBSD, ...)
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,7 +145,7 @@ DWORD | |
| PALAPI | ||
| PAL_GetLogicalCpuCountFromOS() | ||
| { | ||
| static int nrcpus = -1; | ||
| static volatile int nrcpus = -1; | ||
|
|
||
| if (nrcpus == -1) | ||
| { | ||
|
|
@@ -167,6 +167,53 @@ PAL_GetLogicalCpuCountFromOS() | |
| return nrcpus; | ||
| } | ||
|
|
||
| /*++ | ||
| Function: | ||
| GetMaxCpuIndex | ||
|
|
||
| The GetMaxCpuIndex function returns maximum index of CPU available on the system. On some systems, it doesn't match | ||
| the PAL_GetTotalCpuCount() - 1, as the CPU indices may form a noncontinuous range. E.g. on Jetson TX2, there are | ||
| four CPUs available and their indices are 0, 3, 4, 5. While glibc returns 6 as the number of CPUs configured, | ||
| MUSL returns 4. | ||
| --*/ | ||
| int GetMaxCpuIndex() | ||
| { | ||
| static volatile int maxCpuIndex = -1; | ||
|
|
||
| if (maxCpuIndex == -1) | ||
| { | ||
| FILE* cpuInfoFile = fopen("/proc/cpuinfo", "r"); | ||
| if (cpuInfoFile != NULL) | ||
| { | ||
| char *line = nullptr; | ||
| size_t lineLen = 0; | ||
|
|
||
| while (getline(&line, &lineLen, cpuInfoFile) != -1) | ||
|
||
| { | ||
| int cpuIndex; | ||
| int fieldsParsed = sscanf(line, "processor : %d", &cpuIndex); | ||
|
||
|
|
||
| if ((fieldsParsed == 1) && (cpuIndex > maxCpuIndex)) | ||
| { | ||
| maxCpuIndex = cpuIndex; | ||
| } | ||
| } | ||
|
|
||
| free(line); | ||
| fclose(cpuInfoFile); | ||
| } | ||
|
|
||
| if (maxCpuIndex == -1) | ||
| { | ||
| // Reading the /proc/cpuinfo has failed, return the total CPU count - 1 as a fallback as we don't | ||
| // have anything better | ||
| maxCpuIndex = PAL_GetTotalCpuCount() - 1; | ||
| } | ||
| } | ||
|
|
||
| return maxCpuIndex; | ||
| } | ||
|
|
||
| /*++ | ||
| Function: | ||
| GetSystemInfo | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2935,10 +2935,10 @@ PAL_GetCurrentThreadAffinitySet(SIZE_T size, UINT_PTR* data) | |
| if (st == 0) | ||
| { | ||
| const SIZE_T BitsPerBitsetEntry = 8 * sizeof(UINT_PTR); | ||
| int nrcpus = PAL_GetTotalCpuCount(); | ||
| int maxCpuIndex = GetMaxCpuIndex(); | ||
|
||
|
|
||
| // Get info for as much processors as it is possible to fit into the resulting set | ||
| SIZE_T remainingCount = std::min(size * BitsPerBitsetEntry, (SIZE_T)nrcpus); | ||
| SIZE_T remainingCount = std::min(size * BitsPerBitsetEntry, (SIZE_T)maxCpuIndex + 1); | ||
| SIZE_T i = 0; | ||
| while (remainingCount != 0) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be under
#ifdef __linux__or something like this. (procfsexists in other Unices butcpuinfois Linux-only.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is intentionally not like that. I try to avoid checking for specific platform if possible. This code is run just once and on non-linux platforms, it fails to open the file and uses the fallback path.