From a1ac0c6cb47e10863b0bfbb1a6aadc71ccc513ab Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Fri, 29 Mar 2019 17:01:46 -0400 Subject: Addapt thread class to the new Scheduler --- src/core/hle/kernel/thread.h | 55 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 07e989637..c426a7209 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -75,6 +75,21 @@ enum class ThreadActivity : u32 { Paused = 1, }; +enum class ThreadSchedStatus : u32 { None = 0, Paused = 1, Runnable = 2, Exited = 3 }; + +enum ThreadSchedFlags : u32 { + ProcessPauseFlag = 1 << 4, + ThreadPauseFlag = 1 << 5, + ProcessDebugPauseFlag = 1 << 6, + KernelInitPauseFlag = 1 << 8, +}; + +enum ThreadSchedMasks : u32 { + LowMask = 0x000f, + HighMask = 0xfff0, + ForcePauseMask = 0x0070, +}; + class Thread final : public WaitObject { public: using MutexWaitingThreads = std::vector>; @@ -278,6 +293,10 @@ public: return processor_id; } + void SetProcessorID(s32 new_core) { + processor_id = new_core; + } + Process* GetOwnerProcess() { return owner_process; } @@ -383,11 +402,38 @@ public: /// Sleeps this thread for the given amount of nanoseconds. void Sleep(s64 nanoseconds); + /// Yields this thread without rebalancing loads. + void YieldType0(); + + /// Yields this thread and does a load rebalancing. + void YieldType1(); + + /// Yields this thread and if the core is left idle, loads are rebalanced + void YieldType2(); + + ThreadSchedStatus GetSchedulingStatus() { + return static_cast(scheduling_state & ThreadSchedMasks::LowMask); + } + + bool IsRunning() const { + return is_running; + } + + void SetIsRunning(bool value) { + is_running = value; + } + private: explicit Thread(KernelCore& kernel); ~Thread() override; - void ChangeScheduler(); + void SetSchedulingStatus(ThreadSchedStatus new_status); + void SetCurrentPriority(u32 new_priority); + ResultCode SetCoreAndAffinityMask(s32 new_core, u64 new_affinity_mask); + + void AdjustSchedulingOnStatus(u32 old_flags); + void AdjustSchedulingOnPriority(u32 old_priority); + void AdjustSchedulingOnAffinity(u64 old_affinity_mask, s32 old_core); Core::ARM_Interface::ThreadContext context{}; @@ -453,6 +499,13 @@ private: ThreadActivity activity = ThreadActivity::Normal; + s32 ideal_core_override = -1; + u64 affinity_mask_override = 0x1; + u32 affinity_override_count = 0; + + u32 scheduling_state = 0; + bool is_running = false; + std::string name; }; -- cgit v1.2.3 From 82218c925af8bcbaa05ae9f39af2d2393de7681f Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Wed, 19 Jun 2019 09:11:18 -0400 Subject: Kernel: Style and Corrections --- src/core/hle/kernel/thread.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index c426a7209..bf0cae959 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -75,7 +75,12 @@ enum class ThreadActivity : u32 { Paused = 1, }; -enum class ThreadSchedStatus : u32 { None = 0, Paused = 1, Runnable = 2, Exited = 3 }; +enum class ThreadSchedStatus : u32 { + None = 0, + Paused = 1, + Runnable = 2, + Exited = 3, +}; enum ThreadSchedFlags : u32 { ProcessPauseFlag = 1 << 4, @@ -403,15 +408,15 @@ public: void Sleep(s64 nanoseconds); /// Yields this thread without rebalancing loads. - void YieldType0(); + void YieldSimple(); /// Yields this thread and does a load rebalancing. - void YieldType1(); + void YieldAndBalanceLoad(); /// Yields this thread and if the core is left idle, loads are rebalanced - void YieldType2(); + void YieldAndWaitForLoadBalancing(); - ThreadSchedStatus GetSchedulingStatus() { + ThreadSchedStatus GetSchedulingStatus() const { return static_cast(scheduling_state & ThreadSchedMasks::LowMask); } -- cgit v1.2.3 From 103f3a2fe51a09caf3f478226b6957b23c6eff79 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Tue, 10 Sep 2019 10:23:43 -0400 Subject: Scheduler: Add protections for Yield bombing In case of redundant yields, the scheduler will now idle the core for it's timeslice, in order to avoid continuously yielding the same thing over and over. --- src/core/hle/kernel/thread.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index bf0cae959..88255099f 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -408,13 +408,13 @@ public: void Sleep(s64 nanoseconds); /// Yields this thread without rebalancing loads. - void YieldSimple(); + bool YieldSimple(); /// Yields this thread and does a load rebalancing. - void YieldAndBalanceLoad(); + bool YieldAndBalanceLoad(); /// Yields this thread and if the core is left idle, loads are rebalanced - void YieldAndWaitForLoadBalancing(); + bool YieldAndWaitForLoadBalancing(); ThreadSchedStatus GetSchedulingStatus() const { return static_cast(scheduling_state & ThreadSchedMasks::LowMask); -- cgit v1.2.3 From 0cf26cee593c3c6abe909f3db52d972f846b13a9 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Wed, 11 Sep 2019 12:14:37 -0400 Subject: Scheduler: Implement Yield Count and Core migration on Thread Preemption. --- src/core/hle/kernel/thread.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 88255099f..bec23a0e0 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -416,6 +416,14 @@ public: /// Yields this thread and if the core is left idle, loads are rebalanced bool YieldAndWaitForLoadBalancing(); + void IncrementYieldCount() { + yield_count++; + } + + u64 GetYieldCount() const { + return yield_count; + } + ThreadSchedStatus GetSchedulingStatus() const { return static_cast(scheduling_state & ThreadSchedMasks::LowMask); } @@ -460,6 +468,7 @@ private: u64 total_cpu_time_ticks = 0; ///< Total CPU running ticks. u64 last_running_ticks = 0; ///< CPU tick when thread was last running + u64 yield_count = 0; ///< Number of innecessaries yields occured. s32 processor_id = 0; -- cgit v1.2.3 From 1ec1e8137356c64d624d90cd67acebb10f056abd Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Mon, 30 Sep 2019 20:50:59 -0400 Subject: Kernel: Clang Format --- src/core/hle/kernel/thread.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index bec23a0e0..4d220c4f9 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -468,7 +468,7 @@ private: u64 total_cpu_time_ticks = 0; ///< Total CPU running ticks. u64 last_running_ticks = 0; ///< CPU tick when thread was last running - u64 yield_count = 0; ///< Number of innecessaries yields occured. + u64 yield_count = 0; ///< Number of innecessaries yields occured. s32 processor_id = 0; -- cgit v1.2.3 From 1c6a11ab142d18c3444629940f183b7c1865a5e2 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Thu, 10 Oct 2019 08:04:14 -0400 Subject: Kernel: Corrections to Wait Objects clearing in which a thread could still be signalled after a timeout or a cancel. --- src/core/hle/kernel/thread.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 4d220c4f9..ceb4d5159 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -319,6 +319,9 @@ public: } void ClearWaitObjects() { + for (const auto& waiting_object : wait_objects) { + waiting_object->RemoveWaitingThread(this); + } wait_objects.clear(); } -- cgit v1.2.3 From 3073615dbc214a53badc88da68eecbaaa73898de Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Sat, 12 Oct 2019 10:13:25 -0400 Subject: Kernel: Address Feedback. --- src/core/hle/kernel/thread.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index ceb4d5159..e0f3b6204 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -82,19 +82,25 @@ enum class ThreadSchedStatus : u32 { Exited = 3, }; -enum ThreadSchedFlags : u32 { +enum class ThreadSchedFlags : u32 { ProcessPauseFlag = 1 << 4, ThreadPauseFlag = 1 << 5, ProcessDebugPauseFlag = 1 << 6, KernelInitPauseFlag = 1 << 8, }; -enum ThreadSchedMasks : u32 { +enum class ThreadSchedMasks : u32 { LowMask = 0x000f, HighMask = 0xfff0, ForcePauseMask = 0x0070, }; +enum class CoreFlags : s32 { + IgnoreIdealCore = -1, + ProcessIdealCore = -2, + DontChangeIdealCore = -3, +}; + class Thread final : public WaitObject { public: using MutexWaitingThreads = std::vector>; @@ -428,7 +434,8 @@ public: } ThreadSchedStatus GetSchedulingStatus() const { - return static_cast(scheduling_state & ThreadSchedMasks::LowMask); + return static_cast(scheduling_state & + static_cast(ThreadSchedMasks::LowMask)); } bool IsRunning() const { @@ -471,7 +478,8 @@ private: u64 total_cpu_time_ticks = 0; ///< Total CPU running ticks. u64 last_running_ticks = 0; ///< CPU tick when thread was last running - u64 yield_count = 0; ///< Number of innecessaries yields occured. + u64 yield_count = 0; ///< Number of redundant yields carried by this thread. + ///< a redundant yield is one where no scheduling is changed s32 processor_id = 0; -- cgit v1.2.3 From e28c7f521765a85e27259539f0873b15c18a98f8 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Sat, 12 Oct 2019 10:38:55 -0400 Subject: Kernel: Address Feedback 2 --- src/core/hle/kernel/thread.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index e0f3b6204..7ee437e17 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -35,6 +35,9 @@ enum ThreadProcessorId : s32 { /// Run thread on the ideal core specified by the process. THREADPROCESSORID_IDEAL = -2, + /// when setting Core and Affiny, keeps the ideal core intact + THREADDONTCHANGE_IDEAL = -3, + /// Indicates that the preferred processor ID shouldn't be updated in /// a core mask setting operation. THREADPROCESSORID_DONT_UPDATE = -3, @@ -95,12 +98,6 @@ enum class ThreadSchedMasks : u32 { ForcePauseMask = 0x0070, }; -enum class CoreFlags : s32 { - IgnoreIdealCore = -1, - ProcessIdealCore = -2, - DontChangeIdealCore = -3, -}; - class Thread final : public WaitObject { public: using MutexWaitingThreads = std::vector>; -- cgit v1.2.3 From 64e652d8cbcc4cc67442879ab7e379d62b72703c Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Sat, 12 Oct 2019 10:55:34 -0400 Subject: Kernel Thread: Cleanup THREADPROCESSORID_DONT_UPDATE. --- src/core/hle/kernel/thread.h | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/core/hle/kernel/thread.h') diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 7ee437e17..c9870873d 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -35,9 +35,6 @@ enum ThreadProcessorId : s32 { /// Run thread on the ideal core specified by the process. THREADPROCESSORID_IDEAL = -2, - /// when setting Core and Affiny, keeps the ideal core intact - THREADDONTCHANGE_IDEAL = -3, - /// Indicates that the preferred processor ID shouldn't be updated in /// a core mask setting operation. THREADPROCESSORID_DONT_UPDATE = -3, -- cgit v1.2.3