# HG changeset patch
# User ws
# Date 844279496 0
# Node ID a07dfe38114a6cc5c3caf6bc0aa7608c892c2f04
# Parent  d5f1ab122d250d2363a619dc59c6c7543c032f20
Fix p_nice vs. NZERO code.
Change NZERO to 20 to always make p_nice positive.
On Christos' suggestion make p_nice explicitly u_char.

diff --git a/sys/compat/hpux/hpux_compat.c b/sys/compat/hpux/hpux_compat.c
--- a/sys/compat/hpux/hpux_compat.c
+++ b/sys/compat/hpux/hpux_compat.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: hpux_compat.c,v 1.24 1996/02/01 00:18:04 jtc Exp $	*/
+/*	$NetBSD: hpux_compat.c,v 1.25 1996/06/23 11:10:01 mycroft Exp $	*/
 
 /*
  * Copyright (c) 1988 University of Utah.
@@ -548,8 +548,8 @@
 		p = cp;
 	else if ((p = pfind(SCARG(uap, pid))) == 0)
 		return (ESRCH);
-	nice = p->p_nice;
-	if (nice < NZERO)
+	nice = p->p_nice - NZERO;
+	if (nice < 0)
 		*retval = (nice + 16) << 3;
 	else
 		*retval = RTPRIO_RTOFF;
@@ -559,9 +559,9 @@
 		return (0);
 
 	case RTPRIO_RTOFF:
-		if (nice >= NZERO)
+		if (nice >= 0)
 			return (0);
-		nice = NZERO;
+		nice = 0;
 		break;
 
 	default:
@@ -1321,7 +1321,7 @@
 	} */ *uap = v;
 	int error;
 
-	error = donice(p, p, (p->p_nice-NZERO)+SCARG(uap, nval));
+	error = donice(p, p, (p->p_nice - NZERO) + SCARG(uap, nval));
 	if (error == 0)
 		*retval = p->p_nice - NZERO;
 	return (error);
diff --git a/sys/compat/ibcs2/ibcs2_misc.c b/sys/compat/ibcs2/ibcs2_misc.c
--- a/sys/compat/ibcs2/ibcs2_misc.c
+++ b/sys/compat/ibcs2/ibcs2_misc.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: ibcs2_misc.c,v 1.19 1996/09/03 03:12:24 mycroft Exp $	*/
+/*	$NetBSD: ibcs2_misc.c,v 1.20 1996/09/07 12:40:43 mycroft Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Scott Bartram
@@ -1027,10 +1027,10 @@
 
 	SCARG(&sa, which) = PRIO_PROCESS;
 	SCARG(&sa, who) = 0;
-	SCARG(&sa, prio) = p->p_nice + SCARG(uap, incr);
+	SCARG(&sa, prio) = p->p_nice - NZERO + SCARG(uap, incr);
 	if ((error = sys_setpriority(p, &sa, retval)) != 0)
 		return EPERM;
-	*retval = p->p_nice;
+	*retval = p->p_nice - NZERO;
 	return 0;
 }
 
diff --git a/sys/conf/param.c b/sys/conf/param.c
--- a/sys/conf/param.c
+++ b/sys/conf/param.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: param.c,v 1.16 1996/03/12 03:08:40 mrg Exp $	*/
+/*	$NetBSD: param.c,v 1.17 1996/07/17 21:52:18 explorer Exp $	*/
 
 /*
  * Copyright (c) 1980, 1986, 1989 Regents of the University of California.
@@ -174,16 +174,17 @@
 /*
  * These control when and to what priority a process gets after a certain
  * amount of CPU time expires.  AUTONICETIME is in seconds.
+ * AUTONICEVAL is NOT offset by NZERO, i.e. it's between PRIO_MIN and PRIO_MAX.
  */
 #ifdef AUTONICETIME
 int autonicetime = AUTONICETIME;
 #else
-int autonicetime = (60 * 10);  /* 10 minutes */
+int autonicetime = (60 * 10);	/* 10 minutes */
 #endif
 
 #ifdef AUTONICEVAL
 int autoniceval = AUTONICEVAL;
 #else
-int autoniceval = NZERO + 4;   /* default + 4 (usually 0 + 4) */
+int autoniceval = 4;		/* default + 4 */
 #endif
 
diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c
--- a/sys/kern/kern_resource.c
+++ b/sys/kern/kern_resource.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_resource.c,v 1.35 1996/06/13 23:22:22 jtc Exp $	*/
+/*	$NetBSD: kern_resource.c,v 1.36 1996/07/11 00:09:29 jtc Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1991, 1993
@@ -69,7 +69,7 @@
 		syscallarg(int) who;
 	} */ *uap = v;
 	register struct proc *p;
