Round robin scheduling improvements.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@3930 35acf78f-673a-0410-8e92-d51de3d6d3f4
master
gdisirio 2012-02-06 19:45:47 +00:00
parent 28c1f163a9
commit 4401d0e7b2
18 changed files with 152 additions and 139 deletions

View File

@ -5,8 +5,8 @@ Settings: SYSCLK=168, ACR=0x705 (5 wait states)
*** ChibiOS/RT test suite
***
*** Kernel: 2.4.0
*** Compiled: Jan 15 2012 - 20:10:20
*** Kernel: 2.5.0
*** Compiled: Feb 6 2012 - 20:30:43
*** Compiler: GCC 4.6.2
*** Architecture: ARMv7-ME
*** Core Variant: Cortex-M4
@ -100,11 +100,11 @@ Settings: SYSCLK=168, ACR=0x705 (5 wait states)
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.1 (Benchmark, messages #1)
--- Score : 749359 msgs/S, 1498718 ctxswc/S
--- Score : 752723 msgs/S, 1505446 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.2 (Benchmark, messages #2)
--- Score : 617119 msgs/S, 1234238 ctxswc/S
--- Score : 617124 msgs/S, 1234248 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.3 (Benchmark, messages #3)
@ -112,23 +112,23 @@ Settings: SYSCLK=168, ACR=0x705 (5 wait states)
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.4 (Benchmark, context switch)
--- Score : 2491424 ctxswc/S
--- Score : 2528968 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.5 (Benchmark, threads, full cycle)
--- Score : 451234 threads/S
--- Score : 448823 threads/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.6 (Benchmark, threads, create only)
--- Score : 635830 threads/S
--- Score : 635833 threads/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.7 (Benchmark, mass reschedule, 5 threads)
--- Score : 193114 reschedules/S, 1158684 ctxswc/S
--- Score : 193383 reschedules/S, 1160298 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.8 (Benchmark, round robin context switching)
--- Score : 1367460 ctxswc/S
--- Score : 1356420 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.9 (Benchmark, I/O Queues throughput)
@ -148,7 +148,7 @@ Settings: SYSCLK=168, ACR=0x705 (5 wait states)
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.13 (Benchmark, RAM footprint)
--- System: 376 bytes
--- System: 372 bytes
--- Thread: 72 bytes
--- Timer : 20 bytes
--- Semaph: 12 bytes

View File

@ -95,9 +95,6 @@ typedef struct {
Thread *r_older; /**< @brief Older registry element. */
#endif
/* End of the fields shared with the Thread structure.*/
#if (CH_TIME_QUANTUM > 0) || defined(__DOXYGEN__)
cnt_t r_preempt; /**< @brief Round robin counter. */
#endif
Thread *r_current; /**< @brief The currently running
thread. */
} ReadyList;
@ -154,6 +151,12 @@ extern "C" {
#if !defined(PORT_OPTIMIZED_ISPREEMPTIONREQUIRED)
bool_t chSchIsPreemptionRequired(void);
#endif
#if !defined(PORT_OPTIMIZED_DORESCHEDULEBEHIND) || defined(__DOXYGEN__)
void chSchDoRescheduleBehind(void);
#endif
#if !defined(PORT_OPTIMIZED_DORESCHEDULEAHEAD) || defined(__DOXYGEN__)
void chSchDoRescheduleAhead(void);
#endif
#if !defined(PORT_OPTIMIZED_DORESCHEDULE)
void chSchDoReschedule(void);
#endif
@ -197,7 +200,7 @@ extern "C" {
#if !defined(PORT_OPTIMIZED_DOYIELDS) || defined(__DOXYGEN__)
#define chSchDoYieldS() { \
if (chSchCanYieldS()) \
chSchDoReschedule(); \
chSchDoRescheduleBehind(); \
}
#endif /* !defined(PORT_OPTIMIZED_DOYIELDS) */
@ -212,19 +215,19 @@ extern "C" {
#define chSchPreemption() { \
tprio_t p1 = firstprio(&rlist.r_queue); \
tprio_t p2 = currp->p_prio; \
if (rlist.r_preempt) { \
if (currp->p_preempt) { \
if (p1 > p2) \
chSchDoReschedule(); \
chSchDoRescheduleAhead(); \
} \
else { \
if (p1 >= p2) \
chSchDoReschedule(); \
chSchDoRescheduleBehind(); \
} \
}
#else /* CH_TIME_QUANTUM == 0 */
#define chSchPreemption() { \
if (p1 >= p2) \
chSchDoReschedule(); \
chSchDoRescheduleAhead(); \
}
#endif /* CH_TIME_QUANTUM == 0 */
/** @} */

View File

@ -121,6 +121,12 @@ struct Thread {
* @brief References to this thread.
*/
trefs_t p_refs;
#endif
/**
* @brief Number of ticks remaining to this thread.
*/
#if (CH_TIME_QUANTUM > 0) || defined(__DOXYGEN__)
tslices_t p_preempt;
#endif
#if CH_DBG_THREADS_PROFILING || defined(__DOXYGEN__)
/**

View File

@ -49,9 +49,6 @@ void _scheduler_init(void) {
queue_init(&rlist.r_queue);
rlist.r_prio = NOPRIO;
#if CH_TIME_QUANTUM > 0
rlist.r_preempt = CH_TIME_QUANTUM;
#endif
#if CH_USE_REGISTRY
rlist.r_newer = rlist.r_older = (Thread *)&rlist;
#endif
@ -59,6 +56,8 @@ void _scheduler_init(void) {
/**
* @brief Inserts a thread in the Ready List.
* @details The thread is positioned behind all threads with higher or equal
* priority.
* @pre The thread must not be already inserted in any list through its
* @p p_next and @p p_prev or list corruption would occur.
* @post This function does not reschedule so a call to a rescheduling
@ -111,7 +110,9 @@ void chSchGoSleepS(tstate_t newstate) {
(otp = currp)->p_state = newstate;
#if CH_TIME_QUANTUM > 0
rlist.r_preempt = CH_TIME_QUANTUM;
/* The thread is renouncing its remaining time slices so it will have a new
time quantum when it will wakeup.*/
otp->p_preempt = CH_TIME_QUANTUM;
#endif
setcurrp(fifo_remove(&rlist.r_queue));
currp->p_state = THD_STATE_CURRENT;
@ -222,9 +223,6 @@ void chSchWakeupS(Thread *ntp, msg_t msg) {
chSchReadyI(ntp);
else {
Thread *otp = chSchReadyI(currp);
#if CH_TIME_QUANTUM > 0
rlist.r_preempt = CH_TIME_QUANTUM;
#endif
setcurrp(ntp);
ntp->p_state = THD_STATE_CURRENT;
chSysSwitch(ntp, otp);
@ -237,7 +235,7 @@ void chSchWakeupS(Thread *ntp, msg_t msg) {
* @details If a thread with a higher priority than the current thread is in
* the ready list then make the higher priority thread running.
*
* @iclass
* @sclass
*/
#if !defined(PORT_OPTIMIZED_RESCHEDULES) || defined(__DOXYGEN__)
void chSchRescheduleS(void) {
@ -245,7 +243,7 @@ void chSchRescheduleS(void) {
chDbgCheckClassS();
if (chSchIsRescRequiredI())
chSchDoReschedule();
chSchDoRescheduleAhead();
}
#endif /* !defined(PORT_OPTIMIZED_RESCHEDULES) */
@ -271,7 +269,7 @@ bool_t chSchIsPreemptionRequired(void) {
if the first thread on the ready queue has a higher priority.
Otherwise, if the running thread has used up its time quantum, reschedule
if the first thread on the ready queue has equal or higher priority.*/
return rlist.r_preempt ? p1 > p2 : p1 >= p2;
return currp->r_preempt ? p1 > p2 : p1 >= p2;
#else
/* If the round robin preemption feature is not enabled then performs a
simpler comparison.*/
@ -282,6 +280,65 @@ bool_t chSchIsPreemptionRequired(void) {
/**
* @brief Switches to the first thread on the runnable queue.
* @details The current thread is positioned in the ready list behind all
* threads having the same priority. The thread regains its time
* quantum.
* @note Not a user function, it is meant to be invoked by the scheduler
* itself or from within the port layer.
*
* @special
*/
#if !defined(PORT_OPTIMIZED_DORESCHEDULEBEHIND) || defined(__DOXYGEN__)
void chSchDoRescheduleBehind(void) {
Thread *otp;
otp = currp;
/* Picks the first thread from the ready queue and makes it current.*/
setcurrp(fifo_remove(&rlist.r_queue));
currp->p_state = THD_STATE_CURRENT;
otp->p_preempt = CH_TIME_QUANTUM;
chSchReadyI(otp);
chSysSwitch(currp, otp);
}
#endif /* !defined(PORT_OPTIMIZED_DORESCHEDULEBEHIND) */
/**
* @brief Switches to the first thread on the runnable queue.
* @details The current thread is positioned in the ready list ahead of all
* threads having the same priority.
* @note Not a user function, it is meant to be invoked by the scheduler
* itself or from within the port layer.
*
* @special
*/
#if !defined(PORT_OPTIMIZED_DORESCHEDULEAHEAD) || defined(__DOXYGEN__)
void chSchDoRescheduleAhead(void) {
Thread *otp, *cp;
otp = currp;
/* Picks the first thread from the ready queue and makes it current.*/
setcurrp(fifo_remove(&rlist.r_queue));
currp->p_state = THD_STATE_CURRENT;
otp->p_state = THD_STATE_READY;
cp = (Thread *)&rlist.r_queue;
do {
cp = cp->p_next;
} while (cp->p_prio > otp->p_prio);
/* Insertion on p_prev.*/
otp->p_next = cp;
otp->p_prev = cp->p_prev;
otp->p_prev->p_next = cp->p_prev = otp;
chSysSwitch(currp, otp);
}
#endif /* !defined(PORT_OPTIMIZED_DORESCHEDULEAHEAD) */
/**
* @brief Switches to the first thread on the runnable queue.
* @details The current thread is positioned in the ready list behind or
* ahead of all threads having the same priority depending on
* if it used its whole time slice.
* @note Not a user function, it is meant to be invoked by the scheduler
* itself or from within the port layer.
*
@ -289,17 +346,25 @@ bool_t chSchIsPreemptionRequired(void) {
*/
#if !defined(PORT_OPTIMIZED_DORESCHEDULE) || defined(__DOXYGEN__)
void chSchDoReschedule(void) {
Thread *otp;
#if CH_TIME_QUANTUM > 0
rlist.r_preempt = CH_TIME_QUANTUM;
#endif
otp = currp;
/* Picks the first thread from the ready queue and makes it current.*/
setcurrp(fifo_remove(&rlist.r_queue));
currp->p_state = THD_STATE_CURRENT;
chSchReadyI(otp);
chSysSwitch(currp, otp);
/* If CH_TIME_QUANTUM is enabled then there are two different scenarios to
handle on preemption: time quantum elapsed or not.*/
if (currp->p_preempt == 0) {
/* The thread consumed its time quantum so it is enqueued behind threads
with same priority level, however, it acquires a new time quantum.*/
chSchDoRescheduleBehind();
}
else {
/* The thread didn't consume all its time quantum so it is put ahead of
threads with equal priority and does not acquire a new time quantum.*/
chSchDoRescheduleAhead();
}
#else /* !(CH_TIME_QUANTUM > 0) */
/* If the round-robin mechanism is disabled then the thread goes always
ahead of its peers.*/
chSchDoRescheduleAhead();
#endif /* !(CH_TIME_QUANTUM > 0) */
}
#endif /* !defined(PORT_OPTIMIZED_DORESCHEDULE) */

View File

@ -129,9 +129,9 @@ void chSysTimerHandlerI(void) {
#if CH_TIME_QUANTUM > 0
/* Running thread has not used up quantum yet? */
if (rlist.r_preempt > 0)
if (currp->p_preempt > 0)
/* Decrement remaining quantum.*/
rlist.r_preempt--;
currp->p_preempt--;
#endif
#if CH_DBG_THREADS_PROFILING
currp->p_time++;

View File

@ -71,6 +71,9 @@ Thread *_thread_init(Thread *tp, tprio_t prio) {
tp->p_prio = prio;
tp->p_state = THD_STATE_SUSPENDED;
tp->p_flags = THD_MEM_MODE_STATIC;
#if CH_TIME_QUANTUM > 0
tp->p_preempt = CH_TIME_QUANTUM;
#endif
#if CH_USE_MUTEXES
tp->p_realprio = prio;
tp->p_mtxlist = NULL;

View File

@ -42,6 +42,7 @@ typedef int32_t bool_t; /**< Fast boolean type. */
typedef uint8_t tmode_t; /**< Thread flags. */
typedef uint8_t tstate_t; /**< Thread state. */
typedef uint8_t trefs_t; /**< Thread references counter. */
typedef uint8_t tslices_t; /**< Thread time slices counter. */
typedef uint32_t tprio_t; /**< Thread priority. */
typedef int32_t msg_t; /**< Inter-thread message. */
typedef int32_t eventid_t; /**< Event Id. */

View File

@ -168,8 +168,8 @@ struct intctx {};
* @brief Inlineable version of this kernel function.
*/
#define chSchIsPreemptionRequired() \
(rlist.r_preempt ? firstprio(&rlist.r_queue) > currp->p_prio : \
firstprio(&rlist.r_queue) >= currp->p_prio)
(currp->p_preempt ? firstprio(&rlist.r_queue) > currp->p_prio : \
firstprio(&rlist.r_queue) >= currp->p_prio)
#else /* CH_TIME_QUANTUM == 0 */
#define chSchIsPreemptionRequired() \
(firstprio(&rlist.r_queue) > currp->p_prio)

View File

@ -40,6 +40,7 @@ typedef int32_t bool_t; /**< Fast boolean type. */
typedef uint8_t tmode_t; /**< Thread flags. */
typedef uint8_t tstate_t; /**< Thread state. */
typedef uint8_t trefs_t; /**< Thread references counter. */
typedef uint8_t tslices_t; /**< Thread time slices counter. */
typedef uint32_t tprio_t; /**< Thread priority. */
typedef int32_t msg_t; /**< Inter-thread message. */
typedef int32_t eventid_t; /**< Event Id. */

View File

@ -42,6 +42,7 @@ typedef int8_t bool_t; /**< Fast boolean type. */
typedef uint8_t tmode_t; /**< Thread flags. */
typedef uint8_t tstate_t; /**< Thread state. */
typedef uint8_t trefs_t; /**< Thread references counter. */
typedef uint8_t tslices_t; /**< Thread time slices counter. */
typedef uint8_t tprio_t; /**< Thread priority. */
typedef int16_t msg_t; /**< Inter-thread message. */
typedef uint8_t eventid_t; /**< Event Id. */

View File

@ -42,6 +42,7 @@ typedef int16_t bool_t; /**< Fast boolean type. */
typedef uint8_t tmode_t; /**< Thread flags. */
typedef uint8_t tstate_t; /**< Thread state. */
typedef uint8_t trefs_t; /**< Thread references counter. */
typedef uint8_t tslices_t; /**< Thread time slices counter. */
typedef uint16_t tprio_t; /**< Thread priority. */
typedef int16_t msg_t; /**< Inter-thread message. */
typedef int16_t eventid_t; /**< Event Id. */

View File

@ -54,6 +54,7 @@ typedef int32_t bool_t; /**< Fast boolean type. */
typedef uint8_t tmode_t; /**< Thread flags. */
typedef uint8_t tstate_t; /**< Thread state. */
typedef uint8_t trefs_t; /**< Thread references counter. */
typedef uint8_t tslices_t; /**< Thread time slices counter. */
typedef uint32_t tprio_t; /**< Thread priority. */
typedef int32_t msg_t; /**< Inter-thread message. */
typedef int32_t eventid_t; /**< Event Id. */

View File

@ -34,6 +34,7 @@ typedef int32_t bool_t; /**< Fast boolean type. */
typedef uint8_t tmode_t; /**< Thread flags. */
typedef uint8_t tstate_t; /**< Thread state. */
typedef uint8_t trefs_t; /**< Thread references counter. */
typedef uint8_t tslices_t; /**< Thread time slices counter. */
typedef uint32_t tprio_t; /**< Thread priority. */
typedef int32_t msg_t; /**< Inter-thread message. */
typedef int32_t eventid_t; /**< Event Id. */

View File

@ -40,6 +40,7 @@ typedef int32_t bool_t; /**< Fast boolean type. */
typedef uint8_t tmode_t; /**< Thread flags. */
typedef uint8_t tstate_t; /**< Thread state. */
typedef uint8_t trefs_t; /**< Thread references counter. */
typedef uint8_t tslices_t; /**< Thread time slices counter. */
typedef uint32_t tprio_t; /**< Thread priority. */
typedef int32_t msg_t; /**< Inter-thread message. */
typedef int32_t eventid_t; /**< Event Id. */

View File

@ -47,55 +47,17 @@ typedef uint8_t uint_fast8_t; /**< C99-style 8 bits unsigned. */
typedef uint16_t uint_fast16_t; /**< C99-style 16 bits unsigned. */
typedef uint32_t uint_fast32_t; /**< C99-style 32 bits unsigned. */
/**
* @brief Boolean, recommended the fastest signed.
*/
typedef int8_t bool_t;
/**
* @brief Thread mode flags, uint8_t is ok.
*/
typedef uint8_t tmode_t;
/**
* @brief Thread state, uint8_t is ok.
*/
typedef uint8_t tstate_t;
/**
* @brief Thread references counter, uint8_t is ok.
*/
typedef uint8_t trefs_t;
/**
* @brief Priority, use the fastest unsigned type.
*/
typedef uint8_t tprio_t;
/**
* @brief Message, use signed pointer equivalent.
*/
typedef int16_t msg_t;
/**
* @brief Event Id, use fastest signed.
*/
typedef int8_t eventid_t;
/**
* @brief Event Mask, recommended fastest unsigned.
*/
typedef uint8_t eventmask_t;
/**
* @brief System Time, recommended fastest unsigned.
*/
typedef uint16_t systime_t;
/**
* @brief Counter, recommended fastest signed.
*/
typedef int8_t cnt_t;
typedef int8_t bool_t; /**< Fast boolean type. */
typedef uint8_t tmode_t; /**< Thread flags. */
typedef uint8_t tstate_t; /**< Thread state. */
typedef uint8_t trefs_t; /**< Thread references counter. */
typedef uint8_t tslices_t; /**< Thread time slices counter. */
typedef uint8_t tprio_t; /**< Thread priority. */
typedef int16_t msg_t; /**< Inter-thread message. */
typedef int8_t eventid_t; /**< Event Id. */
typedef uint8_t eventmask_t; /**< Events mask. */
typedef uint16_t systime_t; /**< System time. */
typedef int8_t cnt_t; /**< Resources counter. */
/**
* @brief Inline function modifier.

View File

@ -40,6 +40,7 @@ typedef int32_t bool_t; /**< Fast boolean type. */
typedef uint8_t tmode_t; /**< Thread flags. */
typedef uint8_t tstate_t; /**< Thread state. */
typedef uint8_t trefs_t; /**< Thread references counter. */
typedef uint8_t tslices_t; /**< Thread time slices counter. */
typedef uint32_t tprio_t; /**< Thread priority. */
typedef int32_t msg_t; /**< Inter-thread message. */
typedef int32_t eventid_t; /**< Event Id. */

View File

@ -47,55 +47,17 @@ typedef uint8_t uint_fast8_t; /**< C99-style 8 bits unsigned. */
typedef uint16_t uint_fast16_t; /**< C99-style 16 bits unsigned. */
typedef uint32_t uint_fast32_t; /**< C99-style 32 bits unsigned. */
/**
* @brief Boolean, recommended the fastest signed.
*/
typedef int8_t bool_t;
/**
* @brief Thread mode flags, uint8_t is ok.
*/
typedef uint8_t tmode_t;
/**
* @brief Thread state, uint8_t is ok.
*/
typedef uint8_t tstate_t;
/**
* @brief Thread references counter, uint8_t is ok.
*/
typedef uint8_t trefs_t;
/**
* @brief Priority, use the fastest unsigned type.
*/
typedef uint8_t tprio_t;
/**
* @brief Message, use signed pointer equivalent.
*/
typedef int16_t msg_t;
/**
* @brief Event Id, use fastest signed.
*/
typedef int8_t eventid_t;
/**
* @brief Event Mask, recommended fastest unsigned.
*/
typedef uint8_t eventmask_t;
/**
* @brief System Time, recommended fastest unsigned.
*/
typedef uint16_t systime_t;
/**
* @brief Counter, recommended fastest signed.
*/
typedef int8_t cnt_t;
typedef int8_t bool_t; /**< Fast boolean type. */
typedef uint8_t tmode_t; /**< Thread flags. */
typedef uint8_t tstate_t; /**< Thread state. */
typedef uint8_t trefs_t; /**< Thread references counter. */
typedef uint8_t tslices_t; /**< Thread time slices counter. */
typedef uint8_t tprio_t; /**< Thread priority. */
typedef int16_t msg_t; /**< Inter-thread message. */
typedef int8_t eventid_t; /**< Event Id. */
typedef uint8_t eventmask_t; /**< Events mask. */
typedef uint16_t systime_t; /**< System time. */
typedef int8_t cnt_t; /**< Resources counter. */
/**
* @brief Inline function modifier.

View File

@ -82,6 +82,10 @@
- NEW: Updated debug plugin 1.0.8 (backported to 2.4.0).
- NEW: Added more accurate UBRR calculation in AVR serial driver (backported
to 2.4.0).
- NEW: Revision of the round-robin scheduling, now threads do not lose their
time slice when preempted. Each thread has its own time slices counter.
TODO: Half done, extend it to all ports.
TODO: Seek optimizations.
*** 2.3.5 ***
- FIX: Fixed RTC compile problem on STM32F103 (bug 3468445).