From: fhanik Date: Wed, 25 Oct 2006 22:11:10 +0000 (+0000) Subject: Documented socket properties X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=ddb52f512e61b22a34e2ba27e43b6da869a9c9b7;p=tomcat7.0 Documented socket properties Added in the ability to cache bytebuffers based on number of channels or number of bytes Added in nonGC poller events to lower CPU usage during high traffic git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@467787 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/tomcat/util/net/NioChannel.java b/java/org/apache/tomcat/util/net/NioChannel.java index 2cff92b2e..d28fc5631 100644 --- a/java/org/apache/tomcat/util/net/NioChannel.java +++ b/java/org/apache/tomcat/util/net/NioChannel.java @@ -55,6 +55,14 @@ public class NioChannel implements ByteChannel{ bufHandler.getReadBuffer().clear(); bufHandler.getWriteBuffer().clear(); } + + public int getBufferSize() { + if ( bufHandler == null ) return 0; + int size = 0; + size += bufHandler.getReadBuffer()!=null?bufHandler.getReadBuffer().capacity():0; + size += bufHandler.getWriteBuffer()!=null?bufHandler.getWriteBuffer().capacity():0; + return size; + } /** * returns true if the network buffer has diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java index 4e262b8a0..ddc24de8c 100644 --- a/java/org/apache/tomcat/util/net/NioEndpoint.java +++ b/java/org/apache/tomcat/util/net/NioEndpoint.java @@ -47,6 +47,7 @@ import org.apache.tomcat.util.IntrospectionUtils; import org.apache.tomcat.util.net.SecureNioChannel.ApplicationBufferHandler; import org.apache.tomcat.util.res.StringManager; import org.apache.tomcat.util.net.NioEndpoint.KeyAttachment; +import java.util.concurrent.atomic.AtomicInteger; /** * NIO tailored thread pool, providing the following services: @@ -150,6 +151,8 @@ public class NioEndpoint { protected ConcurrentLinkedQueue nioChannels = new ConcurrentLinkedQueue() { + protected AtomicInteger size = new AtomicInteger(0); + protected AtomicInteger bytes = new AtomicInteger(0); public boolean offer(NioChannel socket) { Poller pol = socket.getPoller(); Selector sel = pol!=null?pol.getSelector():null; @@ -157,13 +160,32 @@ public class NioEndpoint { KeyAttachment att = key!=null?(KeyAttachment)key.attachment():null; if ( att!=null ) att.reset(); if ( key!=null ) key.attach(null); + boolean offer = socketProperties.getBufferPool()==-1?true:size.get() events = new ConcurrentLinkedQueue(); + protected ConcurrentLinkedQueue eventCache = new ConcurrentLinkedQueue(); protected boolean close = false; protected long nextExpiration = 0;//optimize expiration handling @@ -1000,30 +1068,15 @@ public class NioEndpoint { * @param socket to add to the poller */ public void add(final NioChannel socket) { - final SelectionKey key = socket.getIOChannel().keyFor(selector); - final KeyAttachment att = (KeyAttachment)key.attachment(); - Runnable r = new Runnable() { - public void run() { - try { - if (key != null) { - key.interestOps(SelectionKey.OP_READ); - att.interestOps(SelectionKey.OP_READ); - } - }catch ( CancelledKeyException ckx ) { - try { - if ( key != null && key.attachment() != null ) { - KeyAttachment ka = (KeyAttachment)key.attachment(); - ka.setError(true); //set to collect this socket immediately - } - try {socket.close();}catch (Exception ignore){} - if ( socket.isOpen() ) socket.close(true); - } catch ( Exception ignore ) {} - } - } - }; + add(socket,SelectionKey.OP_READ); + } + + public void add(final NioChannel socket, final int interestOps) { + PollerEvent r = this.eventCache.poll(); + if ( r==null) r = new PollerEvent(socket,interestOps); addEvent(r); } - + public boolean events() { boolean result = false; //synchronized (events) { @@ -1032,6 +1085,10 @@ public class NioEndpoint { while ( (r = (Runnable)events.poll()) != null ) { try { r.run(); + if ( r instanceof PollerEvent ) { + ((PollerEvent)r).reset(); + eventCache.offer((PollerEvent)r); + } } catch ( Exception x ) { log.error("",x); } @@ -1049,7 +1106,6 @@ public class NioEndpoint { Runnable r = new Runnable() { public void run() { try { - socket.getIOChannel().register(selector, SelectionKey.OP_READ, ka); } catch (Exception x) { log.error("", x); @@ -1398,25 +1454,7 @@ public class NioEndpoint { final SelectionKey fk = key; final int intops = handshake; final KeyAttachment ka = (KeyAttachment)fk.attachment(); - //register for handshake ops - Runnable r = new Runnable() { - public void run() { - try { - fk.interestOps(intops); - ka.interestOps(intops); - } catch (CancelledKeyException ckx) { - try { - if ( fk != null && fk.attachment() != null ) { - ka.setError(true); //set to collect this socket immediately - try {ka.getChannel().getIOChannel().socket().close();}catch(Exception ignore){} - try {ka.getChannel().close();}catch(Exception ignore){} - } - } catch (Exception ignore) {} - } - - } - }; - ka.getPoller().addEvent(r); + ka.getPoller().add(socket,intops); } } } finally { diff --git a/java/org/apache/tomcat/util/net/SecureNioChannel.java b/java/org/apache/tomcat/util/net/SecureNioChannel.java index c7981cbe0..44da15e5d 100644 --- a/java/org/apache/tomcat/util/net/SecureNioChannel.java +++ b/java/org/apache/tomcat/util/net/SecureNioChannel.java @@ -68,9 +68,16 @@ public class SecureNioChannel extends NioChannel { //initiate handshake sslEngine.beginHandshake(); initHandshakeStatus = sslEngine.getHandshakeStatus(); - } + public int getBufferSize() { + int size = super.getBufferSize(); + size += netInBuffer!=null?netInBuffer.capacity():0; + size += netOutBuffer!=null?netOutBuffer.capacity():0; + return size; + } + + //=========================================================================================== // NIO SSL METHODS //=========================================================================================== diff --git a/java/org/apache/tomcat/util/net/SocketProperties.java b/java/org/apache/tomcat/util/net/SocketProperties.java index 86c1e2bf7..0ff3e429d 100644 --- a/java/org/apache/tomcat/util/net/SocketProperties.java +++ b/java/org/apache/tomcat/util/net/SocketProperties.java @@ -4,21 +4,92 @@ import java.net.Socket; import java.net.SocketException; public class SocketProperties { + /** + * Enable/disable direct buffers for the network buffers + * Default value is enabled + */ protected boolean directBuffer = true; + /** + * Socket receive buffer size in bytes (SO_RCVBUF) + * Default value is 25188 + */ protected int rxBufSize = 25188; + /** + * Socket send buffer size in bytes (SO_SNDBUF) + * Default value is 43800 + */ protected int txBufSize = 43800; - protected int directBufferPool = 500; + /** + * NioChannel pool size for the endpoint, + * this value is how many channels + * -1 means unlimited cached, 0 means no cache + * Default value is 500 + */ + protected int bufferPool = 500; + + + /** + * Buffer pool size in bytes to be cached + * -1 means unlimited, 0 means no cache + * Default value is 100MB (1024*1024*100 bytes) + */ + protected int bufferPoolSize = 1024*1024*100; + + /** + * TCP_NO_DELAY option, default is false + */ protected boolean tcpNoDelay = false; + /** + * SO_KEEPALIVE option, default is false + */ protected boolean soKeepAlive = false; + /** + * OOBINLINE option, default is true + */ protected boolean ooBInline = true; + /** + * SO_REUSEADDR option, default is true + */ protected boolean soReuseAddress = true; + /** + * SO_LINGER option, default is true, paired with the soLingerTime value + */ protected boolean soLingerOn = true; + /** + * SO_LINGER option, default is 25 seconds. + */ protected int soLingerTime = 25; + /** + * SO_TIMEOUT option, default is 5000 milliseconds + */ protected int soTimeout = 5000; + /** + * Traffic class option, value between 0 and 255 + * IPTOS_LOWCOST (0x02) + * IPTOS_RELIABILITY (0x04) + * IPTOS_THROUGHPUT (0x08) + * IPTOS_LOWDELAY (0x10) + * Default value is 0x04 | 0x08 | 0x010 + */ protected int soTrafficClass = 0x04 | 0x08 | 0x010; + /** + * Performance preferences according to + * http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#setPerformancePreferences(int,%20int,%20int) + * Default value is 1 + */ protected int performanceConnectionTime = 1; + /** + * Performance preferences according to + * http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#setPerformancePreferences(int,%20int,%20int) + * Default value is 0 + */ protected int performanceLatency = 0; + /** + * Performance preferences according to + * http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#setPerformancePreferences(int,%20int,%20int) + * Default value is 1 + */ protected int performanceBandwidth = 1; @@ -91,8 +162,16 @@ public class SocketProperties { return txBufSize; } + public int getBufferPool() { + return bufferPool; + } + + public int getBufferPoolSize() { + return bufferPoolSize; + } + public int getDirectBufferPool() { - return directBufferPool; + return bufferPool; } public void setPerformanceConnectionTime(int performanceConnectionTime) { @@ -151,8 +230,16 @@ public class SocketProperties { this.soLingerOn = soLingerOn; } + public void setBufferPool(int bufferPool) { + this.bufferPool = bufferPool; + } + + public void setBufferPoolSize(int bufferPoolSize) { + this.bufferPoolSize = bufferPoolSize; + } + public void setDirectBufferPool(int directBufferPool) { - this.directBufferPool = directBufferPool; + this.bufferPool = directBufferPool; } } \ No newline at end of file