From bfc50bbd32a402ae5d5679757d420478166bcff1 Mon Sep 17 00:00:00 2001 From: fhanik Date: Wed, 25 Oct 2006 23:20:35 +0000 Subject: [PATCH] Zero GC objects. everything gets cached. git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@467808 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/tomcat/util/net/NioEndpoint.java | 117 ++++++++++++++--------- 1 file changed, 70 insertions(+), 47 deletions(-) diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java index ddc24de8c..dd6e04bb7 100644 --- a/java/org/apache/tomcat/util/net/NioEndpoint.java +++ b/java/org/apache/tomcat/util/net/NioEndpoint.java @@ -97,7 +97,7 @@ public class NioEndpoint { */ public static final String SESSION_ID_KEY = "javax.servlet.request.ssl_session"; - + public static final int OP_REGISTER = -1; //register interest op // ----------------------------------------------------------------- Fields @@ -149,7 +149,19 @@ public class NioEndpoint { */ protected ServerSocketChannel serverSock = null; + /** + * Cache for key attachment objects + */ + protected ConcurrentLinkedQueue keyCache = new ConcurrentLinkedQueue(); + + /** + * Cache for poller events + */ + protected ConcurrentLinkedQueue eventCache = new ConcurrentLinkedQueue(); + /** + * Bytebuffer cache, each channel holds a set of buffers (two, except for SSL holds four) + */ protected ConcurrentLinkedQueue nioChannels = new ConcurrentLinkedQueue() { protected AtomicInteger size = new AtomicInteger(0); protected AtomicInteger bytes = new AtomicInteger(0); @@ -158,7 +170,7 @@ public class NioEndpoint { Selector sel = pol!=null?pol.getSelector():null; SelectionKey key = sel!=null?socket.getIOChannel().keyFor(sel):null; KeyAttachment att = key!=null?(KeyAttachment)key.attachment():null; - if ( att!=null ) att.reset(); + if ( att!=null ) { att.reset(); keyCache.offer(att); } 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 @@ -1051,6 +1075,7 @@ public class NioEndpoint { // exit, otherwise parallel descturction of sockets which are still // in the poller can cause problems close = true; + events.clear(); selector.wakeup(); } @@ -1072,8 +1097,9 @@ public class NioEndpoint { } public void add(final NioChannel socket, final int interestOps) { - PollerEvent r = this.eventCache.poll(); - if ( r==null) r = new PollerEvent(socket,interestOps); + PollerEvent r = eventCache.poll(); + if ( r==null) r = new PollerEvent(socket,null,interestOps); + else r.reset(socket,null,interestOps); addEvent(r); } @@ -1101,18 +1127,12 @@ public class NioEndpoint { public void register(final NioChannel socket) { socket.setPoller(this); - final KeyAttachment ka = new KeyAttachment(this); - ka.setChannel(socket); - Runnable r = new Runnable() { - public void run() { - try { - socket.getIOChannel().register(selector, SelectionKey.OP_READ, ka); - } catch (Exception x) { - log.error("", x); - } - } - - }; + KeyAttachment key = keyCache.poll(); + final KeyAttachment ka = key!=null?key:new KeyAttachment(); + ka.reset(this,socket); + PollerEvent r = eventCache.poll(); + if ( r==null) r = new PollerEvent(socket,ka,OP_REGISTER); + else r.reset(socket,ka,OP_REGISTER); addEvent(r); } @@ -1157,6 +1177,7 @@ public class NioEndpoint { try { wakeupCounter.set(0); keyCount = selector.select(selectorTimeout); + if ( close ) { selector.close(); return; } } catch ( NullPointerException x ) { //sun bug 5076772 on windows JDK 1.5 if ( wakeupCounter == null || selector == null ) throw x; @@ -1169,12 +1190,9 @@ public class NioEndpoint { log.error("",x); continue; } - //either we timed out or we woke up, process events first if ( keyCount == 0 ) hasEvents = (hasEvents | events()); - //if (keyCount == 0) continue; - Iterator iterator = keyCount > 0 ? selector.selectedKeys().iterator() : null; // Walk through the collection of ready keys and dispatch // any active event. @@ -1256,18 +1274,23 @@ public class NioEndpoint { public static class KeyAttachment { - public KeyAttachment(Poller poller) { - this.poller = poller; + public KeyAttachment() { + } - public void reset() { - //mutex = new Object(); + public void reset(Poller poller, NioChannel channel) { + this.channel = channel; + this.poller = poller; lastAccess = System.currentTimeMillis(); currentAccess = false; comet = false; timeout = -1; error = false; - channel = null; } + + public void reset() { + reset(null,null); + } + public Poller getPoller() { return poller;} public void setPoller(Poller poller){this.poller = poller;} public long getLastAccess() { return lastAccess; } @@ -1289,7 +1312,7 @@ public class NioEndpoint { public int interestOps() { return interestOps;} public int interestOps(int ops) { this.interestOps = ops; return ops; } protected Object mutex = new Object(); - protected long lastAccess = System.currentTimeMillis(); + protected long lastAccess = -1; protected boolean currentAccess = false; protected boolean comet = false; protected long timeout = -1; -- 2.11.0