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: stuefe
Backport-of: 9e4fc568a6f1a93c84a84d6cc5220c6eb4e546a5
  • Loading branch information
shipilev committed Aug 25, 2023
1 parent 1421540 commit f667b35
Show file tree
Hide file tree
Showing 20 changed files with 847 additions and 7 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 @@ -47,7 +47,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 @@ -51,7 +51,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 os::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 @@ -49,6 +49,7 @@
#include "runtime/safepointVerifiers.hpp"
#include "runtime/timerTrace.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/trimNativeHeap.hpp"
#include "services/diagnosticCommand.hpp"
#include "utilities/concurrentHashTable.inline.hpp"
#include "utilities/concurrentHashTableTasks.inline.hpp"
Expand Down Expand Up @@ -431,6 +432,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 @@ -697,6 +698,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 @@ -187,6 +187,7 @@
LOG_TAG(timer) \
LOG_TAG(tlab) \
LOG_TAG(tracking) \
LOG_TAG(trimnative) /* trim native heap */ \
LOG_TAG(unload) /* Trace unloading of classes */ \
LOG_TAG(unshareable) \
LOG_TAG(update) \
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/task.hpp"
#include "runtime/threadCritical.hpp"
#include "services/memTracker.hpp"
#include "runtime/trimNativeHeap.hpp"
#include "utilities/ostream.hpp"

//--------------------------------------------------------------------------------------
Expand Down Expand Up @@ -136,6 +137,7 @@ class ChunkPool: public CHeapObj<mtInternal> {
}

static void clean() {
NativeHeapTrimmer::SuspendMark sm("chunk pool cleaner");
enum { BlocksToKeep = 5 };
_tiny_pool->free_all_but(BlocksToKeep);
_small_pool->free_all_but(BlocksToKeep);
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 @@ -2364,6 +2364,14 @@ WB_ENTRY(void, WB_UnlockCritical(JNIEnv* env, jobject wb))
GCLocker::unlock_critical(thread);
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 @@ -2631,6 +2639,7 @@ static JNINativeMethod methods[] = {

{CC"lockCritical", CC"()V", (void*)&WB_LockCritical},
{CC"unlockCritical", CC"()V", (void*)&WB_UnlockCritical},
{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 @@ -2093,6 +2093,13 @@ const intx ObjectAlignmentInBytes = 8;
\
develop(bool, TraceOptimizedUpcallStubs, false, \
"Trace optimized upcall stub generation") \
\
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 @@ -71,6 +71,7 @@
#include "runtime/task.hpp"
#include "runtime/thread.inline.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 @@ -467,6 +468,8 @@ void before_exit(JavaThread* thread, bool halt) {
StringDedup::stop();
}

NativeHeapTrimmer::cleanup();

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

Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/share/runtime/synchronizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "runtime/synchronizer.hpp"
#include "runtime/thread.inline.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 @@ -1544,6 +1545,8 @@ size_t ObjectSynchronizer::deflate_idle_monitors() {
}
}

NativeHeapTrimmer::SuspendMark sm("monitor deletion");

// After the handshake, safely free the ObjectMonitors that were
// deflated in this cycle.
size_t deleted_count = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/hotspot/share/runtime/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
#include "runtime/threadWXSetters.inline.hpp"
#include "runtime/timer.hpp"
#include "runtime/timerTrace.hpp"
#include "runtime/trimNativeHeap.hpp"
#include "runtime/vframe.inline.hpp"
#include "runtime/vframeArray.hpp"
#include "runtime/vframe_hp.hpp"
Expand Down Expand Up @@ -3082,6 +3083,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 f667b35

@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.