From d3ae2bc976fcbba8a8063ce0fea23392bfc831f6 Mon Sep 17 00:00:00 2001 From: Thomas Schrott Date: Wed, 31 Jul 2024 14:22:32 +0200 Subject: [PATCH 1/3] Always use and link libcontainer --- .../oracle/svm/core/container/ContainerLibrary.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/container/ContainerLibrary.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/container/ContainerLibrary.java index de92b545d852..3e7a8ec8496e 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/container/ContainerLibrary.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/container/ContainerLibrary.java @@ -32,7 +32,6 @@ import org.graalvm.nativeimage.c.function.CLibrary; import org.graalvm.word.UnsignedWord; -import com.oracle.svm.core.SubstrateOptions; import com.oracle.svm.core.util.BasedOnJDKFile; /** @@ -89,7 +88,7 @@ @BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+8/src/hotspot/share/runtime/globals.hpp") @BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/debug.cpp") @BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/debug.hpp") -class ContainerLibrary { +public class ContainerLibrary { static final int VERSION = 240100; // keep in sync with svm_container.hpp @@ -142,14 +141,9 @@ class ContainerLibrary { class ContainerLibraryDirectives implements CContext.Directives { /** * True if {@link ContainerLibrary} should be linked. - * - * Note that although this method returns {@code true} only for certain GCs, the - * {@link CFunction}s defined in {@link ContainerLibrary} are always registered and can be - * called even if this method returns {@code false}, as other GCs provide alternative - * implementations themselves. */ @Override public boolean isInConfiguration() { - return Container.isSupported() && (SubstrateOptions.useSerialGC() || SubstrateOptions.useEpsilonGC()); + return Container.isSupported(); } } From f3550d7cd338e8d9a95a1202f49da63d703701dd Mon Sep 17 00:00:00 2001 From: Thomas Schrott Date: Mon, 5 Aug 2024 10:38:19 +0200 Subject: [PATCH 2/3] Add namespace to libcontainer --- .../os/linux/cgroupSubsystem_linux.cpp | 6 +++ .../os/linux/cgroupSubsystem_linux.hpp | 6 +++ .../src/hotspot/os/linux/cgroupUtil_linux.cpp | 6 +++ .../src/hotspot/os/linux/cgroupUtil_linux.hpp | 6 +++ .../os/linux/cgroupV1Subsystem_linux.cpp | 6 +++ .../os/linux/cgroupV1Subsystem_linux.hpp | 6 +++ .../os/linux/cgroupV2Subsystem_linux.cpp | 6 +++ .../os/linux/cgroupV2Subsystem_linux.hpp | 6 +++ .../hotspot/os/linux/osContainer_linux.cpp | 6 +++ .../hotspot/os/linux/osContainer_linux.hpp | 6 +++ .../src/hotspot/os/linux/os_linux.cpp | 18 +++++++ .../src/hotspot/os/linux/os_linux.hpp | 6 +++ .../src/hotspot/os/linux/os_linux.inline.hpp | 6 +++ .../src/hotspot/os/posix/os_posix.cpp | 12 +++++ .../src/hotspot/os/posix/os_posix.hpp | 6 +++ .../src/hotspot/os/posix/os_posix.inline.hpp | 6 +++ .../src/hotspot/share/memory/allStatic.hpp | 6 +++ .../src/hotspot/share/memory/allocation.hpp | 6 +++ .../share/memory/allocation.inline.hpp | 12 +++++ .../src/hotspot/share/nmt/memflags.hpp | 6 +++ .../src/hotspot/share/runtime/os.cpp | 12 +++++ .../src/hotspot/share/runtime/os.hpp | 12 +++++ .../src/hotspot/share/runtime/os.inline.hpp | 24 +++++++++ .../hotspot/share/utilities/checkedCast.hpp | 6 +++ .../share/utilities/globalDefinitions.hpp | 52 ++++++++++++++++++- .../share/utilities/globalDefinitions_gcc.hpp | 24 +++++++++ .../src/hotspot/share/utilities/ostream.cpp | 24 +++++++++ .../src/hotspot/share/utilities/ostream.hpp | 12 +++++ .../src/java.base/share/native/include/jni.h | 6 +++ .../java.base/unix/native/include/jni_md.h | 6 +++ .../src/svm/share/memory/allocation.cpp | 6 +++ .../src/svm/share/runtime/globals.hpp | 6 +++ .../src/svm/share/utilities/debug.cpp | 12 +++++ .../src/svm/share/utilities/debug.hpp | 12 +++++ .../src/svm/svm_container.cpp | 6 +++ .../src/svm/svm_container.hpp | 6 +++ 36 files changed, 368 insertions(+), 2 deletions(-) diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupSubsystem_linux.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupSubsystem_linux.cpp index 98b8bfde3e64..a0912fbb0e2e 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupSubsystem_linux.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupSubsystem_linux.cpp @@ -38,6 +38,9 @@ #include "utilities/globalDefinitions.hpp" // controller names have to match the *_IDX indices + +namespace svm_container { + static const char* cg_controller_name[] = { "cpu", "cpuset", "cpuacct", "memory", "pids" }; CgroupSubsystem* CgroupSubsystemFactory::create() { @@ -820,3 +823,6 @@ void CgroupSubsystem::print_version_specific_info(outputStream* st) { memory_controller()->controller()->print_version_specific_info(st, phys_mem); } #endif // !NATIVE_IMAGE + +} // namespace svm_container + diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupSubsystem_linux.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupSubsystem_linux.hpp index f660e1f6b1f2..69806e9f01c9 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupSubsystem_linux.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupSubsystem_linux.hpp @@ -103,6 +103,9 @@ log_trace(os, container)(log_string " is: %s", retval); \ } + +namespace svm_container { + class CgroupController: public CHeapObj { public: virtual char* subsystem_path() = 0; @@ -327,4 +330,7 @@ class CgroupSubsystemFactory: AllStatic { static void cleanup(CgroupInfo* cg_infos); }; + +} // namespace svm_container + #endif // CGROUP_SUBSYSTEM_LINUX_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupUtil_linux.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupUtil_linux.cpp index 19ccf9580fa5..256702e15ba7 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupUtil_linux.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupUtil_linux.cpp @@ -26,6 +26,9 @@ #include "cgroupUtil_linux.hpp" + +namespace svm_container { + int CgroupUtil::processor_count(CgroupCpuController* cpu_ctrl, int host_cpus) { assert(host_cpus > 0, "physical host cpus must be positive"); int limit_count = host_cpus; @@ -48,3 +51,6 @@ int CgroupUtil::processor_count(CgroupCpuController* cpu_ctrl, int host_cpus) { log_trace(os, container)("OSContainer::active_processor_count: %d", result); return result; } + +} // namespace svm_container + diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupUtil_linux.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupUtil_linux.hpp index 43d7a157c8ce..0c56b7500245 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupUtil_linux.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupUtil_linux.hpp @@ -30,10 +30,16 @@ #include "utilities/globalDefinitions.hpp" #include "cgroupSubsystem_linux.hpp" + +namespace svm_container { + class CgroupUtil: AllStatic { public: static int processor_count(CgroupCpuController* cpu, int host_cpus); }; + +} // namespace svm_container + #endif // CGROUP_UTIL_LINUX_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV1Subsystem_linux.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV1Subsystem_linux.cpp index 6fef7f1139ba..b5b97ffe8bd5 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV1Subsystem_linux.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV1Subsystem_linux.cpp @@ -39,6 +39,9 @@ * Set directory to subsystem specific files based * on the contents of the mountinfo and cgroup files. */ + +namespace svm_container { + void CgroupV1Controller::set_subsystem_path(char *cgroup_path) { stringStream ss; if (_root != nullptr && cgroup_path != nullptr) { @@ -419,3 +422,6 @@ jlong CgroupV1Subsystem::pids_current() { CONTAINER_READ_NUMBER_CHECKED(_pids, "/pids.current", "Current number of tasks", pids_current); return (jlong)pids_current; } + +} // namespace svm_container + diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV1Subsystem_linux.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV1Subsystem_linux.hpp index 6677fdb0a03b..ab8dc896c751 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV1Subsystem_linux.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV1Subsystem_linux.hpp @@ -32,6 +32,9 @@ // Cgroups version 1 specific implementation + +namespace svm_container { + class CgroupV1Controller: public CgroupController { private: /* mountinfo contents */ @@ -172,4 +175,7 @@ class CgroupV1Subsystem: public CgroupSubsystem { } }; + +} // namespace svm_container + #endif // CGROUP_V1_SUBSYSTEM_LINUX_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV2Subsystem_linux.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV2Subsystem_linux.cpp index 110e1c0fd5eb..6b51e33ea30b 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV2Subsystem_linux.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV2Subsystem_linux.cpp @@ -37,6 +37,9 @@ * -1 for no share setup * OSCONTAINER_ERROR for not supported */ + +namespace svm_container { + int CgroupV2CpuController::cpu_shares() { julong shares; CONTAINER_READ_NUMBER_CHECKED(reader(), "/cpu.weight", "Raw value for CPU Shares", shares); @@ -313,3 +316,6 @@ jlong CgroupV2Subsystem::pids_current() { CONTAINER_READ_NUMBER_CHECKED(unified(), "/pids.current", "Current number of tasks", pids_current); return pids_current; } + +} // namespace svm_container + diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV2Subsystem_linux.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV2Subsystem_linux.hpp index f105cd201f94..87f835d21cec 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV2Subsystem_linux.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/cgroupV2Subsystem_linux.hpp @@ -29,6 +29,9 @@ #include "cgroupSubsystem_linux.hpp" + +namespace svm_container { + class CgroupV2Controller: public CgroupController { private: /* the mount path of the cgroup v2 hierarchy */ @@ -136,4 +139,7 @@ class CgroupV2Subsystem: public CgroupSubsystem { CachingCgroupController* cpu_controller() { return _cpu; } }; + +} // namespace svm_container + #endif // CGROUP_V2_SUBSYSTEM_LINUX_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/osContainer_linux.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/osContainer_linux.cpp index a4bce201ad17..3dbafec1edf8 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/osContainer_linux.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/osContainer_linux.cpp @@ -34,6 +34,9 @@ #include "cgroupSubsystem_linux.hpp" + +namespace svm_container { + bool OSContainer::_is_initialized = false; bool OSContainer::_is_containerized = false; CgroupSubsystem* cgroup_subsystem; @@ -204,3 +207,6 @@ void OSContainer::print_container_helper(outputStream* st, jlong j, const char* } } #endif // !NATIVE_IMAGE + +} // namespace svm_container + diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/osContainer_linux.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/osContainer_linux.hpp index e6573eab54e6..a0ed7bae1949 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/osContainer_linux.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/osContainer_linux.hpp @@ -36,6 +36,9 @@ // 20ms timeout between re-reads of memory limit and _active_processor_count. #define OSCONTAINER_CACHE_TIMEOUT (NANOSECS_PER_SEC/50) + +namespace svm_container { + class OSContainer: AllStatic { private: @@ -80,4 +83,7 @@ inline bool OSContainer::is_containerized() { return _is_containerized; } + +} // namespace svm_container + #endif // OS_LINUX_OSCONTAINER_LINUX_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.cpp index 8b8d458909e9..61a2428b3604 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.cpp @@ -160,24 +160,39 @@ #ifdef MUSL_LIBC // dlvsym is not a part of POSIX // and musl libc doesn't implement it. + +namespace svm_container { + static void *dlvsym(void *handle, const char *symbol, const char *version) { // load the latest version of symbol return dlsym(handle, symbol); } + +} // namespace svm_container + #endif + +namespace svm_container { + enum CoredumpFilterBit { FILE_BACKED_PVT_BIT = 1 << 2, FILE_BACKED_SHARED_BIT = 1 << 3, LARGEPAGES_BIT = 1 << 6, DAX_SHARED_BIT = 1 << 8 }; + +} // namespace svm_container + #endif // !NATIVE_IMAGE //////////////////////////////////////////////////////////////////////////////// // global variables + +namespace svm_container { + julong os::Linux::_physical_memory = 0; #ifndef NATIVE_IMAGE @@ -5557,3 +5572,6 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) { } // end: os::pd_dll_unload() #endif // !NATIVE_IMAGE + +} // namespace svm_container + diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.hpp index bfcd92a6233f..2af1f3fa862c 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.hpp @@ -30,6 +30,9 @@ // os::Linux defines the interface to Linux operating systems + +namespace svm_container { + class os::Linux { friend class CgroupSubsystem; friend class os; @@ -442,4 +445,7 @@ class os::Linux { #endif // !NATIVE_IMAGE }; + +} // namespace svm_container + #endif // OS_LINUX_OS_LINUX_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.inline.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.inline.hpp index a31cfbf30cd4..4ebdfc8623d5 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.inline.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/linux/os_linux.inline.hpp @@ -32,6 +32,9 @@ #include "os_posix.inline.hpp" #ifndef NATIVE_IMAGE + +namespace svm_container { + inline bool os::zero_page_read_protected() { return true; } @@ -57,6 +60,9 @@ inline bool os::can_trim_native_heap() { return false; // musl #endif } + +} // namespace svm_container + #endif // !NATIVE_IMAGE #endif // OS_LINUX_OS_LINUX_INLINE_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.cpp index 46c1b2330a11..c794bf84b14d 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.cpp @@ -100,6 +100,9 @@ #endif /* Input/Output types for mincore(2) */ + +namespace svm_container { + typedef LINUX_ONLY(unsigned) char mincore_vec_t; static jlong initial_time_count = 0; @@ -521,9 +524,15 @@ char* os::map_memory_to_file_aligned(size_t size, size_t alignment, int file_des MemTracker::record_virtual_memory_commit((address)aligned_base, size, CALLER_PC); return aligned_base; } + +} // namespace svm_container + #endif // !NATIVE_IMAGE #ifndef NATIVE_IMAGE + +namespace svm_container { + int os::get_fileno(FILE* fp) { return NOT_AIX(::)fileno(fp); } @@ -2202,4 +2211,7 @@ char* os::pd_map_memory(int fd, const char* unused, bool os::pd_unmap_memory(char* addr, size_t bytes) { return munmap(addr, bytes) == 0; } + +} // namespace svm_container + #endif // !NATIVE_IMAGE diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.hpp index fa42acbd9246..5c2f6d52b537 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.hpp @@ -52,6 +52,9 @@ return _result; \ } while(false) + +namespace svm_container { + class os::Posix { friend class os; @@ -104,6 +107,9 @@ class os::Posix { const void* ucVoid, address* stub); }; + +} // namespace svm_container + #endif // !NATIVE_IMAGE #endif // OS_POSIX_OS_POSIX_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.inline.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.inline.hpp index de050c3c4453..a0a2bf66dabb 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.inline.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/os/posix/os_posix.inline.hpp @@ -37,6 +37,9 @@ #include // Aix does not have NUMA support but need these for compilation. + +namespace svm_container { + inline bool os::numa_has_group_homing() { AIX_ONLY(ShouldNotReachHere();) return false; } // Platform Mutex/Monitor implementation @@ -66,6 +69,9 @@ inline void PlatformMonitor::notify_all() { int status = pthread_cond_broadcast(cond()); assert_status(status == 0, status, "cond_broadcast"); } + +} // namespace svm_container + #endif // !NATIVE_IMAGE #endif // OS_POSIX_OS_POSIX_INLINE_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allStatic.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allStatic.hpp index 4f3761b470ee..031c8b19df40 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allStatic.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allStatic.hpp @@ -30,9 +30,15 @@ // using classes for grouping. Deriving from this class indicates the // derived class is intended to be a namespace, with no instances ever // created. + +namespace svm_container { + struct AllStatic { AllStatic() = delete; ~AllStatic() = delete; }; + +} // namespace svm_container + #endif // SHARE_MEMORY_ALLSTATIC_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allocation.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allocation.hpp index 021a3816c0ba..d6e98fb9a8b5 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allocation.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allocation.hpp @@ -34,6 +34,9 @@ #include + +namespace svm_container { + class outputStream; class Thread; class JavaThread; @@ -590,4 +593,7 @@ class MallocArrayAllocator : public AllStatic { }; #endif // !NATIVE_IMAGE + +} // namespace svm_container + #endif // SHARE_MEMORY_ALLOCATION_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allocation.inline.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allocation.inline.hpp index 64c4a3104eaf..b94beee2c10f 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allocation.inline.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/memory/allocation.inline.hpp @@ -39,6 +39,9 @@ #ifndef PRODUCT // Increments unsigned long value for statistics (not atomic on MP, but avoids word-tearing on 32 bit). + +namespace svm_container { + inline void inc_stat_counter(volatile julong* dest, julong add_value) { #ifdef _LP64 *dest += add_value; @@ -47,8 +50,14 @@ inline void inc_stat_counter(volatile julong* dest, julong add_value) { Atomic::store(dest, value + add_value); #endif } + +} // namespace svm_container + #endif + +namespace svm_container { + template size_t MmapArrayAllocator::size_for(size_t length) { size_t size = length * sizeof(E); @@ -112,6 +121,9 @@ template void MallocArrayAllocator::free(E* addr) { FreeHeap(addr); } + +} // namespace svm_container + #endif // !NATIVE_IMAGE #endif // SHARE_MEMORY_ALLOCATION_INLINE_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/nmt/memflags.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/nmt/memflags.hpp index dba4b0002d78..744e2adefa82 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/nmt/memflags.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/nmt/memflags.hpp @@ -65,6 +65,9 @@ #define MEMORY_TYPE_DECLARE_ENUM(type, human_readable) \ type, + +namespace svm_container { + enum class MEMFLAGS : uint8_t { MEMORY_TYPES_DO(MEMORY_TYPE_DECLARE_ENUM) mt_number_of_types // number of memory types (mtDontTrack @@ -80,4 +83,7 @@ MEMORY_TYPES_DO(MEMORY_TYPE_SHORTNAME) // Make an int version of the sentinel end value. constexpr int mt_number_of_types = static_cast(MEMFLAGS::mt_number_of_types); + +} // namespace svm_container + #endif // SHARE_NMT_MEMFLAGS_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.cpp index 09c95a0c9b2e..d4593c3fa10b 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.cpp @@ -97,9 +97,18 @@ # include # include + +namespace svm_container { + OSThread* os::_starting_thread = nullptr; volatile unsigned int os::_rand_seed = 1234567; + +} // namespace svm_container + #endif // !NATIVE_IMAGE + +namespace svm_container { + int os::_processor_count = 0; #ifndef NATIVE_IMAGE int os::_initial_active_processor_count = 0; @@ -2506,3 +2515,6 @@ jint os::set_minimum_stack_sizes() { return JNI_OK; } #endif // !NATIVE_IMAGE + +} // namespace svm_container + diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.hpp index 87e933d2ab8b..900828690372 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.hpp @@ -38,6 +38,9 @@ # include #endif + +namespace svm_container { + class frame; class JvmtiAgent; @@ -155,10 +158,16 @@ const bool ExecMem = true; typedef void (*java_call_t)(JavaValue* value, const methodHandle& method, JavaCallArguments* args, JavaThread* thread); class MallocTracker; + +} // namespace svm_container + #endif // !NATIVE_IMAGE // Preserve errno across a range of calls + +namespace svm_container { + class ErrnoPreserver { int _e; @@ -1124,4 +1133,7 @@ class os: AllStatic { extern "C" int SpinPause(); #endif // !NATIVE_IMAGE + +} // namespace svm_container + #endif // SHARE_RUNTIME_OS_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.inline.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.inline.hpp index 0719c472f3f8..0b4f2b1602ba 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.inline.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/runtime/os.inline.hpp @@ -36,29 +36,53 @@ // Provide default empty implementation. #ifndef HAVE_PLATFORM_PRINT_NATIVE_STACK + +namespace svm_container { + inline bool os::platform_print_native_stack(outputStream* st, const void* context, char *buf, int buf_size, address& lastpc) { return false; } + +} // namespace svm_container + #endif #ifndef HAVE_CDS_CORE_REGION_ALIGNMENT + +namespace svm_container { + inline size_t os::cds_core_region_alignment() { return (size_t)os::vm_allocation_granularity(); } + +} // namespace svm_container + #endif #ifndef _WINDOWS // Currently used only on Windows. + +namespace svm_container { + inline bool os::register_code_area(char *low, char *high) { return true; } + +} // namespace svm_container + #endif #ifndef HAVE_FUNCTION_DESCRIPTORS + +namespace svm_container { + inline void* os::resolve_function_descriptor(void* p) { return nullptr; } + +} // namespace svm_container + #endif #endif // !NATIVE_IMAGE diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/checkedCast.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/checkedCast.hpp index 3379586aded9..27922a92b909 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/checkedCast.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/checkedCast.hpp @@ -36,6 +36,9 @@ // reversible without loss of information. It doesn't check // everything: it isn't intended to make sure that pointer types are // compatible, for example. + +namespace svm_container { + template constexpr T2 checked_cast(T1 thing) { T2 result = static_cast(thing); @@ -43,5 +46,8 @@ constexpr T2 checked_cast(T1 thing) { return result; } + +} // namespace svm_container + #endif // SHARE_UTILITIES_CHECKEDCAST_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/globalDefinitions.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/globalDefinitions.hpp index ee96928fe02b..704beead1a97 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/globalDefinitions.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/globalDefinitions.hpp @@ -43,7 +43,13 @@ #include #ifndef NATIVE_IMAGE + +namespace svm_container { + class oopDesc; + +} // namespace svm_container + #endif // !NATIVE_IMAGE // Defaults for macros that might be defined per compiler. @@ -189,6 +195,9 @@ class oopDesc; #endif // _LP64 // Convert pointer to intptr_t, for use in printing pointers. + +namespace svm_container { + inline intptr_t p2i(const volatile void* p) { return (intptr_t) p; } @@ -308,6 +317,9 @@ inline size_t heap_word_size(size_t byte_size) { inline jfloat jfloat_cast(jint x); inline jdouble jdouble_cast(jlong x); + +} // namespace svm_container + #endif // !NATIVE_IMAGE //------------------------------------------- @@ -317,6 +329,9 @@ inline jdouble jdouble_cast(jlong x); #define CONST64(x) (x ## LL) #define UCONST64(x) (x ## ULL) + +namespace svm_container { + const jlong min_jlong = CONST64(0x8000000000000000); const jlong max_jlong = CONST64(0x7fffffffffffffff); @@ -556,7 +571,13 @@ typedef jshort s2; typedef jint s4; typedef jlong s8; + +} // namespace svm_container + #ifndef NATIVE_IMAGE + +namespace svm_container { + const jbyte min_jbyte = -(1 << 7); // smallest jbyte const jbyte max_jbyte = (1 << 7) - 1; // largest jbyte const jshort min_jshort = -(1 << 15); // smallest jshort @@ -605,6 +626,9 @@ extern uint64_t OopEncodingHeapMax; // Machine dependent stuff + +} // namespace svm_container + #include CPU_HEADER(globalDefinitions) // The maximum size of the code cache. Can be overridden by targets. @@ -622,11 +646,23 @@ extern uint64_t OopEncodingHeapMax; // by Luc Maranget, Susmit Sarkar and Peter Sewell, INRIA/Cambridge) #ifdef CPU_MULTI_COPY_ATOMIC // Not needed. + +namespace svm_container { + const bool support_IRIW_for_not_multiple_copy_atomic_cpu = false; + +} // namespace svm_container + #else // From all non-multi-copy-atomic architectures, only PPC64 supports IRIW at the moment. // Final decision is subject to JEP 188: Java Memory Model Update. + +namespace svm_container { + const bool support_IRIW_for_not_multiple_copy_atomic_cpu = PPC64_ONLY(true) NOT_PPC64(false); + +} // namespace svm_container + #endif // The expected size in bytes of a cache line. @@ -647,6 +683,9 @@ const bool support_IRIW_for_not_multiple_copy_atomic_cpu = PPC64_ONLY(true) NOT_ // All fabs() callers should call this function instead, which will implicitly // convert the operand to double, avoiding a dependency on __fabsf which // doesn't exist in early versions of Solaris 8. + +namespace svm_container { + inline double fabsd(double value) { return fabs(value); } @@ -1064,8 +1103,8 @@ const intptr_t badDispHeaderOSR = 0xDEAD05A0; // value to fill unu // (These must be implemented as #defines because C++ compilers are // not obligated to inline non-integral constants!) -#define badAddress ((address)::badAddressVal) -#define badHeapWord (::badHeapWordVal) +#define badAddress ((address)svm_container::badAddressVal) +#define badHeapWord (svm_container::badHeapWordVal) // Default TaskQueue size is 16K (32-bit) or 128K (64-bit) const uint TASKQUEUE_SIZE = (NOT_LP64(1<<14) LP64_ONLY(1<<17)); @@ -1113,6 +1152,9 @@ inline intptr_t bitfield(intptr_t x, int start_bit_no, int field_length) { #ifdef min #undef min #endif + +} // namespace svm_container + #endif // !NATIVE_IMAGE // It is necessary to use templates here. Having normal overloaded @@ -1120,6 +1162,9 @@ inline intptr_t bitfield(intptr_t x, int start_bit_no, int field_length) { // and 64-bit overloaded functions, which does not work, and having // explicitly-typed versions of these routines (i.e., MAX2I, MAX2L) // will be even more error-prone than macros. + +namespace svm_container { + template constexpr T MAX2(T a, T b) { return (a > b) ? a : b; } template constexpr T MIN2(T a, T b) { return (a < b) ? a : b; } #ifndef NATIVE_IMAGE @@ -1376,4 +1421,7 @@ std::add_rvalue_reference_t declval() noexcept; bool IEEE_subnormal_handling_OK(); #endif // !NATIVE_IMAGE + +} // namespace svm_container + #endif // SHARE_UTILITIES_GLOBALDEFINITIONS_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/globalDefinitions_gcc.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/globalDefinitions_gcc.hpp index a67c12ba0d50..741bfa52c68c 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/globalDefinitions_gcc.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/globalDefinitions_gcc.hpp @@ -47,7 +47,13 @@ #if (defined(__VEC__) || defined(__AIXVEC)) && defined(AIX) \ && defined(__open_xl_version__) && __open_xl_version__ >= 17 #undef malloc + +namespace svm_container { + extern void *malloc(size_t) asm("vec_malloc"); + +} // namespace svm_container + #endif #include @@ -92,10 +98,22 @@ // checking for nanness #if defined(__APPLE__) + +namespace svm_container { + inline int g_isnan(double f) { return isnan(f); } + +} // namespace svm_container + #elif defined(LINUX) || defined(_ALLBSD_SOURCE) || defined(_AIX) + +namespace svm_container { + inline int g_isnan(float f) { return isnan(f); } inline int g_isnan(double f) { return isnan(f); } + +} // namespace svm_container + #else #error "missing platform-specific definition here" #endif @@ -105,6 +123,9 @@ inline int g_isnan(double f) { return isnan(f); } // Checking for finiteness + +namespace svm_container { + inline int g_isfinite(jfloat f) { return isfinite(f); } inline int g_isfinite(jdouble f) { return isfinite(f); } @@ -149,4 +170,7 @@ inline int g_isfinite(jdouble f) { return isfinite(f); } #define ALWAYSINLINE inline __attribute__ ((always_inline)) #define ATTRIBUTE_FLATTEN __attribute__ ((flatten)) + +} // namespace svm_container + #endif // SHARE_UTILITIES_GLOBALDEFINITIONS_GCC_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/ostream.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/ostream.cpp index d781a5e2f6e4..7f799ede794d 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/ostream.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/ostream.cpp @@ -47,6 +47,9 @@ #include "utilities/xmlstream.hpp" // Declarations of jvm methods + +namespace svm_container { + extern "C" void jio_print(const char* s, size_t len); extern "C" int jio_printf(const char *fmt, ...); @@ -190,7 +193,13 @@ void outputStream::vprint_cr(const char* format, va_list argptr) { do_vsnprintf_and_write(format, argptr, true); } + +} // namespace svm_container + #endif // !NATIVE_IMAGE + +namespace svm_container { + void outputStream::print_raw(const char* str, size_t len) { #ifndef NATIVE_IMAGE if (_autoindent && _position == 0) { @@ -461,6 +470,9 @@ stringStream::~stringStream() { } } + +} // namespace svm_container + #ifndef NATIVE_IMAGE // tty needs to be always accessible since there are code paths that may write to it // outside of the VM lifespan. @@ -471,6 +483,9 @@ stringStream::~stringStream() { // The policy followed here is a compromise reached during review of JDK-8292351: // - pre-init: we silently swallow all output. We won't see anything, but at least won't crash // - post-exit: we write to a simple fdStream, but somewhat mimic the behavior of the real defaultStream + +namespace svm_container { + static nullStream tty_preinit_stream; outputStream* tty = &tty_preinit_stream; @@ -1090,6 +1105,9 @@ bufferedStream::~bufferedStream() { FREE_C_HEAP_ARRAY(char, buffer); } + +} // namespace svm_container + #ifndef PRODUCT #if defined(LINUX) || defined(AIX) || defined(_ALLBSD_SOURCE) @@ -1103,6 +1121,9 @@ bufferedStream::~bufferedStream() { #endif // Network access + +namespace svm_container { + networkStream::networkStream() : bufferedStream(1024*10, 1024*10) { _socket = -1; @@ -1166,6 +1187,9 @@ bool networkStream::connect(const char *host, short port) { freeaddrinfo(addr_info); return (conn >= 0); } + +} // namespace svm_container + #endif // !NATIVE_IMAGE #endif diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/ostream.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/ostream.hpp index 6ad829783430..b4b70f5ff7dc 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/ostream.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/hotspot/share/utilities/ostream.hpp @@ -34,7 +34,13 @@ #include "utilities/macros.hpp" #ifndef NATIVE_IMAGE + +namespace svm_container { + DEBUG_ONLY(class ResourceMark;) + +} // namespace svm_container + #endif // !NATIVE_IMAGE // Output streams for printing @@ -48,6 +54,9 @@ DEBUG_ONLY(class ResourceMark;) // This allows for redirection via -XX:+DisplayVMOutputToStdout and // -XX:+DisplayVMOutputToStderr. + +namespace svm_container { + class outputStream : public CHeapObjBase { #ifndef NATIVE_IMAGE private: @@ -398,4 +407,7 @@ class networkStream : public bufferedStream { #endif #endif // !NATIVE_IMAGE + +} // namespace svm_container + #endif // SHARE_UTILITIES_OSTREAM_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/java.base/share/native/include/jni.h b/substratevm/src/com.oracle.svm.native.libcontainer/src/java.base/share/native/include/jni.h index c85da1bc67f2..6350b61d3fa3 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/java.base/share/native/include/jni.h +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/java.base/share/native/include/jni.h @@ -44,6 +44,9 @@ #include "jni_md.h" + +namespace svm_container { + #ifdef __cplusplus extern "C" { #endif @@ -1998,4 +2001,7 @@ JNI_OnUnload(JavaVM *vm, void *reserved); } /* extern "C" */ #endif /* __cplusplus */ + +} // namespace svm_container + #endif /* !_JAVASOFT_JNI_H_ */ diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/java.base/unix/native/include/jni_md.h b/substratevm/src/com.oracle.svm.native.libcontainer/src/java.base/unix/native/include/jni_md.h index 6e583da7147e..ce339cecb289 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/java.base/unix/native/include/jni_md.h +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/java.base/unix/native/include/jni_md.h @@ -54,6 +54,9 @@ #define JNICALL + +namespace svm_container { + typedef int jint; #ifdef _LP64 typedef long jlong; @@ -63,4 +66,7 @@ typedef long long jlong; typedef signed char jbyte; + +} // namespace svm_container + #endif /* !_JAVASOFT_JNI_MD_H_ */ diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/memory/allocation.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/memory/allocation.cpp index 3dcc3a474cb1..99c998a2cdaa 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/memory/allocation.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/memory/allocation.cpp @@ -26,6 +26,9 @@ #include "memory/allocation.hpp" #include "runtime/os.hpp" + +namespace svm_container { + char* AllocateHeap(size_t size, MEMFLAGS flags, AllocFailType alloc_failmode /* = AllocFailStrategy::EXIT_OOM*/) { @@ -46,3 +49,6 @@ char* ReallocateHeap(char* old, void FreeHeap(void* p) { os::free(p); } + +} // namespace svm_container + diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/runtime/globals.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/runtime/globals.hpp index 8c6b3cb6cf6c..bc0aecdd8151 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/runtime/globals.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/runtime/globals.hpp @@ -29,7 +29,13 @@ #include "utilities/globalDefinitions.hpp" #include "utilities/macros.hpp" + +namespace svm_container { + constexpr bool UseCpuAllocPath = false; constexpr bool UseContainerSupport = true; + +} // namespace svm_container + #endif // SHARE_RUNTIME_GLOBALS_HPP diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/utilities/debug.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/utilities/debug.cpp index 0be1d5d79791..73d1d316813a 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/utilities/debug.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/utilities/debug.cpp @@ -31,6 +31,9 @@ #include #ifdef PRINT_WARNINGS + +namespace svm_container { + ATTRIBUTE_PRINTF(1, 2) void warning(const char* format, ...) { FILE* const err = stderr; @@ -40,9 +43,15 @@ void warning(const char* format, ...) { va_end(ap); fputc('\n', err); } + +} // namespace svm_container + #endif #ifdef ASSERT + +namespace svm_container { + ATTRIBUTE_PRINTF(4, 5) void report_vm_error(const char* file, int line, const char* error_msg, const char* detail_fmt, ...) { FILE* const err = stderr; @@ -58,4 +67,7 @@ void report_vm_error(const char* file, int line, const char* error_msg) { report_vm_error(file, line, error_msg, "%s", ""); } + +} // namespace svm_container + #endif diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/utilities/debug.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/utilities/debug.hpp index 8056a057a722..dc125fb09ed9 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/utilities/debug.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/share/utilities/debug.hpp @@ -30,6 +30,9 @@ #ifdef ASSERT // error reporting helper functions + +namespace svm_container { + [[noreturn]] void report_vm_error(const char* file, int line, const char* error_msg); @@ -37,6 +40,9 @@ void report_vm_error(const char* file, int line, const char* error_msg); ATTRIBUTE_PRINTF(4, 5) void report_vm_error(const char* file, int line, const char* error_msg, const char* detail_fmt, ...); + +} // namespace svm_container + #endif #ifdef ASSERT @@ -64,7 +70,13 @@ do { \ #ifndef PRINT_WARNINGS #define warning(format, ...) #else + +namespace svm_container { + void warning(const char* format, ...); + +} // namespace svm_container + #endif #define STATIC_ASSERT(Cond) static_assert((Cond), #Cond) diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/svm_container.cpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/svm_container.cpp index 7b1ed8c88201..accce218d6ab 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/svm_container.cpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/svm_container.cpp @@ -28,6 +28,9 @@ #include "osContainer_linux.hpp" #include "svm_container.hpp" + +namespace svm_container { + extern "C" { // keep in sync with ContainerLibrary.java @@ -111,3 +114,6 @@ EXPORT_FOR_SVM int svm_container_active_processor_count() { } } // extern C + +} // namespace svm_container + diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/svm_container.hpp b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/svm_container.hpp index d7a3c958b27f..dc22951769d6 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/svm_container.hpp +++ b/substratevm/src/com.oracle.svm.native.libcontainer/src/svm/svm_container.hpp @@ -40,6 +40,9 @@ #endif #endif + +namespace svm_container { + extern "C" { EXPORT_FOR_SVM int svm_container_initialize(int version); EXPORT_FOR_SVM jlong svm_container_physical_memory(); @@ -53,4 +56,7 @@ extern "C" { EXPORT_FOR_SVM int svm_container_active_processor_count(); } + +} // namespace svm_container + #endif // SVM_CONTAINER_HPP From 318060c4eed5d8b8ad7dda1ba5a377a47810e557 Mon Sep 17 00:00:00 2001 From: Thomas Schrott Date: Wed, 7 Aug 2024 08:55:30 +0200 Subject: [PATCH 3/3] Add scripts for adding and removing the namespace; update README --- .../README.md | 10 +- .../scripts/addNamespace.py | 356 ++++++++++++++++++ .../scripts/removeNamespace.py | 158 ++++++++ 3 files changed, 522 insertions(+), 2 deletions(-) create mode 100644 substratevm/src/com.oracle.svm.native.libcontainer/scripts/addNamespace.py create mode 100644 substratevm/src/com.oracle.svm.native.libcontainer/scripts/removeNamespace.py diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/README.md b/substratevm/src/com.oracle.svm.native.libcontainer/README.md index dc014ad7af60..9e1d6eedfc2c 100644 --- a/substratevm/src/com.oracle.svm.native.libcontainer/README.md +++ b/substratevm/src/com.oracle.svm.native.libcontainer/README.md @@ -41,11 +41,17 @@ To help keeping the `@BasedOnJDKFile` annotations up to date, the `mx gate --tags check_libcontainer_annotations` command ensures that the actual files and annotations are in sync. -To do a full reimport, replace the files in [`src/hotspot`](./src/hotspot) with those from the OpenJDK. +To do a full reimport, first remove the C++-namespace from the source code using the +[`removeNamespace.py`](./scripts/removeNamespace.py)-script, in order to minimize the diff to the files +from the OpenJDK. Execute it like `python ./scripts/removeNamespace.py -n svm_container -d ./src`. +Then commit these changes, otherwise the namespace will still show in the diff later. +Then replace the files in [`src/hotspot`](./src/hotspot) with those from the OpenJDK. The `mx reimport-libcontainer-files --jdk-repo path/to/jdk` can help with that. Then reapply all the changes (`#ifdef` guards) using the diff tool of your choice. Then, adopt the files in -[`src/svm`](./src/svm) to provide new functionality, if needed. Finally, update the `@BasedOnJDKFile` +[`src/svm`](./src/svm) to provide new functionality, if needed. Then, update the `@BasedOnJDKFile` annotations in `ContainerLibrary.java` to reflect the import revision. +Finally, add the C++-namespace back to the source code using the [`addNamespace.py`](./scripts/addNamespace.py) +script. Execute it like `python ./scripts/addNamespace.py -n svm_container -d ./src`. ## Local Testing diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/scripts/addNamespace.py b/substratevm/src/com.oracle.svm.native.libcontainer/scripts/addNamespace.py new file mode 100644 index 000000000000..c7589b89ec45 --- /dev/null +++ b/substratevm/src/com.oracle.svm.native.libcontainer/scripts/addNamespace.py @@ -0,0 +1,356 @@ +# +# Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# This code is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 only, as +# published by the Free Software Foundation. Oracle designates this +# particular file as subject to the "Classpath" exception as provided +# by Oracle in the LICENSE file that accompanied this code. +# +# This code is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# version 2 for more details (a copy is included in the LICENSE file that +# accompanied this code). +# +# You should have received a copy of the GNU General Public License version +# 2 along with this work; if not, write to the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +# or visit www.oracle.com if you need additional information or have any +# questions. +# + +# Adds a C++-namespace to the source files. +# If no command line arguments are passed, the namespace is added to all files. +# It is possible to pass filepaths as command line arguments. Then the namespace is only added to the +# specified files. + + +import os +import re +import argparse + + +# Names of symbols that are currently accessed globally (e.g., ::swap(...)), but after adding the +# namespace are not global symbols anymore. +qualify_with_namespace = {"swap", "CardTableBarrierSet", "G1BarrierSet", "tty", "badHeapWordVal", "badAddressVal"} + +# Some files must not have the namespace added, as they are included within a class definition. Normally includes +# need to be outside the namespace, but the includes of these files need to be inside the namespace. +ignore_files = {"copy_x86.hpp", "copy_aarch64.hpp", "osThread_linux.hpp"} +ignore_includes = {"CPU_HEADER(copy)", "OS_HEADER(osThread)"} + +files_with_cpp_guard = {"sharedGCStructs.hpp"} + + +def main(): + + parser = argparse.ArgumentParser() + + pathGroup = parser.add_mutually_exclusive_group(required=True) + pathGroup.add_argument("-d", "--directory", type=str, help="Path to the src-directory for adding the namespace.") + pathGroup.add_argument("-f", "--files", type=str, nargs="+", help="Path to the files for adding the namespace.") + + parser.add_argument("-n", "--namespace", required=True, type=str, + help="The namespace that gets added to the files.") + + args = parser.parse_args() + + global namespaceName + namespaceName = args.namespace + + global namespaceBeginWithGuard + global namespaceEndWithGuard + global namespaceBegin + global namespaceEnd + + namespaceBeginWithGuard = f"\n#ifdef __cplusplus\n namespace {namespaceName} {{\n#endif\n\n" + namespaceEndWithGuard = f"\n#ifdef __cplusplus\n }} // namespace {namespaceName}\n#endif\n\n" + + namespaceBegin = f"\nnamespace {namespaceName} {{\n\n" + namespaceEnd = f"\n}} // namespace {namespaceName}\n\n" + + if args.directory: + # src-directory specified, add namespace to all files. + add_namespace(args.directory) + else: + for file in args.files: + if not is_c_file(file): + continue + + if not os.path.isfile(file): + print(f"Skipping {file}. File does not exist.") + else: + add_namespace_to_file(file, os.path.basename(file) in files_with_cpp_guard) + + +def is_c_file(file): + return file.endswith(".hpp") or file.endswith(".h") or file.endswith(".cpp") or file.endswith(".c") + + +def add_namespace(svmRootDirectory): + for subdir, dirs, files in os.walk(svmRootDirectory): + for file in files: + if is_c_file(file): + + if file not in ignore_files: + print(f"Add namespace to {os.path.join(subdir, file)}") + add_namespace_to_file(os.path.join(subdir, file), file in files_with_cpp_guard) + + else: + print(f"Ignore file: {os.path.join(subdir, file)}") + + +def add_namespace_to_file(file, add_cpp_guard): + with open(file, 'r') as f: + lines = f.readlines() + insertion_indices = calc_insert_indices(lines) + + with open(file, 'w') as sf: + i = 0 + for (start, end) in insertion_indices: + print(f" start {start}, end {end}") + while i < start: + write_str(sf, lines[i]) + i += 1 + + if add_cpp_guard: + sf.write(namespaceBeginWithGuard) + else: + sf.write(namespaceBegin) + + while i < end: + write_str(sf, lines[i]) + i += 1 + + if add_cpp_guard: + sf.write(namespaceEndWithGuard) + else: + sf.write(namespaceEnd) + + while i < len(lines): + write_str(sf, lines[i]) + i += 1 + + +def write_str(file, str): + pattern = re.compile(r"[^a-zA-Z0-9>_]::\w") + match = pattern.search(str) + + while match: + qualifiedName = "" + i = 3 + while is_valid_identifier_character(str[match.start() + i]): + qualifiedName += str[match.start() + i] + i += 1 + + if qualifiedName in qualify_with_namespace: + str = str[:match.start() + 1] + namespaceName + str[match.start() + 1:] + + match = pattern.search(str, match.end()) + + file.write(str) + + +def calc_insert_indices(lines): + indices = [] + idx_namespace_begin = 0 + + # skip the header + for i in range(len(lines)): + if not is_header_line(lines[i]): + idx_namespace_begin = i + break + + # Earliest line for namespace is directly after header, if no #include are following + + # Keep track of the current open #ifs in general and inside the namespace, so the namespace is opened and closed + # on the same level. + cur_n_open_ifs = 0 + namespace_n_open_ifs = 0 + namespace_open = False + + i = idx_namespace_begin + while i < len(lines): + line = lines[i] + + # skip block comments + if line.lstrip().startswith("/*"): + end_index = line.find("*/") + while end_index == -1 and i < len(lines): + i += 1 + end_index = lines[i].find("*/") + + # Line containing the block comment end was found. Set line to the rest of this line. + line = lines[i][end_index+2:] + + if namespace_open: + if is_if_statement(line): + cur_n_open_ifs += 1 + strippedLine = line.strip() + while strippedLine.endswith("\\"): + # #if continues on the next line + i += 1 + strippedLine = lines[i].strip() + + elif is_endif_statement(line): + + if namespace_n_open_ifs == cur_n_open_ifs: + # The #if in which the namespace was opened, is closed, so also close the namespace. + indices.append((idx_namespace_begin, i)) + namespace_open = False + + cur_n_open_ifs -= 1 + assert cur_n_open_ifs >= 0 + + elif is_else_statement(line) or is_elif_statement(line): + + if namespace_n_open_ifs == cur_n_open_ifs: + # The #if in which the namespace was opened, has an #else/#elsif part, so close the namespace + # before that. + indices.append((idx_namespace_begin, i)) + namespace_open = False + + # The number of current ifs does not change as there exists an #else/#elsif part. + elif is_include_statement(line): + # The namespace is open but another include occurs. Close the namespace if necessary. + if line[len("#include "):].strip() not in ignore_includes: + assert namespace_n_open_ifs <= cur_n_open_ifs + + idx_namespace_end = i + end_namespace_open_ifs = cur_n_open_ifs + while namespace_n_open_ifs < end_namespace_open_ifs: + # Some #if was opened inside this namespace. Go back and close the namespace before this #if. + idx_namespace_end -= 1 + if is_if_statement(lines[idx_namespace_end]): + end_namespace_open_ifs -= 1 + elif is_endif_statement(lines[idx_namespace_end]): + end_namespace_open_ifs += 1 + + i = idx_namespace_end + + indices.append((idx_namespace_begin, idx_namespace_end)) + namespace_open = False + + else: + # Namespace is not open + if is_empty(line) or is_line_comment(line): + pass + elif is_preprocessor_directive(line): + + if is_if_statement(line): + cur_n_open_ifs += 1 + + elif is_endif_statement(line): + cur_n_open_ifs -= 1 + assert cur_n_open_ifs >= 0 + + strippedLine = line.strip() + while strippedLine.endswith("\\"): + # Preprocessor statement continues in the next line. + i += 1 + strippedLine = lines[i].strip() + + else: + + if is_extern(line) and cur_n_open_ifs > 0: + # Extern is called inside an if, check if lines before and after are matching #if #endif + j = 0 + while is_if_statement(lines[i - j - 1]) and is_endif_statement(lines[i + j + 1]): + j += 1 + + idx_namespace_begin = i - j + namespace_n_open_ifs = cur_n_open_ifs - j + else: + idx_namespace_begin = i + namespace_n_open_ifs = cur_n_open_ifs + + namespace_open = True + + i += 1 + + # Make sure the namespace is closed at the file end. + if namespace_open: + assert namespace_n_open_ifs <= cur_n_open_ifs + + idx_namespace_end = i + 1 + end_namespace_open_ifs = cur_n_open_ifs + while namespace_n_open_ifs < end_namespace_open_ifs: + # some #if was opened before this include + idx_namespace_end -= 1 + if is_if_statement(lines[idx_namespace_end]): + end_namespace_open_ifs -= 1 + elif is_endif_statement(lines[idx_namespace_end]): + end_namespace_open_ifs += 1 + + indices.append((idx_namespace_begin, idx_namespace_end - 1)) + + return indices + + +def is_header_line(line): + return line.startswith("/*") or line.startswith(" /*") or line.startswith(" *") or line.startswith("*") + + +def is_preprocessor_directive(line): + stripped_line = line.lstrip() + return stripped_line.startswith("#") + + +def is_preprocessor_directive_name(line, name): + stripped_line = line.lstrip() + if not is_preprocessor_directive(stripped_line): + return False + + stripped_line = stripped_line[1:].lstrip() # Remove # and whitespace from the line. + return stripped_line.startswith(name) + + +def is_include_statement(line): + return is_preprocessor_directive_name(line, "include ") + + +def is_if_statement(line): + return is_preprocessor_directive_name(line, "if") + + +def is_else_statement(line): + return is_preprocessor_directive_name(line, "else") + + +def is_elif_statement(line): + return is_preprocessor_directive_name(line, "elif") + + +def is_endif_statement(line): + return is_preprocessor_directive_name(line, "endif") + + +def is_define_statement(line): + return is_preprocessor_directive_name(line, "define") + + +def is_empty(line): + stripped_line = line.strip() + return len(stripped_line) == 0 + + +def is_line_comment(line): + stripped_line = line.lstrip() + return stripped_line.startswith("//") + + +def is_extern(line): + stripped_line = line.lstrip() + return stripped_line.startswith("extern ") + + +def is_valid_identifier_character(c): + return c == '_' or ('a' <= c <= 'z') or ('A' <= c <= 'Z') or ('0' <= c <= '9') + + +if __name__ == "__main__": + main() diff --git a/substratevm/src/com.oracle.svm.native.libcontainer/scripts/removeNamespace.py b/substratevm/src/com.oracle.svm.native.libcontainer/scripts/removeNamespace.py new file mode 100644 index 000000000000..d52039edad10 --- /dev/null +++ b/substratevm/src/com.oracle.svm.native.libcontainer/scripts/removeNamespace.py @@ -0,0 +1,158 @@ +# +# Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# This code is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 only, as +# published by the Free Software Foundation. Oracle designates this +# particular file as subject to the "Classpath" exception as provided +# by Oracle in the LICENSE file that accompanied this code. +# +# This code is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# version 2 for more details (a copy is included in the LICENSE file that +# accompanied this code). +# +# You should have received a copy of the GNU General Public License version +# 2 along with this work; if not, write to the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +# or visit www.oracle.com if you need additional information or have any +# questions. +# + +# Removes a C++-namespace from the source files. +# If no command line arguments are passed, the namespace is removed from all files. +# It is possible to pass filepaths as command line arguments. Then the namespace is only removed from the +# specified files. + + +import os +import re +import argparse + +from addNamespace import * + + +def main(): + parser = argparse.ArgumentParser() + + pathGroup = parser.add_mutually_exclusive_group(required=True) + pathGroup.add_argument("-d", "--directory", type=str, help="Path to the src-directory for removing the namespace.") + pathGroup.add_argument("-f", "--files", type=str, nargs="+", help="Path to the files for removing the namespace.") + + parser.add_argument("-n", "--namespace", required=True, type=str, + help="The namespace that gets removed from the files.") + + args = parser.parse_args() + + global namespaceName + namespaceName = args.namespace + + global namespaceBeginWithGuard + global namespaceEndWithGuard + global namespaceBegin + global namespaceEnd + + namespaceBeginWithGuard = f"\n#ifdef __cplusplus\n namespace {namespaceName} {{\n#endif\n\n" + namespaceEndWithGuard = f"\n#ifdef __cplusplus\n }} // namespace {namespaceName}\n#endif\n\n" + + namespaceBegin = f"\nnamespace {namespaceName} {{\n\n" + namespaceEnd = f"\n}} // namespace {namespaceName}\n\n" + + if args.directory: + # src-directory specified, remove namespace from all files. + remove_namespace(args.directory) + else: + for file in args.files: + if not is_c_file(file): + continue + + if not os.path.isfile(file): + print(f"Skipping {file}. File does not exist.") + else: + remove_namespace_from_file(file, os.path.basename(file) in files_with_cpp_guard) + + +def remove_namespace(svmRootDirectory): + for subdir, dirs, files in os.walk(svmRootDirectory): + for file in files: + if is_c_file(file): + + if file not in ignore_files: + print(f"Remove namespace from {os.path.join(subdir, file)}") + remove_namespace_from_file(os.path.join(subdir, file), file in files_with_cpp_guard) + + else: + print(f"Ignore file: {os.path.join(subdir, file)}") + + +def remove_namespace_from_file(file, add_cpp_guard): + with open(file, 'r') as f: + lines = f.readlines() + + namespace_open = False + + if add_cpp_guard: + begin = f" namespace {namespaceName} {{" + end = f" }} // namespace {namespaceName}" + else: + begin = f"namespace {namespaceName} {{" + end = f"}} // namespace {namespaceName}" + + for i in range(len(lines)): + if not namespace_open: + assert not lines[i].startswith(end) + + if lines[i].startswith(begin): + namespace_open = True + print(f" start {i}", end="") + + lines[i] = "" + + if add_cpp_guard: + lines[i - 1] = "" + lines[i + 1] = "" + lines[i - 2] = remove_if_newline(lines[i - 2]) + lines[i + 2] = remove_if_newline(lines[i + 2]) + else: + lines[i - 1] = remove_if_newline(lines[i - 1]) + lines[i + 1] = remove_if_newline(lines[i + 1]) + else: + assert not lines[i].startswith(begin) + + if lines[i].startswith(end): + namespace_open = False + print(f", end {i}") + + lines[i] = "" + + if add_cpp_guard: + lines[i - 1] = "" + lines[i + 1] = "" + lines[i - 2] = remove_if_newline(lines[i - 2]) + lines[i + 2] = remove_if_newline(lines[i + 2]) + else: + lines[i - 1] = remove_if_newline(lines[i - 1]) + lines[i + 1] = remove_if_newline(lines[i + 1]) + + pattern = re.compile(f"{namespaceName}::\\w") + match = pattern.search(lines[i]) + + while match: + lines[i] = lines[i][:match.start()] + lines[i][match.start() + len(namespaceName):] + match = pattern.search(lines[i]) + + with open(file, 'w') as sf: + for line in lines: + sf.write(line) + + +def remove_if_newline(line): + return "" if line == "\n" else line + + +if __name__ == "__main__": + main()