-	register int low = PRIO_MAX + 1;
+	register int low = NZERO + PRIO_MAX + 1;
 
 	switch (SCARG(uap, which)) {
 
@@ -109,9 +109,9 @@
 	default:
 		return (EINVAL);
 	}
-	if (low == PRIO_MAX + 1)
+	if (low == NZERO + PRIO_MAX + 1)
 		return (ESRCH);
-	*retval = low;
+	*retval = low - NZERO;
 	return (0);
 }
 
@@ -191,6 +191,7 @@
 		n = PRIO_MAX;
 	if (n < PRIO_MIN)
 		n = PRIO_MIN;
+	n += NZERO;
 	if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
 		return (EACCES);
 	chgp->p_nice = n;
diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c
--- a/sys/kern/kern_synch.c
+++ b/sys/kern/kern_synch.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $	*/
+/*	$NetBSD: kern_synch.c,v 1.38 1996/07/17 21:54:06 explorer Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1990, 1991, 1993
@@ -204,7 +204,8 @@
 			(p->p_cpticks * FSCALE / hz)) >> FSHIFT;
 #endif
 		p->p_cpticks = 0;
-		newcpu = (u_int) decay_cpu(loadfac, p->p_estcpu) + p->p_nice;
+		newcpu = (u_int)decay_cpu(loadfac, p->p_estcpu)
+		    + p->p_nice - NZERO;
 		p->p_estcpu = min(newcpu, UCHAR_MAX);
 		resetpriority(p);
 		if (p->p_priority >= PUSER) {
@@ -599,7 +600,7 @@
 		}
 	}
 	if (autonicetime && s > autonicetime && p->p_ucred->cr_uid && p->p_nice == NZERO) {
-		p->p_nice = autoniceval;
+		p->p_nice = autoniceval + NZERO;
 		resetpriority(p);
 	}
 
@@ -680,7 +681,7 @@
 {
 	register unsigned int newpriority;
 
-	newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice;
+	newpriority = PUSER + p->p_estcpu / 4 + 2 * (p->p_nice - NZERO);
 	newpriority = min(newpriority, MAXPRI);
 	p->p_usrpri = newpriority;
 	if (newpriority < curpriority)
diff --git a/sys/sys/param.h b/sys/sys/param.h
--- a/sys/sys/param.h
+++ b/sys/sys/param.h
@@ -1,4 +1,4 @@
-/*	$NetBSD: param.h,v 1.23 1996/03/17 01:02:29 thorpej Exp $	*/
+/*	$NetBSD: param.h,v 1.24 1996/05/25 22:07:35 jtc Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -111,7 +111,7 @@
 #define	PRIMASK	0x0ff
 #define	PCATCH	0x100		/* OR'd with pri for tsleep to check signals */
 
-#define	NZERO	0		/* default "nice" */
+#define	NZERO	20		/* default "nice" */
 
 #define	NBPW	sizeof(int)	/* number of bytes per word (integer) */
 
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -1,4 +1,4 @@
-/*	$NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $	*/
+/*	$NetBSD: proc.h,v 1.45 1996/09/07 12:41:33 mycroft Exp $	*/
 
 /*-
  * Copyright (c) 1986, 1989, 1991, 1993
@@ -179,7 +179,7 @@
 
 	u_char	p_priority;	/* Process priority. */
 	u_char	p_usrpri;	/* User-priority based on p_cpu and p_nice. */
-	char	p_nice;		/* Process "nice" value. */
+	u_char	p_nice;		/* Process "nice" value. */
 	char	p_comm[MAXCOMLEN+1];
 
 	struct 	pgrp *p_pgrp;	/* Pointer to process group. */
diff --git a/sys/vm/vm_glue.c b/sys/vm/vm_glue.c
--- a/sys/vm/vm_glue.c
+++ b/sys/vm/vm_glue.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: vm_glue.c,v 1.55 1996/05/19 10:00:38 ragge Exp $	*/
+/*	$NetBSD: vm_glue.c,v 1.56 1996/05/29 21:20:11 pk Exp $	*/
 
 /* 
  * Copyright (c) 1991, 1993
@@ -379,7 +379,8 @@
 	ppri = INT_MIN;
 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
 		if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) {
-			pri = p->p_swtime + p->p_slptime - p->p_nice * 8;
+			pri = p->p_swtime + p->p_slptime
+			    - (p->p_nice - NZERO) * 8;
 			if (pri > ppri) {
 				pp = p;
 				ppri = pri;
