Lower tracing overhead 26/22326/2
authorRobert Varga <rovarga@cisco.com>
Tue, 9 Jun 2015 23:38:42 +0000 (01:38 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 11 Jun 2015 08:33:05 +0000 (08:33 +0000)
Arrays.toString() and ByteBufUtil.hexDump() are expensive relative to an
disabled trace call. Guard the corresponding trace() calls with
LOG.isTraceEnabled(). Discovered in BGP performance traces to have some
4% overhead.

Change-Id: I94ce9b57b86bae9be35e5839447a19b268064469
Signed-off-by: Robert Varga <rovarga@cisco.com>
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPOpenMessageParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPUpdateMessageParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/CapabilityParameterParser.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPByteToMessageDecoder.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPMessageToByteEncoder.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/LocRibWriter.java

index 36e9fffef050ec4c08af7c3d50b0ecd3e85bc71a..6f2a02c5e30dfd84ba685cd11c09ef3174db4a26 100644 (file)
@@ -91,7 +91,9 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali
         msgBody.writeByte(paramsBuffer.writerIndex());
         msgBody.writeBytes(paramsBuffer);
 
-        LOG.trace("Open message serialized to: {}", ByteBufUtil.hexDump(msgBody));
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Open message serialized to: {}", ByteBufUtil.hexDump(msgBody));
+        }
         MessageUtil.formatMessage(TYPE, msgBody, bytes);
     }
 
@@ -106,7 +108,9 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali
     @Override
     public Open parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
         Preconditions.checkArgument(body != null, "Byte array cannot be null.");
-        LOG.trace("Started parsing of open message: {}", ByteBufUtil.hexDump(body));
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Started parsing of open message: {}", ByteBufUtil.hexDump(body));
+        }
 
         if (body.readableBytes() < MIN_MSG_LENGTH) {
             throw BGPDocumentedException.badMessageLength("Open message too small.", messageLength);
@@ -139,7 +143,9 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali
 
     private void fillParams(final ByteBuf buffer, final List<BgpParameters> params) throws BGPDocumentedException {
         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Byte array cannot be null or empty.");
-        LOG.trace("Started parsing of BGP parameter: {}", ByteBufUtil.hexDump(buffer));
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Started parsing of BGP parameter: {}", ByteBufUtil.hexDump(buffer));
+        }
         while (buffer.isReadable()) {
             if (buffer.readableBytes() <= 2) {
                 throw new BGPDocumentedException("Malformed parameter encountered (" + buffer.readableBytes() + " bytes left)", BGPError.OPT_PARAM_NOT_SUPPORTED);
@@ -160,6 +166,8 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali
                 LOG.debug("Ignoring BGP Parameter type: {}", paramType);
             }
         }
-        LOG.trace("Parsed BGP parameters: {}", Arrays.toString(params.toArray()));
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Parsed BGP parameters: {}", Arrays.toString(params.toArray()));
+        }
     }
 }
index 0525ee43cbfd921300487352fecbd962e1480b5c..093f0cbfc174627528c5f3fb317c6abc01b141ef 100644 (file)
@@ -65,7 +65,9 @@ public final class BGPUpdateMessageParser implements MessageParser, MessageSeria
     @Override
     public Update parseMessageBody(final ByteBuf buffer, final int messageLength) throws BGPDocumentedException {
         Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
-        LOG.trace("Started parsing of update message: {}", ByteBufUtil.hexDump(buffer));
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Started parsing of update message: {}", ByteBufUtil.hexDump(buffer));
+        }
 
         final int withdrawnRoutesLength = buffer.readUnsignedShort();
         final UpdateBuilder eventBuilder = new UpdateBuilder();
index d0a8db85b8f6af8b7ccff313969cdf44f13ec958..04c7cf82feb6f7d6a89c821f0d96d83bdff082fc 100644 (file)
@@ -45,7 +45,11 @@ public final class CapabilityParameterParser implements ParameterParser, Paramet
     @Override
     public BgpParameters parseParameter(final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException {
         Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
-        LOG.trace("Started parsing of BGP Capabilities: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
+
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Started parsing of BGP Capabilities: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
+        }
+
         final List<OptionalCapabilities> optionalCapas = Lists.newArrayList();
         while (buffer.isReadable()) {
             final OptionalCapabilities optionalCapa = parseOptionalCapability(buffer);
@@ -88,7 +92,10 @@ public final class CapabilityParameterParser implements ParameterParser, Paramet
             if (bytes == null) {
                 throw new IllegalArgumentException("Unhandled capability class" + cap.getImplementedInterface());
             }
-            LOG.trace("BGP capability serialized to: {}", ByteBufUtil.hexDump(bytes));
+
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("BGP capability serialized to: {}", ByteBufUtil.hexDump(bytes));
+            }
             byteAggregator.writeBytes(bytes);
         }
     }
index 4ceb42de8592defb1b6cd4880d8d3731bae04fd9..b83de5d0cebb2f31d32bec2a9bc969e652905599 100644 (file)
@@ -8,14 +8,11 @@
 package org.opendaylight.protocol.bgp.rib.impl;
 
 import com.google.common.base.Preconditions;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufUtil;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.ByteToMessageDecoder;
-
 import java.util.List;
-
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
@@ -37,7 +34,9 @@ final class BGPByteToMessageDecoder extends ByteToMessageDecoder {
     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws BGPDocumentedException,
             BGPParsingException {
         if (in.isReadable()) {
-            LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
+            }
             out.add(this.registry.parseMessage(in));
         } else {
             LOG.trace("No more content in incoming buffer.");
index 7ccb289599c9deec15e871442c175a5d4b9cb874..7d576920e5c8bfcfd0aa6e101e75fdeb3d36d2ef 100644 (file)
@@ -8,13 +8,11 @@
 package org.opendaylight.protocol.bgp.rib.impl;
 
 import com.google.common.base.Preconditions;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufUtil;
 import io.netty.channel.ChannelHandler.Sharable;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.MessageToByteEncoder;
-
 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
 import org.opendaylight.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
@@ -36,7 +34,9 @@ final class BGPMessageToByteEncoder extends MessageToByteEncoder<Notification> {
     protected void encode(final ChannelHandlerContext ctx, final Notification msg, final ByteBuf out) {
         LOG.trace("Encoding message: {}", msg);
         this.registry.serializeMessage(msg, out);
-        LOG.trace("Encoded message: {}", ByteBufUtil.hexDump(out));
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Encoded message: {}", ByteBufUtil.hexDump(out));
+        }
         LOG.debug("Message sent to output: {}", msg);
     }
 }
index 7743d2d4939d67b71e0ce88f68356d30aa7f3d83..8d958cf341fbc9d841dd092ba4c46ec590186af9 100644 (file)
@@ -119,7 +119,10 @@ final class LocRibWriter implements AutoCloseable, DOMDataTreeChangeListener {
     @Override
     public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
         final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
-        LOG.trace("Received data change to LocRib {}", Arrays.toString(changes.toArray()));
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Received data change to LocRib {}", Arrays.toString(changes.toArray()));
+        }
+
         /*
          * We use two-stage processing here in hopes that we avoid duplicate
          * calculations when multiple peers have changed a particular entry.