Lower tracing overhead 43/22243/2
authorRobert Varga <rovarga@cisco.com>
Tue, 9 Jun 2015 23:38:42 +0000 (01:38 +0200)
committerRobert Varga <rovarga@cisco.com>
Tue, 9 Jun 2015 23:46:05 +0000 (01:46 +0200)
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 515f6d06a0019a7a5906e676e9475fede496036f..7101cfa46ff36cc22b9c25d3b94854e5ea72a9eb 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(bytes));
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Open message serialized to: {}", ByteBufUtil.hexDump(bytes));
+        }
         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 8f76c4e01320b93619c01f047f23720722f0975f..2cf0c93a09497729e83e8c581a428820cd9f512c 100644 (file)
@@ -65,7 +65,9 @@ public class BGPUpdateMessageParser implements MessageParser, MessageSerializer
     @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 6ef1d75d3353cd7f480fb59fab16f7ecd87249cd..2cdd48362bc51c111e728cfdf5487e3fa5858186 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 bab39f312d6ade463dd91ebe5e913213d0516f69..197512d3a0ab5ebeb15f698da46d140536903856 100644 (file)
@@ -113,7 +113,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.