Author: fhanik Date: Tue Mar 13 19:48:39 2007 New Revision: 517977 URL: http://svn.apache.org/viewvc?view=rev&rev=517977 Log: Implemented the cache properly with its own attribute, removed one processor that is not needed
Modified: tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/SocketProperties.java tomcat/tc6.0.x/trunk/webapps/docs/config/http.xml Modified: tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java?view=diff&rev=517977&r1=517976&r2=517977 ============================================================================== --- tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java (original) +++ tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Tue Mar 13 19:48:39 2007 @@ -155,6 +155,40 @@ protected ServerSocketChannel serverSock = null; /** + * Cache for SocketProcessor objects + */ + protected ConcurrentLinkedQueue<SocketProcessor> processorCache = new ConcurrentLinkedQueue<SocketProcessor>() { + protected AtomicInteger size = new AtomicInteger(0); + public boolean offer(SocketProcessor sc) { + sc.reset(null,null); + boolean offer = socketProperties.getProcessorCache()==-1?true:size.get()<socketProperties.getProcessorCache(); + //avoid over growing our cache or add after we have stopped + if ( running && (!paused) && (offer) ) { + boolean result = super.offer(sc); + if ( result ) { + size.incrementAndGet(); + } + return result; + } + else return false; + } + + public SocketProcessor poll() { + SocketProcessor result = super.poll(); + if ( result != null ) { + size.decrementAndGet(); + } + return result; + } + + public void clear() { + super.clear(); + size.set(0); + } + }; + + + /** * Cache for key attachment objects */ protected ConcurrentLinkedQueue<KeyAttachment> keyCache = new ConcurrentLinkedQueue<KeyAttachment>() { @@ -727,6 +761,7 @@ eventCache.clear(); keyCache.clear(); nioChannels.clear(); + processorCache.clear(); if ( executor!=null ) { ThreadPoolExecutor tpe = (ThreadPoolExecutor)executor; tpe.shutdown(); @@ -977,19 +1012,7 @@ * Process given socket. */ protected boolean processSocket(NioChannel socket) { - try { - if (executor == null) { - getWorkerThread().assign(socket); - } else { - executor.execute(new SocketProcessor(socket,null)); - } - } catch (Throwable t) { - // This means we got an OOM or similar creating a thread, or that - // the pool and its queue are full - log.error(sm.getString("endpoint.process.fail"), t); - return false; - } - return true; + return processSocket(socket,null); } @@ -1001,7 +1024,10 @@ if (executor == null) { getWorkerThread().assign(socket, status); } else { - executor.execute(new SocketProcessor(socket, status)); + SocketProcessor sc = processorCache.poll(); + if ( sc == null ) sc = new SocketProcessor(socket,status); + else sc.reset(socket,status); + executor.execute(sc); } } catch (Throwable t) { // This means we got an OOM or similar creating a thread, or that @@ -1794,32 +1820,6 @@ } - // ---------------------------------------------- SocketOptionsProcessor Inner Class - - - /** - * This class is the equivalent of the Worker, but will simply use in an - * external Executor thread pool. - */ - protected class SocketOptionsProcessor implements Runnable { - - protected SocketChannel sc = null; - - public SocketOptionsProcessor(SocketChannel socket) { - this.sc = socket; - } - - public void run() { - if ( !setSocketOptions(sc) ) { - try { - sc.socket().close(); - sc.close(); - }catch ( IOException ix ) { - if ( log.isDebugEnabled() ) log.debug("",ix); - } - } - } - } // ---------------------------------------------- SocketProcessor Inner Class @@ -1833,6 +1833,10 @@ protected SocketStatus status = null; public SocketProcessor(NioChannel socket, SocketStatus status) { + reset(socket,status); + } + + public void reset(NioChannel socket, SocketStatus status) { this.socket = socket; this.status = status; } @@ -1850,10 +1854,11 @@ } catch ( Exception x ) { log.error("",x); } - socket = null; - status = null; } - + socket = null; + status = null; + //return to cache + processorCache.offer(this); } } Modified: tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/SocketProperties.java URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/SocketProperties.java?view=diff&rev=517977&r1=517976&r2=517977 ============================================================================== --- tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/SocketProperties.java (original) +++ tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/SocketProperties.java Tue Mar 13 19:48:39 2007 @@ -26,18 +26,29 @@ */ public class SocketProperties { /** - * Enable/disable key cache, this bounced cache stores + * Enable/disable key cache, this bounded cache stores * KeyAttachment objects to reduce GC - * Default is 100 + * Default is 500 * -1 is unlimited * 0 is disabled */ protected int keyCache = 500; + + /** + * Enable/disable socket processor cache, this bounded cache stores + * SocketProcessor objects to reduce GC + * Default is 500 + * -1 is unlimited + * 0 is disabled + */ + protected int processorCache = 500; + + /** * Enable/disable poller event cache, this bounded cache stores * PollerEvent objects to reduce GC for the poller - * Default is -1 (unlimited) + * Default is 500 * -1 is unlimited * 0 is disabled * >0 the max number of objects to keep in cache. @@ -243,6 +254,10 @@ return appWriteBufSize; } + public int getProcessorCache() { + return processorCache; + } + public int getDirectBufferPool() { return bufferPool; } @@ -325,6 +340,10 @@ public void setAppWriteBufSize(int appWriteBufSize) { this.appWriteBufSize = appWriteBufSize; + } + + public void setProcessorCache(int processorCache) { + this.processorCache = processorCache; } public void setDirectBufferPool(int directBufferPool) { Modified: tomcat/tc6.0.x/trunk/webapps/docs/config/http.xml URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/docs/config/http.xml?view=diff&rev=517977&r1=517976&r2=517977 ============================================================================== --- tomcat/tc6.0.x/trunk/webapps/docs/config/http.xml (original) +++ tomcat/tc6.0.x/trunk/webapps/docs/config/http.xml Tue Mar 13 19:48:39 2007 @@ -457,6 +457,12 @@ The value is in bytes, the default value is 1024*1024*100 (100MB) </p> </attribute> + <attribute name="socket.processorCache" required="false"> + <p>Tomcat will cache SocketProcessor objects to reduce garbage collection. + The integer value specifies how many objects to keep in the cache at most. + The default is 500. + Other values are -1. unlimited cache, and 0, no cache.</p> + </attribute> <attribute name="socket.keyCache" required="false"> <p>Tomcat will cache KeyAttachment objects to reduce garbage collection. The integer value specifies how many objects to keep in the cache at most. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]