From: Robert Varga Date: Sat, 6 Jun 2015 02:02:50 +0000 (+0200) Subject: Speed up StackedOutboundQueue.reserve() X-Git-Tag: release/lithium~2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F21%2F22021%2F7;p=openflowjava.git Speed up StackedOutboundQueue.reserve() In the case of multiple segments being present, we do not need to completely synchronize if the corresponding segment has already been allocated. Communicate this fact when allocating segments and check this flag before embarking on the entire synchronized affair during reserve(). Change-Id: I10a9ecb141f619b94a20d74152124134d36591b1 Signed-off-by: Robert Varga --- diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/StackedOutboundQueue.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/StackedOutboundQueue.java index 3c3ceee3..efe71aa7 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/StackedOutboundQueue.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/StackedOutboundQueue.java @@ -36,8 +36,9 @@ final class StackedOutboundQueue implements OutboundQueue { private final List uncompletedSegments = new ArrayList<>(2); private final OutboundQueueManager manager; - private volatile long lastXid = -1; + private volatile long allocatedXid = -1; private volatile long barrierXid = -1; + private volatile long lastXid = -1; @GuardedBy("unflushedSegments") private Integer shutdownOffset; @@ -62,6 +63,8 @@ final class StackedOutboundQueue implements OutboundQueue { LOG.debug("Adding segment {}", newSegment); unflushedSegments.add(newSegment); } + + allocatedXid = uncompletedSegments.get(uncompletedSegments.size() - 1).getEndXid(); } /* @@ -73,27 +76,31 @@ final class StackedOutboundQueue implements OutboundQueue { final StackedSegment fastSegment = firstSegment; if (xid >= fastSegment.getBaseXid() + StackedSegment.SEGMENT_SIZE) { - LOG.debug("Queue {} falling back to slow reservation for XID {}", this, xid); + if (xid >= allocatedXid) { + // Multiple segments, this a slow path + LOG.debug("Queue {} falling back to slow reservation for XID {}", this, xid); - // Multiple segments, this a slow path - synchronized (unflushedSegments) { - LOG.debug("Queue {} executing slow reservation for XID {}", this, xid); + synchronized (unflushedSegments) { + LOG.debug("Queue {} executing slow reservation for XID {}", this, xid); - // Shutdown was scheduled, need to fail the reservation - if (shutdownOffset != null) { - LOG.debug("Queue {} is being shutdown, failing reservation", this); - return null; - } + // Shutdown was scheduled, need to fail the reservation + if (shutdownOffset != null) { + LOG.debug("Queue {} is being shutdown, failing reservation", this); + return null; + } - // Ensure we have the appropriate segment for the specified XID - final StackedSegment slowSegment = firstSegment; - final int slowOffset = (int) (xid - slowSegment.getBaseXid()); - Verify.verify(slowOffset >= 0); + // Ensure we have the appropriate segment for the specified XID + final StackedSegment slowSegment = firstSegment; + final int slowOffset = (int) (xid - slowSegment.getBaseXid()); + Verify.verify(slowOffset >= 0); - // Now, we let's see if we need to allocate a new segment - ensureSegment(slowSegment, slowOffset); + // Now, we let's see if we need to allocate a new segment + ensureSegment(slowSegment, slowOffset); - LOG.debug("Queue {} slow reservation finished", this); + LOG.debug("Queue {} slow reservation finished", this); + } + } else { + LOG.debug("Queue {} XID {} is already backed", this, xid); } } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/StackedSegment.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/StackedSegment.java index 23bdb0be..b9030b81 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/StackedSegment.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/StackedSegment.java @@ -91,6 +91,10 @@ final class StackedSegment { return baseXid; } + long getEndXid() { + return endXid; + } + OutboundQueueEntry getEntry(final int offset) { return entries[offset]; }