00001 package org.hfbk.util; 00002 00003 import java.io.FileNotFoundException; 00004 import java.io.PrintStream; 00005 import java.util.Map; 00006 00007 import org.hfbk.vis.VisClient; 00008 00009 public class StartupProfiler extends Thread { 00010 00011 Counter<String> sup = new Counter<String>(); 00012 00013 boolean running=true; 00014 00015 public StartupProfiler() { 00016 Runtime.getRuntime().addShutdownHook(new Thread(){ 00017 public void run() { 00018 close(); 00019 } 00020 }); 00021 setName("StartupProfiler"); 00022 setDaemon(true); 00023 setPriority(Thread.MAX_PRIORITY); 00024 start(); 00025 } 00026 00027 public void run() { 00028 try { 00029 while (running) { 00030 for (Map.Entry<Thread,StackTraceElement[]> st: Thread.getAllStackTraces().entrySet()){ 00031 String trace=st.getKey().getName(); 00032 for (StackTraceElement ste : st.getValue()) 00033 trace+=":"+ste.getClassName()+"."+ste.getMethodName(); 00034 sup.add(trace); 00035 } 00036 Thread.sleep(10); 00037 } 00038 } catch (Exception e) { 00039 throw new RuntimeException (e); 00040 } 00041 } 00042 00043 void close(){ 00044 running=false; 00045 try { 00046 PrintStream ps=new PrintStream("profile.txt"); 00047 00048 for (Map.Entry<String, Integer> count: sup.entrySet()) 00049 ps.printf("%8d %s \n", count.getValue(), count.getKey() ); 00050 00051 ps.close(); 00052 System.out.println("Saved Thread Samples."); 00053 } catch (FileNotFoundException e) { 00054 throw new RuntimeException (e); 00055 } 00056 } 00057 00058 public static void main(String[] args) throws Exception { 00059 new StartupProfiler(); 00060 VisClient.main(args); 00061 00062 00063 } 00064 }