From 788cdf7a9457feb9871726ed11e261ad642ab9c1 Mon Sep 17 00:00:00 2001
From: fhanik
Date: Wed, 14 Mar 2007 02:48:39 +0000
Subject: [PATCH] Implemented the cache properly with its own attribute,
removed one processor that is not needed
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@517977 13f79535-47bb-0310-9956-ffa450edef68
---
java/org/apache/tomcat/util/net/NioEndpoint.java | 91 ++++++++++++----------
.../apache/tomcat/util/net/SocketProperties.java | 25 +++++-
webapps/docs/config/http.xml | 6 ++
3 files changed, 76 insertions(+), 46 deletions(-)
diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java
index 990057c46..0a32a030e 100644
--- a/java/org/apache/tomcat/util/net/NioEndpoint.java
+++ b/java/org/apache/tomcat/util/net/NioEndpoint.java
@@ -155,6 +155,40 @@ public class NioEndpoint {
protected ServerSocketChannel serverSock = null;
/**
+ * Cache for SocketProcessor objects
+ */
+ protected ConcurrentLinkedQueue processorCache = new ConcurrentLinkedQueue() {
+ protected AtomicInteger size = new AtomicInteger(0);
+ public boolean offer(SocketProcessor sc) {
+ sc.reset(null,null);
+ boolean offer = socketProperties.getProcessorCache()==-1?true:size.get() keyCache = new ConcurrentLinkedQueue() {
@@ -727,6 +761,7 @@ public class NioEndpoint {
eventCache.clear();
keyCache.clear();
nioChannels.clear();
+ processorCache.clear();
if ( executor!=null ) {
ThreadPoolExecutor tpe = (ThreadPoolExecutor)executor;
tpe.shutdown();
@@ -977,19 +1012,7 @@ public class NioEndpoint {
* 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 @@ public class NioEndpoint {
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 @@ public class NioEndpoint {
}
- // ---------------------------------------------- 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 @@ public class NioEndpoint {
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 @@ public class NioEndpoint {
} catch ( Exception x ) {
log.error("",x);
}
- socket = null;
- status = null;
}
-
+ socket = null;
+ status = null;
+ //return to cache
+ processorCache.offer(this);
}
}
diff --git a/java/org/apache/tomcat/util/net/SocketProperties.java b/java/org/apache/tomcat/util/net/SocketProperties.java
index 71c9c2c6b..6551834d0 100644
--- a/java/org/apache/tomcat/util/net/SocketProperties.java
+++ b/java/org/apache/tomcat/util/net/SocketProperties.java
@@ -26,18 +26,29 @@ import java.net.SocketException;
*/
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 @@ public class SocketProperties {
return appWriteBufSize;
}
+ public int getProcessorCache() {
+ return processorCache;
+ }
+
public int getDirectBufferPool() {
return bufferPool;
}
@@ -327,6 +342,10 @@ public class SocketProperties {
this.appWriteBufSize = appWriteBufSize;
}
+ public void setProcessorCache(int processorCache) {
+ this.processorCache = processorCache;
+ }
+
public void setDirectBufferPool(int directBufferPool) {
this.bufferPool = directBufferPool;
}
diff --git a/webapps/docs/config/http.xml b/webapps/docs/config/http.xml
index ee9f2172c..e801291fc 100644
--- a/webapps/docs/config/http.xml
+++ b/webapps/docs/config/http.xml
@@ -457,6 +457,12 @@
The value is in bytes, the default value is 1024*1024*100 (100MB)
+
+ 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.
+
Tomcat will cache KeyAttachment objects to reduce garbage collection.
The integer value specifies how many objects to keep in the cache at most.
--
2.11.0