From antoine@massena-4-82-67-196-50.fbx.proxad.net  Wed Oct 13 23:02:33 2004
Return-Path: <antoine@massena-4-82-67-196-50.fbx.proxad.net>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 5E7B716A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 13 Oct 2004 23:02:33 +0000 (GMT)
Received: from massena-4-82-67-196-50.fbx.proxad.net (massena-4-82-67-196-50.fbx.proxad.net [82.67.196.50])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 8133743D2D
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 13 Oct 2004 23:02:32 +0000 (GMT)
	(envelope-from antoine@massena-4-82-67-196-50.fbx.proxad.net)
Received: from massena-4-82-67-196-50.fbx.proxad.net (localhost.fbx.proxad.net [127.0.0.1])
	by massena-4-82-67-196-50.fbx.proxad.net (8.13.1/8.13.1) with ESMTP id i9DN2VbU001134
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 14 Oct 2004 01:02:31 +0200 (CEST)
	(envelope-from antoine@massena-4-82-67-196-50.fbx.proxad.net)
Received: (from antoine@localhost)
	by massena-4-82-67-196-50.fbx.proxad.net (8.13.1/8.13.1/Submit) id i9DN2P3S001133;
	Thu, 14 Oct 2004 01:02:25 +0200 (CEST)
	(envelope-from antoine)
Message-Id: <200410132302.i9DN2P3S001133@massena-4-82-67-196-50.fbx.proxad.net>
Date: Thu, 14 Oct 2004 01:02:25 +0200 (CEST)
From: Antoine Brodin <antoine.brodin@laposte.net>
Reply-To: Antoine Brodin <antoine.brodin@laposte.net>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [patch] little bug in sched_ule interractivty scorer
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         72659
>Category:       kern
>Synopsis:       [sched_ule] [patch] little bug in sched_ule interactivty scorer
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    jeff
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 13 23:10:28 GMT 2004
>Closed-Date:    Tue Mar 11 02:58:04 UTC 2008
>Last-Modified:  Tue Mar 11 02:58:04 UTC 2008
>Originator:     Antoine Brodin
>Release:        FreeBSD 6.0-CURRENT i386
>Organization:
none
>Environment:
System: FreeBSD massena-4-82-67-196-50.fbx.proxad.net 6.0-CURRENT FreeBSD 6.0-CURRENT #0: Wed Oct 13 00:03:44 CEST 2004 antoine@massena-4-82-67-196-50.fbx.proxad.net:/usr/obj/usr/src/sys/BARTON i386
>Description:
There's a little bug in the interactivty scorer in sched_ule :
when the sleeptime is equal to the runtime, it returns 0 instead of
either SCHED_INTERACT_HALF or 0.
The routine also uses max() with signed ints, they should be replaced
by imax().
>How-To-Repeat:
You can print sleeptime, runtime and interactivity in a file and then
use gnuplot to show it in 3D.
The problem when sleeptime is equal to runtime is obvious.
Note : I can't reproduce the sleeptime is equal to runtime case easily,
durring boot I have a thread that has sleeptime=runtime=30720
but the applications I use don't seem to be "half interractive".
I can reproduce it by running during 2 or 3 minutes this ugly program
on my box (and adding a printf in the scorer code) :

#include <stdio.h>
#include <unistd.h>
int
main(void)
{
        int i, j;
        while (1) {
                for (i = 0; i < 100000000 - j; i++)
                        ;
                usleep(250000 + j);
                j += 100;
                printf("%d\n", j);
        }
}

>Fix:
Apply the attached patch

--- sched_ule.patch begins here ---
Index: sched_ule.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/sched_ule.c,v
retrieving revision 1.134
diff -u -r1.134 sched_ule.c
--- sched_ule.c	5 Oct 2004 22:14:02 -0000	1.134
+++ sched_ule.c	13 Oct 2004 20:09:53 -0000
@@ -1143,19 +1143,17 @@
 	int div;
 
 	if (kg->kg_runtime > kg->kg_slptime) {
-		div = max(1, kg->kg_runtime / SCHED_INTERACT_HALF);
+		div = imax(1, kg->kg_runtime / SCHED_INTERACT_HALF);
 		return (SCHED_INTERACT_HALF +
 		    (SCHED_INTERACT_HALF - (kg->kg_slptime / div)));
-	} if (kg->kg_slptime > kg->kg_runtime) {
-		div = max(1, kg->kg_slptime / SCHED_INTERACT_HALF);
+	}
+	if (kg->kg_slptime > kg->kg_runtime) {
+		div = imax(1, kg->kg_slptime / SCHED_INTERACT_HALF);
 		return (kg->kg_runtime / div);
 	}
