Re: test_cleanup hangs with N:1 scheduling

"Bill Abt" <[email protected]> Sun, 19 May 2002 10:26:34 -0400
Newsgroups gmane.linux.ngpt.user
Message-ID <[email protected]>
On 05/19/2002 at 02:16:12 PM ZE2, Marijn Ros <[email protected]>
wrote:

>
> With MAXNATIVETHREADS=1 (default on uni-processors), the main thread
> of test_cleanup never wakes from its sleep, as more then one other
> thread is doing endless calculations. The calculating-threads do yield
> a lot (through pthread_testcancel), but only to themselves.

We've been considering making the default for MAXNATIVETHREADS=4 for the
normal case, even on a UP machine.  The problem with doing this is the
simple application that uses maybe 1 or 2 threads and plays by the rules ;
-)   It ends up taking a performance hit which we find unacceptable.  So
for now, adjust MAXNATIVETHREADS as needed for your app.

>
> Although this behaviour is probably allowed by the POSIX
> specifications, nobody actually expects it to happen.

Neither does POSIX...

>
> The workaround I use is to allow the eventmanager to run on every
> schedule-point, instead of only when there are no or exactly one
> threads ready to run.

This is only necessary for the UP case (MAXNATIVETHREADS = 1).  The MP
cases shouldn't have this problem.

>
> The one-liner patch I made is attached for reference. But please
> notice, I don't understand the scheduler and so also don't know what
> the side-effects will be (except slower scheduling, of course).

This patch is a "bit heavy handed" for the MP case.  I've enclosed a CVS
patch (against 1.9.0) the handles the UP case properly without hindering
the MP case...

patch-1.9.0-1
-------------------------------- start cut here
----------------------------------------
Index: ChangeLog
===================================================================
RCS file: /opt/CVS/cvsroot/ngpt/ChangeLog,v
retrieving revision 1.85
diff -u -r1.85 ChangeLog
--- ChangeLog     14 May 2002 17:52:22 -0000    1.85
+++ ChangeLog     19 May 2002 14:14:01 -0000
@@ -15,6 +15,11 @@

   Changes between 1.2.2 and 1.9.0

+   *) pth_sched.c:
+     If max native threads is equal to 1, also wait in the scheduler.
This
+     will avoid thread starvation on UP machines.
+      [Bill Abt, <[email protected]>]
+
    *) ngptc.c:
      Ensure that file permissions are set properly.
       [Bill Abt, <[email protected]>]
cvs diff: Diffing .
Index: pth_sched.c
===================================================================
RCS file: /opt/CVS/cvsroot/ngpt/pth_sched.c,v
retrieving revision 1.27
diff -u -r1.27 pth_sched.c
--- pth_sched.c   29 Apr 2002 13:02:51 -0000    1.27
+++ pth_sched.c   19 May 2002 14:09:41 -0000
@@ -631,7 +631,7 @@
             && pth_pqueue_elements(NQ) == 0 ) {
          pth_sched_eventmanager(&snapshot, descr, FALSE /* wait */);
         } else {
-         if (rq_elements == 1)
+         if (rq_elements == 1 || pth_max_native_threads == 1)
            pth_sched_eventmanager(&snapshot, descr, TRUE  /* poll */);
      }
     }
--------------------------------- end cut here
-----------------------------------------

Thank Marijn and keep those reports coming.  We appreciate the feedback and
help.

Regards,
     Bill Abt
     Senior Software Engineer
     Next Generation POSIX Threading for Linux
     IBM Cambridge, MA, USA 02142
     Ext: +(00)1 603-673-3440 or +(00)1 617-693-1591
     T/L: 253-9938 (Normal)
     T/L: 693-1591 (Backup)
     Cell: +(00)1 617-803-7514
     [email protected] or [email protected]
     http://oss.software.ibm.com/developerworks/opensource/pthreads