aboutsummaryrefslogtreecommitdiffstats
path: root/jrt
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@oath.com>2021-06-15 11:47:17 +0000
committerHåvard Pettersen <havardpe@oath.com>2021-06-15 11:47:57 +0000
commit44974c98740ab358e4157c8c4e0358312873ebb3 (patch)
tree657733046c5fd72cf8356fae64c6d2c08f4adeab /jrt
parent062427cbeacac0f2850558976da56a7789983b62 (diff)
try to use less memory for buffers
- fix default 64k max sizes - start with empty buffers - simplify Buffer class
Diffstat (limited to 'jrt')
-rw-r--r--jrt/src/com/yahoo/jrt/Buffer.java19
-rw-r--r--jrt/src/com/yahoo/jrt/Connection.java40
-rw-r--r--jrt/src/com/yahoo/jrt/Supervisor.java13
-rw-r--r--jrt/tests/com/yahoo/jrt/BufferTest.java58
4 files changed, 23 insertions, 107 deletions
diff --git a/jrt/src/com/yahoo/jrt/Buffer.java b/jrt/src/com/yahoo/jrt/Buffer.java
index 937666a28ae..06a658740e5 100644
--- a/jrt/src/com/yahoo/jrt/Buffer.java
+++ b/jrt/src/com/yahoo/jrt/Buffer.java
@@ -6,9 +6,6 @@ import java.nio.ByteBuffer;
class Buffer {
-
- static final int MAX_IO = 65000;
-
private ByteBuffer buf;
private int readPos;
private int writePos;
@@ -111,20 +108,4 @@ class Buffer {
ensureFree(minFree);
return buf;
}
-
- public ByteBuffer getChannelReadable() {
- ByteBuffer bb = getReadable();
- if (bb.remaining() > MAX_IO) {
- bb.limit(bb.position() + MAX_IO);
- }
- return bb;
- }
-
- public ByteBuffer getChannelWritable(int minFree) {
- ByteBuffer bb = getWritable(minFree);
- if (bb.remaining() > MAX_IO) {
- bb.limit(bb.position() + MAX_IO);
- }
- return bb;
- }
}
diff --git a/jrt/src/com/yahoo/jrt/Connection.java b/jrt/src/com/yahoo/jrt/Connection.java
index 891558684ed..6158576348a 100644
--- a/jrt/src/com/yahoo/jrt/Connection.java
+++ b/jrt/src/com/yahoo/jrt/Connection.java
@@ -19,9 +19,9 @@ class Connection extends Target {
private static final Logger log = Logger.getLogger(Connection.class.getName());
- private static final int READ_SIZE = 32768;
+ private static final int READ_SIZE = 16*1024;
private static final int READ_REDO = 10;
- private static final int WRITE_SIZE = 32768;
+ private static final int WRITE_SIZE = 16*1024;
private static final int WRITE_REDO = 10;
private static final int INITIAL = 0;
@@ -32,11 +32,11 @@ class Connection extends Target {
private int state = INITIAL;
private final Queue queue = new Queue();
private final Queue myQueue = new Queue();
- private final Buffer input = new Buffer(0x1000); // Start off with small buffer.
- private final Buffer output = new Buffer(0x1000); // Start off with small buffer.
- private int maxInputSize = 64*1024;
- private int maxOutputSize = 64*1024;
- private boolean dropEmptyBuffers = false;
+ private final Buffer input = new Buffer(0); // Start off with empty buffer.
+ private final Buffer output = new Buffer(0); // Start off with empty buffer.
+ private final int maxInputSize;
+ private final int maxOutputSize;
+ private final boolean dropEmptyBuffers;
private final boolean tcpNoDelay;
private final Map<Integer, ReplyHandler> replyMap = new HashMap<>();
private final Map<TargetWatcher, TargetWatcher> watchers = new IdentityHashMap<>();
@@ -98,6 +98,9 @@ class Connection extends Target {
this.socket = parent.transport().createServerCryptoSocket(channel);
this.spec = null;
this.tcpNoDelay = tcpNoDelay;
+ maxInputSize = owner.getMaxInputBufferSize();
+ maxOutputSize = owner.getMaxOutputBufferSize();
+ dropEmptyBuffers = owner.getDropEmptyBuffers();
server = true;
owner.sessionInit(this);
}
@@ -108,22 +111,13 @@ class Connection extends Target {
this.owner = owner;
this.spec = spec;
this.tcpNoDelay = tcpNoDelay;
+ maxInputSize = owner.getMaxInputBufferSize();
+ maxOutputSize = owner.getMaxOutputBufferSize();
+ dropEmptyBuffers = owner.getDropEmptyBuffers();
server = false;
owner.sessionInit(this);
}
- public void setMaxInputSize(int bytes) {
- maxInputSize = bytes;
- }
-
- public void setMaxOutputSize(int bytes) {
- maxOutputSize = bytes;
- }
-
- public void setDropEmptyBuffers(boolean value) {
- dropEmptyBuffers = value;
- }
-
public TransportThread transportThread() {
return parent;
}
@@ -235,7 +229,7 @@ class Connection extends Target {
readSize = socket.getMinimumReadBufferSize();
}
setState(CONNECTED);
- while (socket.drain(input.getChannelWritable(readSize)) > 0) {
+ while (socket.drain(input.getWritable(readSize)) > 0) {
handlePackets();
}
break;
@@ -302,14 +296,14 @@ class Connection extends Target {
private void read() throws IOException {
boolean doneRead = false;
for (int i = 0; !doneRead && i < READ_REDO; i++) {
- ByteBuffer wb = input.getChannelWritable(readSize);
+ ByteBuffer wb = input.getWritable(readSize);
if (socket.read(wb) == -1) {
throw new IOException("jrt: Connection closed by peer");
}
doneRead = (wb.remaining() > 0);
handlePackets();
}
- while (socket.drain(input.getChannelWritable(readSize)) > 0) {
+ while (socket.drain(input.getWritable(readSize)) > 0) {
handlePackets();
}
if (dropEmptyBuffers) {
@@ -346,7 +340,7 @@ class Connection extends Target {
owner.writePacket(info);
info.encodePacket(packet, wb);
}
- ByteBuffer rb = output.getChannelReadable();
+ ByteBuffer rb = output.getReadable();
if (rb.remaining() == 0) {
break;
}
diff --git a/jrt/src/com/yahoo/jrt/Supervisor.java b/jrt/src/com/yahoo/jrt/Supervisor.java
index 5975e191a5b..b82664b2f56 100644
--- a/jrt/src/com/yahoo/jrt/Supervisor.java
+++ b/jrt/src/com/yahoo/jrt/Supervisor.java
@@ -21,8 +21,8 @@ public class Supervisor {
private SessionHandler sessionHandler = null;
private final Object methodMapLock = new Object();
private final AtomicReference<HashMap<String, Method>> methodMap = new AtomicReference<>(new HashMap<>());
- private int maxInputBufferSize = 0;
- private int maxOutputBufferSize = 0;
+ private int maxInputBufferSize = 64*1024;
+ private int maxOutputBufferSize = 64*1024;
private boolean dropEmptyBuffers = false;
/**
@@ -47,6 +47,7 @@ public class Supervisor {
dropEmptyBuffers = value;
return this;
}
+ boolean getDropEmptyBuffers() { return dropEmptyBuffers; }
/**
* Set maximum input buffer size. This value will only affect
@@ -61,6 +62,7 @@ public class Supervisor {
public void setMaxInputBufferSize(int bytes) {
maxInputBufferSize = bytes;
}
+ int getMaxInputBufferSize() { return maxInputBufferSize; }
/**
* Set maximum output buffer size. This value will only affect
@@ -75,6 +77,7 @@ public class Supervisor {
public void setMaxOutputBufferSize(int bytes) {
maxOutputBufferSize = bytes;
}
+ int getMaxOutputBufferSize() { return maxOutputBufferSize; }
/**
* Obtain the method map for this Supervisor
@@ -192,12 +195,6 @@ public class Supervisor {
* @param target the target
**/
void sessionInit(Target target) {
- if (target instanceof Connection) {
- Connection conn = (Connection) target;
- conn.setMaxInputSize(maxInputBufferSize);
- conn.setMaxOutputSize(maxOutputBufferSize);
- conn.setDropEmptyBuffers(dropEmptyBuffers);
- }
SessionHandler handler = sessionHandler;
if (handler != null) {
handler.handleSessionInit(target);
diff --git a/jrt/tests/com/yahoo/jrt/BufferTest.java b/jrt/tests/com/yahoo/jrt/BufferTest.java
index 7f3145365d9..10f1fbc17d3 100644
--- a/jrt/tests/com/yahoo/jrt/BufferTest.java
+++ b/jrt/tests/com/yahoo/jrt/BufferTest.java
@@ -28,7 +28,7 @@ public class BufferTest {
@org.junit.Test
public void testBuffer() {
- int size = Buffer.MAX_IO + (Buffer.MAX_IO / 10);
+ int size = 70*1024;
Buffer buf = new Buffer(1024);
ByteBuffer b = null;
@@ -118,62 +118,6 @@ public class BufferTest {
}
@org.junit.Test
- public void testBufferMax() {
- int size = Buffer.MAX_IO + (Buffer.MAX_IO / 10);
- Buffer buf = new Buffer(1024);
- ByteBuffer b = null;
-
- byte[] x = new byte[size];
- byte[] y = new byte[size];
-
- Arrays.fill(x, (byte) 10);
- Arrays.fill(y, (byte) 55);
-
- assertEquals(buf.bytes(), 0);
- assertFalse(Arrays.equals(x, y));
-
- b = buf.getChannelWritable(size);
- assertEquals(b.remaining(), Buffer.MAX_IO);
- assertTrue(b.remaining() < size);
- assertEquals(buf.bytes(), 0);
- b.put(x, 0, Buffer.MAX_IO);
- assertEquals(buf.bytes(), Buffer.MAX_IO);
- assertEquals(b.remaining(), 0);
-
- b = buf.getChannelWritable(size - Buffer.MAX_IO);
- assertTrue(b.remaining() >= size - Buffer.MAX_IO);
- assertEquals(buf.bytes(), Buffer.MAX_IO);
- b.put(x, Buffer.MAX_IO, x.length - Buffer.MAX_IO);
- assertEquals(buf.bytes(), size);
-
- b = buf.getChannelReadable();
- assertEquals(buf.bytes(), size);
-
- b = buf.getChannelWritable(512);
- assertEquals(buf.bytes(), size);
- b.put((byte)42);
- assertEquals(buf.bytes(), size + 1);
-
- b = buf.getChannelReadable();
- assertEquals(buf.bytes(), size + 1);
- assertEquals(b.remaining(), Buffer.MAX_IO);
- b.get(y, 0, Buffer.MAX_IO);
- assertEquals(buf.bytes(), size - Buffer.MAX_IO + 1);
-
- b = buf.getChannelReadable();
- assertEquals(buf.bytes(), size - Buffer.MAX_IO + 1);
- assertEquals(b.remaining(), size - Buffer.MAX_IO + 1);
- b.get(y, Buffer.MAX_IO, y.length - Buffer.MAX_IO);
- assertEquals(buf.bytes(), 1);
- assertEquals(b.remaining(), 1);
- assertEquals(b.get(), 42);
- assertEquals(buf.bytes(), 0);
- assertEquals(b.remaining(), 0);
-
- assertTrue(Arrays.equals(x, y));
- }
-
- @org.junit.Test
public void testBufferShrink() {
Buffer buf = new Buffer(500);
ByteBuffer b = null;