Bug 40525 - Native library causes 100% cpu use when idle
Summary: Native library causes 100% cpu use when idle
Status: CLOSED FIXED
Alias: None
Product: Tomcat Native
Classification: Unclassified
Component: Library (show other bugs)
Version: unspecified
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
URL:
Keywords: PatchAvailable
: 40606 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-09-16 14:13 UTC by Tom Donovan
Modified: 2008-10-05 02:47 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Donovan 2006-09-16 14:13:18 UTC
The jni native method poll() in poll.c does not check if the socket has no
timeout (i.e. it does not check if max_ttl is negative) before adjusting the
timeout passed to apr_pollset_poll().  

The timeout passed to apr_pollset_poll() is in the variable 'ptime'.  The
current code sets ptime to zero when there is a valid timeout (usually 2000ms)
and no socket timeout (usually -1000).

These values are common because AprEndpoint.java defaults to pollTime=2000 ms
and soTimeout=-1 sec (which gets adjusted to -1000 ms).
    
When a zero timeout is passed to apr_pollset_poll(), it is passed along to the 
native select() as a zero timeval which causes select() to return immediately
instead of blocking. A negative value should be passed to apr_pollset_poll() for
an infinite timeout, but I don't think that's what we want here.

The observed result is 100% cpu use when Tomcat is idle because calls to poll()
are made continuously. These calls return immediately.

The solution is to recognize when max_ttl is negative, and if so; do not use it 
to adjust the 'ptime' variable passed as an argument to poll().  Here is a patch 
for http://svn.apache.org/repos/asf/tomcat/connectors/trunk/.

Index: jni/native/src/poll.c
===================================================================
--- jni/native/src/poll.c	(revision 446772)
+++ jni/native/src/poll.c	(working copy)
@@ -263,7 +263,7 @@
         /* Find the minimum timeout */
         for (i = 0; i < p->nelts; i++) {
             apr_interval_time_t t = now - p->socket_ttl[i];
-            if (t >= p->max_ttl) {
+            if (p->max_ttl > 0 && t >= p->max_ttl) {
                 ptime = 0;
                 break;
             }
Comment 1 Tom Donovan 2006-09-18 23:56:46 UTC
(In reply to comment #0)
The patch in my original comment is incorrect.  It does not preserve the timeout
when there is no socket timeout. Apologies!
Here is a corrected patch:


Index: jni/native/src/poll.c
===================================================================
--- jni/native/src/poll.c	(revision 446772)
+++ jni/native/src/poll.c	(working copy)
@@ -263,13 +263,16 @@
         /* Find the minimum timeout */
         for (i = 0; i < p->nelts; i++) {
             apr_interval_time_t t = now - p->socket_ttl[i];
-            if (t >= p->max_ttl) {
-                ptime = 0;
-                break;
-            }
-            else {
-                ptime = TCN_MIN(p->max_ttl - t, ptime);
-            }
+			if (p->max_ttl > 0)
+			{
+				if (t >= p->max_ttl) {
+					ptime = 0;
+					break;
+				}
+				else {
+					ptime = TCN_MIN(p->max_ttl - t, ptime);
+				}
+			}
         }
     }
     for (;;) {
Comment 2 Remy Maucherat 2006-09-27 20:31:55 UTC
*** Bug 40606 has been marked as a duplicate of this bug. ***
Comment 3 Mladen Turk 2006-09-28 07:38:41 UTC
Fixed in the SVN trunk, although the patch aplied is slightly different.
Comment 4 Rainer Jung 2008-01-01 16:48:33 UTC
Removing Native:JK component, because this issue belongs to tcnative, which
seems to not have a component value.

Also moving from resolved to closed.