home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 27 / CDROM27.iso / share / wnt / jig / data1.cab / Program_Executable_Files / src / Samples / ProfileNester.java < prev   
Encoding:
Java Source  |  1998-08-19  |  5.7 KB  |  185 lines

  1. /*
  2.  * Copyright (c) 1998 S Cubed. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * S CUBED MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. S CUBED SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17.  
  18. package Samples;
  19.  
  20. import java.util.*;
  21. import java.io.*;
  22.  
  23. /**
  24.  * This class provides a utility which accepts a file of 
  25.  * java profile information generated by the java -prof option
  26.  * and prints out a call tree of the called methods, 
  27.  * their call counts and amounts of time.
  28.  * 
  29.  * See the main method for invocation protocol.
  30.  */
  31.  
  32. public class ProfileNester  {
  33. /**
  34.  * The associations between the callee and the caller.
  35.  */
  36. private Hashtable calleeTable = null;
  37.  
  38. /**
  39.  * The associations between the callee-caller and the amounts of time spent in the calls.
  40.  */
  41. private Hashtable timesTable = null;
  42.  
  43. /**
  44.  * The associations between the callee-caller and the counts of calls from caller to callee.
  45.  */
  46. private Hashtable countsTable = null;
  47.  
  48. /**
  49.  * A string for formatting the indentation representing the nesting level.
  50.  */
  51. private static String TabString = "    ";
  52.  
  53. /**
  54.  * Constructs an initialized ProfileNester.
  55.  */
  56. ProfileNester  (  ) {
  57.     calleeTable = new Hashtable();
  58.     timesTable = new Hashtable();
  59.     countsTable = new Hashtable();
  60.     }
  61. /**
  62.  * Captures the information from the profile file.
  63.  * Parse the fields from the java.prof file populating association tables. 
  64.  *
  65.  * @param filename the complete file name of the java.prof file
  66.  */
  67. void parse(String filename) {
  68.  
  69.     BufferedReader br = null;    // open the file for readLine() access
  70.     try {
  71.         br = new BufferedReader(
  72.     new InputStreamReader(
  73.     new FileInputStream(filename)));
  74.         } catch (IOException e) {
  75.         System.err.println(filename + ", "+ e);
  76.         return;
  77.         }
  78.  
  79.     String line = null;
  80.     String callee = null;
  81.     String caller = null;
  82.     String times = null;
  83.     String counts = null;
  84.  
  85.     try { while ((line = br.readLine()) != null) { // get the next line
  86.  
  87.     // parse line delimited by spaces
  88.         StringTokenizer st = new StringTokenizer(line);
  89.  
  90.     // cases are ordinal positions of tokens
  91.     for (int i=0;st.hasMoreTokens();i++) switch (i){
  92.             case 0:
  93.             counts = st.nextToken();
  94.         if (counts.equals("handles_used:")) return;
  95.             break;
  96.  
  97.             case 1:
  98.             callee = st.nextToken();
  99.             break;
  100.  
  101.             case 2:
  102.             caller = st.nextToken();
  103.             if (caller.charAt(0) == '<') caller += " " + st.nextToken();
  104.             break;
  105.  
  106.             case 3:
  107.             times = st.nextToken();
  108.             break;
  109.  
  110.             default:;
  111.             }
  112.  
  113.     // load up the association tables
  114.     calleeTable.put(callee,caller);
  115.     timesTable.put(callee+caller,times);
  116.     countsTable.put(callee+caller,counts);
  117.         }
  118.         } catch (IOException e) {
  119.         System.err.println(filename + ", "+ e);
  120.         return;
  121.         }
  122.     }
  123. /**
  124.  * Invokes the ProfileNester service.
  125.  * To invoke this utility call ProfileNester, 
  126.  * Usage: ProfileNester java.prof caller_entry_point
  127.  * where java.prof is the file generated by the java -prof option
  128.  * and caller_entry_point is the desired root node of the call tree,
  129.  * this must match exactly the desired caller as specified in the java.prof file
  130.  *
  131.  * For example, running Samples.GetQuotes with jdk1.1.6\bin\java_g -prof 
  132.  * (jdk1.2beta3\bin\java -Xprof), see GetQuotes.main() for protocols,
  133.  * or, running it in the debugger with a Debug option of -prof,
  134.  * will produce a java.prof that this example will process.  
  135.  * Create the java.prof file, then from the JIG Browser,
  136.  * highlight this command line and select run,
  137.  * <pre>
  138. Samples.ProfileNester ..\Samples\java.prof Samples/GetQuotes.main([Ljava/lang/String;)V
  139. </pre>
  140.  *
  141.  * @param args    command line argument list
  142.  */
  143. public static void main(String args[]) {
  144.     ProfileNester pn = new ProfileNester();
  145.     if (args.length != 2) {
  146.         System.out.println("Usage: ProfileNester java.prof caller_entry_point");
  147.         System.exit(1);
  148.         }
  149.     pn.parse(args[0]);
  150.     System.out.println(args[1] + " [count, time]");
  151.     pn.call(args[1],0);
  152.     System.exit(1);
  153.     }
  154. /**
  155.  * Prints nested nodes in the call tree.
  156.  * Recursively called method to print out the next node in the call tree.
  157.  * A new nesting level is indicated by increased indentation.
  158.  *
  159.  * @param caller the name of the calling method
  160.  * @param tab the numeric value of the nesting level
  161.  */
  162. void call  (String caller,int tab) {
  163.     tab++;    // set indentation for this level
  164.  
  165.     // iterate through "callee" associations to find match for caller
  166.     for (Enumeration e = calleeTable.keys() ; e.hasMoreElements() ;) {
  167.         String name = (String) e.nextElement();
  168.  
  169.     // if match is found print a formatted node
  170.         if (calleeTable.get(name).equals(caller)) {
  171.             for (int i=0;i<tab;i++) System.out.print(TabString);
  172.             System.out.println(
  173.             name
  174.             +" ["
  175.             +countsTable.get(name+calleeTable.get(name))
  176.             +", "
  177.             +timesTable.get(name+calleeTable.get(name))
  178.             +"]"
  179.             );
  180.             call(name,tab);
  181.             }
  182.         }
  183.     }
  184. }
  185.