Skip to content

Commit

Permalink
statically linking GC PAL on linux (#76985)
Browse files Browse the repository at this point in the history
* statically linking GC PAL

The GC PAL will be used for both coreclr and standalone GC on linux

* fixing arm64 and nativeaot build breaks

* macos build break and reducing renaming.

* trying to remove numa support from PAL

* one more rename to resolve MacOS break

* delete pal numa code.

* Adding missing madvise in GC PAL

* added missing MADV_DONTDUMP calls.

* CR feedback

* undo (long long) cast in GetMemoryStatus

* only invoke madvise on success.
  • Loading branch information
mangod9 committed Oct 25, 2022
1 parent 9bed84a commit c4341d4
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 354 deletions.
3 changes: 0 additions & 3 deletions src/coreclr/dlls/mscordac/mscordac_unixexports.src
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ nativeStringResourceTable_mscorrc
#PAL_free
#PAL_GetLogicalCpuCountFromOS
#PAL_GetTotalCpuCount
#PAL_GetNumaProcessorNode
#PAL_GetUnwindInfoSize
#PAL_get_stdout
#PAL_get_stderr
Expand Down Expand Up @@ -122,7 +121,6 @@ nativeStringResourceTable_mscorrc
#GetFullPathNameW
#GetLastError
#GetModuleFileNameW
#GetNumaHighestNodeNumber
#GetProcAddress
#GetStdHandle
#GetSystemInfo
Expand Down Expand Up @@ -163,7 +161,6 @@ nativeStringResourceTable_mscorrc
#SwitchToThread
#TerminateProcess
#VirtualAlloc
#VirtualAllocExNuma
#VirtualFree
#VirtualProtect
#VirtualQuery
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ set_property(TARGET coreclr APPEND_STRING PROPERTY LINK_DEPENDS ${EXPORTS_FILE})

if (CLR_CMAKE_HOST_UNIX)
set(LIB_UNWINDER unwinder_wks)
set(GC_PAL gc_unix)
endif (CLR_CMAKE_HOST_UNIX)

# IMPORTANT! Please do not rearrange the order of the libraries. The linker on Linux is
Expand All @@ -108,6 +109,7 @@ set(CORECLR_LIBRARIES
System.Globalization.Native-Static
interop
coreclrminipal
${GC_PAL}
)

if(CLR_CMAKE_TARGET_WIN32)
Expand Down
8 changes: 3 additions & 5 deletions src/coreclr/gc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ set(GC_SOURCES
handletablecache.cpp)

if(CLR_CMAKE_HOST_UNIX)
add_subdirectory(unix)
include(unix/configure.cmake)
set (GC_SOURCES
${GC_SOURCES}
unix/gcenv.unix.cpp
unix/events.cpp
unix/cgroup.cpp)
${GC_SOURCES})
else()
set (GC_SOURCES
${GC_SOURCES}
Expand Down Expand Up @@ -101,7 +99,7 @@ if(CLR_CMAKE_HOST_WIN32)
kernel32.lib
advapi32.lib)
else()
set (GC_LINK_LIBRARIES)
set (GC_LINK_LIBRARIES gc_unix)
endif(CLR_CMAKE_HOST_WIN32)

list(APPEND GC_SOURCES ${GC_HEADERS})
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/gc/unix/cgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Module Name:

extern bool ReadMemoryValueFromFile(const char* filename, uint64_t* val);

namespace
{
class CGroup
{
// the cgroup version number or 0 to indicate cgroups are not found or not enabled
Expand Down Expand Up @@ -453,6 +455,7 @@ class CGroup
return foundInactiveFileValue;
}
};
}

int CGroup::s_cgroup_version = 0;
char *CGroup::s_memory_cgroup_path = nullptr;
Expand Down
32 changes: 31 additions & 1 deletion src/coreclr/gc/unix/gcenv.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,10 @@ static void* VirtualReserveInner(size_t size, size_t alignment, uint32_t flags,
}

pRetVal = pAlignedRetVal;
#ifdef MADV_DONTDUMP
// Do not include reserved memory in coredump.
madvise(pRetVal, size, MADV_DONTDUMP);
#endif
}

