From ede552ab25e23e2aa64b6953c4ef848699266881 Mon Sep 17 00:00:00 2001 From: Pawel Dziepak Date: Mon, 23 Dec 2013 22:06:33 +0100 Subject: [PATCH] scheduler: Keep thread effective priority cached --- .../kernel/scheduler/scheduler_thread.cpp | 18 ++++++++++++++ .../kernel/scheduler/scheduler_thread.h | 24 +++++++++---------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/system/kernel/scheduler/scheduler_thread.cpp b/src/system/kernel/scheduler/scheduler_thread.cpp index 3bcb4d79be..ab45e78de0 100644 --- a/src/system/kernel/scheduler/scheduler_thread.cpp +++ b/src/system/kernel/scheduler/scheduler_thread.cpp @@ -22,6 +22,7 @@ ThreadData::Init() { fPriorityPenalty = 0; fAdditionalPenalty = 0; + fEffectivePriority = -1; fTimeLeft = 0; fStolenTime = 0; @@ -121,6 +122,23 @@ ThreadData::ComputeQuantum() } +void +ThreadData::_ComputeEffectivePriority() const +{ + if (thread_is_idle_thread(fThread)) + fEffectivePriority = B_IDLE_PRIORITY; + else if (fThread->priority >= B_FIRST_REAL_TIME_PRIORITY) + fEffectivePriority = fThread->priority; + else { + fEffectivePriority = fThread->priority; + fEffectivePriority -= _GetPenalty(); + + ASSERT(fEffectivePriority < B_FIRST_REAL_TIME_PRIORITY); + ASSERT(fEffectivePriority >= B_LOWEST_ACTIVE_PRIORITY); + } +} + + inline CoreEntry* ThreadData::_ChooseCore() const { diff --git a/src/system/kernel/scheduler/scheduler_thread.h b/src/system/kernel/scheduler/scheduler_thread.h index 7a8b6163cf..4260e82586 100644 --- a/src/system/kernel/scheduler/scheduler_thread.h +++ b/src/system/kernel/scheduler/scheduler_thread.h @@ -74,6 +74,8 @@ private: inline int32 _GetPenalty() const; inline int32 _GetMinimalPriority() const; + void _ComputeEffectivePriority() const; + inline CoreEntry* _ChooseCore() const; inline CPUEntry* _ChooseCPU(CoreEntry* core, bool& rescheduleNeeded) const; @@ -98,6 +100,8 @@ private: int32 fPriorityPenalty; int32 fAdditionalPenalty; + mutable int32 fEffectivePriority; + bigtime_t fTimeLeft; bigtime_t fMeasureActiveTime; @@ -133,18 +137,9 @@ ThreadData::ShouldRebalance() const inline int32 ThreadData::GetEffectivePriority() const { - if (thread_is_idle_thread(fThread)) - return B_IDLE_PRIORITY; - if (fThread->priority >= B_FIRST_REAL_TIME_PRIORITY) - return fThread->priority; - - int32 effectivePriority = fThread->priority; - effectivePriority -= _GetPenalty(); - - ASSERT(effectivePriority < B_FIRST_REAL_TIME_PRIORITY); - ASSERT(effectivePriority >= B_LOWEST_ACTIVE_PRIORITY); - - return effectivePriority; + if (fEffectivePriority == -1) + _ComputeEffectivePriority(); + return fEffectivePriority; } @@ -158,6 +153,7 @@ ThreadData::IncreasePenalty() TRACE("increasing thread %ld penalty\n", fThread->id); + fEffectivePriority = -1; int32 oldPenalty = fPriorityPenalty++; ASSERT(fThread->priority - oldPenalty >= B_LOWEST_ACTIVE_PRIORITY); @@ -173,8 +169,10 @@ ThreadData::IncreasePenalty() inline void ThreadData::CancelPenalty() { - if (fPriorityPenalty != 0) + if (fPriorityPenalty != 0) { TRACE("cancelling thread %ld penalty\n", fThread->id); + fEffectivePriority = -1; + } fAdditionalPenalty = 0; fPriorityPenalty = 0;