001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: EventTrace.java,v 1.11.2.2 2008/01/07 15:14:18 cwl Exp $
007: */
008:
009: package com.sleepycat.je.utilint;
010:
011: /**
012: * Internal class used for transient event tracing. Subclass this with
013: * specific events. Subclasses should have toString methods for display and
014: * events should be added by calling EventTrace.addEvent();
015: */
016: public class EventTrace {
017: static private int MAX_EVENTS = 100;
018:
019: static public final boolean TRACE_EVENTS = false;
020:
021: static int currentEvent = 0;
022:
023: static final EventTrace[] events = new EventTrace[MAX_EVENTS];
024: static final int[] threadIdHashes = new int[MAX_EVENTS];
025: static boolean disableEvents = false;
026:
027: protected String comment;
028:
029: public EventTrace(String comment) {
030: this .comment = comment;
031: }
032:
033: public EventTrace() {
034: comment = null;
035: }
036:
037: public String toString() {
038: return comment;
039: }
040:
041: static public void addEvent(EventTrace event) {
042: if (disableEvents) {
043: return;
044: }
045: int nextEventIdx = currentEvent++ % MAX_EVENTS;
046: events[nextEventIdx] = event;
047: threadIdHashes[nextEventIdx] = System.identityHashCode(Thread
048: .currentThread());
049: }
050:
051: static public void addEvent(String comment) {
052: if (disableEvents) {
053: return;
054: }
055: addEvent(new EventTrace(comment));
056: }
057:
058: static public void dumpEvents() {
059: if (disableEvents) {
060: return;
061: }
062: System.out.println("----- Event Dump -----");
063: EventTrace[] oldEvents = events;
064: int[] oldThreadIdHashes = threadIdHashes;
065: disableEvents = true;
066:
067: int j = 0;
068: for (int i = currentEvent; j < MAX_EVENTS; i++) {
069: EventTrace ev = oldEvents[i % MAX_EVENTS];
070: if (ev != null) {
071: int this EventIdx = i % MAX_EVENTS;
072: System.out.print(oldThreadIdHashes[this EventIdx] + " ");
073: System.out.println(j + "(" + this EventIdx + "): " + ev);
074: }
075: j++;
076: }
077: }
078:
079: static public class ExceptionEventTrace extends EventTrace {
080: private Exception event;
081:
082: public ExceptionEventTrace() {
083: event = new Exception();
084: }
085:
086: public String toString() {
087: return Tracer.getStackTrace(event);
088: }
089: }
090: }
091:
092: /*
093: static public class EvictEvent extends EventTrace {
094: long nodeId;
095: int addr;
096:
097: public EvictEvent(String comment, long nodeId, int addr) {
098: super(comment);
099: this.nodeId = nodeId;
100: this.addr = addr;
101: }
102:
103: static public void addEvent(String comment, IN node) {
104: long nodeId = node.getNodeId();
105: int addr = System.identityHashCode(node);
106: EventTrace.addEvent(new EvictEvent(comment, nodeId, addr));
107: }
108:
109: public String toString() {
110: StringBuffer sb = new StringBuffer(comment);
111: sb.append(" IN: ").append(nodeId);
112: sb.append(" sIH ").append(addr);
113: return sb.toString();
114: }
115: }
116:
117: static public class CursorTrace extends EventTrace {
118: long nodeId;
119: int index;
120:
121: public CursorTrace(String comment, long nodeId, int index) {
122: super(comment);
123: this.nodeId = nodeId;
124: this.index = index;
125: }
126:
127: static public void addEvent(String comment, CursorImpl cursor) {
128: long nodeId =
129: (cursor.getBIN() == null) ? -1 : cursor.getBIN().getNodeId();
130: EventTrace.addEvent
131: (new CursorTrace(comment, nodeId, cursor.getIndex()));
132: }
133:
134: public String toString() {
135: StringBuffer sb = new StringBuffer(comment);
136: sb.append(" BIN: ").append(nodeId);
137: sb.append(" idx: ").append(index);
138: return sb.toString();
139: }
140: }
141: */
142:
143: /*
144: class CursorEventTrace extends EventTrace {
145: private String comment;
146: private Node node1;
147: private Node node2;
148:
149: CursorEventTrace(String comment, Node node1, Node node2) {
150: this.comment = comment;
151: this.node1 = node1;
152: this.node2 = node2;
153: }
154:
155: public String toString() {
156: StringBuffer sb = new StringBuffer(comment);
157: if (node1 != null) {
158: sb.append(" ");
159: sb.append(node1.getNodeId());
160: }
161: if (node2 != null) {
162: sb.append(" ");
163: sb.append(node2.getNodeId());
164: }
165: return sb.toString();
166: }
167: }
168:
169: */
170: /*
171:
172: static class UndoEventTrace extends EventTrace {
173: private String comment;
174: private boolean success;
175: private Node node;
176: private DbLsn logLsn;
177: private Node parent;
178: private boolean found;
179: private boolean replaced;
180: private boolean inserted;
181: private DbLsn replacedLsn;
182: private DbLsn abortLsn;
183: private int index;
184:
185: UndoEventTrace(String comment) {
186: this.comment = comment;
187: }
188:
189: UndoEventTrace(boolean success,
190: Node node,
191: DbLsn logLsn,
192: Node parent,
193: boolean found,
194: boolean replaced,
195: boolean inserted,
196: DbLsn replacedLsn,
197: DbLsn abortLsn,
198: int index) {
199: this.comment = null;
200: this.success = success;
201: this.node = node;
202: this.logLsn = logLsn;
203: this.parent = parent;
204: this.found = found;
205: this.replaced = replaced;
206: this.inserted = inserted;
207: this.replacedLsn = replacedLsn;
208: this.abortLsn = abortLsn;
209: this.index = index;
210: }
211:
212: public String toString() {
213: if (comment != null) {
214: return comment;
215: }
216: StringBuffer sb = new StringBuffer();
217: sb.append(" success=").append(success);
218: sb.append(" node=");
219: sb.append(node.getNodeId());
220: sb.append(" logLsn=");
221: sb.append(logLsn.getNoFormatString());
222: if (parent != null) {
223: sb.append(" parent=").append(parent.getNodeId());
224: }
225: sb.append(" found=");
226: sb.append(found);
227: sb.append(" replaced=");
228: sb.append(replaced);
229: sb.append(" inserted=");
230: sb.append(inserted);
231: if (replacedLsn != null) {
232: sb.append(" replacedLsn=");
233: sb.append(replacedLsn.getNoFormatString());
234: }
235: if (abortLsn != null) {
236: sb.append(" abortLsn=");
237: sb.append(abortLsn.getNoFormatString());
238: }
239: sb.append(" index=").append(index);
240: return sb.toString();
241: }
242: }
243: */
244: /*
245: class CursorAdjustEventTrace extends EventTrace {
246: private int insertIndex;
247: private int cursorIndex;
248: private long nodeid;
249:
250: CursorAdjustEventTrace(int insertIndex, int cursorIndex) {
251: this.insertIndex = insertIndex;
252: this.cursorIndex = cursorIndex;
253: this.nodeid = getNodeId();
254: }
255:
256: public String toString() {
257: StringBuffer sb = new StringBuffer("cursor adjust ");
258: sb.append(insertIndex).append(" ");
259: sb.append(cursorIndex).append(" ");
260: sb.append(nodeid);
261: return sb.toString();
262: }
263: }
264:
265: */
266: /*
267: class CompressEventTrace extends EventTrace {
268: private int entryIndex;
269: private long nodeid;
270:
271: CompressEventTrace(int entryIndex) {
272: this.entryIndex = entryIndex;
273: this.nodeid = getNodeId();
274: }
275:
276: public String toString() {
277: StringBuffer sb = new StringBuffer("bin compress ");
278: sb.append(entryIndex).append(" ");
279: sb.append(nodeid);
280: return sb.toString();
281: }
282: }
283:
284: */
285: /*
286: class TreeEventTrace extends EventTrace {
287: private String comment;
288: private Node node1;
289: private Node node2;
290:
291: TreeEventTrace(String comment, Node node1, Node node2) {
292: this.comment = comment;
293: this.node1 = node1;
294: this.node2 = node2;
295: }
296:
297: public String toString() {
298: StringBuffer sb = new StringBuffer(comment);
299: if (node1 != null) {
300: sb.append(" ");
301: sb.append(node1.getNodeId());
302: }
303: if (node2 != null) {
304: sb.append(" ");
305: sb.append(node2.getNodeId());
306: }
307: return sb.toString();
308: }
309: }
310:
311: */
|