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 2016/04/05 17:53:14 UTC
[22/23] logging-log4j2 git commit: LOG4J2-1344 let
MemoryMappedFileManager implement ByteBufferDestination so it can be used in
a garbage-free manner
LOG4J2-1344 let MemoryMappedFileManager implement ByteBufferDestination so it can be used in a garbage-free manner
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f86f86f0
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f86f86f0
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f86f86f0
Branch: refs/heads/LOG4J2-1343-no-gc-outputstreamappenders
Commit: f86f86f0a5a2d7980e523e6f3d6b1ca33ecf0ba6
Parents: fd4ac53
Author: rpopma <rp...@apache.org>
Authored: Tue Apr 5 23:22:32 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Tue Apr 5 23:22:32 2016 +0900
----------------------------------------------------------------------
.../core/appender/MemoryMappedFileManager.java | 37 ++++++++++++++++----
1 file changed, 30 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f86f86f0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/MemoryMappedFileManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/MemoryMappedFileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/MemoryMappedFileManager.java
index 9335ddb..744623f 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/MemoryMappedFileManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/MemoryMappedFileManager.java
@@ -22,6 +22,7 @@ import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.lang.reflect.Method;
+import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
@@ -33,6 +34,7 @@ import java.util.Map;
import java.util.Objects;
import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.layout.ByteBufferDestination;
import org.apache.logging.log4j.core.util.Closer;
import org.apache.logging.log4j.core.util.NullOutputStream;
@@ -42,7 +44,7 @@ import org.apache.logging.log4j.core.util.NullOutputStream;
* Extends OutputStreamManager but instead of using a buffered output stream, this class maps a region of a file into
* memory and writes to this memory region.
* <p>
- *
+ *
* @see <a href="http://www.codeproject.com/Tips/683614/Things-to-Know-about-Memory-Mapped-File-in-Java">
* http://www.codeproject.com/Tips/683614/Things-to-Know-about-Memory-Mapped-File-in-Java</a>
* @see <a href="http://bugs.java.com/view_bug.do?bug_id=6893654">http://bugs.java.com/view_bug.do?bug_id=6893654</a>
@@ -50,11 +52,11 @@ import org.apache.logging.log4j.core.util.NullOutputStream;
* @see <a
* href="http://stackoverflow.com/questions/9261316/memory-mapped-mappedbytebuffer-or-direct-bytebuffer-for-db-implementation">
* http://stackoverflow.com/questions/9261316/memory-mapped-mappedbytebuffer-or-direct-bytebuffer-for-db-implementation</a>
- *
+ *
* @since 2.1
*/
//CHECKSTYLE:ON
-public class MemoryMappedFileManager extends OutputStreamManager {
+public class MemoryMappedFileManager extends OutputStreamManager implements ByteBufferDestination {
/**
* Default length of region to map.
*/
@@ -141,7 +143,7 @@ public class MemoryMappedFileManager extends OutputStreamManager {
final float millis = (float) ((System.nanoTime() - startNanos) / NANOS_PER_MILLISEC);
LOGGER.debug("{} {} extended {} OK in {} millis", getClass().getSimpleName(), getName(), getFileName(),
millis);
-
+
mappedBuffer = mmap(randomAccessFile.getChannel(), getFileName(), offset, length);
mappingOffset = offset;
} catch (final Exception ex) {
@@ -235,7 +237,7 @@ public class MemoryMappedFileManager extends OutputStreamManager {
/**
* Returns the length of the memory mapped region.
- *
+ *
* @return the length of the mapped region
*/
public int getRegionLength() {
@@ -245,7 +247,7 @@ public class MemoryMappedFileManager extends OutputStreamManager {
/**
* Returns {@code true} if the content of the buffer should be forced to the storage device on every write,
* {@code false} otherwise.
- *
+ *
* @return whether each write should be force-sync'ed
*/
public boolean isImmediateFlush() {
@@ -257,7 +259,7 @@ public class MemoryMappedFileManager extends OutputStreamManager {
* <p>
* Key: "fileURI" Value: provided "advertiseURI" param.
* </p>
- *
+ *
* @return Map of content format keys supporting FileManager
*/
@Override
@@ -267,6 +269,27 @@ public class MemoryMappedFileManager extends OutputStreamManager {
return result;
}
+ @Override
+ public ByteBufferDestination getByteBufferDestination() {
+ return this;
+ }
+
+ @Override
+ public synchronized ByteBuffer getByteBuffer() {
+ return mappedBuffer;
+ }
+
+ @Override
+ public synchronized ByteBuffer drain(final ByteBuffer buf) {
+ remap();
+ return mappedBuffer;
+ }
+
+ @Override
+ public synchronized long size() {
+ return this.mappingOffset + mappedBuffer.position();
+ }
+
/**
* Factory Data.
*/