home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-08-19 | 5.7 KB | 185 lines |
- /*
- * Copyright (c) 1998 S Cubed. All Rights Reserved.
- *
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for NON-COMMERCIAL purposes and without
- * fee is hereby granted provided that this copyright notice
- * appears in all copies. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * S CUBED MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. S CUBED SHALL NOT BE LIABLE FOR
- * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- package Samples;
-
- import java.util.*;
- import java.io.*;
-
- /**
- * This class provides a utility which accepts a file of
- * java profile information generated by the java -prof option
- * and prints out a call tree of the called methods,
- * their call counts and amounts of time.
- *
- * See the main method for invocation protocol.
- */
-
- public class ProfileNester {
- /**
- * The associations between the callee and the caller.
- */
- private Hashtable calleeTable = null;
-
- /**
- * The associations between the callee-caller and the amounts of time spent in the calls.
- */
- private Hashtable timesTable = null;
-
- /**
- * The associations between the callee-caller and the counts of calls from caller to callee.
- */
- private Hashtable countsTable = null;
-
- /**
- * A string for formatting the indentation representing the nesting level.
- */
- private static String TabString = " ";
-
- /**
- * Constructs an initialized ProfileNester.
- */
- ProfileNester ( ) {
- calleeTable = new Hashtable();
- timesTable = new Hashtable();
- countsTable = new Hashtable();
- }
- /**
- * Captures the information from the profile file.
- * Parse the fields from the java.prof file populating association tables.
- *
- * @param filename the complete file name of the java.prof file
- */
- void parse(String filename) {
-
- BufferedReader br = null; // open the file for readLine() access
- try {
- br = new BufferedReader(
- new InputStreamReader(
- new FileInputStream(filename)));
- } catch (IOException e) {
- System.err.println(filename + ", "+ e);
- return;
- }
-
- String line = null;
- String callee = null;
- String caller = null;
- String times = null;
- String counts = null;
-
- try { while ((line = br.readLine()) != null) { // get the next line
-
- // parse line delimited by spaces
- StringTokenizer st = new StringTokenizer(line);
-
- // cases are ordinal positions of tokens
- for (int i=0;st.hasMoreTokens();i++) switch (i){
- case 0:
- counts = st.nextToken();
- if (counts.equals("handles_used:")) return;
- break;
-
- case 1:
- callee = st.nextToken();
- break;
-
- case 2:
- caller = st.nextToken();
- if (caller.charAt(0) == '<') caller += " " + st.nextToken();
- break;
-
- case 3:
- times = st.nextToken();
- break;
-
- default:;
- }
-
- // load up the association tables
- calleeTable.put(callee,caller);
- timesTable.put(callee+caller,times);
- countsTable.put(callee+caller,counts);
- }
- } catch (IOException e) {
- System.err.println(filename + ", "+ e);
- return;
- }
- }
- /**
- * Invokes the ProfileNester service.
- * To invoke this utility call ProfileNester,
- * Usage: ProfileNester java.prof caller_entry_point
- * where java.prof is the file generated by the java -prof option
- * and caller_entry_point is the desired root node of the call tree,
- * this must match exactly the desired caller as specified in the java.prof file
- *
- * For example, running Samples.GetQuotes with jdk1.1.6\bin\java_g -prof
- * (jdk1.2beta3\bin\java -Xprof), see GetQuotes.main() for protocols,
- * or, running it in the debugger with a Debug option of -prof,
- * will produce a java.prof that this example will process.
- * Create the java.prof file, then from the JIG Browser,
- * highlight this command line and select run,
- * <pre>
- Samples.ProfileNester ..\Samples\java.prof Samples/GetQuotes.main([Ljava/lang/String;)V
- </pre>
- *
- * @param args command line argument list
- */
- public static void main(String args[]) {
- ProfileNester pn = new ProfileNester();
- if (args.length != 2) {
- System.out.println("Usage: ProfileNester java.prof caller_entry_point");
- System.exit(1);
- }
- pn.parse(args[0]);
- System.out.println(args[1] + " [count, time]");
- pn.call(args[1],0);
- System.exit(1);
- }
- /**
- * Prints nested nodes in the call tree.
- * Recursively called method to print out the next node in the call tree.
- * A new nesting level is indicated by increased indentation.
- *
- * @param caller the name of the calling method
- * @param tab the numeric value of the nesting level
- */
- void call (String caller,int tab) {
- tab++; // set indentation for this level
-
- // iterate through "callee" associations to find match for caller
- for (Enumeration e = calleeTable.keys() ; e.hasMoreElements() ;) {
- String name = (String) e.nextElement();
-
- // if match is found print a formatted node
- if (calleeTable.get(name).equals(caller)) {
- for (int i=0;i<tab;i++) System.out.print(TabString);
- System.out.println(
- name
- +" ["
- +countsTable.get(name+calleeTable.get(name))
- +", "
- +timesTable.get(name+calleeTable.get(name))
- +"]"
- );
- call(name,tab);
- }
- }
- }
- }
-