jdk/src/share/classes/sun/misc/PerformanceLogger.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 51 6fe31bc95bbc
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static Vector times;
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String perfLoggingProp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            new sun.security.action.GetPropertyAction("sun.perflog"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (perfLoggingProp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            perfLoggingOn = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // Check if we should use nanoTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            String perfNanoProp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                new sun.security.action.GetPropertyAction("sun.perflog.nano"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (perfNanoProp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                useNanoTime = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            // Now, figure out what the user wants to do with the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (perfLoggingProp.regionMatches(true, 0, "file:", 0, 5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                logFileName = perfLoggingProp.substring(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (logFileName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                if (logWriter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                File logFile = new File(logFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                logFile.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                logWriter = new FileWriter(logFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                System.out.println(e + ": Creating logfile " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                                   logFileName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                                   ".  Log to console");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        }
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
            if (logWriter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                logWriter = new OutputStreamWriter(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        times = new Vector(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Reserve predefined slots
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        for (int i = 0; i <= LAST_RESERVED; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            times.add(new TimeData("Time " + i + " not set", 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
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
     * Returns status of whether logging is enabled or not.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * provided as a convenience method so that users do not have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * perform the same GetPropertyAction check as above to determine whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * to enable performance logging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public static boolean loggingEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return perfLoggingOn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
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
     * Internal class used to store time/message data together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static class TimeData {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        String message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        long time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        TimeData(String message, long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            this.message = message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            this.time = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        String getMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return time;
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
     * Return the current time, in millis or nanos as appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private static long getCurrentTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (useNanoTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
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
     * Sets the start time.  Ideally, this is the earliest time available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * during the startup of a Java applet or application.  This time is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * later used to analyze the difference between the initial startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * time and other events in the system (such as an applet's init time).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public static void setStartTime(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            long nowTime = getCurrentTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            setStartTime(message, nowTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
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
     * Sets the start time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * This version of the method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * given the time to log, instead of expecting this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * get the time itself.  This is done in case the time was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * recorded much earlier than this method was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public static void setStartTime(String message, long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            times.set(START_INDEX, new TimeData(message, time));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Gets the start time, which should be the time when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * the java process started, prior to the VM actually being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public static long getStartTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return ((TimeData)times.get(START_INDEX)).getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Sets the value of a given time and returns the index of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * slot that that time was stored in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public static int setTime(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            long nowTime = getCurrentTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return setTime(message, nowTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Sets the value of a given time and returns the index of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * slot that that time was stored in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * This version of the method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * given the time to log, instead of expecting this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * get the time itself.  This is done in case the time was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * recorded much earlier than this method was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public static int setTime(String message, long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            // times is already synchronized, but we need to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // the size used in times.set() is the same used when returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            // the index of that operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            synchronized (times) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                times.add(new TimeData(message, time));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                return (times.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Returns time at given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public static long getTimeAtIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return ((TimeData)times.get(index)).getTime();
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 message at given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public static String getMessageAtIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return ((TimeData)times.get(index)).getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return null;
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
     * Outputs all data to parameter-specified Writer object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public static void outputLog(Writer writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                synchronized(times) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    for (int i = 0; i < times.size(); ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        TimeData td = (TimeData)times.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        if (td != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                            writer.write(i + " " + td.getMessage() + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                         td.getTime() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                writer.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                System.out.println(e + ": Writing performance log to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                   writer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Outputs all data to whatever location the user specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * via sun.perflog command-line parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public static void outputLog() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        outputLog(logWriter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
}