return pRetVal;
Expand Down Expand Up @@ -724,6 +728,14 @@ bool GCToOSInterface::VirtualCommit(void* address, size_t size, uint16_t node)
{
bool success = mprotect(address, size, PROT_WRITE | PROT_READ) == 0;

#ifdef MADV_DODUMP
if (success)
{
// Include committed memory in coredump.
madvise(address, size, MADV_DODUMP);
}
#endif

#if HAVE_NUMA_H
if (success && g_numaAvailable && (node != NUMA_NODE_UNDEFINED))
{
Expand Down Expand Up @@ -760,7 +772,17 @@ bool GCToOSInterface::VirtualDecommit(void* address, size_t size)
// that much more clear to the operating system that we no
// longer need these pages. Also, GC depends on re-committed pages to
// be zeroed-out.
return mmap(address, size, PROT_NONE, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0) != NULL;
bool bRetVal = mmap(address, size, PROT_NONE, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0) != MAP_FAILED;

#ifdef MADV_DONTDUMP
if (bRetVal)
{
// Do not include freed memory in coredump.
madvise(address, size, MADV_DONTDUMP);
}
#endif

return bRetVal;
}

// Reset virtual memory range. Indicates that data in the memory range specified by address and size is no
Expand Down Expand Up @@ -791,6 +813,14 @@ bool GCToOSInterface::VirtualReset(void * address, size_t size, bool unlock)
#endif
}

#ifdef MADV_DONTDUMP
if (st == 0)
{
// Do not include reset memory in coredump.
madvise(address, size, MADV_DONTDUMP);
}
#endif

return (st == 0);
}

Expand Down
1 change: 0 additions & 1 deletion src/coreclr/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ set(SOURCES
misc/sysinfo.cpp
misc/time.cpp
misc/utils.cpp
numa/numa.cpp
objmgr/palobjbase.cpp
objmgr/shmobject.cpp
objmgr/shmobjectmanager.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/pal/src/include/pal/palinternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ typedef enum _TimeConversionConstants
}

bool
ReadMemoryValueFromFile(const char* filename, uint64_t* val);
PAL_ReadMemoryValueFromFile(const char* filename, uint64_t* val);

#ifdef __APPLE__
bool
Expand Down
7 changes: 0 additions & 7 deletions src/coreclr/pal/src/init/pal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,13 +673,6 @@ Initialize(
goto CLEANUP15;
}

if (FALSE == NUMASupportInitialize())
{
ERROR("Unable to initialize NUMA support\n");
palError = ERROR_PALINIT_NUMA;
goto CLEANUP15;
}

TRACE("First-time PAL initialization complete.\n");
init_count++;

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/pal/src/misc/cgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ class CGroup

static bool ReadMemoryValueFromFile(const char* filename, uint64_t* val)
{
return ::ReadMemoryValueFromFile(filename, val);
return ::PAL_ReadMemoryValueFromFile(filename, val);
}

static bool GetCGroup1CpuLimit(UINT *val)
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/pal/src/misc/sysinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ PAL_HasGetCurrentProcessorNumber()
}

bool
ReadMemoryValueFromFile(const char* filename, uint64_t* val)
PAL_ReadMemoryValueFromFile(const char* filename, uint64_t* val)
{
bool result = false;
char *line = nullptr;
Expand Down Expand Up @@ -585,11 +585,11 @@ PAL_GetLogicalProcessorCacheSizeFromOS()
{
path_to_size_file[index] = (char)(48 + i);

if (ReadMemoryValueFromFile(path_to_size_file, &size))
if (PAL_ReadMemoryValueFromFile(path_to_size_file, &size))
{
path_to_level_file[index] = (char)(48 + i);

if (ReadMemoryValueFromFile(path_to_level_file, &level))
if (PAL_ReadMemoryValueFromFile(path_to_level_file, &level))
{
UPDATE_CACHE_SIZE_AND_LEVEL(size, level)
}
Expand Down
Loading

0 comments on commit c4341d4

Please sign in to comment.