jdk/src/share/classes/sun/misc/PerformanceLogger.java
author chegar
Mon, 08 Apr 2013 06:15:18 +0100
changeset 18223 35a5c2462991
parent 5506 202f599c92aa
child 21278 ef8a3a2a72f2
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: 3111
diff changeset
     2
 * Copyright (c) 2002, 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: 3111
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: 3111
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: 3111
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.FileWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.OutputStreamWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.Writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class is intended to be a central place for the jdk to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * log timing events of interest.  There is pre-defined event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * of startTime, as well as a general
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * mechanism of setting aribtrary times in an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * All unreserved times in the array can be used by callers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * in application-defined situations.  The caller is responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * for setting and getting all times and for doing whatever
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * analysis is interesting; this class is merely a central container
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * for those timing values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Note that, due to the variables in this class being static,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * use of particular time values by multiple applets will cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * confusing results.  For example, if plugin runs two applets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * simultaneously, the initTime for those applets will collide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and the results may be undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * To automatically track startup performance in an app or applet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * use the command-line parameter sun.perflog as follows:<BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     -Dsun.perflog[=file:<filename>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * where simply using the parameter with no value will enable output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * to the console and a value of "file:<filename>" will cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * that given filename to be created and used for all output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * By default, times are measured using System.currentTimeMillis().  To use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * System.nanoTime() instead, add the command-line parameter:<BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
       -Dsun.perflog.nano=true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <B>Warning: Use at your own risk!</B>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * This class is intended for internal testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * purposes only and may be removed at any time.  More
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * permanent monitoring and profiling APIs are expected to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * developed for future releases and this class will cease to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * exist once those APIs are in place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @author Chet Haase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public class PerformanceLogger {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // Timing values of global interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final int START_INDEX    = 0;    // VM start
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final int LAST_RESERVED  = START_INDEX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static boolean perfLoggingOn = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static boolean useNanoTime = false;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    81
    private static Vector<TimeData> times;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static String logFileName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static Writer logWriter = null;
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
    84
    private static long baseTime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        String perfLoggingProp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            new sun.security.action.GetPropertyAction("sun.perflog"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (perfLoggingProp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            perfLoggingOn = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // Check if we should use nanoTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            String perfNanoProp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                new sun.security.action.GetPropertyAction("sun.perflog.nano"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (perfNanoProp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                useNanoTime = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // Now, figure out what the user wants to do with the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (perfLoggingProp.regionMatches(true, 0, "file:", 0, 5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                logFileName = perfLoggingProp.substring(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (logFileName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (logWriter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   108
                    new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   109
                        public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                File logFile = new File(logFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                logFile.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                logWriter = new FileWriter(logFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                System.out.println(e + ": Creating logfile " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                                   logFileName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                                   ".  Log to console");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                            return null;
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
            if (logWriter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                logWriter = new OutputStreamWriter(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   128
        times = new Vector<TimeData>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // Reserve predefined slots
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        for (int i = 0; i <= LAST_RESERVED; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            times.add(new TimeData("Time " + i + " not set", 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Returns status of whether logging is enabled or not.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * provided as a convenience method so that users do not have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * perform the same GetPropertyAction check as above to determine whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * to enable performance logging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static boolean loggingEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return perfLoggingOn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Internal class used to store time/message data together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    static class TimeData {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        String message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        long time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        TimeData(String message, long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            this.message = message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            this.time = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        String getMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return time;
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
     * Return the current time, in millis or nanos as appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private static long getCurrentTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (useNanoTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Sets the start time.  Ideally, this is the earliest time available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * during the startup of a Java applet or application.  This time is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * later used to analyze the difference between the initial startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * time and other events in the system (such as an applet's init time).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public static void setStartTime(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            long nowTime = getCurrentTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            setStartTime(message, nowTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   192
     * Sets the base time, output can then
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   193
     * be displayed as offsets from the base time;.
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   194
     */
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   195
    public static void setBaseTime(long time) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   196
        if (loggingEnabled()) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   197
            baseTime = time;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   198
        }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   199
    }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   200
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   201
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Sets the start time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * This version of the method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * given the time to log, instead of expecting this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * get the time itself.  This is done in case the time was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * recorded much earlier than this method was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public static void setStartTime(String message, long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            times.set(START_INDEX, new TimeData(message, time));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Gets the start time, which should be the time when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * the java process started, prior to the VM actually being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public static long getStartTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (loggingEnabled()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   221
            return times.get(START_INDEX).getTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Sets the value of a given time and returns the index of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * slot that that time was stored in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public static int setTime(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            long nowTime = getCurrentTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return setTime(message, nowTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Sets the value of a given time and returns the index of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * slot that that time was stored in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * This version of the method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * given the time to log, instead of expecting this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * get the time itself.  This is done in case the time was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * recorded much earlier than this method was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public static int setTime(String message, long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // times is already synchronized, but we need to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // the size used in times.set() is the same used when returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // the index of that operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            synchronized (times) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                times.add(new TimeData(message, time));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                return (times.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Returns time at given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public static long getTimeAtIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (loggingEnabled()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   267
            return times.get(index).getTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Returns message at given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public static String getMessageAtIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (loggingEnabled()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   278
            return times.get(index).getMessage();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Outputs all data to parameter-specified Writer object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public static void outputLog(Writer writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                synchronized(times) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    for (int i = 0; i < times.size(); ++i) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   292
                        TimeData td = times.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        if (td != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            writer.write(i + " " + td.getMessage() + ": " +
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   295
                                         (td.getTime() - baseTime) + "\n");
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 715
diff changeset
   296
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                writer.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                System.out.println(e + ": Writing performance log to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                   writer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Outputs all data to whatever location the user specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * via sun.perflog command-line parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public static void outputLog() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        outputLog(logWriter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
}