# HG changeset patch # User shade # Date 1547826024 -3600 # Node ID e8ed617dc2bc70b82d74d6a0c35106381e14a528 # Parent 09909d1356c8b610f24a48f48b8ced71203946b9 8217014: Epsilon should not ignore Metadata GC causes Reviewed-by: stuefe, zgu diff -r 09909d1356c8 -r e8ed617dc2bc src/hotspot/share/gc/epsilon/epsilonHeap.cpp --- a/src/hotspot/share/gc/epsilon/epsilonHeap.cpp Fri Jan 18 16:21:07 2019 +0100 +++ b/src/hotspot/share/gc/epsilon/epsilonHeap.cpp Fri Jan 18 16:40:24 2019 +0100 @@ -161,12 +161,8 @@ { size_t last = _last_heap_print; if ((used - last >= _step_heap_print) && Atomic::cmpxchg(used, &_last_heap_print, last) == last) { - log_info(gc)("Heap: " SIZE_FORMAT "M reserved, " SIZE_FORMAT "M (%.2f%%) committed, " SIZE_FORMAT "M (%.2f%%) used", - max_capacity() / M, - capacity() / M, - capacity() * 100.0 / max_capacity(), - used / M, - used * 100.0 / max_capacity()); + print_heap_info(used); + print_metaspace_info(); } } @@ -268,13 +264,26 @@ } void EpsilonHeap::collect(GCCause::Cause cause) { - log_info(gc)("GC request for \"%s\" is ignored", GCCause::to_string(cause)); + switch (cause) { + case GCCause::_metadata_GC_threshold: + case GCCause::_metadata_GC_clear_soft_refs: + // Receiving these causes means the VM itself entered the safepoint for metadata collection. + // While Epsilon does not do GC, it has to perform sizing adjustments, otherwise we would + // re-enter the safepoint again very soon. + + assert(SafepointSynchronize::is_at_safepoint(), "Expected at safepoint"); + log_info(gc)("GC request for \"%s\" is handled", GCCause::to_string(cause)); + MetaspaceGC::compute_new_size(); + print_metaspace_info(); + break; + default: + log_info(gc)("GC request for \"%s\" is ignored", GCCause::to_string(cause)); + } _monitoring_support->update_counters(); } void EpsilonHeap::do_full_collection(bool clear_all_soft_refs) { - log_info(gc)("Full GC request for \"%s\" is ignored", GCCause::to_string(gc_cause())); - _monitoring_support->update_counters(); + collect(gc_cause()); } void EpsilonHeap::safe_object_iterate(ObjectClosure *cl) { @@ -289,13 +298,46 @@ st->print_cr("Allocation space:"); _space->print_on(st); + + MetaspaceUtils::print_on(st); } void EpsilonHeap::print_tracing_info() const { - Log(gc) log; - size_t allocated_kb = used() / K; - log.info("Total allocated: " SIZE_FORMAT " KB", - allocated_kb); - log.info("Average allocation rate: " SIZE_FORMAT " KB/sec", - (size_t)(allocated_kb * NANOSECS_PER_SEC / os::elapsed_counter())); + print_heap_info(used()); + print_metaspace_info(); } + +void EpsilonHeap::print_heap_info(size_t used) const { + size_t reserved = max_capacity(); + size_t committed = capacity(); + + if (reserved != 0) { + log_info(gc)("Heap: " SIZE_FORMAT "%s reserved, " SIZE_FORMAT "%s (%.2f%%) committed, " + SIZE_FORMAT "%s (%.2f%%) used", + byte_size_in_proper_unit(reserved), proper_unit_for_byte_size(reserved), + byte_size_in_proper_unit(committed), proper_unit_for_byte_size(committed), + committed * 100.0 / reserved, + byte_size_in_proper_unit(used), proper_unit_for_byte_size(used), + used * 100.0 / reserved); + } else { + log_info(gc)("Heap: no reliable data"); + } +} + +void EpsilonHeap::print_metaspace_info() const { + size_t reserved = MetaspaceUtils::reserved_bytes(); + size_t committed = MetaspaceUtils::committed_bytes(); + size_t used = MetaspaceUtils::used_bytes(); + + if (reserved != 0) { + log_info(gc, metaspace)("Metaspace: " SIZE_FORMAT "%s reserved, " SIZE_FORMAT "%s (%.2f%%) committed, " + SIZE_FORMAT "%s (%.2f%%) used", + byte_size_in_proper_unit(reserved), proper_unit_for_byte_size(reserved), + byte_size_in_proper_unit(committed), proper_unit_for_byte_size(committed), + committed * 100.0 / reserved, + byte_size_in_proper_unit(used), proper_unit_for_byte_size(used), + used * 100.0 / reserved); + } else { + log_info(gc, metaspace)("Metaspace: no reliable data"); + } +} diff -r 09909d1356c8 -r e8ed617dc2bc src/hotspot/share/gc/epsilon/epsilonHeap.hpp --- a/src/hotspot/share/gc/epsilon/epsilonHeap.hpp Fri Jan 18 16:21:07 2019 +0100 +++ b/src/hotspot/share/gc/epsilon/epsilonHeap.hpp Fri Jan 18 16:40:24 2019 +0100 @@ -147,6 +147,10 @@ virtual void print_on(outputStream* st) const; virtual void print_tracing_info() const; +private: + void print_heap_info(size_t used) const; + void print_metaspace_info() const; + }; #endif // SHARE_GC_EPSILON_EPSILONHEAP_HPP diff -r 09909d1356c8 -r e8ed617dc2bc test/hotspot/jtreg/gc/epsilon/TestClasses.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/hotspot/jtreg/gc/epsilon/TestClasses.java Fri Jan 18 16:40:24 2019 +0100 @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2019, Red Hat, Inc. 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. + * + * 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. + */ + +/** + * @test TestClasses + * @key gc + * @requires vm.gc.Epsilon & !vm.graal.enabled + * @summary Epsilon is able to allocate a lot of classes + * + * @modules java.base/jdk.internal.org.objectweb.asm + * java.base/jdk.internal.misc + * + * @run main/othervm -Xmx128m -XX:MetaspaceSize=1m -XX:MaxMetaspaceSize=32m -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xlog:gc -Xlog:gc+metaspace TestClasses + */ + +import jdk.internal.org.objectweb.asm.ClassWriter; +import jdk.internal.org.objectweb.asm.Opcodes; + +import java.util.*; +import java.io.*; +import java.nio.*; +import java.nio.file.*; + +public class TestClasses { + + static final int COUNT = 32*1024; + + static volatile Object sink; + + static class MyClassLoader extends ClassLoader { + public byte[] createClass(String name) { + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, name, null, "java/lang/Object", null); + return cw.toByteArray(); + } + + public Class loadClass(String name) throws ClassNotFoundException { + if (!name.startsWith("Dummy")) { + return super.loadClass(name); + } + byte[] cls = createClass(name); + return defineClass(name, cls, 0, cls.length, null); + } + } + + public static void main(String[] args) throws Exception { + ClassLoader cl = new MyClassLoader(); + for (int c = 0; c < COUNT; c++) { + Class clazz = Class.forName("Dummy" + c, true, cl); + if (clazz.getClassLoader() != cl) { + throw new IllegalStateException("Should have loaded by target loader"); + } + sink = c; + } + } +}