Path: blob/master/src/java.rmi/share/classes/sun/rmi/transport/GC.java
41154 views
/*1* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.rmi.transport;2627import java.security.AccessController;28import java.security.PrivilegedAction;29import java.util.SortedSet;30import java.util.TreeSet;31import jdk.internal.misc.InnocuousThread;323334/**35* Support for garbage-collection latency requests.36*37* @author Mark Reinhold38* @since 1.239*/4041@SuppressWarnings("removal")42class GC {4344private GC() { } /* To prevent instantiation */454647/* Latency-target value indicating that there's no active target48*/49private static final long NO_TARGET = Long.MAX_VALUE;5051/* The current latency target, or NO_TARGET if there is no target52*/53private static long latencyTarget = NO_TARGET;5455/* The daemon thread that implements the latency-target mechanism,56* or null if there is presently no daemon thread57*/58private static Thread daemon = null;5960/* The lock object for the latencyTarget and daemon fields. The daemon61* thread, if it exists, waits on this lock for notification that the62* latency target has changed.63*/64private static class LatencyLock extends Object { };65private static Object lock = new LatencyLock();666768/**69* Returns the maximum <em>object-inspection age</em>, which is the number70* of real-time milliseconds that have elapsed since the71* least-recently-inspected heap object was last inspected by the garbage72* collector.73*74* <p> For simple stop-the-world collectors this value is just the time75* since the most recent collection. For generational collectors it is the76* time since the oldest generation was most recently collected. Other77* collectors are free to return a pessimistic estimate of the elapsed78* time, or simply the time since the last full collection was performed.79*80* <p> Note that in the presence of reference objects, a given object that81* is no longer strongly reachable may have to be inspected multiple times82* before it can be reclaimed.83*/84public static native long maxObjectInspectionAge();8586static {87AccessController.doPrivileged(new PrivilegedAction<Void>() {88public Void run() {89System.loadLibrary("rmi");90return null;91}});92}9394private static class Daemon implements Runnable {9596@Override97public void run() {98for (;;) {99long l;100synchronized (lock) {101102l = latencyTarget;103if (l == NO_TARGET) {104/* No latency target, so exit */105GC.daemon = null;106return;107}108109long d = maxObjectInspectionAge();110if (d >= l) {111/* Do a full collection. There is a remote possibility112* that a full collection will occurr between the time113* we sample the inspection age and the time the GC114* actually starts, but this is sufficiently unlikely115* that it doesn't seem worth the more expensive JVM116* interface that would be required.117*/118System.gc();119d = 0;120}121122/* Wait for the latency period to expire,123* or for notification that the period has changed124*/125try {126lock.wait(l - d);127} catch (InterruptedException x) {128continue;129}130}131}132}133134/* Create a new daemon thread */135public static void create() {136PrivilegedAction<Void> pa = new PrivilegedAction<Void>() {137public Void run() {138Thread t = InnocuousThread.newSystemThread("RMI GC Daemon",139new Daemon());140assert t.getContextClassLoader() == null;141t.setDaemon(true);142t.setPriority(Thread.MIN_PRIORITY + 1);143t.start();144GC.daemon = t;145return null;146}};147AccessController.doPrivileged(pa);148}149150}151152153/* Sets the latency target to the given value.154* Must be invoked while holding the lock.155*/156private static void setLatencyTarget(long ms) {157latencyTarget = ms;158if (daemon == null) {159/* Create a new daemon thread */160Daemon.create();161} else {162/* Notify the existing daemon thread163* that the lateency target has changed164*/165lock.notify();166}167}168169170/**171* Represents an active garbage-collection latency request. Instances of172* this class are created by the <code>{@link #requestLatency}</code>173* method. Given a request, the only interesting operation is that of174* cancellation.175*/176public static class LatencyRequest177implements Comparable<LatencyRequest> {178179/* Instance counter, used to generate unique identifers */180private static long counter = 0;181182/* Sorted set of active latency requests */183private static SortedSet<LatencyRequest> requests = null;184185/* Examine the request set and reset the latency target if necessary.186* Must be invoked while holding the lock.187*/188private static void adjustLatencyIfNeeded() {189if ((requests == null) || requests.isEmpty()) {190if (latencyTarget != NO_TARGET) {191setLatencyTarget(NO_TARGET);192}193} else {194LatencyRequest r = requests.first();195if (r.latency != latencyTarget) {196setLatencyTarget(r.latency);197}198}199}200201/* The requested latency, or NO_TARGET202* if this request has been cancelled203*/204private long latency;205206/* Unique identifier for this request */207private long id;208209private LatencyRequest(long ms) {210if (ms <= 0) {211throw new IllegalArgumentException("Non-positive latency: "212+ ms);213}214this.latency = ms;215synchronized (lock) {216this.id = ++counter;217if (requests == null) {218requests = new TreeSet<LatencyRequest>();219}220requests.add(this);221adjustLatencyIfNeeded();222}223}224225/**226* Cancels this latency request.227*228* @throws IllegalStateException229* If this request has already been cancelled230*/231public void cancel() {232synchronized (lock) {233if (this.latency == NO_TARGET) {234throw new IllegalStateException("Request already"235+ " cancelled");236}237if (!requests.remove(this)) {238throw new InternalError("Latency request "239+ this + " not found");240}241if (requests.isEmpty()) requests = null;242this.latency = NO_TARGET;243adjustLatencyIfNeeded();244}245}246247public int compareTo(LatencyRequest r) {248long d = this.latency - r.latency;249if (d == 0) d = this.id - r.id;250return (d < 0) ? -1 : ((d > 0) ? +1 : 0);251}252253public String toString() {254return (LatencyRequest.class.getName()255+ "[" + latency + "," + id + "]");256}257258}259260261/**262* Makes a new request for a garbage-collection latency of the given263* number of real-time milliseconds. A low-priority daemon thread makes a264* best effort to ensure that the maximum object-inspection age never265* exceeds the smallest of the currently active requests.266*267* @param latency268* The requested latency269*270* @throws IllegalArgumentException271* If the given <code>latency</code> is non-positive272*/273public static LatencyRequest requestLatency(long latency) {274return new LatencyRequest(latency);275}276277278/**279* Returns the current smallest garbage-collection latency request, or zero280* if there are no active requests.281*/282public static long currentLatencyTarget() {283long t = latencyTarget;284return (t == NO_TARGET) ? 0 : t;285}286287}288289290