Author: mturk
Date: Wed Nov 28 07:19:46 2012
New Revision: 1414560
URL: http://svn.apache.org/viewvc?rev=1414560&view=rev
Log:
Limit socket to a single instance in the pollset. This allows to optimize
remove loop - actually remove it.
Modified:
tomcat/native/branches/1.1.x/native/include/tcn.h
tomcat/native/branches/1.1.x/native/src/poll.c
Modified: tomcat/native/branches/1.1.x/native/include/tcn.h
URL:
http://svn.apache.org/viewvc/tomcat/native/branches/1.1.x/native/include/tcn.h?rev=1414560&r1=1414559&r2=1414560&view=diff
==============================================================================
--- tomcat/native/branches/1.1.x/native/include/tcn.h (original)
+++ tomcat/native/branches/1.1.x/native/include/tcn.h Wed Nov 28 07:19:46 2012
@@ -162,6 +162,7 @@ struct tcn_socket_t {
char *jsbbuff;
char *jrbbuff;
tcn_nlayer_t *net;
+ tcn_pfde_t *pe;
apr_time_t last_active;
apr_interval_time_t timeout;
};
Modified: tomcat/native/branches/1.1.x/native/src/poll.c
URL:
http://svn.apache.org/viewvc/tomcat/native/branches/1.1.x/native/src/poll.c?rev=1414560&r1=1414559&r2=1414560&view=diff
==============================================================================
--- tomcat/native/branches/1.1.x/native/src/poll.c (original)
+++ tomcat/native/branches/1.1.x/native/src/poll.c Wed Nov 28 07:19:46 2012
@@ -179,6 +179,14 @@ static apr_status_t do_add(tcn_pollset_t
#endif
return APR_ENOMEM;
}
+ if (s->pe != NULL) {
+ /* Socket is already added to the pollset.
+ */
+#ifdef TCN_DO_STATISTICS
+ sp_equals++;
+#endif
+ return APR_EEXIST;
+ }
if (timeout == TCN_NO_SOCKET_TIMEOUT) {
timeout = p->default_timeout;
}
@@ -209,6 +217,7 @@ static apr_status_t do_add(tcn_pollset_t
}
else {
APR_RING_INSERT_TAIL(&p->poll_ring, elem, tcn_pfde_t, link);
+ s->pe = elem;
}
return rv;
}
@@ -238,45 +247,21 @@ TCN_IMPLEMENT_CALL(jint, Poll, addWithTi
return (jint) do_add(p, s, (apr_int16_t)reqevents, J2T(socket_timeout));
}
-static apr_status_t do_remove(tcn_pollset_t *p, const apr_pollfd_t *fd)
-{
- apr_status_t rv;
- tcn_pfde_t *ep;
-
- rv = apr_pollset_remove(p->pollset, fd);
- APR_RING_FOREACH(ep, &p->poll_ring, tcn_pfde_t, link)
- {
- if (fd->desc.s == ep->fd.desc.s) {
- APR_RING_REMOVE(ep, link);
- APR_RING_INSERT_TAIL(&p->dead_ring, ep, tcn_pfde_t, link);
- p->nelts--;
-#ifdef TCN_DO_STATISTICS
- p->sp_removed++;
-#endif
- break;
- }
- }
- return rv;
-}
-
-static void update_last_active(tcn_pollset_t *p, const apr_pollfd_t *fd,
apr_time_t t)
-{
- tcn_socket_t *s = (tcn_socket_t *)fd->client_data;
- TCN_ASSERT(s != 0);
- s->last_active = t;
-}
-
-
TCN_IMPLEMENT_CALL(jint, Poll, remove)(TCN_STDARGS, jlong pollset,
jlong socket)
{
apr_pollfd_t fd;
+ apr_status_t rv;
tcn_pollset_t *p = J2P(pollset, tcn_pollset_t *);
- tcn_socket_t *s = J2P(socket, tcn_socket_t *);
+ tcn_socket_t *s = J2P(socket, tcn_socket_t *);
UNREFERENCED_STDARGS;
TCN_ASSERT(socket != 0);
+ if (s->pe == NULL) {
+ /* Already removed */
+ return APR_SUCCESS;
+ }
fd.desc_type = APR_POLL_SOCKET;
fd.desc.s = s->sock;
fd.client_data = s;
@@ -285,7 +270,15 @@ TCN_IMPLEMENT_CALL(jint, Poll, remove)(T
p->sp_remove++;
#endif
- return (jint)do_remove(p, &fd);
+ rv = apr_pollset_remove(p->pollset, &fd);
+ APR_RING_REMOVE(s->pe, link);
+ APR_RING_INSERT_TAIL(&p->dead_ring, s->pe, tcn_pfde_t, link);
+ s->pe = NULL;
+ p->nelts--;
+#ifdef TCN_DO_STATISTICS
+ p->sp_removed++;
+#endif
+ return rv;
}
@@ -314,8 +307,7 @@ TCN_IMPLEMENT_CALL(jint, Poll, poll)(TCN
APR_RING_FOREACH(ep, &p->poll_ring, tcn_pfde_t, link)
{
apr_interval_time_t socket_timeout = 0;
- tcn_socket_t *s;
- s = (tcn_socket_t *)ep->fd.client_data;
+ tcn_socket_t *s = (tcn_socket_t *)ep->fd.client_data;
if (s->timeout == TCN_NO_SOCKET_TIMEOUT) {
socket_timeout = p->default_timeout;
}
@@ -366,12 +358,25 @@ TCN_IMPLEMENT_CALL(jint, Poll, poll)(TCN
if (!remove)
now = apr_time_now();
for (i = 0; i < num; i++) {
+ tcn_socket_t *s = (tcn_socket_t *)fd->client_data;
p->set[i*2+0] = (jlong)(fd->rtnevents);
- p->set[i*2+1] = P2J(fd->client_data);
- if (remove)
- do_remove(p, fd);
- else
- update_last_active(p, fd, now);
+ p->set[i*2+1] = P2J(s);
+ if (remove) {
+ apr_pollset_remove(p->pollset, fd);
+ APR_RING_REMOVE(s->pe, link);
+ APR_RING_INSERT_TAIL(&p->dead_ring, s->pe, tcn_pfde_t, link);
+ s->pe = NULL;
+ p->nelts--;
+#ifdef TCN_DO_STATISTICS
+ p->sp_removed++;
+#endif
+ }
+ else {
+ /* Update last active with the current time
+ * after the poll call.
+ */
+ s->last_active = now;
+ }
fd ++;
}
(*e)->SetLongArrayRegion(e, set, 0, num * 2, p->set);
@@ -407,31 +412,35 @@ TCN_IMPLEMENT_CALL(jint, Poll, maintain)
}
if ((now - s->last_active) >= timeout) {
p->set[num++] = P2J(s);
- APR_RING_REMOVE(ep, link);
- APR_RING_INSERT_TAIL(&p->dead_ring, ep, tcn_pfde_t, link);
- p->nelts--;
+ if (remove) {
+ APR_RING_REMOVE(ep, link);
+ APR_RING_INSERT_TAIL(&p->dead_ring, ep, tcn_pfde_t, link);
+ s->pe = NULL;
+ p->nelts--;
#ifdef TCN_DO_STATISTICS
- p->sp_removed++;
+ p->sp_removed++;
#endif
+ }
}
}
- if (remove && num) {
+ if (num) {
#ifdef TCN_DO_STATISTICS
- p->sp_maintained += num;
- p->sp_max_maintained = TCN_MAX(p->sp_max_maintained, num);
+ p->sp_maintained += num;
+ p->sp_max_maintained = TCN_MAX(p->sp_max_maintained, num);
#endif
- for (i = 0; i < num; i++) {
- apr_pollfd_t fd;
- tcn_socket_t *s = J2P(p->set[i], tcn_socket_t *);
- fd.desc_type = APR_POLL_SOCKET;
- fd.desc.s = s->sock;
- fd.client_data = s;
- fd.reqevents = APR_POLLIN | APR_POLLOUT;
- apr_pollset_remove(p->pollset, &fd);
+ if (remove) {
+ for (i = 0; i < num; i++) {
+ apr_pollfd_t fd;
+ tcn_socket_t *s = J2P(p->set[i], tcn_socket_t *);
+ fd.desc_type = APR_POLL_SOCKET;
+ fd.desc.s = s->sock;
+ fd.client_data = s;
+ fd.reqevents = APR_POLLIN | APR_POLLOUT;
+ apr_pollset_remove(p->pollset, &fd);
+ }
}
- }
- if (num)
(*e)->SetLongArrayRegion(e, set, 0, num, p->set);
+ }
return (jint)num;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]