Skip to content

Commit

Permalink
Update Environment.ProcessorCount on Windows to take into account t…
Browse files Browse the repository at this point in the history
…he processor affinity mask

Port of dotnet/runtime#45943

Delete some dead code
  • Loading branch information
jkotas committed Jan 27, 2021
1 parent 91e5c36 commit b0cbf15
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 169 deletions.
2 changes: 2 additions & 0 deletions src/coreclr/gc/gcload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ GC_Initialize(
// various components may want to query the current configuration.
GCConfig::Initialize();

#ifndef FEATURE_REDHAWK // GCToOSInterface is initialized directly
if (!GCToOSInterface::Initialize())
{
return E_FAIL;
}
#endif

IGCHandleManager* handleManager = CreateGCHandleManager();
if (handleManager == nullptr)
Expand Down
5 changes: 1 addition & 4 deletions src/coreclr/nativeaot/Runtime/PalRedhawk.h
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ EventDataDescCreate(_Out_ EVENT_DATA_DESCRIPTOR * EventDataDescriptor, _In_opt_
}
#endif // _EVNTPROV_H_

extern GCSystemInfo g_RhSystemInfo;
extern uint32_t g_RhNumberOfProcessors;

#ifdef TARGET_UNIX
#define REDHAWK_PALIMPORT extern "C"
Expand All @@ -570,8 +570,6 @@ extern GCSystemInfo g_RhSystemInfo;
#define REDHAWK_PALAPI __stdcall
#endif // TARGET_UNIX

bool InitializeSystemInfo();

#ifndef DACCESS_COMPILE

#ifdef _DEBUG
Expand Down Expand Up @@ -679,7 +677,6 @@ REDHAWK_PALIMPORT void REDHAWK_PALAPI PalSleep(uint32_t milliseconds);
REDHAWK_PALIMPORT UInt32_BOOL REDHAWK_PALAPI PalSwitchToThread();
REDHAWK_PALIMPORT HANDLE REDHAWK_PALAPI PalCreateEventW(_In_opt_ LPSECURITY_ATTRIBUTES pEventAttributes, UInt32_BOOL manualReset, UInt32_BOOL initialState, _In_opt_z_ LPCWSTR pName);
REDHAWK_PALIMPORT uint64_t REDHAWK_PALAPI PalGetTickCount64();
REDHAWK_PALIMPORT HANDLE REDHAWK_PALAPI PalCreateLowMemoryNotification();
REDHAWK_PALIMPORT void REDHAWK_PALAPI PalTerminateCurrentProcess(uint32_t exitCode);
REDHAWK_PALIMPORT HANDLE REDHAWK_PALAPI PalGetModuleHandleFromPointer(_In_ void* pointer);

Expand Down
12 changes: 0 additions & 12 deletions src/coreclr/nativeaot/Runtime/PalRedhawkFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,6 @@ inline void PalRaiseException(uint32_t arg1, uint32_t arg2, uint32_t arg3, const
RaiseException(arg1, arg2, arg3, arg4);
}

extern "C" UInt32_BOOL __stdcall ReleaseMutex(HANDLE);
inline UInt32_BOOL PalReleaseMutex(HANDLE arg1)
{
return ReleaseMutex(arg1);
}

extern "C" UInt32_BOOL __stdcall ResetEvent(HANDLE);
inline UInt32_BOOL PalResetEvent(HANDLE arg1)
{
Expand All @@ -159,12 +153,6 @@ inline UInt32_BOOL PalSetEvent(HANDLE arg1)
return SetEvent(arg1);
}

extern "C" void __stdcall TerminateProcess(HANDLE, uint32_t);
inline void PalTerminateProcess(HANDLE arg1, uint32_t arg2)
{
TerminateProcess(arg1, arg2);
}

extern "C" uint32_t __stdcall WaitForSingleObjectEx(HANDLE, uint32_t, UInt32_BOOL);
inline uint32_t PalWaitForSingleObjectEx(HANDLE arg1, uint32_t arg2, UInt32_BOOL arg3)
{
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/nativeaot/Runtime/RWLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void ReaderWriterLock::AcquireReadLockWorker()
if (TryAcquireReadLock())
return;

if (g_RhSystemInfo.dwNumberOfProcessors <= 1)
if (g_RhNumberOfProcessors <= 1)
break;

// Delay by approximately 2*i clock cycles (Pentium III).
Expand Down Expand Up @@ -241,7 +241,7 @@ void ReaderWriterLock::AcquireWriteLock()
RedhawkGCInterface::WaitForGCCompletion();
}

if (g_RhSystemInfo.dwNumberOfProcessors <= 1)
if (g_RhNumberOfProcessors <= 1)
{
break;
}
Expand Down
13 changes: 0 additions & 13 deletions src/coreclr/nativeaot/Runtime/arm/StubDispatch.S
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@

#ifdef FEATURE_CACHED_INTERFACE_DISPATCH

// TODO: Implement Arm support
#ifdef _DEBUG
.rodata
AssertMsg: .asciz "__FILE__:%s: %s is not implemented\n"
FileName: .asciz "StubDispatch.S"
RhpVTableOffsetDispatchName: .asciz "RhpVTableOffsetDispatch"
.text
.macro GEN_ASSERT_FUNC func
GEN_ASSERT AssertMsg, FileName, \func
.endm
#endif


// Macro that generates a stub consuming a cache with the given number of entries.
.macro DEFINE_INTERFACE_DISPATCH_STUB entries

Expand Down
6 changes: 4 additions & 2 deletions src/coreclr/nativeaot/Runtime/gcrhenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,14 @@ bool RedhawkGCInterface::InitializeSubsystems()
MICROSOFT_WINDOWS_REDHAWK_GC_PUBLIC_PROVIDER_Context.RegistrationHandle = Microsoft_Windows_Redhawk_GC_PublicHandle;
#endif // FEATURE_ETW

if (!InitializeSystemInfo())
if (!GCToOSInterface::Initialize())
{
return false;
}

// Use the same adjustment for current processor count as GC
g_RhNumberOfProcessors = GCToOSInterface::GetCurrentProcessCpuCount();

// Initialize the special EEType used to mark free list entries in the GC heap.
g_FreeObjectEEType.InitializeAsGcFreeType();
g_pFreeObjectEEType = &g_FreeObjectEEType;
Expand All @@ -188,7 +191,6 @@ bool RedhawkGCInterface::InitializeSubsystems()
return false;

#ifdef FEATURE_SVR_GC
// TODO: This should use the logical CPU count adjusted for process affinity and cgroup limits
g_heap_type = (g_pRhConfig->GetUseServerGC() && PalGetProcessCpuCount() > 1) ? GC_HEAP_SVR : GC_HEAP_WKS;
#else
g_heap_type = GC_HEAP_WKS;
Expand Down
15 changes: 1 addition & 14 deletions src/coreclr/nativeaot/Runtime/rhassert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void Assert(const char * expr, const char * file, uint32_t line_num, const char
switch (result)
{
case IDABORT:
PalTerminateProcess(PalGetCurrentProcess(), 666);
PalTerminateCurrentProcess(42);
break;
case IDRETRY:
PalDebugBreak();
Expand All @@ -94,17 +94,4 @@ void Assert(const char * expr, const char * file, uint32_t line_num, const char
#endif //!DACCESS_COMPILE
}

extern "C" void NYI_Assert(const char *message, ...)
{
#if !defined(DACCESS_COMPILE)
va_list args;
va_start(args, message);
vprintf(message, args);
va_end(args);
ASSERT_UNCONDITIONALLY("NYI");
#else
UNREFERENCED_PARAMETER(message);
#endif
}

#endif // _DEBUG
6 changes: 0 additions & 6 deletions src/coreclr/nativeaot/Runtime/rhassert.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ void Assert(const char * expr, const char * file, unsigned int line_num, const c
#define _ASSERTE(_expr) ASSERT(_expr)
#endif

#if defined(_DEBUG)

void NYI_ASSERT();

#endif

#define PORTABILITY_ASSERT(message) \
ASSERT_UNCONDITIONALLY(message); \
ASSUME(0); \
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/nativeaot/Runtime/threadstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void ThreadStore::SuspendAllThreads(bool waitForGCEvent, bool fireDebugEvent)

if (keepWaiting)
{
if (PalSwitchToThread() == 0 && g_RhSystemInfo.dwNumberOfProcessors > 1)
if (PalSwitchToThread() == 0 && g_RhNumberOfProcessors > 1)
{
// No threads are scheduled on this processor. Perhaps we're waiting for a thread
// that's scheduled on another processor. If so, let's give it a little time
Expand Down
81 changes: 4 additions & 77 deletions src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ static const int tccMilliSecondsToMicroSeconds = 1000;
static const int tccMilliSecondsToNanoSeconds = 1000000;
static const int tccMicroSecondsToNanoSeconds = 1000;

static uint32_t g_cNumProcs = 0;

bool QueryLogicalProcessorCount();

extern "C" void RaiseFailFastException(PEXCEPTION_RECORD arg1, PCONTEXT arg2, uint32_t arg3)
{
// Abort aborts the process and causes creation of a crash dump
Expand Down Expand Up @@ -401,9 +397,6 @@ void ConfigureSignals()
// initialization and false on failure.
REDHAWK_PALEXPORT bool REDHAWK_PALAPI PalInit()
{
if (!QueryLogicalProcessorCount())
return false;

#ifndef USE_PORTABLE_HELPERS
if (!InitializeHardwareExceptionHandling())
{
Expand Down Expand Up @@ -705,30 +698,6 @@ REDHAWK_PALEXPORT void PalPrintFatalError(const char* message)
write(STDERR_FILENO, message, sizeof(message));
}

bool QueryLogicalProcessorCount()
{
#if HAVE_SYSCONF
g_cNumProcs = sysconf(SYSCONF_GET_NUMPROCS);
if (g_cNumProcs < 1)
{
ASSERT_UNCONDITIONALLY("sysconf failed for " SYSCONF_GET_NUMPROCS_NAME "\n");
return false;
}
#elif HAVE_SYSCTL
size_t sz = sizeof(g_cNumProcs);

int st = 0;
if (sysctlbyname("hw.logicalcpu_max", &g_cNumProcs, &sz, NULL, 0) != 0)
{
ASSERT_UNCONDITIONALLY("sysctl failed for hw.logicalcpu_max\n");
return false;
}

#endif // HAVE_SYSCONF

return true;
}

static int W32toUnixAccessControl(uint32_t flProtect)
{
int prot = 0;
Expand Down Expand Up @@ -904,14 +873,6 @@ extern "C" UInt32_BOOL IsDebuggerPresent()
#endif
}

extern "C" void TerminateProcess(HANDLE arg1, uint32_t arg2)
{
// TODO: change it to TerminateCurrentProcess
// Then if we modified the signature of the DuplicateHandle too, we can
// get rid of the PalGetCurrentProcess.
PORTABILITY_ASSERT("UNIXTODO: Implement this function");
}

extern "C" UInt32_BOOL SetEvent(HANDLE event)
{
EventUnixHandle* unixHandle = (EventUnixHandle*)event;
Expand Down Expand Up @@ -1002,9 +963,12 @@ extern "C" int32_t _stricmp(const char *string1, const char *string2)
return strcasecmp(string1, string2);
}

uint32_t g_RhNumberOfProcessors;

REDHAWK_PALEXPORT int32_t PalGetProcessCpuCount()
{
return g_cNumProcs;
ASSERT(g_RhNumberOfProcessors > 0);
return g_RhNumberOfProcessors;
}

__thread void* pStackHighOut = NULL;
Expand Down Expand Up @@ -1081,43 +1045,6 @@ REDHAWK_PALEXPORT int32_t PalGetModuleFileName(_Out_ const TCHAR** pModuleNameOu
#endif // defined(HOST_WASM)
}

GCSystemInfo g_RhSystemInfo;

// Initialize the g_SystemInfo
bool InitializeSystemInfo()
{
long pagesize = getpagesize();
g_RhSystemInfo.dwPageSize = pagesize;
g_RhSystemInfo.dwAllocationGranularity = pagesize;

int nrcpus = 0;

#if HAVE_SYSCONF
nrcpus = sysconf(SYSCONF_GET_NUMPROCS);
if (nrcpus < 1)
{
ASSERT_UNCONDITIONALLY("sysconf failed for " SYSCONF_GET_NUMPROCS_NAME "\n");
return false;
}
#elif HAVE_SYSCTL
int mib[2];

size_t sz = sizeof(nrcpus);
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
int rc = sysctl(mib, 2, &nrcpus, &sz, NULL, 0);
if (rc != 0)
{
ASSERT_UNCONDITIONALLY("sysctl failed for HW_NCPU\n");
return false;
}
#endif // HAVE_SYSCONF

g_RhSystemInfo.dwNumberOfProcessors = nrcpus;

return true;
}

extern "C" void FlushProcessWriteBuffers()
{
GCToOSInterface::FlushProcessWriteBuffers();
Expand Down
9 changes: 0 additions & 9 deletions src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm.inc
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,6 @@ C_FUNC(\Name):
EPILOG_STACK_FREE 4 // Discard caller's SP
.endm

#ifdef _DEBUG
.macro GEN_ASSERT message, fileName, funcName
ldr r0, =\message
ldr r1, =\fileName
ldr r2, =\funcName
bl C_FUNC(NYI_Assert)
.endm
#endif

// thumb with PIC version
.macro INLINE_GET_TLS_VAR Var
ldr r0, 2f
Expand Down
17 changes: 4 additions & 13 deletions src/coreclr/nativeaot/Runtime/windows/PalRedhawkCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,12 @@ REDHAWK_PALEXPORT void REDHAWK_PALAPI PalGetPDBInfo(HANDLE hOsHandle, _Out_ GUID
}
}

uint32_t g_RhNumberOfProcessors;

REDHAWK_PALEXPORT int32_t REDHAWK_PALAPI PalGetProcessCpuCount()
{
static int CpuCount = 0;

if (CpuCount != 0)
return CpuCount;
else
{
// The concept of process CPU affinity is going away and so CoreSystem obsoletes the APIs used to
// fetch this information. Instead we'll just return total cpu count.
SYSTEM_INFO sysInfo;
::GetSystemInfo(&sysInfo);
CpuCount = sysInfo.dwNumberOfProcessors;
return sysInfo.dwNumberOfProcessors;
}
ASSERT(g_RhNumberOfProcessors > 0);
return g_RhNumberOfProcessors;
}

// Retrieves the entire range of memory dedicated to the calling thread's stack. This does
Expand Down
18 changes: 2 additions & 16 deletions src/coreclr/nativeaot/Runtime/windows/PalRedhawkMinWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ uint32_t PalEventWrite(REGHANDLE arg1, const EVENT_DESCRIPTOR * arg2, uint32_t a
// Index for the fiber local storage of the attached thread pointer
static uint32_t g_flsIndex = FLS_OUT_OF_INDEXES;

GCSystemInfo g_RhSystemInfo;

bool InitializeSystemInfo()
{
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);

g_RhSystemInfo.dwNumberOfProcessors = systemInfo.dwNumberOfProcessors;
g_RhSystemInfo.dwPageSize = systemInfo.dwPageSize;
g_RhSystemInfo.dwAllocationGranularity = systemInfo.dwAllocationGranularity;

return true;
}

// This is called when each *fiber* is destroyed. When the home fiber of a thread is destroyed,
// it means that the thread itself is destroyed.
// Since we receive that notification outside of the Loader Lock, it allows us to safely acquire
Expand Down Expand Up @@ -380,9 +366,9 @@ REDHAWK_PALEXPORT bool REDHAWK_PALAPI PalEventEnabled(REGHANDLE regHandle, _In_
return !!EventEnabled(regHandle, eventDescriptor);
}

REDHAWK_PALEXPORT HANDLE REDHAWK_PALAPI PalCreateLowMemoryNotification()
REDHAWK_PALEXPORT void REDHAWK_PALAPI PalTerminateCurrentProcess(uint32_t arg2)
{
return CreateMemoryResourceNotification(LowMemoryResourceNotification);
TerminateProcess(GetCurrentProcess(), arg2);
}

REDHAWK_PALEXPORT HANDLE REDHAWK_PALAPI PalGetModuleHandleFromPointer(_In_ void* pointer)
Expand Down

0 comments on commit b0cbf15

Please sign in to comment.