jdk/src/share/classes/sun/misc/PerformanceLogger.java
author herrick
Fri, 12 Jun 2009 14:56:32 -0400
changeset 3111 fefdeafb7ab9
parent 715 f16baef3a20e
child 5506 202f599c92aa
permissions -rw-r--r--
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7 6845973: Update JDK7 with deployment changes in 6u13, 6u14 4802695: Support 64-bit Java Plug-in and Java webstart on Windows/Linux on AMD64 6825019: DownloadManager should not be loaded and referenced for full JRE 6738770: REGRESSION:JSException throws when use LiveConnect javascript facility 6772884: plugin2 : java.lang.OutOfMemoryError or crash 6707535: Crossing domain hole affecting multiple sites/domains using plug-in 6728071: Non-verification of Update files may allow unintended updates 6704154: Code loaded from local filesystem should not get access to localhost 6727081: Web Start security restrictions bypass using special extension jnlp 6727079: Java Web Start Socket() restriction bypass 6727071: Cache location/user name information disclosure in SingleInstanceImpl. 6716217: AppletClassLoader adds permissions based on codebase regardless of CS 6694892: Java Webstart inclusion via system properties override [CVE-2008-2086] 6704074: localhost socket access due to cache location exposed 6703909: Java webstart arbitrary file creation using nativelib 6665315: browser crashes when deployment.properties has more slashes ( / ) 6660121: Encoding values in JNLP files can cause buffer overflow 6606110: URLConnection.setProxiedHost for resources that are loaded via proxy 6581221: SSV(VISTA): Redirection FAILS to work if user does a downgrade install 6609756: Buffer Overflow in Java ActiveX component 6608712: Bypassing the same origin policy in Java with crafted names 6534630: "gnumake clobber" doesn't 6849953: JDK7 - replacement of bufferoverflowU.lib on amd64 breaks build 6849029: Need some JDK7 merge clean-up after comments on the webrev 6847582: Build problem on JDK7 with isSecureProperty in merge 6827935: JDK 7 deployment merging - problem in Compiler-msvm.gmk 6823215: latest merge fixes from 6u12 -> JDK7 6816153: further mergers for JDK7 deployment integration 6807074: Fix Java Kernel and JQS in initial JDK7 builds Summary: Initial changeset for implementing 6uX Deployment Features into JDK7 Reviewed-by: dgu, billyh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 2002-2008 Sun Microsystems, Inc.  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
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;
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
}