diff options
author | 2019-07-08 18:53:18 +0000 | |
---|---|---|
committer | 2019-07-08 18:53:18 +0000 | |
commit | 55eab86cd4f592594f21d159b23c2a58acefe836 (patch) | |
tree | 34fb972eeddbd3301dcb62990a82450c0dba43c5 | |
parent | Get rid of an old convention of wrapping preprocessor constants in curly (diff) | |
download | wireguard-openbsd-55eab86cd4f592594f21d159b23c2a58acefe836.tar.xz wireguard-openbsd-55eab86cd4f592594f21d159b23c2a58acefe836.zip |
Untangle code setting the scheduling priority of a thread.
- `p_estcpu' and `p_usrpri' represent the priority and are now only set
in a single function.
- Call resched_proc() after updating the priority and stop calling it
from schedclock() since `spc_curpriority' should match curproc's priority.
- Rename updatepri() to match decay_cpu() and stop updating per-thread
member.
- Merge two resched_proc() in one inside setrunnable().
Tweak and ok visa@
-rw-r--r-- | sys/kern/kern_resource.c | 8 | ||||
-rw-r--r-- | sys/kern/sched_bsd.c | 68 | ||||
-rw-r--r-- | sys/sys/proc.h | 4 | ||||
-rw-r--r-- | sys/sys/sched.h | 3 |
4 files changed, 44 insertions, 39 deletions
diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c index 191abef1c35..760f7a73bb8 100644 --- a/sys/kern/kern_resource.c +++ b/sys/kern/kern_resource.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_resource.c,v 1.66 2019/06/24 12:49:03 visa Exp $ */ +/* $OpenBSD: kern_resource.c,v 1.67 2019/07/08 18:53:18 mpi Exp $ */ /* $NetBSD: kern_resource.c,v 1.38 1996/10/23 07:19:38 matthias Exp $ */ /*- @@ -212,8 +212,10 @@ donice(struct proc *curp, struct process *chgpr, int n) return (EACCES); chgpr->ps_nice = n; SCHED_LOCK(s); - TAILQ_FOREACH(p, &chgpr->ps_threads, p_thr_link) - (void)resetpriority(p); + TAILQ_FOREACH(p, &chgpr->ps_threads, p_thr_link) { + setpriority(p, p->p_estcpu, n); + resched_proc(p, p->p_usrpri); + } SCHED_UNLOCK(s); return (0); } diff --git a/sys/kern/sched_bsd.c b/sys/kern/sched_bsd.c index 63e0b59f697..d413f726320 100644 --- a/sys/kern/sched_bsd.c +++ b/sys/kern/sched_bsd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sched_bsd.c,v 1.53 2019/06/01 14:11:17 mpi Exp $ */ +/* $OpenBSD: sched_bsd.c,v 1.54 2019/07/08 18:53:18 mpi Exp $ */ /* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */ /*- @@ -62,7 +62,7 @@ struct __mp_lock sched_lock; #endif void schedcpu(void *); -void updatepri(struct proc *); +uint32_t decay_aftersleep(uint32_t, uint32_t); void scheduler_start(void) @@ -252,8 +252,9 @@ schedcpu(void *arg) #endif p->p_cpticks = 0; newcpu = (u_int) decay_cpu(loadfac, p->p_estcpu); - p->p_estcpu = newcpu; - resetpriority(p); + setpriority(p, newcpu, p->p_p->ps_nice); + resched_proc(p, p->p_usrpri); + if (p->p_priority >= PUSER) { if (p->p_stat == SRUN && (p->p_priority / SCHED_PPQ) != @@ -276,23 +277,23 @@ schedcpu(void *arg) * For all load averages >= 1 and max p_estcpu of 255, sleeping for at * least six times the loadfactor will decay p_estcpu to zero. */ -void -updatepri(struct proc *p) +uint32_t +decay_aftersleep(uint32_t estcpu, uint32_t slptime) { - unsigned int newcpu = p->p_estcpu; fixpt_t loadfac = loadfactor(averunnable.ldavg[0]); + uint32_t newcpu; - SCHED_ASSERT_LOCKED(); - - if (p->p_slptime > 5 * loadfac) - p->p_estcpu = 0; + if (slptime > 5 * loadfac) + newcpu = 0; else { - p->p_slptime--; /* the first time was done in schedcpu */ - while (newcpu && --p->p_slptime) - newcpu = (int) decay_cpu(loadfac, newcpu); - p->p_estcpu = newcpu; + newcpu = estcpu; + slptime--; /* the first time was done in schedcpu */ + while (newcpu && --slptime) + newcpu = decay_cpu(loadfac, newcpu); + } - resetpriority(p); + + return (newcpu); } /* @@ -441,7 +442,7 @@ mi_switch(void) #endif } -static __inline void +void resched_proc(struct proc *p, u_char pri) { struct cpu_info *ci; @@ -496,29 +497,29 @@ setrunnable(struct proc *p) p->p_stat = SRUN; p->p_cpu = sched_choosecpu(p); setrunqueue(p); - if (p->p_slptime > 1) - updatepri(p); + if (p->p_slptime > 1) { + uint32_t newcpu; + + newcpu = decay_aftersleep(p->p_estcpu, p->p_slptime); + setpriority(p, newcpu, p->p_p->ps_nice); + } p->p_slptime = 0; - resched_proc(p, p->p_priority); + resched_proc(p, MIN(p->p_priority, p->p_usrpri)); } /* - * Compute the priority of a process when running in user mode. - * Arrange to reschedule if the resulting priority is better - * than that of the current process. + * Compute the priority of a process. */ void -resetpriority(struct proc *p) +setpriority(struct proc *p, uint32_t newcpu, uint8_t nice) { - unsigned int newpriority; + unsigned int newprio; - SCHED_ASSERT_LOCKED(); + newprio = min((PUSER + newcpu + NICE_WEIGHT * (nice - NZERO)), MAXPRI); - newpriority = PUSER + p->p_estcpu + - NICE_WEIGHT * (p->p_p->ps_nice - NZERO); - newpriority = min(newpriority, MAXPRI); - p->p_usrpri = newpriority; - resched_proc(p, p->p_usrpri); + SCHED_ASSERT_LOCKED(); + p->p_estcpu = newcpu; + p->p_usrpri = newprio; } /* @@ -540,14 +541,15 @@ schedclock(struct proc *p) { struct cpu_info *ci = curcpu(); struct schedstate_percpu *spc = &ci->ci_schedstate; + uint32_t newcpu; int s; if (p == spc->spc_idleproc || spc->spc_spinning) return; SCHED_LOCK(s); - p->p_estcpu = ESTCPULIM(p->p_estcpu + 1); - resetpriority(p); + newcpu = ESTCPULIM(p->p_estcpu + 1); + setpriority(p, newcpu, p->p_p->ps_nice); if (p->p_priority >= PUSER) p->p_priority = p->p_usrpri; SCHED_UNLOCK(s); diff --git a/sys/sys/proc.h b/sys/sys/proc.h index 01391fb1690..6d1fa4d8a2a 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.h,v 1.271 2019/07/01 21:13:03 mpi Exp $ */ +/* $OpenBSD: proc.h,v 1.272 2019/07/08 18:53:18 mpi Exp $ */ /* $NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $ */ /*- @@ -561,7 +561,7 @@ void leavepgrp(struct process *); void killjobc(struct process *); void preempt(void); void procinit(void); -void resetpriority(struct proc *); +void setpriority(struct proc *, uint32_t, uint8_t); void setrunnable(struct proc *); void endtsleep(void *); void unsleep(struct proc *); diff --git a/sys/sys/sched.h b/sys/sys/sched.h index d1ef56b7a9e..198c7ca49a1 100644 --- a/sys/sys/sched.h +++ b/sys/sys/sched.h @@ -1,4 +1,4 @@ -/* $OpenBSD: sched.h,v 1.52 2019/05/16 13:52:47 visa Exp $ */ +/* $OpenBSD: sched.h,v 1.53 2019/07/08 18:53:18 mpi Exp $ */ /* $NetBSD: sched.h,v 1.2 1999/02/28 18:14:58 ross Exp $ */ /*- @@ -164,6 +164,7 @@ void cpu_idle_cycle(void); void cpu_idle_leave(void); void sched_peg_curproc(struct cpu_info *ci); void sched_barrier(struct cpu_info *ci); +void resched_proc(struct proc *, u_char); int sysctl_hwsetperf(void *, size_t *, void *, size_t); int sysctl_hwperfpolicy(void *, size_t *, void *, size_t); |