Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.je.utilint;
  
A subclass of Latch that may be used for debugging performance issues. This latch can be used in place of an exclusive latch or object mutex in order to see who is waiting for a latch acquisition, how long they're waiting, and who the previous holder was. It crudely writes to System.out, but this can easily be changed to a java.util.Log or EventTrace as desired. You can specify a threshold for the wait and previous holder time (nanos).
 
 public class TimingLatch extends Latch {
     private static final int WAIT_THRESHOLD_NANOS = 50000;
     private static final int PREV_HOLD_THRESHOLD_NANOS = 50000;
 
     private long acquireTime;
     private long releaseTime;
     private Thread lastThread;
     private final boolean debug;
     private final int waitThreshold;
     private final int holdThreshold;
 
     public TimingLatch(String nameboolean debug) {
         super(name);
         this. = debug;
         this. = ;
         this. = ;
     }
 
     public TimingLatch(String name,
                        boolean debug,
                        int waitThreshold,
                        int holdThreshold) {
         super(name);
         this. = debug;
         this. = waitThreshold;
         this. = holdThreshold;
     }
 
     public class AcquireRequestEvent extends EventTrace {
         private long startTime;
         private String name;
         Thread us;
 
         public AcquireRequestEvent() {
             super();
              = System.nanoTime();
              = getName();
              = Thread.currentThread();
         }
 
         public String toString() {
             StringBuilder sb =
                 new StringBuilder("AcquireRequestEvent for " +  + " ");
             sb.append().append(" at ").
                 append(String.format("%,d"));
             return sb.toString();
         }
     }
 
     public class AcquireCompleteEvent extends EventTrace {
         private long startTime;
         private long waitTime;
         private String name;
         Thread us;
 
         public AcquireCompleteEvent(long startTimelong waitTime) {
             super();
             this. = startTime;
             this. = waitTime;
              = getName();
              = Thread.currentThread();
         }
 
         public String toString() {
             StringBuilder sb =
                 new StringBuilder("AcquireCompleteEvent for " +  + " ");
             sb.append().append(" at ").
                 append(String.format("%,d")).
                 append(" Took: ").append(String.format("%,d"));
             return sb.toString();
         }
     }
 
     public class ReleaseEvent extends EventTrace {
         private long startTime;
         private String name;
         Thread us;
 
         public ReleaseEvent(long time) {
             super();
             = time;
             = getName();
             = Thread.currentThread();
        }
        public String toString() {
            StringBuilder sb =
                new StringBuilder("ReleaseEvent for " +  + " ");
            sb.append().append(" at ").
                append(String.format("%,d"));
            return sb.toString();
        }
    }
    public void release() {
         = System.nanoTime();
        EventTrace.addEvent(new ReleaseEvent());
        super.release();
    }
    public void acquire() {
        if (!) {
            super.acquire();
            return;
        }
        try {
            EventTrace.addEvent(new AcquireRequestEvent());
            if (acquireNoWait()) {
                EventTrace.addEvent
                    (new AcquireCompleteEvent(System.nanoTime(), 0));
                return;
            }
            long startWait = System.nanoTime();
            super.acquire();
            long endWait = System.nanoTime();
            long ourWaitTime = endWait - startWait;
            EventTrace.addEvent
                (new AcquireCompleteEvent(System.nanoTime(), ourWaitTime));
            long previousHoldTime =  - ;
            if (previousHoldTime >  ||
                ourWaitTime > ) {
                ..println
                    (String.format("%1tT %s waited %,d nanosec for %s\n" +
                                   " Previous held by %s for %,d nanosec.",
                                   System.currentTimeMillis(),
                                   Thread.currentThread(), ourWaitTime,
                                   getName(), previousHoldTime));
                EventTrace.dumpEvents(.);
                . = false;
            }
        } finally {
             = System.nanoTime();
             = Thread.currentThread();
        }
    }
New to GrepCode? Check out our FAQ X