01: package tide.threadanalysis;
02:
03: import java.util.*;
04:
05: /** Collected by the sequencer.
06: */
07: public class ThreadHistory {
08: final List<StackItem> hist = new ArrayList<StackItem>();
09: final Thread ref;
10:
11: //final long created;
12: //final long t0;
13:
14: public ThreadHistory(Thread ref, long t0) {
15: this .ref = ref;
16: //this.t0 = t0;
17: //this.created = System.nanoTime()-t0;
18: }
19:
20: public void add(StackTraceElement[] stack, Thread.State state,
21: long t0) {
22: if (stack.length > 0) {
23: if (hist.isEmpty()) {
24: hist.add(new StackItem(stack[0], state, t0));
25: } else {
26: StackItem si = hist.get(hist.size() - 1);
27: if (si.isSameElement(stack[0])) {
28: si.setEnd(System.nanoTime(), t0);
29: //System.out.println("Same line: "+si);
30: } else {
31: hist.add(new StackItem(stack[0], state, t0));
32: }
33: }
34:
35: //System.out.println(ref.getName()+":: "+stack[0]);
36:
37: } else {
38: //System.out.println(ref.getName()+":: "+"empty stack");
39: }
40: }
41:
42: public int size() {
43: return hist.size();
44: }
45:
46: public final List<StackItem> getHist() {
47: return hist;
48: }
49:
50: /** relative time (nanos)
51: */
52: public long getFirstTR() {
53: if (hist.isEmpty())
54: return -1;
55: return hist.get(0).getCreated();
56: }
57:
58: /** relative time (nanos)
59: */
60: public long getLastTR() {
61: synchronized (hist) {
62: if (hist.isEmpty())
63: return -1;
64: return hist.get(hist.size() - 1).getEnd();
65: }
66: }
67:
68: @Override
69: public final String toString() {
70: StringBuilder sb = new StringBuilder();
71: sb.append("Thread " + ref.getName() + ":");
72: for (StackItem si : hist) {
73: sb.append("\n " + si);
74: }
75: return sb.toString();
76: }
77: }
|