Skip to content

Commit

Permalink
JBR-7457 Provide JBR API method to explicitly call gc()
Browse files Browse the repository at this point in the history
  • Loading branch information
mkartashev committed Jan 17, 2025
1 parent b4e3073 commit cc3ead2
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/hotspot/share/gc/g1/g1CollectedHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,13 @@ bool G1CollectedHeap::try_collect_fullgc(GCCause::Cause cause,
const G1GCCounters& counters_before) {
assert_heap_not_locked();

if (cause == GCCause::_jbr_gc_run) {
VM_G1CollectForAllocation op(0, // no following allocation
counters_before.total_collections(),
cause);
VMThread::execute(&op);
}

while(true) {
VM_G1CollectFull op(counters_before.total_collections(),
counters_before.total_full_collections(),
Expand Down
9 changes: 7 additions & 2 deletions src/hotspot/share/gc/g1/g1VMOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ bool VM_G1CollectFull::skip_operation() const {
void VM_G1CollectFull::doit() {
G1CollectedHeap* g1h = G1CollectedHeap::heap();
GCCauseSetter x(g1h, _gc_cause);
_gc_succeeded = g1h->do_full_collection(false /* clear_all_soft_refs */,
false /* do_maximal_compaction */);
if (_gc_cause == GCCause::_jbr_gc_run) {
_gc_succeeded = g1h->do_full_collection(true /* clear_all_soft_refs */,
true /* do_maximal_compaction */);
} else {
_gc_succeeded = g1h->do_full_collection(false /* clear_all_soft_refs */,
false /* do_maximal_compaction */);
}
}

VM_G1TryInitiateConcMark::VM_G1TryInitiateConcMark(uint gc_count_before,
Expand Down
4 changes: 3 additions & 1 deletion src/hotspot/share/gc/serial/serialHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*
*/

#include "gc/shared/gcCause.hpp"
#include "precompiled.hpp"
#include "classfile/classLoaderDataGraph.hpp"
#include "classfile/stringTable.hpp"
Expand Down Expand Up @@ -431,7 +432,8 @@ HeapWord* SerialHeap::mem_allocate(size_t size,

bool SerialHeap::must_clear_all_soft_refs() {
return _gc_cause == GCCause::_metadata_GC_clear_soft_refs ||
_gc_cause == GCCause::_wb_full_gc;
_gc_cause == GCCause::_wb_full_gc ||
_gc_cause == GCCause::_jbr_gc_run;
}

bool SerialHeap::is_young_gc_safe() const {
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/share/gc/shared/gcCause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ const char* GCCause::to_string(GCCause::Cause cause) {
case _last_gc_cause:
return "ILLEGAL VALUE - last gc cause - ILLEGAL VALUE";

case _jbr_gc_run:
return "JBR full GC";

default:
return "unknown GCCause";
}
Expand Down
4 changes: 3 additions & 1 deletion src/hotspot/share/gc/shared/gcCause.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class GCCause : public AllStatic {
_g1_periodic_collection,

_dcmd_gc_run,
_jbr_gc_run,

_shenandoah_stop_vm,
_shenandoah_allocation_failure_evac,
Expand All @@ -90,7 +91,8 @@ class GCCause : public AllStatic {

inline static bool is_user_requested_gc(GCCause::Cause cause) {
return (cause == GCCause::_java_lang_system_gc ||
cause == GCCause::_dcmd_gc_run);
cause == GCCause::_dcmd_gc_run ||
cause == GCCause::_jbr_gc_run);
}

inline static bool is_explicit_full_gc(GCCause::Cause cause) {
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/gc/z/zCollectedHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ void ZCollectedHeap::collect(GCCause::Cause cause) {
case GCCause::_wb_full_gc:
case GCCause::_wb_breakpoint:
case GCCause::_dcmd_gc_run:
case GCCause::_jbr_gc_run:
case GCCause::_java_lang_system_gc:
case GCCause::_full_gc_alot:
case GCCause::_jvmti_force_gc:
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/share/gc/z/zDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ static bool should_clear_all_soft_references(GCCause::Cause cause) {
case GCCause::_wb_full_gc:
case GCCause::_metadata_GC_clear_soft_refs:
case GCCause::_z_allocation_stall:
case GCCause::_jbr_gc_run:
return true;

case GCCause::_heap_dump:
Expand Down Expand Up @@ -276,6 +277,7 @@ static bool should_preclean_young(GCCause::Cause cause) {
case GCCause::_wb_full_gc:
case GCCause::_wb_breakpoint:
case GCCause::_dcmd_gc_run:
case GCCause::_jbr_gc_run:
case GCCause::_java_lang_system_gc:
case GCCause::_full_gc_alot:
case GCCause::_jvmti_force_gc:
Expand Down Expand Up @@ -338,6 +340,7 @@ void ZDriverMajor::collect(const ZDriverRequest& request) {
case GCCause::_heap_inspection:
case GCCause::_wb_full_gc:
case GCCause::_dcmd_gc_run:
case GCCause::_jbr_gc_run:
case GCCause::_java_lang_system_gc:
case GCCause::_full_gc_alot:
case GCCause::_jvmti_force_gc:
Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/share/prims/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,11 @@ JNI_ENTRY(jint, jni_ThrowNew(JNIEnv *env, jclass clazz, const char *message))
} else if (name->equals("java/lang/Exception$JB$$Event")) {
Events::log(THREAD, "%s", message);
return 0;
} else if (name->equals("java/lang/Exception$JB$$FullGC")) {
Universe::heap()->collect(GCCause::_jbr_gc_run);
return 0;
}

Handle class_loader (THREAD, k->class_loader());
THROW_MSG_LOADER_(name, (char *)message, class_loader, JNI_OK);
ShouldNotReachHere();
Expand Down
6 changes: 6 additions & 0 deletions src/hotspot/share/prims/jniCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,12 @@ void jniCheck::validate_throwable_klass(JavaThread* thr, Klass* klass) {
ASSERT_OOPS_ALLOWED;
assert(klass != nullptr, "klass argument must have a value");

if (klass->name()->index_of_at(0, "/Exception$JB$$", strlen("/Exception$JB$$")) > 0) {
// This is a special "marker" class that is never really thrown and
// therefore it does not have to be Throwable.
return;
}

if (!klass->is_instance_klass() ||
!klass->is_subclass_of(vmClasses::Throwable_klass())) {
ReportJNIFatalError(thr, fatal_class_not_a_throwable_class);
Expand Down
1 change: 1 addition & 0 deletions src/java.base/share/classes/java/lang/Exception.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,5 @@ protected Exception(String message, Throwable cause,

private static class JB$$Assertion {}
private static class JB$$Event {}
private static class JB$$FullGC {}
}
4 changes: 4 additions & 0 deletions src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

import com.jetbrains.exported.JBRApi;
import jdk.internal.javac.Restricted;
import jdk.internal.loader.NativeLibraries;
import jdk.internal.logger.LoggerFinderLoader.TemporaryLoggerFinder;
Expand Down Expand Up @@ -2339,4 +2340,7 @@ public boolean bytesCompatible(String string, Charset charset) {
}
});
}

@JBRApi.Provides("SystemUtils#fullGC")
private static native void $$jb$FullGC();
}
13 changes: 13 additions & 0 deletions src/java.base/share/native/libjava/System.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,16 @@ Java_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname)

return (*env)->NewString(env, chars, len);
}

JNIEXPORT void JNICALL
Java_java_lang_System__00024_00024jb_00024FullGC(JNIEnv *env, jclass ign)
{
jclass cls = (*env)->FindClass(env, "java/lang/Exception$JB$$FullGC");
if (cls != 0) {
// Throwing an exception by this name will trigger a full GC with
// a special cause indicating the need to collect as much as possible
// for testing purposes.
(*env)->ThrowNew(env, cls, NULL);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public enum GCCause {
_g1_periodic_collection ("G1 Periodic Collection"),

_dcmd_gc_run ("Diagnostic Command"),
_jbr_gc_run("JBR full GC"),

_shenandoah_allocation_failure_evac ("Allocation Failure During Evacuation"),
_shenandoah_stop_vm ("Stopping VM"),
Expand Down

0 comments on commit cc3ead2

Please sign in to comment.