-
-	/*
-	 * This can happen if slptime and runtime are 0.
-	 */
+	if (kg->kg_runtime > 0)
+		return (SCHED_INTERACT_HALF);
 	return (0);
-
 }
 
 /*
--- sched_ule.patch ends here ---


>Release-Note:
>Audit-Trail:

From: Antoine Brodin <antoine.brodin@laposte.net>
To: freebsd-gnats-submit@FreeBSD.org, antoine.brodin@laposte.net
Cc:  
Subject: Re: kern/72659: [patch] little bug in sched_ule interractivty
 scorer
Date: Sun, 17 Oct 2004 00:28:12 +0200

 This is a multi-part message in MIME format.
 
 --Multipart=_Sun__17_Oct_2004_00_28_12_+0200_slFX=AB9uvnDw5CT
 Content-Type: text/plain; charset=US-ASCII
 Content-Transfer-Encoding: 7bit
 
 I've identified two more bugs :
 
 1) The computation of hzticks in sched_wakeup() is broken :
 On a system with kern.hz="1000", if we leave a shell 50 minutes alone,
 when we come back, hzticks = (ticks - td->td_slptime) << 10 is negative
 so we lose our kg_slptime and this kg_slptime won't be positive before a
 long time. The shell will stay uninteractive during this time.
 A fix that solves the problem if we don't sleep INT_MAX-INT_MIN+1 ticks
 or more is attached. If we sleep more, at least we don't get a negative
 hzticks.
 
 2) Threads that voluntarily sleep at ticks = 0 (typically kernel
 threads that sleep during boot) are not handled the same way as others
 when they're woken up because a non null td_slptime is used to
 distinguish voluntarily aslept threads.
 A dirty fix is attached.
 
 Note : the difference of signification of kg_slptime in
 sched_4bsd.c/kern_proc.c/vm_glue.c and sched_ule.c is quite confusing
 and the use of kg_slptime in fill_kinfo_thread() in kern_proc.c and in
 vm_glue.c when we use SCHED_ULE seems bogus...
 
 --Multipart=_Sun__17_Oct_2004_00_28_12_+0200_slFX=AB9uvnDw5CT
 Content-Type: text/plain;
  name="sched_ule.txt"
 Content-Disposition: attachment;
  filename="sched_ule.txt"
 Content-Transfer-Encoding: 7bit
 
 Index: sched_ule.c
 ===================================================================
 RCS file: /home/ncvs/src/sys/kern/sched_ule.c,v
 retrieving revision 1.134
 diff -u -r1.134 sched_ule.c
 --- sched_ule.c	5 Oct 2004 22:14:02 -0000	1.134
 +++ sched_ule.c	16 Oct 2004 20:05:28 -0000
 @@ -1143,19 +1143,17 @@
  	int div;
  
  	if (kg->kg_runtime > kg->kg_slptime) {
 -		div = max(1, kg->kg_runtime / SCHED_INTERACT_HALF);
 +		div = imax(1, kg->kg_runtime / SCHED_INTERACT_HALF);
  		return (SCHED_INTERACT_HALF +
  		    (SCHED_INTERACT_HALF - (kg->kg_slptime / div)));
 -	} if (kg->kg_slptime > kg->kg_runtime) {
 -		div = max(1, kg->kg_slptime / SCHED_INTERACT_HALF);
 +	}
 +	if (kg->kg_slptime > kg->kg_runtime) {
 +		div = imax(1, kg->kg_slptime / SCHED_INTERACT_HALF);
  		return (kg->kg_runtime / div);
  	}
 -
 -	/*
 -	 * This can happen if slptime and runtime are 0.
 -	 */
 +	if (kg->kg_runtime > 0)
 +		return (SCHED_INTERACT_HALF);
  	return (0);
 -
  }
  
  /*
 @@ -1351,7 +1349,7 @@
  {
  	mtx_assert(&sched_lock, MA_OWNED);
  
 -	td->td_slptime = ticks;
 +	td->td_slptime = ticks != 0 ? ticks : ticks - 1;
  	td->td_base_pri = td->td_priority;
  
  	CTR2(KTR_ULE, "sleep thread %p (tick: %d)",
 @@ -1372,12 +1370,12 @@
  		int hzticks;
  
  		kg = td->td_ksegrp;
 -		hzticks = (ticks - td->td_slptime) << 10;
 -		if (hzticks >= SCHED_SLP_RUN_MAX) {
 +		hzticks = ticks - td->td_slptime;
 +		if (hzticks < 0 || hzticks >= SCHED_SLP_RUN_MAX >> 10) {
  			kg->kg_slptime = SCHED_SLP_RUN_MAX;
  			kg->kg_runtime = 1;
  		} else {
 -			kg->kg_slptime += hzticks;
 +			kg->kg_slptime += hzticks << 10;
  			sched_interact_update(kg);
  		}
  		sched_priority(kg);
 
 --Multipart=_Sun__17_Oct_2004_00_28_12_+0200_slFX=AB9uvnDw5CT--
Responsible-Changed-From-To: freebsd-bugs->jeff 
Responsible-Changed-By: arved 
Responsible-Changed-When: Fri Nov 19 11:57:53 GMT 2004 
Responsible-Changed-Why:  
Jeff wrote sched_ule 

http://www.freebsd.org/cgi/query-pr.cgi?pr=72659 
State-Changed-From-To: open->feedback 
State-Changed-By: linimon 
State-Changed-When: Wed Mar 14 22:23:05 UTC 2007 
State-Changed-Why:  
The ULE scheduler has been extensively upgraded in -CURRENT.  Do these 
patches still apply? 

http://www.freebsd.org/cgi/query-pr.cgi?pr=72659 

From: Antoine Brodin <antoine.brodin@laposte.net>
To: bug-followup@freebsd.org, linimon@freebsd.org
Cc: jeff@freebsd.org
Subject: Re: kern/72659: [sched_ule] [patch] little bug in sched_ule
 interactivty scorer
Date: Thu, 15 Mar 2007 15:52:55 +0100

 Mark Linimon <linimon@FreeBSD.org> wrote:
 > Synopsis: [sched_ule] [patch] little bug in sched_ule interactivty scorer
 > 
 > State-Changed-From-To: open->feedback
 > State-Changed-By: linimon
 > State-Changed-When: Wed Mar 14 22:23:05 UTC 2007
 > State-Changed-Why: 
 > The ULE scheduler has been extensively upgraded in -CURRENT.  Do these
 > patches still apply?
 > 
 > http://www.freebsd.org/cgi/query-pr.cgi?pr=72659
 
 These patches must be adapted but I think the problems still exist:
 - in sched_interact_score, if skg_runtime == skg_slptime, 0 is returned
 instead of SCHED_INTERACT_HALF
 - in sched_interact_score, max is used for signed ints and not imax so
 score could be negative (not sure if it can happen)
 - ts->ts_slptime = 0 is a bit special because it can happen either when
 sched_wakeup is called, or when sched_sleep is called and ticks = 0
State-Changed-From-To: feedback->open 
State-Changed-By: linimon 
State-Changed-When: Thu Mar 15 17:10:39 UTC 2007 
State-Changed-Why:  
Submitter notes that the problem still exists, although the patches need 
to be updated. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=72659 

From: Jeff Roberson <jroberson@chesapeake.net>
To: Antoine Brodin <antoine.brodin@laposte.net>
Cc: bug-followup@freebsd.org, linimon@freebsd.org, jeff@freebsd.org
Subject: Re: kern/72659: [sched_ule] [patch] little bug in sched_ule interactivty
 scorer
Date: Sat, 17 Mar 2007 12:26:35 -0800 (PST)

 On Thu, 15 Mar 2007, Antoine Brodin wrote:
 
 > Mark Linimon <linimon@FreeBSD.org> wrote:
 >> Synopsis: [sched_ule] [patch] little bug in sched_ule interactivty scorer
 >>
 >> State-Changed-From-To: open->feedback
 >> State-Changed-By: linimon
 >> State-Changed-When: Wed Mar 14 22:23:05 UTC 2007
 >> State-Changed-Why:
 >> The ULE scheduler has been extensively upgraded in -CURRENT.  Do these
 >> patches still apply?
 >>
 >> http://www.freebsd.org/cgi/query-pr.cgi?pr=72659
 >
 > These patches must be adapted but I think the problems still exist:
 > - in sched_interact_score, if skg_runtime == skg_slptime, 0 is returned
 > instead of SCHED_INTERACT_HALF
 > - in sched_interact_score, max is used for signed ints and not imax so
 > score could be negative (not sure if it can happen)
 > - ts->ts_slptime = 0 is a bit special because it can happen either when
 > sched_wakeup is called, or when sched_sleep is called and ticks = 0
 >
 
 slptime/runtime are now unsigned so imax is not needed.  You are right 
 about the case of slptime == runtime.  I have the following patch:
 
 @@ -1220,10 +1220,14 @@
                  div = max(1, td->td_sched->skg_runtime / 
 SCHED_INTERACT_HALF);
                  return (SCHED_INTERACT_HALF +
                      (SCHED_INTERACT_HALF - (td->td_sched->skg_slptime / 
 div)));
 -       } if (td->td_sched->skg_slptime > td->td_sched->skg_runtime) {
 +       }
 +       if (td->td_sched->skg_slptime > td->td_sched->skg_runtime) {
                  div = max(1, td->td_sched->skg_slptime / 
 SCHED_INTERACT_HALF);
                  return (td->td_sched->skg_runtime / div);
          }
 +       /* runtime == slptime */
 +       if (td->td_sched->skg_runtime)
 +               return (SCHED_INTERACT_HALF);
 
          /*
           * This can happen if slptime and runtime are 0.
 @@ -1855,8 +1859,7 @@
 
 
 How does this look?

From: Antoine Brodin <antoine.brodin@laposte.net>
To: Jeff Roberson <jroberson@chesapeake.net>
Cc: bug-followup@freebsd.org, jeff@freebsd.org
Subject: Re: kern/72659: [sched_ule] [patch] little bug in sched_ule
 interactivty scorer
Date: Sat, 17 Mar 2007 21:42:24 +0100

 Jeff Roberson <jroberson@chesapeake.net> wrote:
 > slptime/runtime are now unsigned so imax is not needed.  You are right 
 > about the case of slptime == runtime.  I have the following patch:
 > 
 > @@ -1220,10 +1220,14 @@
 >                  div = max(1, td->td_sched->skg_runtime / 
 > SCHED_INTERACT_HALF);
 >                  return (SCHED_INTERACT_HALF +
 >                      (SCHED_INTERACT_HALF - (td->td_sched->skg_slptime / 
 > div)));
 > -       } if (td->td_sched->skg_slptime > td->td_sched->skg_runtime) {
 > +       }
 > +       if (td->td_sched->skg_slptime > td->td_sched->skg_runtime) {
 >                  div = max(1, td->td_sched->skg_slptime / 
 > SCHED_INTERACT_HALF);
 >                  return (td->td_sched->skg_runtime / div);
 >          }
 > +       /* runtime == slptime */
 > +       if (td->td_sched->skg_runtime)
 > +               return (SCHED_INTERACT_HALF);
 > 
 >          /*
 >           * This can happen if slptime and runtime are 0.
 > @@ -1855,8 +1859,7 @@
 > 
 > 
 > How does this look?
 
 This looks good.
 
 Cheers,
 
 Antoine
State-Changed-From-To: open->closed 
State-Changed-By: vwe 
State-Changed-When: Tue Mar 11 02:57:32 UTC 2008 
State-Changed-Why:  

already committed to cvs, rev. 1.191 

http://www.freebsd.org/cgi/query-pr.cgi?pr=72659 
>Unformatted:
