Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 8c478a8

Browse files
janvorliAnipik
authored andcommitted
Port to 3.1 - Fix getting affinity set on MUSL on Jetson TX2 (#27957)
Ports dotnet/runtime#206 to release/3.1. 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, ...) Fixes dotnet/runtime#170
1 parent e5bbbe6 commit 8c478a8

File tree

3 files changed

+4
-5
lines changed

3 files changed

+4
-5
lines changed

src/gc/unix/gcenv.unix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ bool GCToOSInterface::Initialize()
326326

327327
if (st == 0)
328328
{
329-
for (size_t i = 0; i < g_totalCpuCount; i++)
329+
for (size_t i = 0; i < CPU_SETSIZE; i++)
330330
{
331331
if (CPU_ISSET(i, &cpuSet))
332332
{
@@ -1152,7 +1152,7 @@ bool GCToOSInterface::GetProcessorForHeap(uint16_t heap_number, uint16_t* proc_n
11521152
bool success = false;
11531153

11541154
uint16_t availableProcNumber = 0;
1155-
for (size_t procNumber = 0; procNumber < g_totalCpuCount; procNumber++)
1155+
for (size_t procNumber = 0; procNumber < MAX_SUPPORTED_CPUS; procNumber++)
11561156
{
11571157
if (g_processAffinitySet.Contains(procNumber))
11581158
{

src/pal/src/thread/thread.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,10 +3079,9 @@ PAL_GetCurrentThreadAffinitySet(SIZE_T size, UINT_PTR* data)
30793079
if (st == 0)
30803080
{
30813081
const SIZE_T BitsPerBitsetEntry = 8 * sizeof(UINT_PTR);
3082-
int nrcpus = PAL_GetTotalCpuCount();
30833082

30843083
// Get info for as much processors as it is possible to fit into the resulting set
3085-
SIZE_T remainingCount = std::min(size * BitsPerBitsetEntry, (SIZE_T)nrcpus);
3084+
SIZE_T remainingCount = std::min(size * BitsPerBitsetEntry, (SIZE_T)CPU_SETSIZE);
30863085
SIZE_T i = 0;
30873086
while (remainingCount != 0)
30883087
{

src/vm/gcenv.os.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ bool GCToOSInterface::GetProcessorForHeap(uint16_t heap_number, uint16_t* proc_n
10131013
// Locate heap_number-th available processor
10141014
uint16_t procIndex;
10151015
size_t cnt = heap_number;
1016-
for (uint16_t i = 0; i < GCToOSInterface::GetTotalProcessorCount(); i++)
1016+
for (uint16_t i = 0; i < MAX_SUPPORTED_CPUS; i++)
10171017
{
10181018
if (g_processAffinitySet.Contains(i))
10191019
{

0 commit comments

Comments
 (0)