1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:47:34 +00:00

Kernel: Refactor scheduler to use dynamic thread priorities

Threads now have numeric priorities with a base priority in the 1-99
range.

Whenever a runnable thread is *not* scheduled, its effective priority
is incremented by 1. This is tracked in Thread::m_extra_priority.
The effective priority of a thread is m_priority + m_extra_priority.

When a runnable thread *is* scheduled, its m_extra_priority is reset to
zero and the effective priority returns to base.

This means that lower-priority threads will always eventually get
scheduled to run, once its effective priority becomes high enough to
exceed the base priority of threads "above" it.

The previous values for ThreadPriority (Low, Normal and High) are now
replaced as follows:

    Low -> 10
    Normal -> 30
    High -> 50

In other words, it will take 20 ticks for a "Low" priority thread to
get to "Normal" effective priority, and another 20 to reach "High".

This is not perfect, and I've used some quite naive data structures,
but I think the mechanism will allow us to build various new and
interesting optimizations, and we can figure out better data structures
later on. :^)
This commit is contained in:
Andreas Kling 2019-12-30 18:46:17 +01:00
parent 816d3e6208
commit 50677bf806
16 changed files with 109 additions and 149 deletions

View file

@ -44,6 +44,12 @@ int module_unload(const char* name, size_t name_length);
int profiling_enable(pid_t);
int profiling_disable(pid_t);
#define THREAD_PRIORITY_MIN 1
#define THREAD_PRIORITY_LOW 10
#define THREAD_PRIORITY_NORMAL 30
#define THREAD_PRIORITY_HIGH 50
#define THREAD_PRIORITY_MAX 99
#define FUTEX_WAIT 1
#define FUTEX_WAKE 2

View file

@ -54,7 +54,8 @@ HashMap<pid_t, CProcessStatistics> CProcessStatisticsReader::get_all()
thread.name = thread_object.get("name").to_string();
thread.state = thread_object.get("state").to_string();
thread.ticks = thread_object.get("ticks").to_u32();
thread.priority = thread_object.get("priority").to_string();
thread.priority = thread_object.get("priority").to_u32();
thread.effective_priority = thread_object.get("effective_priority").to_u32();
thread.syscall_count = thread_object.get("syscall_count").to_u32();
thread.inode_faults = thread_object.get("inode_faults").to_u32();
thread.zero_faults = thread_object.get("zero_faults").to_u32();

View file

@ -19,7 +19,8 @@ struct CThreadStatistics {
unsigned file_read_bytes;
unsigned file_write_bytes;
String state;
String priority;
u32 priority;
u32 effective_priority;
String name;
};

View file

@ -302,8 +302,7 @@ int pthread_attr_setschedparam(pthread_attr_t* attributes, const struct sched_pa
if (!attributes_impl || !p_sched_param)
return EINVAL;
// NOTE: This must track sched_get_priority_[min,max] and ThreadPriority enum in Thread.h
if (p_sched_param->sched_priority < 0 || p_sched_param->sched_priority > 3)
if (p_sched_param->sched_priority < THREAD_PRIORITY_MIN || p_sched_param->sched_priority > THREAD_PRIORITY_MAX)
return ENOTSUP;
attributes_impl->m_schedule_priority = p_sched_param->sched_priority;