Donjuanplatinum, le jeu. 26 nov. 2026 22:40:44 +0800, a ecrit: > I have updated, added a helper for mapping priorities and also extended > checkrq().
Applied, thanks! Samuel > From 5ef777682744fbbf4a4d399d5593d06d2bea4e60 Mon Sep 17 00:00:00 2001 > From: Donjuanplatinum <[email protected]> > Date: Thu, 26 Nov 2026 22:06:48 +0800 > Subject: [PATCH] sched: use a bitmap to speed up run queue lookup > > * kern/eventcount.c (simpler_thread_setrun): Set bit when enquence. > * kern/processor.c (pset_init, processor_init): Add bitmap init. > * kern/sched.h (NRQS_BITMAP_SIZE, run_queue, runq_setbit, runq_clrbit) > (runq_findbit): > * kern/sched_prim.c (thread_select, rem_runq, choose_thread) > (choose_pset_thread, do_runq_scan): Use bitmap-based run queue lookup. > --- > kern/eventcount.c | 1 + > kern/processor.c | 10 +++- > kern/sched.h | 51 ++++++++++++++++ > kern/sched_prim.c | 150 ++++++++++++++++++++++++++++------------------ > 4 files changed, 153 insertions(+), 59 deletions(-) > > diff --git a/kern/eventcount.c b/kern/eventcount.c > index 70b9f0d5..32cd99cd 100644 > --- a/kern/eventcount.c > +++ b/kern/eventcount.c > @@ -342,6 +342,7 @@ simpler_thread_setrun( > runq_lock(rq); /* lock the run queue */ > enqueue_head(&(rq)->runq[whichq], &((th)->links)); > > + runq_setbit((rq)->bitmap, whichq); > if (whichq < (rq)->low || (rq)->count == 0) > (rq)->low = whichq; /* minimize */ > (rq)->count++; > diff --git a/kern/processor.c b/kern/processor.c > index 485eb51b..8bc60c0e 100644 > --- a/kern/processor.c > +++ b/kern/processor.c > @@ -138,8 +138,11 @@ void pset_init( > int i; > > simple_lock_init(&pset->runq.lock); > - pset->runq.low = 0; > + pset->runq.low = NRQS; > pset->runq.count = 0; > + for (i = 0; i < NRQS_BITMAP_SIZE; i++) { > + pset->runq.bitmap[i] = 0; > + } > for (i = 0; i < NRQS; i++) { > queue_init(&(pset->runq.runq[i])); > } > @@ -190,8 +193,11 @@ void processor_init( > int i; > > simple_lock_init(&pr->runq.lock); > - pr->runq.low = 0; > + pr->runq.low = NRQS; > pr->runq.count = 0; > + for (i = 0; i < NRQS_BITMAP_SIZE; i++) { > + pr->runq.bitmap[i] = 0; > + } > for (i = 0; i < NRQS; i++) { > queue_init(&(pr->runq.runq[i])); > } > diff --git a/kern/sched.h b/kern/sched.h > index 1f33f018..e5b74752 100644 > --- a/kern/sched.h > +++ b/kern/sched.h > @@ -62,11 +62,17 @@ > #endif /* STAT_TIME */ > #define NRQS 65 /* 65 run queues per cpu */ > > +/* > + * Bitmap size for priority tracking. > + */ > +#define NRQS_BITMAP_SIZE ((NRQS + (sizeof(unsigned long) * 8) - 1) / > (sizeof(unsigned long) * 8)) > + > struct run_queue { > queue_head_t runq[NRQS]; /* one for each priority */ > decl_simple_lock_data(, lock); /* one lock for all queues, > shall be taken at splsched > only */ > + unsigned long bitmap[NRQS_BITMAP_SIZE]; /* priority bitmap */ > int low; /* low queue value */ > int count; /* count of threads runable */ > }; > @@ -158,6 +164,51 @@ struct shift { > > typedef struct shift *shift_t, shift_data_t; > > +/* > + * Bitmap operations for run queue priority tracking. > + */ > + > +static inline void runq_setbit(unsigned long *bitmap, int pri) { > + bitmap[pri / (sizeof(unsigned long) * 8)] |= > + (1UL << (pri % (sizeof(unsigned long) * 8))); > +} > + > +static inline void runq_clrbit(unsigned long *bitmap, int pri) { > + bitmap[pri / (sizeof(unsigned long) * 8)] &= > + ~(1UL << (pri % (sizeof(unsigned long) * 8))); > +} > + > +static inline void runq_findbit(const unsigned long *bitmap, int *low) { > + int i; > + unsigned long word; > + const int bits_per_long = sizeof(unsigned long) * 8; > + > + if (*low >= NRQS) { > + *low = NRQS; > + return; > + } > + > + > + for (i = *low / bits_per_long; i < NRQS_BITMAP_SIZE; i++) { > + word = bitmap[i]; > + > + if (word != 0) { > + int bit; > +#if defined(__GNUC__) > + bit = __builtin_ctzl(word); > +#else > + for (bit = 0; bit < bits_per_long; bit++) { > + if (word & (1UL << bit)) > + break; > + } > +#endif > + *low = i * bits_per_long + bit; > + return; > + } > + } > + *low = NRQS; > +} > + > /* > * sched_tick increments once a second. Used to age priorities. > */ > diff --git a/kern/sched_prim.c b/kern/sched_prim.c > index 8cf2ab27..a9be57de 100644 > --- a/kern/sched_prim.c > +++ b/kern/sched_prim.c > @@ -578,20 +578,22 @@ static thread_t thread_select( > else { > thread = (thread_t) dequeue_head(q); > thread->runq = RUN_QUEUE_NULL; > + if (queue_empty(q)) > + runq_clrbit(pset->runq.bitmap, > pset->runq.low); > pset->runq.count--; > + > + if (pset->runq.count == 0) > + pset->runq.low = NRQS; > #if MACH_FIXPRI > /* > * Cannot lazy evaluate pset->runq.low for > * fixed priority policy > */ > - if ((pset->runq.count > 0) && > - (pset->policies & POLICY_FIXEDPRI)) { > - while (queue_empty(q)) { > - pset->runq.low++; > - q++; > - } > + else if (queue_empty(q) && (pset->policies & > POLICY_FIXEDPRI)) { > + pset->runq.low++; > + runq_findbit(pset->runq.bitmap, > &pset->runq.low); > } > -#endif /* MACH_FIXPRI */ > +#endif /* MACH_FIXPRI */ > #if DEBUG > checkrq(&pset->runq, "thread_select: after"); > #endif /* DEBUG */ > @@ -1181,6 +1183,16 @@ void update_priority( > } > } > > +/* > + * Map sched_pri to a valid run queue index. > + */ > +static inline unsigned int runq_index (int pri) { > + if (invalid_pri(pri)) { > + printf("thread_setrun: pri too high (%d)\n", pri); > + return NRQS - 1; > + } > + return pri; > +} > /* > * run_queue_enqueue macro for thread_setrun(). > */ > @@ -1189,15 +1201,11 @@ void update_priority( > MACRO_BEGIN \ > unsigned int whichq; \ > \ > - whichq = (th)->sched_pri; \ > - if (whichq >= NRQS) { \ > - printf("thread_setrun: pri too high (%d)\n", (th)->sched_pri); \ > - whichq = NRQS - 1; \ > - } \ > - \ > + whichq = runq_index((th)->sched_pri); \ > runq_lock(rq); /* lock the run queue */ \ > checkrq((rq), "thread_setrun: before adding thread"); \ > enqueue_tail(&(rq)->runq[whichq], &((th)->links)); \ > + runq_setbit((rq)->bitmap, whichq); \ > \ > if (whichq < (rq)->low || (rq)->count == 0) \ > (rq)->low = whichq; /* minimize */ \ > @@ -1213,14 +1221,10 @@ void update_priority( > MACRO_BEGIN \ > unsigned int whichq; \ > \ > - whichq = (th)->sched_pri; \ > - if (whichq >= NRQS) { \ > - printf("thread_setrun: pri too high (%d)\n", (th)->sched_pri); \ > - whichq = NRQS - 1; \ > - } \ > - \ > + whichq = runq_index((th)->sched_pri); \ > runq_lock(rq); /* lock the run queue */ \ > enqueue_tail(&(rq)->runq[whichq], &((th)->links)); \ > + runq_setbit((rq)->bitmap, whichq); \ > \ > if (whichq < (rq)->low || (rq)->count == 0) \ > (rq)->low = whichq; /* minimize */ \ > @@ -1441,6 +1445,7 @@ struct run_queue *rem_runq( > thread_t th) > { > struct run_queue *rq; > + int whichq; > > rq = th->runq; > /* > @@ -1463,7 +1468,13 @@ struct run_queue *rem_runq( > thread_check(th, rq); > #endif /* DEBUG */ > remqueue(&rq->runq[0], (queue_entry_t) th); > + whichq = runq_index(th->sched_pri); > + if (queue_empty(&rq->runq[whichq])) > + runq_clrbit(rq->bitmap, whichq); > rq->count--; > + > + if (rq->count == 0) > + rq->low = NRQS; > #if DEBUG > checkrq(rq, "rem_runq: after removing thread"); > #endif /* DEBUG */ > @@ -1515,19 +1526,29 @@ thread_t choose_thread( > > simple_lock(&runq->lock); > if (runq->count > 0) { > - q = runq->runq + runq->low; > - for (i = runq->low; i < NRQS ; i++, q++) { > - if (!queue_empty(q)) { > - th = (thread_t) dequeue_head(q); > - th->runq = RUN_QUEUE_NULL; > - runq->count--; > - runq->low = i; > - simple_unlock(&runq->lock); > - return th; > - } > - } > - panic("choose_thread"); > - /*NOTREACHED*/ > + runq_findbit(runq->bitmap, &runq->low); > + i = runq->low; > + if (i >= NRQS) { > + panic("choose_thread: bitmap/count mismatch"); > + } > + q = runq->runq + i; > + if (queue_empty(q)) { > + panic("choose_thread: bitmap points to empty queue"); > + } > + th = (thread_t) dequeue_head(q); > + th->runq = RUN_QUEUE_NULL; > + > + if (queue_empty(q)) { > + runq_clrbit(runq->bitmap, i); > + if (runq->count > 1) > + runq->low = i + 1; > + else > + runq->low = NRQS; > + } > + runq->count--; > + > + simple_unlock(&runq->lock); > + return th; > } > simple_unlock(&runq->lock); > > @@ -1561,35 +1582,40 @@ thread_t choose_pset_thread( > runq = &pset->runq; > > if (runq->count > 0) { > - q = runq->runq + runq->low; > - for (i = runq->low; i < NRQS ; i++, q++) { > - if (!queue_empty(q)) { > - th = (thread_t) dequeue_head(q); > - th->runq = RUN_QUEUE_NULL; > - runq->count--; > - /* > - * For POLICY_FIXEDPRI, runq->low must be > - * accurate! > - */ > + runq_findbit(runq->bitmap, &runq->low); > + i = runq->low; > + if (i >= NRQS) > + panic("choose_pset_thread: bitmap/count mismatch"); > + q = runq->runq + i; > + if (queue_empty(q)) > + panic("choose_pset_thread: bitmap points to empty queue"); > + th = (thread_t) dequeue_head(q); > + th->runq = RUN_QUEUE_NULL; > + > + if (queue_empty(q)) { > + runq_clrbit(runq->bitmap, i); > + if (runq->count > 1) > + runq->low = i + 1; > + else > + runq->low = NRQS; > + } > + runq->count--; > + > + /* > + * For POLICY_FIXEDPRI, runq->low must be > + * accurate! > + */ > #if MACH_FIXPRI > - if ((runq->count > 0) && > - (pset->policies & POLICY_FIXEDPRI)) { > - while (queue_empty(q)) { > - q++; > - i++; > - } > - } > + if (queue_empty(q) && > + (pset->policies & POLICY_FIXEDPRI)) { > + runq_findbit(runq->bitmap, &runq->low); > + } > #endif /* MACH_FIXPRI */ > - runq->low = i; > #if DEBUG > - checkrq(runq, "choose_pset_thread"); > + checkrq(runq, "choose_pset_thread"); > #endif /* DEBUG */ > - simple_unlock(&runq->lock); > - return th; > - } > - } > - panic("choose_pset_thread"); > - /*NOTREACHED*/ > + simple_unlock(&runq->lock); > + return th; > } > simple_unlock(&runq->lock); > > @@ -1937,9 +1963,14 @@ do_runq_scan( > * from the runq to make it safe. > */ > remqueue(q, (queue_entry_t) thread); > + > + if (queue_empty(q)) > + runq_clrbit(runq->bitmap, q - runq->runq); > runq->count--; > thread->runq = RUN_QUEUE_NULL; > > + if (runq->count == 0) > + runq->low = NRQS; > stuck_threads[stuck_count++] = thread; > if (do_thread_scan_debug) > printf("do_runq_scan: adding thread %p\n", thread); > @@ -2017,11 +2048,16 @@ void checkrq( > int i, j; > queue_entry_t e; > int low; > + unsigned const int bits_per_long = sizeof(unsigned long) * 8; > + unsigned long mask; > > low = -1; > j = 0; > q1 = rq->runq; > for (i = 0; i < NRQS; i++) { > + mask = 1UL << (i % bits_per_long); > + if (((rq->bitmap[i / bits_per_long] & mask) != 0) == (queue_empty > (q1))) > + panic("checkrq: bitmap mismatch for queue %d at %s", i, msg); > if (q1->next == q1) { > if (q1->prev != q1) > panic("checkrq: empty at %s", msg); > -- > 2.56.0 > > > -- > Donjuanplatinum, Yifei Yao <[email protected]> > GPG: 1C9E EEE5 4C8E D5A8 3039 1C87 A9F6 8632 D259 40E6 > 满堂兮美人 忽独与余兮目成 -- Samuel > ±z¬O§_¸g±`¬°¦p¦ó©Ý®i¦æ¾PºÞ¹D¶Ë¸£µ¬? > ¥Zµn³ø¯È,Âø»x¼s§i©Î¶l±H¢Ò¢Û (Direct Mail)¬O§_¤w¸gµLªk¹F¦¨±z¹w´Áªº¼s§i®ÄªG? Je vous invite a consulter dans un premier temps le french-Howto. -+- JCD in Guide du linuxien pervers - "Bien configurer la lisibilité de fcolm"
