Skip to content

Commit

Permalink
8293114: JVM should trim the native heap
Browse files Browse the repository at this point in the history
Reviewed-by: shade, rehn, dholmes
  • Loading branch information
tstuefe committed Jul 21, 2023
1 parent 59f66a3 commit 9e4fc56
Show file tree
Hide file tree
Showing 20 changed files with 843 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/hotspot/os/aix/os_aix.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ inline bool os::must_commit_stack_guard_pages() {
inline void os::map_stack_shadow_pages(address sp) {
}

// stubbed-out trim-native support
// Trim-native support, stubbed out for now, may be enabled later
inline bool os::can_trim_native_heap() { return false; }
inline bool os::trim_native_heap(os::size_change_t* rss_change) { return false; }

Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/os/bsd/os_bsd.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ inline bool os::must_commit_stack_guard_pages() {
inline void os::map_stack_shadow_pages(address sp) {
}

// stubbed-out trim-native support
// Trim-native support, stubbed out for now, may be enabled later
inline bool os::can_trim_native_heap() { return false; }
inline bool os::trim_native_heap(os::size_change_t* rss_change) { return false; }

Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/os/windows/os_windows.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ inline void PlatformMonitor::notify_all() {
WakeAllConditionVariable(&_cond);
}

// stubbed-out trim-native support
// Trim-native support, stubbed out for now, may be enabled later
inline bool os::can_trim_native_heap() { return false; }
inline bool os::trim_native_heap(os::size_change_t* rss_change) { return false; }

Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/classfile/stringTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "runtime/mutexLocker.hpp"
#include "runtime/safepointVerifiers.hpp"
#include "runtime/timerTrace.hpp"
#include "runtime/trimNativeHeap.hpp"
#include "services/diagnosticCommand.hpp"
#include "utilities/concurrentHashTable.inline.hpp"
#include "utilities/concurrentHashTableTasks.inline.hpp"
Expand Down Expand Up @@ -456,6 +457,7 @@ void StringTable::clean_dead_entries(JavaThread* jt) {

StringTableDeleteCheck stdc;
StringTableDoDelete stdd;
NativeHeapTrimmer::SuspendMark sm("stringtable");
{
TraceTime timer("Clean", TRACETIME_LOG(Debug, stringtable, perf));
while(bdt.do_task(jt, stdc, stdd)) {
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/classfile/symbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "runtime/atomic.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/timerTrace.hpp"
#include "runtime/trimNativeHeap.hpp"
#include "services/diagnosticCommand.hpp"
#include "utilities/concurrentHashTable.inline.hpp"
#include "utilities/concurrentHashTableTasks.inline.hpp"
Expand Down Expand Up @@ -737,6 +738,7 @@ void SymbolTable::clean_dead_entries(JavaThread* jt) {

SymbolTableDeleteCheck stdc;
SymbolTableDoDelete stdd;
NativeHeapTrimmer::SuspendMark sm("symboltable");
{
TraceTime timer("Clean", TRACETIME_LOG(Debug, symboltable, perf));
while (bdt.do_task(jt, stdc, stdd)) {
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/logging/logTag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class outputStream;
LOG_TAG(timer) \
LOG_TAG(tlab) \
LOG_TAG(tracking) \
LOG_TAG(trimnative) /* trim native heap */ \
LOG_TAG(unload) /* Trace unloading of classes */ \
LOG_TAG(unmap) \
LOG_TAG(unshareable) \
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/memory/arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "runtime/os.hpp"
#include "runtime/task.hpp"
#include "runtime/threadCritical.hpp"
#include "runtime/trimNativeHeap.hpp"
#include "services/memTracker.inline.hpp"
#include "utilities/align.hpp"
#include "utilities/debug.hpp"
Expand Down Expand Up @@ -92,6 +93,7 @@ class ChunkPool {
}

static void clean() {
NativeHeapTrimmer::SuspendMark sm("chunk pool cleaner");
for (int i = 0; i < _num_pools; i++) {
_pools[i].prune();
}
Expand Down
9 changes: 9 additions & 0 deletions src/hotspot/share/prims/whitebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,14 @@ WB_ENTRY(jboolean, WB_SetVirtualThreadsNotifyJvmtiMode(JNIEnv* env, jobject wb,
return result;
WB_END

WB_ENTRY(void, WB_PreTouchMemory(JNIEnv* env, jobject wb, jlong addr, jlong size))
void* const from = (void*)addr;
void* const to = (void*)(addr + size);
if (from > to) {
os::pretouch_memory(from, to, os::vm_page_size());
}
WB_END

#define CC (char*)

static JNINativeMethod methods[] = {
Expand Down Expand Up @@ -2869,6 +2877,7 @@ static JNINativeMethod methods[] = {
{CC"lockCritical", CC"()V", (void*)&WB_LockCritical},
{CC"unlockCritical", CC"()V", (void*)&WB_UnlockCritical},
{CC"setVirtualThreadsNotifyJvmtiMode", CC"(Z)Z", (void*)&WB_SetVirtualThreadsNotifyJvmtiMode},
{CC"preTouchMemory", CC"(JJ)V", (void*)&WB_PreTouchMemory},
};


Expand Down
7 changes: 7 additions & 0 deletions src/hotspot/share/runtime/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,13 @@ const int ObjectAlignmentInBytes = 8;
"1: monitors & legacy stack-locking (LM_LEGACY, default), " \
"2: monitors & new lightweight locking (LM_LIGHTWEIGHT)") \
range(0, 2) \
\
product(uint, TrimNativeHeapInterval, 0, EXPERIMENTAL, \
"Interval, in ms, at which the JVM will trim the native heap if " \
"the platform supports that. Lower values will reclaim memory " \
"more eagerly at the cost of higher overhead. A value of 0 " \
"(default) disables native heap trimming.") \
range(0, UINT_MAX) \

// end of RUNTIME_FLAGS

Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/share/runtime/java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#include "runtime/task.hpp"
#include "runtime/threads.hpp"
#include "runtime/timer.hpp"
#include "runtime/trimNativeHeap.hpp"
#include "runtime/vmOperations.hpp"
#include "runtime/vmThread.hpp"
#include "runtime/vm_version.hpp"
Expand Down Expand Up @@ -477,6 +478,8 @@ void before_exit(JavaThread* thread, bool halt) {
StatSampler::disengage();
StatSampler::destroy();

NativeHeapTrimmer::cleanup();

// Stop concurrent GC threads
Universe::heap()->stop();

Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/runtime/synchronizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "runtime/synchronizer.hpp"
#include "runtime/threads.hpp"
#include "runtime/timer.hpp"
#include "runtime/trimNativeHeap.hpp"
#include "runtime/vframe.hpp"
#include "runtime/vmThread.hpp"
#include "utilities/align.hpp"
Expand Down Expand Up @@ -1646,6 +1647,7 @@ class VM_RendezvousGCThreads : public VM_Operation {
};

static size_t delete_monitors(GrowableArray<ObjectMonitor*>* delete_list) {
NativeHeapTrimmer::SuspendMark sm("monitor deletion");
size_t count = 0;
for (ObjectMonitor* monitor: *delete_list) {
delete monitor;
Expand Down
5 changes: 5 additions & 0 deletions src/hotspot/share/runtime/threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
#include "runtime/threadSMR.inline.hpp"
#include "runtime/timer.hpp"
#include "runtime/timerTrace.hpp"
#include "runtime/trimNativeHeap.hpp"
#include "runtime/vmOperations.hpp"
#include "runtime/vm_version.hpp"
#include "services/attachListener.hpp"
Expand Down Expand Up @@ -759,6 +760,10 @@ jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
}
#endif

if (NativeHeapTrimmer::enabled()) {
NativeHeapTrimmer::initialize();
}

// Always call even when there are not JVMTI environments yet, since environments
// may be attached late and JVMTI must track phases of VM execution
JvmtiExport::enter_live_phase();
Expand Down
Loading

1 comment on commit 9e4fc56

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.