diff options
author | mav <mav@FreeBSD.org> | 2012-08-09 19:26:13 +0000 |
---|---|---|
committer | mav <mav@FreeBSD.org> | 2012-08-09 19:26:13 +0000 |
commit | 1f0ad947a6c993e63b43f3e0425966cb88699f30 (patch) | |
tree | 0309debb19e0fbda2144215613bcee44d7097bc7 /sys/kern/sched_4bsd.c | |
parent | 278be30294f435b7b5e07d641af9c4c6b7dc3b95 (diff) | |
download | FreeBSD-src-1f0ad947a6c993e63b43f3e0425966cb88699f30.zip FreeBSD-src-1f0ad947a6c993e63b43f3e0425966cb88699f30.tar.gz |
Rework r220198 change (by fabient). I believe it solves the problem from
the wrong direction. Before it, if preemption and end of time slice happen
same time, thread was put to the head of the queue as for only preemption.
It could cause single thread to run for indefinitely long time. r220198
handles it by not clearing TDF_NEEDRESCHED in case of preemption. But that
causes delayed context switch every time preemption happens, even when not
needed.
Solve problem by introducing scheduler-specifoc thread flag TDF_SLICEEND,
set when thread's time slice is over and it should be put to the tail of
queue. Using SW_PREEMPT flag for that purpose as it was before just not
enough informative to work correctly.
On my tests this by 2-3 times reduces run time deviation (improves fairness)
in cases when several threads share one CPU.
Reviewed by: fabient
MFC after: 2 months
Sponsored by: iXsystems, Inc.
Diffstat (limited to 'sys/kern/sched_4bsd.c')
-rw-r--r-- | sys/kern/sched_4bsd.c | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/sys/kern/sched_4bsd.c b/sys/kern/sched_4bsd.c index 91d00b2..472d124 100644 --- a/sys/kern/sched_4bsd.c +++ b/sys/kern/sched_4bsd.c @@ -105,6 +105,7 @@ struct td_sched { /* flags kept in td_flags */ #define TDF_DIDRUN TDF_SCHED0 /* thread actually ran. */ #define TDF_BOUND TDF_SCHED1 /* Bound to one CPU. */ +#define TDF_SLICEEND TDF_SCHED2 /* Thread time slice is over. */ /* flags kept in ts_flags */ #define TSF_AFFINITY 0x0001 /* Has a non-"full" CPU set. */ @@ -727,7 +728,7 @@ sched_clock(struct thread *td) */ if (!TD_IS_IDLETHREAD(td) && (--ts->ts_slice <= 0)) { ts->ts_slice = sched_slice; - td->td_flags |= TDF_NEEDRESCHED; + td->td_flags |= TDF_NEEDRESCHED | TDF_SLICEEND; } stat = DPCPU_PTR(idlestat); @@ -943,6 +944,7 @@ sched_switch(struct thread *td, struct thread *newtd, int flags) struct mtx *tmtx; struct td_sched *ts; struct proc *p; + int preempted; tmtx = NULL; ts = td->td_sched; @@ -964,8 +966,8 @@ sched_switch(struct thread *td, struct thread *newtd, int flags) sched_load_rem(); td->td_lastcpu = td->td_oncpu; - if (!(flags & SW_PREEMPT)) - td->td_flags &= ~TDF_NEEDRESCHED; + preempted = !(td->td_flags & TDF_SLICEEND); + td->td_flags &= ~(TDF_NEEDRESCHED | TDF_SLICEEND); td->td_owepreempt = 0; td->td_oncpu = NOCPU; @@ -983,7 +985,7 @@ sched_switch(struct thread *td, struct thread *newtd, int flags) } else { if (TD_IS_RUNNING(td)) { /* Put us back on the run queue. */ - sched_add(td, (flags & SW_PREEMPT) ? + sched_add(td, preempted ? SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED : SRQ_OURSELF|SRQ_YIELDING); } |