Optimize time checking in flush() 69/7669/1
authorRobert Varga <rovarga@cisco.com>
Wed, 4 Jun 2014 09:15:17 +0000 (11:15 +0200)
committerRobert Varga <rovarga@cisco.com>
Wed, 4 Jun 2014 09:20:05 +0000 (11:20 +0200)
Pre-calculate deadline and do not do timeunit conversions while checking
if we should yield.

Change-Id: Ie315a9650f6f7cdbd1bbd2ac611df2a3dfda4975
Signed-off-by: Robert Varga <rovarga@cisco.com>
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/ChannelOutboundQueue.java

index d84cfc61a1272ea329cb00216e82a4ebb1522b47..765a979434d89c2f7e8d5b0f88512152a9e6152a 100644 (file)
@@ -26,7 +26,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Preconditions;
-import com.google.common.base.Stopwatch;
 
 /**
  * Channel handler which bypasses wraps on top of normal Netty pipeline, allowing
@@ -108,7 +107,7 @@ final class ChannelOutboundQueue extends ChannelInboundHandlerAdapter {
          */
         this.queue = new LinkedBlockingQueue<>(queueDepth);
         this.channel = Preconditions.checkNotNull(channel);
-        this.maxWorkTime = DEFAULT_WORKTIME_MICROS;
+        this.maxWorkTime = TimeUnit.MICROSECONDS.toNanos(DEFAULT_WORKTIME_MICROS);
     }
 
     /**
@@ -163,7 +162,8 @@ final class ChannelOutboundQueue extends ChannelInboundHandlerAdapter {
      * uncontended.
      */
     private synchronized void flush() {
-        final Stopwatch w = new Stopwatch().start();
+        final long start = System.nanoTime();
+        final long deadline = start + maxWorkTime;
 
         LOG.debug("Dequeuing messages to channel {}", channel);
 
@@ -193,10 +193,12 @@ final class ChannelOutboundQueue extends ChannelInboundHandlerAdapter {
              *      should be able to perform dynamic adjustments here.
              *      is that additional complexity needed, though?
              */
-            if ((messages % WORKTIME_RECHECK_MSGS) == 0 &&
-                    w.elapsed(TimeUnit.MICROSECONDS) >= maxWorkTime) {
-                LOG.trace("Exceeded allotted work time {}us", maxWorkTime);
-                break;
+            if ((messages % WORKTIME_RECHECK_MSGS) == 0) {
+                if (System.nanoTime() >= deadline) {
+                    LOG.trace("Exceeded allotted work time {}us",
+                            TimeUnit.NANOSECONDS.toMicros(maxWorkTime));
+                    break;
+                }
             }
         }
 
@@ -205,9 +207,9 @@ final class ChannelOutboundQueue extends ChannelInboundHandlerAdapter {
             channel.flush();
         }
 
-        w.stop();
+        final long stop = System.nanoTime();
         LOG.debug("Flushed {} messages in {}us to channel {}",
-                messages, w.elapsed(TimeUnit.MICROSECONDS), channel);
+                messages, TimeUnit.NANOSECONDS.toMicros(stop - start), channel);
 
         /*
          * We are almost ready to terminate. This is a bit tricky, because