0N/A/*
2273N/A * Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1472N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
1879N/A
1879N/A
1879N/A
1879N/Apackage sun.misc;
1879N/A
1879N/Aimport java.util.Vector;
1879N/Aimport java.io.FileWriter;
1879N/Aimport java.io.File;
1879N/Aimport java.io.OutputStreamWriter;
0N/Aimport java.io.Writer;
0N/A
0N/A/**
0N/A * This class is intended to be a central place for the jdk to
0N/A * log timing events of interest. There is pre-defined event
0N/A * of startTime, as well as a general
0N/A * mechanism of setting aribtrary times in an array.
0N/A * All unreserved times in the array can be used by callers
0N/A * in application-defined situations. The caller is responsible
0N/A * for setting and getting all times and for doing whatever
0N/A * analysis is interesting; this class is merely a central container
0N/A * for those timing values.
0N/A * Note that, due to the variables in this class being static,
0N/A * use of particular time values by multiple applets will cause
0N/A * confusing results. For example, if plugin runs two applets
0N/A * simultaneously, the initTime for those applets will collide
0N/A * and the results may be undefined.
0N/A * <P>
0N/A * To automatically track startup performance in an app or applet,
0N/A * use the command-line parameter sun.perflog as follows:<BR>
0N/A * -Dsun.perflog[=file:<filename>]
1564N/A * <BR>
1564N/A * where simply using the parameter with no value will enable output
0N/A * to the console and a value of "file:<filename>" will cause
0N/A * that given filename to be created and used for all output.
989N/A * <P>
1202N/A * By default, times are measured using System.currentTimeMillis(). To use
0N/A * System.nanoTime() instead, add the command-line parameter:<BR>
0N/A -Dsun.perflog.nano=true
989N/A * <BR>
989N/A * <P>
989N/A * <B>Warning: Use at your own risk!</B>
989N/A * This class is intended for internal testing
4141N/A * purposes only and may be removed at any time. More
4141N/A * permanent monitoring and profiling APIs are expected to be
0N/A * developed for future releases and this class will cease to
0N/A * exist once those APIs are in place.
0N/A * @author Chet Haase
0N/A */
0N/Apublic class PerformanceLogger {
4141N/A
4141N/A // Timing values of global interest
0N/A private static final int START_INDEX = 0; // VM start
0N/A private static final int LAST_RESERVED = START_INDEX;
0N/A
0N/A private static boolean perfLoggingOn = false;
0N/A private static boolean useNanoTime = false;
0N/A private static Vector<TimeData> times;
0N/A private static String logFileName = null;
0N/A private static Writer logWriter = null;
0N/A private static long baseTime;
989N/A
0N/A static {
0N/A String perfLoggingProp =
0N/A java.security.AccessController.doPrivileged(
0N/A new sun.security.action.GetPropertyAction("sun.perflog"));
0N/A if (perfLoggingProp != null) {
0N/A perfLoggingOn = true;
0N/A
0N/A // Check if we should use nanoTime
0N/A String perfNanoProp =
3863N/A java.security.AccessController.doPrivileged(
3863N/A new sun.security.action.GetPropertyAction("sun.perflog.nano"));
3863N/A if (perfNanoProp != null) {
0N/A useNanoTime = true;
107N/A }
107N/A
107N/A // Now, figure out what the user wants to do with the data
107N/A if (perfLoggingProp.regionMatches(true, 0, "file:", 0, 5)) {
107N/A logFileName = perfLoggingProp.substring(5);
107N/A }
107N/A if (logFileName != null) {
107N/A if (logWriter == null) {
107N/A java.security.AccessController.doPrivileged(
107N/A new java.security.PrivilegedAction<Void>() {
107N/A public Void run() {
107N/A try {
107N/A File logFile = new File(logFileName);
107N/A logFile.createNewFile();
107N/A logWriter = new FileWriter(logFile);
107N/A } catch (Exception e) {
0N/A System.out.println(e + ": Creating logfile " +
0N/A logFileName +
0N/A ". Log to console");
0N/A }
0N/A return null;
0N/A }
0N/A });
0N/A }
1458N/A }
1458N/A if (logWriter == null) {
0N/A logWriter = new OutputStreamWriter(System.out);
1564N/A }
1564N/A }
0N/A times = new Vector<TimeData>(10);
0N/A // Reserve predefined slots
0N/A for (int i = 0; i <= LAST_RESERVED; ++i) {
0N/A times.add(new TimeData("Time " + i + " not set", 0));
2222N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns status of whether logging is enabled or not. This is
0N/A * provided as a convenience method so that users do not have to
0N/A * perform the same GetPropertyAction check as above to determine whether
989N/A * to enable performance logging.
989N/A */
989N/A public static boolean loggingEnabled() {
989N/A return perfLoggingOn;
989N/A }
989N/A
989N/A
989N/A /**
989N/A * Internal class used to store time/message data together.
989N/A */
989N/A static class TimeData {
989N/A String message;
0N/A long time;
0N/A
0N/A TimeData(String message, long time) {
0N/A this.message = message;
0N/A this.time = time;
989N/A }
1827N/A
2200N/A String getMessage() {
0N/A return message;
0N/A }
0N/A
0N/A long getTime() {
4141N/A return time;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the current time, in millis or nanos as appropriate
0N/A */
0N/A private static long getCurrentTime() {
2304N/A if (useNanoTime) {
2200N/A return System.nanoTime();
0N/A } else {
0N/A return System.currentTimeMillis();
0N/A }
0N/A }
0N/A
1202N/A /**
1202N/A * Sets the start time. Ideally, this is the earliest time available
1202N/A * during the startup of a Java applet or application. This time is
1202N/A * later used to analyze the difference between the initial startup
0N/A * time and other events in the system (such as an applet's init time).
0N/A */
0N/A public static void setStartTime(String message) {
0N/A if (loggingEnabled()) {
0N/A long nowTime = getCurrentTime();
0N/A setStartTime(message, nowTime);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the base time, output can then
0N/A * be displayed as offsets from the base time;.
0N/A */
4141N/A public static void setBaseTime(long time) {
4141N/A if (loggingEnabled()) {
0N/A baseTime = time;
1879N/A }
1879N/A }
/**
* Sets the start time.
* This version of the method is
* given the time to log, instead of expecting this method to
* get the time itself. This is done in case the time was
* recorded much earlier than this method was called.
*/
public static void setStartTime(String message, long time) {
if (loggingEnabled()) {
times.set(START_INDEX, new TimeData(message, time));
}
}
/**
* Gets the start time, which should be the time when
* the java process started, prior to the VM actually being
* loaded.
*/
public static long getStartTime() {
if (loggingEnabled()) {
return times.get(START_INDEX).getTime();
} else {
return 0;
}
}
/**
* Sets the value of a given time and returns the index of the
* slot that that time was stored in.
*/
public static int setTime(String message) {
if (loggingEnabled()) {
long nowTime = getCurrentTime();
return setTime(message, nowTime);
} else {
return 0;
}
}
/**
* Sets the value of a given time and returns the index of the
* slot that that time was stored in.
* This version of the method is
* given the time to log, instead of expecting this method to
* get the time itself. This is done in case the time was
* recorded much earlier than this method was called.
*/
public static int setTime(String message, long time) {
if (loggingEnabled()) {
// times is already synchronized, but we need to ensure that
// the size used in times.set() is the same used when returning
// the index of that operation.
synchronized (times) {
times.add(new TimeData(message, time));
return (times.size() - 1);
}
} else {
return 0;
}
}
/**
* Returns time at given index.
*/
public static long getTimeAtIndex(int index) {
if (loggingEnabled()) {
return times.get(index).getTime();
} else {
return 0;
}
}
/**
* Returns message at given index.
*/
public static String getMessageAtIndex(int index) {
if (loggingEnabled()) {
return times.get(index).getMessage();
} else {
return null;
}
}
/**
* Outputs all data to parameter-specified Writer object
*/
public static void outputLog(Writer writer) {
if (loggingEnabled()) {
try {
synchronized(times) {
for (int i = 0; i < times.size(); ++i) {
TimeData td = times.get(i);
if (td != null) {
writer.write(i + " " + td.getMessage() + ": " +
(td.getTime() - baseTime) + "\n");
}
}
}
writer.flush();
} catch (Exception e) {
System.out.println(e + ": Writing performance log to " +
writer);
}
}
}
/**
* Outputs all data to whatever location the user specified
* via sun.perflog command-line parameter.
*/
public static void outputLog() {
outputLog(logWriter);
}
}