You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2014/09/18 18:24:49 UTC
[02/17] git commit: Add method Integers.ceilingNextPowerOfTwo and use
it instead of the Disruptor's Util class.
Add method Integers.ceilingNextPowerOfTwo and use it instead of the
Disruptor's Util class.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/ebbe63ad
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/ebbe63ad
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/ebbe63ad
Branch: refs/heads/master
Commit: ebbe63ad8d0c44ed1b126296195c65d171c1314b
Parents: fc3d480
Author: rpopma <rp...@apache.org>
Authored: Tue Sep 16 22:54:07 2014 +0900
Committer: rpopma <rp...@apache.org>
Committed: Tue Sep 16 22:54:07 2014 +0900
----------------------------------------------------------------------
.../logging/log4j/core/async/AsyncLogger.java | 4 +-
.../core/async/AsyncLoggerConfigHelper.java | 4 +-
.../logging/log4j/core/util/Integers.java | 11 ++++++
.../logging/log4j/core/util/IntegersTest.java | 41 ++++++++++++++++++++
4 files changed, 56 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ebbe63ad/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
index 2cabc4f..8d8d656 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java
@@ -30,6 +30,7 @@ import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
import org.apache.logging.log4j.core.util.Clock;
import org.apache.logging.log4j.core.util.ClockFactory;
+import org.apache.logging.log4j.core.util.Integers;
import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.MessageFactory;
@@ -44,7 +45,6 @@ import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
-import com.lmax.disruptor.util.Util;
/**
* AsyncLogger is a logger designed for high throughput and low latency logging.
@@ -146,7 +146,7 @@ public class AsyncLogger extends Logger {
} catch (final Exception ex) {
LOGGER.warn("Invalid RingBufferSize {}, using default size {}.", userPreferredRBSize, ringBufferSize);
}
- return Util.ceilingNextPowerOfTwo(ringBufferSize);
+ return Integers.ceilingNextPowerOfTwo(ringBufferSize);
}
/**
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ebbe63ad/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigHelper.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigHelper.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigHelper.java
index ead8036..b3b8a93 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigHelper.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigHelper.java
@@ -23,6 +23,7 @@ import java.util.concurrent.ThreadFactory;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
+import org.apache.logging.log4j.core.util.Integers;
import org.apache.logging.log4j.status.StatusLogger;
import com.lmax.disruptor.BlockingWaitStrategy;
@@ -38,7 +39,6 @@ import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
-import com.lmax.disruptor.util.Util;
/**
* Helper class decoupling the {@code AsyncLoggerConfig} class from the LMAX
@@ -160,7 +160,7 @@ class AsyncLoggerConfigHelper {
LOGGER.warn("Invalid RingBufferSize {}, using default size {}.",
userPreferredRBSize, ringBufferSize);
}
- return Util.ceilingNextPowerOfTwo(ringBufferSize);
+ return Integers.ceilingNextPowerOfTwo(ringBufferSize);
}
private static ExceptionHandler getExceptionHandler() {
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ebbe63ad/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Integers.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Integers.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Integers.java
index 296011f..7b09e5a 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Integers.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Integers.java
@@ -53,4 +53,15 @@ public final class Integers {
public static int parseInt(final String s) {
return parseInt(s, 0);
}
+
+ /**
+ * Calculate the next power of 2, greater than or equal to x.<p>
+ * From Hacker's Delight, Chapter 3, Harry S. Warren Jr.
+ *
+ * @param x Value to round up
+ * @return The next power of 2 from x inclusive
+ */
+ public static int ceilingNextPowerOfTwo(final int x) {
+ return 1 << (32 - Integer.numberOfLeadingZeros(x - 1));
+ }
}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ebbe63ad/log4j-core/src/test/java/org/apache/logging/log4j/core/util/IntegersTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/IntegersTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/IntegersTest.java
new file mode 100644
index 0000000..a0c8d1c
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/IntegersTest.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests the Integers class.
+ */
+public class IntegersTest {
+
+ @Test
+ public void testCeilingNextPowerOfTwoReturnsNextPowerOfTwo() {
+ int powerOfTwo = Integers.ceilingNextPowerOfTwo(1000);
+ assertEquals(1024, powerOfTwo);
+ }
+
+ @Test
+ public void testCeilingNextPowerOfTwoReturnsExactPowerOfTwo() {
+ int powerOfTwo = Integers.ceilingNextPowerOfTwo(1024);
+ assertEquals(1024, powerOfTwo);
+ }
+
+}