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