GC.java revision 0
869N/A/*
869N/A * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
869N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
869N/A *
869N/A * This code is free software; you can redistribute it and/or modify it
869N/A * under the terms of the GNU General Public License version 2 only, as
869N/A * published by the Free Software Foundation. Sun designates this
869N/A * particular file as subject to the "Classpath" exception as provided
869N/A * by Sun in the LICENSE file that accompanied this code.
869N/A *
869N/A * This code is distributed in the hope that it will be useful, but WITHOUT
869N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
869N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
869N/A * version 2 for more details (a copy is included in the LICENSE file that
869N/A * accompanied this code).
869N/A *
869N/A * You should have received a copy of the GNU General Public License version
873N/A * 2 along with this work; if not, write to the Free Software Foundation,
869N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
869N/A *
869N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
869N/A * CA 95054 USA or visit www.sun.com if you need additional information or
869N/A * have any questions.
869N/A */
869N/A
0N/Apackage sun.misc;
0N/A
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
869N/Aimport java.util.SortedSet;
0N/Aimport java.util.TreeSet;
0N/A
0N/A
869N/A/**
0N/A * Support for garbage-collection latency requests.
869N/A *
0N/A * @author Mark Reinhold
869N/A * @since 1.2
869N/A */
869N/A
0N/Apublic class GC {
869N/A
48N/A private GC() { } /* To prevent instantiation */
869N/A
0N/A
869N/A /* Latency-target value indicating that there's no active target
716N/A */
869N/A private static final long NO_TARGET = Long.MAX_VALUE;
0N/A
0N/A /* The current latency target, or NO_TARGET if there is no target
869N/A */
868N/A private static long latencyTarget = NO_TARGET;
983N/A
1004N/A /* The daemon thread that implements the latency-target mechanism,
1007N/A * or null if there is presently no daemon thread
1378N/A */
1411N/A private static Thread daemon = null;
1503N/A
869N/A /* The lock object for the latencyTarget and daemon fields. The daemon
869N/A * thread, if it exists, waits on this lock for notification that the
1004N/A * latency target has changed.
0N/A */
0N/A private static class LatencyLock extends Object { };
869N/A private static Object lock = new LatencyLock();
868N/A
0N/A
0N/A /**
65N/A * Returns the maximum <em>object-inspection age</em>, which is the number
869N/A * of real-time milliseconds that have elapsed since the
868N/A * least-recently-inspected heap object was last inspected by the garbage
65N/A * collector.
869N/A *
65N/A * <p> For simple stop-the-world collectors this value is just the time
65N/A * since the most recent collection. For generational collectors it is the
65N/A * time since the oldest generation was most recently collected. Other
65N/A * collectors are free to return a pessimistic estimate of the elapsed
65N/A * time, or simply the time since the last full collection was performed.
65N/A *
65N/A * <p> Note that in the presence of reference objects, a given object that
65N/A * is no longer strongly reachable may have to be inspected multiple times
65N/A * before it can be reclaimed.
65N/A */
65N/A public static native long maxObjectInspectionAge();
65N/A
65N/A
65N/A private static class Daemon extends Thread {
65N/A
0N/A public void run() {
869N/A for (;;) {
868N/A long l;
0N/A synchronized (lock) {
0N/A
0N/A l = latencyTarget;
0N/A if (l == NO_TARGET) {
0N/A /* No latency target, so exit */
0N/A GC.daemon = null;
0N/A return;
1501N/A }
0N/A
0N/A long d = maxObjectInspectionAge();
869N/A if (d >= l) {
868N/A /* Do a full collection. There is a remote possibility
0N/A * that a full collection will occurr between the time
0N/A * we sample the inspection age and the time the GC
869N/A * actually starts, but this is sufficiently unlikely
0N/A * that it doesn't seem worth the more expensive JVM
0N/A * interface that would be required.
869N/A */
868N/A System.gc();
869N/A d = 0;
869N/A }
868N/A
868N/A /* Wait for the latency period to expire,
869N/A * or for notification that the period has changed
869N/A */
0N/A try {
0N/A lock.wait(l - d);
48N/A } catch (InterruptedException x) {
869N/A continue;
869N/A }
869N/A }
868N/A }
869N/A }
868N/A
869N/A private Daemon(ThreadGroup tg) {
1155N/A super(tg, "GC Daemon");
1155N/A }
1155N/A
1155N/A /* Create a new daemon thread in the root thread group */
1155N/A public static void create() {
1155N/A PrivilegedAction pa = new PrivilegedAction() {
1155N/A public Object run() {
1155N/A ThreadGroup tg = Thread.currentThread().getThreadGroup();
1155N/A for (ThreadGroup tgn = tg;
1155N/A tgn != null;
1155N/A tg = tgn, tgn = tg.getParent());
1155N/A Daemon d = new Daemon(tg);
1155N/A d.setDaemon(true);
1155N/A d.setPriority(Thread.MIN_PRIORITY + 1);
1155N/A d.start();
0N/A GC.daemon = d;
0N/A return null;
869N/A }};
868N/A AccessController.doPrivileged(pa);
0N/A }
0N/A
848N/A }
848N/A
848N/A
869N/A /* Sets the latency target to the given value.
868N/A * Must be invoked while holding the lock.
848N/A */
0N/A private static void setLatencyTarget(long ms) {
0N/A latencyTarget = ms;
0N/A if (daemon == null) {
869N/A /* Create a new daemon thread */
0N/A Daemon.create();
0N/A } else {
0N/A /* Notify the existing daemon thread
868N/A * that the lateency target has changed
868N/A */
868N/A lock.notify();
868N/A }
869N/A }
868N/A
115N/A
868N/A /**
868N/A * Represents an active garbage-collection latency request. Instances of
868N/A * this class are created by the <code>{@link #requestLatency}</code>
868N/A * method. Given a request, the only interesting operation is that of
868N/A * cancellation.
869N/A */
868N/A public static class LatencyRequest implements Comparable {
868N/A
868N/A /* Instance counter, used to generate unique identifers */
868N/A private static long counter = 0;
868N/A
868N/A /* Sorted set of active latency requests */
868N/A private static SortedSet requests = null;
869N/A
868N/A /* Examine the request set and reset the latency target if necessary.
868N/A * Must be invoked while holding the lock.
868N/A */
868N/A private static void adjustLatencyIfNeeded() {
868N/A if ((requests == null) || requests.isEmpty()) {
869N/A if (latencyTarget != NO_TARGET) {
868N/A setLatencyTarget(NO_TARGET);
868N/A }
868N/A } else {
868N/A LatencyRequest r = (LatencyRequest)requests.first();
868N/A if (r.latency != latencyTarget) {
869N/A setLatencyTarget(r.latency);
868N/A }
868N/A }
868N/A }
868N/A
868N/A /* The requested latency, or NO_TARGET
868N/A * if this request has been cancelled
868N/A */
869N/A private long latency;
868N/A
868N/A /* Unique identifier for this request */
868N/A private long id;
868N/A
868N/A private LatencyRequest(long ms) {
868N/A if (ms <= 0) {
868N/A throw new IllegalArgumentException("Non-positive latency: "
869N/A + ms);
868N/A }
0N/A this.latency = ms;
869N/A synchronized (lock) {
0N/A this.id = ++counter;
868N/A if (requests == null) {
869N/A requests = new TreeSet();
868N/A }
0N/A requests.add(this);
869N/A adjustLatencyIfNeeded();
0N/A }
868N/A }
869N/A
868N/A /**
868N/A * Cancels this latency request.
869N/A *
869N/A * @throws IllegalStateException
869N/A * If this request has already been cancelled
869N/A */
869N/A public void cancel() {
869N/A synchronized (lock) {
869N/A if (this.latency == NO_TARGET) {
869N/A throw new IllegalStateException("Request already"
869N/A + " cancelled");
869N/A }
869N/A if (!requests.remove(this)) {
869N/A throw new InternalError("Latency request "
869N/A + this + " not found");
869N/A }
869N/A if (requests.isEmpty()) requests = null;
869N/A this.latency = NO_TARGET;
869N/A adjustLatencyIfNeeded();
869N/A }
869N/A }
869N/A
869N/A public int compareTo(Object o) {
869N/A LatencyRequest r = (LatencyRequest)o;
869N/A long d = this.latency - r.latency;
869N/A if (d == 0) d = this.id - r.id;
869N/A return (d < 0) ? -1 : ((d > 0) ? +1 : 0);
869N/A }
869N/A
869N/A public String toString() {
869N/A return (LatencyRequest.class.getName()
869N/A + "[" + latency + "," + id + "]");
869N/A }
869N/A
869N/A }
869N/A
869N/A
869N/A /**
869N/A * Makes a new request for a garbage-collection latency of the given
869N/A * number of real-time milliseconds. A low-priority daemon thread makes a
869N/A * best effort to ensure that the maximum object-inspection age never
869N/A * exceeds the smallest of the currently active requests.
869N/A *
869N/A * @param latency
869N/A * The requested latency
869N/A *
869N/A * @throws IllegalArgumentException
869N/A * If the given <code>latency</code> is non-positive
869N/A */
869N/A public static LatencyRequest requestLatency(long latency) {
869N/A return new LatencyRequest(latency);
869N/A }
869N/A
0N/A
824N/A /**
869N/A * Returns the current smallest garbage-collection latency request, or zero
868N/A * if there are no active requests.
824N/A */
824N/A public static long currentLatencyTarget() {
824N/A long t = latencyTarget;
824N/A return (t == NO_TARGET) ? 0 : t;
824N/A }
869N/A
824N/A}
824N/A