1 /*
   2  * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.rmi.transport;
  27 
  28 import java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import java.util.SortedSet;
  31 import java.util.TreeSet;
  32 import jdk.internal.misc.InnocuousThread;
  33 
  34 
  35 /**
  36  * Support for garbage-collection latency requests.
  37  *
  38  * @author   Mark Reinhold
  39  * @since    1.2
  40  */
  41 
  42 class GC {
  43 
  44     private GC() { }            /* To prevent instantiation */
  45 
  46 
  47     /* Latency-target value indicating that there's no active target
  48      */
  49     private static final long NO_TARGET = Long.MAX_VALUE;
  50 
  51     /* The current latency target, or NO_TARGET if there is no target
  52      */
  53     private static long latencyTarget = NO_TARGET;
  54 
  55     /* The daemon thread that implements the latency-target mechanism,
  56      * or null if there is presently no daemon thread
  57      */
  58     private static Thread daemon = null;
  59 
  60     /* The lock object for the latencyTarget and daemon fields.  The daemon
  61      * thread, if it exists, waits on this lock for notification that the
  62      * latency target has changed.
  63      */
  64     private static class LatencyLock extends Object { };
  65     private static Object lock = new LatencyLock();
  66 
  67 
  68     /**
  69      * Returns the maximum <em>object-inspection age</em>, which is the number
  70      * of real-time milliseconds that have elapsed since the
  71      * least-recently-inspected heap object was last inspected by the garbage
  72      * collector.
  73      *
  74      * <p> For simple stop-the-world collectors this value is just the time
  75      * since the most recent collection.  For generational collectors it is the
  76      * time since the oldest generation was most recently collected.  Other
  77      * collectors are free to return a pessimistic estimate of the elapsed
  78      * 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 that
  81      * is no longer strongly reachable may have to be inspected multiple times
  82      * before it can be reclaimed.
  83      */
  84     public static native long maxObjectInspectionAge();
  85 
  86     static {
  87         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  88             public Void run() {
  89                 System.loadLibrary("rmi");
  90                 return null;
  91             }});
  92     }
  93 
  94     private static class Daemon implements Runnable {
  95 
  96         @Override
  97         public void run() {
  98             for (;;) {
  99                 long l;
 100                 synchronized (lock) {
 101 
 102                     l = latencyTarget;
 103                     if (l == NO_TARGET) {
 104                         /* No latency target, so exit */
 105                         GC.daemon = null;
 106                         return;
 107                     }
 108 
 109                     long d = maxObjectInspectionAge();
 110                     if (d >= l) {
 111                         /* Do a full collection.  There is a remote possibility
 112                          * that a full collection will occurr between the time
 113                          * we sample the inspection age and the time the GC
 114                          * actually starts, but this is sufficiently unlikely
 115                          * that it doesn't seem worth the more expensive JVM
 116                          * interface that would be required.
 117                          */
 118                         System.gc();
 119                         d = 0;
 120                     }
 121 
 122                     /* Wait for the latency period to expire,
 123                      * or for notification that the period has changed
 124                      */
 125                     try {
 126                         lock.wait(l - d);
 127                     } catch (InterruptedException x) {
 128                         continue;
 129                     }
 130                 }
 131             }
 132         }
 133 
 134         /* Create a new daemon thread */
 135         public static void create() {
 136             PrivilegedAction<Void> pa = new PrivilegedAction<Void>() {
 137                 public Void run() {
 138                     Thread t = InnocuousThread.newSystemThread("RMI GC Daemon",
 139                                                                new Daemon());
 140                     assert t.getContextClassLoader() == null;
 141                     t.setDaemon(true);
 142                     t.setPriority(Thread.MIN_PRIORITY + 1);
 143                     t.start();
 144                     GC.daemon = t;
 145                     return null;
 146                 }};
 147             AccessController.doPrivileged(pa);
 148         }
 149 
 150     }
 151 
 152 
 153     /* Sets the latency target to the given value.
 154      * Must be invoked while holding the lock.
 155      */
 156     private static void setLatencyTarget(long ms) {
 157         latencyTarget = ms;
 158         if (daemon == null) {
 159             /* Create a new daemon thread */
 160             Daemon.create();
 161         } else {
 162             /* Notify the existing daemon thread
 163              * that the lateency target has changed
 164              */
 165             lock.notify();
 166         }
 167     }
 168 
 169 
 170     /**
 171      * Represents an active garbage-collection latency request.  Instances of
 172      * this class are created by the <code>{@link #requestLatency}</code>
 173      * method.  Given a request, the only interesting operation is that of
 174      * cancellation.
 175      */
 176     public static class LatencyRequest
 177         implements Comparable<LatencyRequest> {
 178 
 179         /* Instance counter, used to generate unique identifers */
 180         private static long counter = 0;
 181 
 182         /* Sorted set of active latency requests */
 183         private static SortedSet<LatencyRequest> requests = null;
 184 
 185         /* Examine the request set and reset the latency target if necessary.
 186          * Must be invoked while holding the lock.
 187          */
 188         private static void adjustLatencyIfNeeded() {
 189             if ((requests == null) || requests.isEmpty()) {
 190                 if (latencyTarget != NO_TARGET) {
 191                     setLatencyTarget(NO_TARGET);
 192                 }
 193             } else {
 194                 LatencyRequest r = requests.first();
 195                 if (r.latency != latencyTarget) {
 196                     setLatencyTarget(r.latency);
 197                 }
 198             }
 199         }
 200 
 201         /* The requested latency, or NO_TARGET
 202          * if this request has been cancelled
 203          */
 204         private long latency;
 205 
 206         /* Unique identifier for this request */
 207         private long id;
 208 
 209         private LatencyRequest(long ms) {
 210             if (ms <= 0) {
 211                 throw new IllegalArgumentException("Non-positive latency: "
 212                                                    + ms);
 213             }
 214             this.latency = ms;
 215             synchronized (lock) {
 216                 this.id = ++counter;
 217                 if (requests == null) {
 218                     requests = new TreeSet<LatencyRequest>();
 219                 }
 220                 requests.add(this);
 221                 adjustLatencyIfNeeded();
 222             }
 223         }
 224 
 225         /**
 226          * Cancels this latency request.
 227          *
 228          * @throws  IllegalStateException
 229          *          If this request has already been cancelled
 230          */
 231         public void cancel() {
 232             synchronized (lock) {
 233                 if (this.latency == NO_TARGET) {
 234                     throw new IllegalStateException("Request already"
 235                                                     + " cancelled");
 236                 }
 237                 if (!requests.remove(this)) {
 238                     throw new InternalError("Latency request "
 239                                             + this + " not found");
 240                 }
 241                 if (requests.isEmpty()) requests = null;
 242                 this.latency = NO_TARGET;
 243                 adjustLatencyIfNeeded();
 244             }
 245         }
 246 
 247         public int compareTo(LatencyRequest r) {
 248             long d = this.latency - r.latency;
 249             if (d == 0) d = this.id - r.id;
 250             return (d < 0) ? -1 : ((d > 0) ? +1 : 0);
 251         }
 252 
 253         public String toString() {
 254             return (LatencyRequest.class.getName()
 255                     + "[" + latency + "," + id + "]");
 256         }
 257 
 258     }
 259 
 260 
 261     /**
 262      * Makes a new request for a garbage-collection latency of the given
 263      * number of real-time milliseconds.  A low-priority daemon thread makes a
 264      * best effort to ensure that the maximum object-inspection age never
 265      * exceeds the smallest of the currently active requests.
 266      *
 267      * @param   latency
 268      *          The requested latency
 269      *
 270      * @throws  IllegalArgumentException
 271      *          If the given <code>latency</code> is non-positive
 272      */
 273     public static LatencyRequest requestLatency(long latency) {
 274         return new LatencyRequest(latency);
 275     }
 276 
 277 
 278     /**
 279      * Returns the current smallest garbage-collection latency request, or zero
 280      * if there are no active requests.
 281      */
 282     public static long currentLatencyTarget() {
 283         long t = latencyTarget;
 284         return (t == NO_TARGET) ? 0 : t;
 285     }
 286 
 287 }