Mercurial > hg > blitz_condensed
comparison src/org/dancres/blitz/StatsDumper.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.blitz; | |
2 | |
3 import java.net.*; | |
4 | |
5 import java.util.logging.Level; | |
6 | |
7 import org.dancres.blitz.disk.Disk; | |
8 | |
9 import org.dancres.blitz.stats.StatsBoard; | |
10 import org.dancres.blitz.stats.Stat; | |
11 | |
12 /** | |
13 <p>This class implements a remote access feature which allows a programmer | |
14 to trigger dumping of useful debug information. It is triggered by | |
15 <code>telnet</code>'ing to the debug port. StatsDumper detects the connection, | |
16 closes it and dumps relevant output to console.</p> | |
17 | |
18 <p>For example, if Blitz were suffering from a deadlock, one could arrange | |
19 for a connection on the debug port, to dump lock information, the default | |
20 implementation (note that it requires a tweak to Db for it to provide the | |
21 right information, contact dan@dancres.org, for information).</p> | |
22 | |
23 <p>If the port number is non-zero, the StatsDumper is enabled otherwise it's | |
24 disabled. See <code>debugPort</code> in the configuration file.</p> | |
25 | |
26 @todo Find a nice way to handle the stack tracing stuff - only works on | |
27 JDK 1.5 | |
28 */ | |
29 class StatsDumper implements Runnable, ActiveObject { | |
30 | |
31 private static StatsDumper theStatsDumper; | |
32 | |
33 private Thread theDebugThread; | |
34 private boolean stopDebugging; | |
35 private long theCycleTime; | |
36 | |
37 static void start(long aPause) { | |
38 try { | |
39 if (aPause == 0) | |
40 return; | |
41 | |
42 System.err.println("Starting Stats Dumper: " + aPause); | |
43 | |
44 theStatsDumper = new StatsDumper(aPause); | |
45 } catch (Exception anE) { | |
46 System.err.println("StatsDumper didn't start!"); | |
47 anE.printStackTrace(System.err); | |
48 } | |
49 } | |
50 | |
51 private StatsDumper(long aPause) throws Exception { | |
52 theCycleTime = aPause; | |
53 ActiveObjectRegistry.add(this); | |
54 } | |
55 | |
56 public void begin() { | |
57 theDebugThread = new Thread(this, "StatsDumper"); | |
58 theDebugThread.start(); | |
59 } | |
60 | |
61 public void halt() { | |
62 synchronized(this) { | |
63 stopDebugging = true; | |
64 } | |
65 | |
66 theDebugThread.interrupt(); | |
67 | |
68 try { | |
69 theDebugThread.join(); | |
70 theDebugThread = null; | |
71 } catch (InterruptedException anIE) { | |
72 SpaceImpl.theLogger.log(Level.SEVERE, | |
73 "Failed to wait for StatsDumper", | |
74 anIE); | |
75 } | |
76 } | |
77 | |
78 public void run() { | |
79 synchronized(this) { | |
80 while(!stopDebugging) { | |
81 try { | |
82 | |
83 wait(theCycleTime); | |
84 | |
85 if (stopDebugging) { | |
86 continue; | |
87 } | |
88 | |
89 Stat[] myStats = StatsBoard.get().getStats(); | |
90 for (int i = 0; i < myStats.length; i++) { | |
91 System.err.println(myStats[i]); | |
92 } | |
93 | |
94 /* | |
95 Only works on JDK 1.5!!!!!!!! | |
96 | |
97 | |
98 Map myTraces = Thread.getAllStackTraces(); | |
99 | |
100 Iterator myThreads = myTraces.keySet().iterator(); | |
101 while (myThreads.hasNext()) { | |
102 Thread myThread = (Thread) myThreads.next(); | |
103 StackTraceElement[] myTrace = | |
104 (StackTraceElement[]) myTraces.get(myThread); | |
105 | |
106 System.err.println(); | |
107 System.err.println(myThread); | |
108 System.err.println(); | |
109 for (int i = 0; i < myTrace.length; i++) { | |
110 System.err.println(" " + myTrace[i]); | |
111 } | |
112 } | |
113 | |
114 */ | |
115 } catch (InterruptedException anIE) { | |
116 SpaceImpl.theLogger.log(Level.SEVERE, "StatsDumper interrupted"); | |
117 } catch (Exception anE) { | |
118 System.err.println("Whoops couldn't do dump stats"); | |
119 } | |
120 } | |
121 } | |
122 } | |
123 } |