KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > runtime > ThreadDump


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util.runtime;
5
6 import com.tc.process.StreamCollector;
7 import com.tc.test.TestConfigObject;
8 import com.tc.util.concurrent.ThreadUtil;
9
10 import java.io.File JavaDoc;
11
12 public class ThreadDump {
13
14   public static void main(String JavaDoc args[]) {
15     dumpThreadsOnce();
16
17     // This flush()'ing and sleeping is a (perhaps poor) attempt at making ThreadDumpTest pass on Jrockit
18
System.err.flush();
19     System.out.flush();
20     try {
21       Thread.sleep(5000);
22     } catch (InterruptedException JavaDoc e) {
23       e.printStackTrace();
24     }
25   }
26
27   public static void dumpThreadsOnce() {
28     dumpThreadsMany(1, 0L);
29   }
30
31   public static void dumpThreadsMany(int iterations, long delay) {
32     for (int i = 0; i < iterations; i++) {
33       if (Os.isWindows()) {
34         doWindowsDump();
35       } else {
36         doUnixDump();
37       }
38       ThreadUtil.reallySleep(delay);
39     }
40   }
41
42   public static void dumpProcessGroup() {
43     if (!Os.isUnix()) { throw new AssertionError JavaDoc("unix only"); }
44     doSignal(new String JavaDoc[] { "-3" }, 0);
45   }
46
47   private static void doUnixDump() {
48     int pid;
49     try {
50       pid = GetPid.getPID();
51     } catch (Throwable JavaDoc t) {
52       System.err.println("Got Exception trying to get the process ID. Sending Kill signal to entire process group. "
53                          + t.getMessage());
54       System.err.flush();
55       pid = 0;
56     }
57
58     doSignal(new String JavaDoc[] { "-3" }, pid);
59   }
60
61   private static void doWindowsDump() {
62     doSignal(new String JavaDoc[] {}, GetPid.getPID());
63   }
64
65   private static void doSignal(String JavaDoc[] args, int pid) {
66     File JavaDoc program = SignalProgram.PROGRAM;
67
68     try {
69       String JavaDoc[] cmd = new String JavaDoc[1 + args.length + 1];
70       cmd[0] = program.getAbsolutePath();
71       System.arraycopy(args, 0, cmd, 1, args.length);
72
73       cmd[cmd.length - 1] = Integer.toString(pid);
74
75       Process JavaDoc p = Runtime.getRuntime().exec(cmd);
76       p.getOutputStream().close();
77       StreamCollector err = new StreamCollector(p.getErrorStream());
78       StreamCollector out = new StreamCollector(p.getInputStream());
79       err.start();
80       out.start();
81
82       p.waitFor();
83
84       err.join();
85       out.join();
86
87       System.err.print(err.toString());
88       System.err.flush();
89       System.out.print(out.toString());
90       System.out.flush();
91
92     } catch (Exception JavaDoc e) {
93       e.printStackTrace();
94     }
95   }
96
97   private static class SignalProgram {
98     static final File JavaDoc PROGRAM;
99
100     static {
101       PROGRAM = getSignalProgram();
102     }
103
104     private static File JavaDoc getSignalProgram() {
105       File JavaDoc rv = null;
106
107       if (Os.isWindows()) {
108         try {
109           rv = new File JavaDoc(TestConfigObject.getInstance().executableSearchPath(), "SendSignal.EXE");
110         } catch (Exception JavaDoc e) {
111           throw new RuntimeException JavaDoc(e);
112         }
113       } else {
114         File JavaDoc binKill = new File JavaDoc("/bin/kill");
115         File JavaDoc usrBinKill = new File JavaDoc("/usr/bin/kill");
116
117         if (binKill.exists()) {
118           rv = binKill;
119         } else if (usrBinKill.exists()) {
120           rv = usrBinKill;
121         }
122       }
123
124       if (rv != null) {
125         if (rv.exists() && rv.isFile()) { return rv; }
126         System.err.println("Cannot find signal program: " + rv.getAbsolutePath());
127         System.err.flush();
128       }
129
130       throw new RuntimeException JavaDoc("Cannot find signal program");
131     }
132   }
133 }
134
Popular Tags