Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/misc/GC.java
38829 views
/*1* Copyright (c) 1998, 2008, 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.misc;2627import java.security.AccessController;28import java.security.PrivilegedAction;29import java.util.SortedSet;30import java.util.TreeSet;313233/**34* Support for garbage-collection latency requests.35*36* @author Mark Reinhold37* @since 1.238*/3940public class GC {4142private GC() { } /* To prevent instantiation */434445/* Latency-target value indicating that there's no active target46*/47private static final long NO_TARGET = Long.MAX_VALUE;4849/* The current latency target, or NO_TARGET if there is no target50*/51private static long latencyTarget = NO_TARGET;5253/* The daemon thread that implements the latency-target mechanism,54* or null if there is presently no daemon thread55*/56private static Thread daemon = null;5758/* The lock object for the latencyTarget and daemon fields. The daemon59* thread, if it exists, waits on this lock for notification that the60* latency target has changed.61*/62private static class LatencyLock extends Object { };63private static Object lock = new LatencyLock();646566/**67* Returns the maximum <em>object-inspection age</em>, which is the number68* of real-time milliseconds that have elapsed since the69* least-recently-inspected heap object was last inspected by the garbage70* collector.71*72* <p> For simple stop-the-world collectors this value is just the time73* since the most recent collection. For generational collectors it is the74* time since the oldest generation was most recently collected. Other75* collectors are free to return a pessimistic estimate of the elapsed76* time, or simply the time since the last full collection was performed.77*78* <p> Note that in the presence of reference objects, a given object that79* is no longer strongly reachable may have to be inspected multiple times80* before it can be reclaimed.81*/82public static native long maxObjectInspectionAge();838485private static class Daemon extends Thread {8687public void run() {88for (;;) {89long l;90synchronized (lock) {9192l = latencyTarget;93if (l == NO_TARGET) {94/* No latency target, so exit */95GC.daemon = null;96return;97}9899long d = maxObjectInspectionAge();100if (d >= l) {101/* Do a full collection. There is a remote possibility102* that a full collection will occurr between the time103* we sample the inspection age and the time the GC104* actually starts, but this is sufficiently unlikely105* that it doesn't seem worth the more expensive JVM106* interface that would be required.107*/108System.gc();109d = 0;110}111112/* Wait for the latency period to expire,113* or for notification that the period has changed114*/115try {116lock.wait(l - d);117} catch (InterruptedException x) {118continue;119}120}121}122}123124private Daemon(ThreadGroup tg) {125super(tg, "GC Daemon");126}127128/* Create a new daemon thread in the root thread group */129public static void create() {130PrivilegedAction<Void> pa = new PrivilegedAction<Void>() {131public Void run() {132ThreadGroup tg = Thread.currentThread().getThreadGroup();133for (ThreadGroup tgn = tg;134tgn != null;135tg = tgn, tgn = tg.getParent());136Daemon d = new Daemon(tg);137d.setDaemon(true);138d.setPriority(Thread.MIN_PRIORITY + 1);139d.start();140GC.daemon = d;141return null;142}};143AccessController.doPrivileged(pa);144}145146}147148149/* Sets the latency target to the given value.150* Must be invoked while holding the lock.151*/152private static void setLatencyTarget(long ms) {153latencyTarget = ms;154if (daemon == null) {155/* Create a new daemon thread */156Daemon.create();157} else {158/* Notify the existing daemon thread159* that the lateency target has changed160*/161lock.notify();162}163}164165166/**167* Represents an active garbage-collection latency request. Instances of168* this class are created by the <code>{@link #requestLatency}</code>169* method. Given a request, the only interesting operation is that of170* cancellation.171*/172public static class LatencyRequest173implements Comparable<LatencyRequest> {174175/* Instance counter, used to generate unique identifers */176private static long counter = 0;177178/* Sorted set of active latency requests */179private static SortedSet<LatencyRequest> requests = null;180181/* Examine the request set and reset the latency target if necessary.182* Must be invoked while holding the lock.183*/184private static void adjustLatencyIfNeeded() {185if ((requests == null) || requests.isEmpty()) {186if (latencyTarget != NO_TARGET) {187setLatencyTarget(NO_TARGET);188}189} else {190LatencyRequest r = requests.first();191if (r.latency != latencyTarget) {192setLatencyTarget(r.latency);193}194}195}196197/* The requested latency, or NO_TARGET198* if this request has been cancelled199*/200private long latency;201202/* Unique identifier for this request */203private long id;204205private LatencyRequest(long ms) {206if (ms <= 0) {207throw new IllegalArgumentException("Non-positive latency: "208+ ms);209}210this.latency = ms;211synchronized (lock) {212this.id = ++counter;213if (requests == null) {214requests = new TreeSet<LatencyRequest>();215}216requests.add(this);217adjustLatencyIfNeeded();218}219}220221/**222* Cancels this latency request.223*224* @throws IllegalStateException225* If this request has already been cancelled226*/227public void cancel() {228synchronized (lock) {229if (this.latency == NO_TARGET) {230throw new IllegalStateException("Request already"231+ " cancelled");232}233if (!requests.remove(this)) {234throw new InternalError("Latency request "235+ this + " not found");236}237if (requests.isEmpty()) requests = null;238this.latency = NO_TARGET;239adjustLatencyIfNeeded();240}241}242243public int compareTo(LatencyRequest r) {244long d = this.latency - r.latency;245if (d == 0) d = this.id - r.id;246return (d < 0) ? -1 : ((d > 0) ? +1 : 0);247}248249public String toString() {250return (LatencyRequest.class.getName()251+ "[" + latency + "," + id + "]");252}253254}255256257/**258* Makes a new request for a garbage-collection latency of the given259* number of real-time milliseconds. A low-priority daemon thread makes a260* best effort to ensure that the maximum object-inspection age never261* exceeds the smallest of the currently active requests.262*263* @param latency264* The requested latency265*266* @throws IllegalArgumentException267* If the given <code>latency</code> is non-positive268*/269public static LatencyRequest requestLatency(long latency) {270return new LatencyRequest(latency);271}272273274/**275* Returns the current smallest garbage-collection latency request, or zero276* if there are no active requests.277*/278public static long currentLatencyTarget() {279long t = latencyTarget;280return (t == NO_TARGET) ? 0 : t;281}282283}284285286