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