SCHEDULER(9)
SCHEDULER(9) ๐
okay so welcome to the Kernel Developerโs Manual entry for the round-robin scheduler girlies ๐ this page is all about curpriority_cmp, maybe_resched, resetpriority, roundrobin, roundrobin_interval, sched_setup, schedclock, schedcpu, setrunnable, and updatepri โ and yes bestie, they are literally here to perform round-robin scheduling of runnable processes. no cap.
NAME โจ
curpriority_cmp, maybe_resched, resetpriority, roundrobin, roundrobin_interval, sched_setup, schedclock, schedcpu, setrunnable, updatepri โ perform round-robin scheduling of runnable processes ๐ซถ
SYNOPSIS ๐ง
#include <sys/param.h>
#include <sys/proc.h>
int
curpriority_cmp(struct proc *p);
void
maybe_resched(struct thread *td);
void
propagate_priority(struct proc *p);
void
resetpriority(struct ksegrp *kg);
void
roundrobin(void *arg);
int
roundrobin_interval(void);
void
sched_setup(void *dummy);
void
schedclock(struct thread *td);
void
schedcpu(void *arg);
void
setrunnable(struct thread *td);
void
updatepri(struct thread *td);
DESCRIPTION ๐
okay so every process has three different priorities stored in struct proc: p_usrpri, p_nativepri, and p_priority. yes, three. main character energy, honestly ๐
p_usrpriโ this is the user priority of the process, calculated from a processโ estimated CPU time and nice level.p_nativepriโ this is the saved priority used bypropagate_priority(). When a process obtains a mutex, its priority is saved inp_nativepri. While it holds the mutex, the processโs priority may be bumped by another process that blocks on the mutex. When the process releases the mutex, then its priority is restored to the priority saved inp_nativepri.p_priorityโ this is the actual priority of the process, and itโs used to determine whatrunqueue(9)it runs on, for example.
curpriority_cmp()โ compares the cached priority of the currently running process with processp. If the currently running process has a higher priority, then it will return a value less than zero. If the current process has a lower priority, then it will return a value greater than zero. If the current process has the same priority asp, thencurpriority_cmp()will return zero. The cached priority of the currently running process is updated when a process resumes fromtsleep(9)or returns to userland inuserret()and is stored in the private variablecurpriority.maybe_resched()โ compares the priorities of the current thread andtd. Iftdhas a higher priority than the current thread, then a context switch is needed, andKEF_NEEDRESCHEDis set.propagate_priority()โ looks at the process that owns the mutexpis blocked on. That processโs priority is bumped to the priority ofpif needed. If the process is currently running, then the function returns. If the process is on arunqueue(9), then the process is moved to the appropriaterunqueue(9)for its new priority. If the process is blocked on a mutex, its position in the list of processes blocked on the mutex in question is updated to reflect its new priority. Then, the function repeats the procedure using the process that owns the mutex just encountered. Note that a processโs priorities are only bumped to the priority of the original processp, not to the priority of the previously encountered process.resetpriority()โ recomputes the user priority of the ksegrpkg(stored inkg_user_pri) and callsmaybe_resched()to force a reschedule of each thread in the group if needed.roundrobin()โ is used as atimeout(9)function to force a reschedule everysched_quantumticks.roundrobin_interval()โ simply returns the number of clock ticks in between reschedules triggered byroundrobin(). Thus, all it does is return the current value ofsched_quantum.sched_setup()โ is aSYSINIT(9)that is called to start the callout driven scheduler functions. It just calls theroundrobin()andschedcpu()functions for the first time. After the initial call, the two functions will propagate themselves by registering their callout event again at the completion of the respective function.schedclock()โ is called bystatclock()to adjust the priority of the currently running threadโs ksegrp. It updates the groupโs estimated CPU time and then adjusts the priority viaresetpriority().schedcpu()โ updates all process priorities. First, it updates statistics that track how long processes have been in various process states. Secondly, it updates the estimated CPU time for the current process such that about 90% of the CPU usage is forgotten in5 * load average seconds. For example, if the load average is2.00, then at least 90% of the estimated CPU time for the process should be based on the amount of CPU time the process has had in the last10 seconds. It then recomputes the priority of the process and moves it to the appropriaterunqueue(9)if necessary. Thirdly, it updates the%CPUestimate used by utilities such asps(1)andtop(1)so that 95% of the CPU usage is forgotten in60 seconds. Once all process priorities have been updated,schedcpu()callsvmmeter()to update various other statistics including the load average. Finally, it schedules itself to run again inhzclock ticks.setrunnable()โ is used to change a processโs state to be runnable. The process is placed on arunqueue(9)if needed, and the swapper process is woken up and told to swap the process in if the process is swapped out. If the process has been asleep for at least one run ofschedcpu(), thenupdatepri()is used to adjust the priority of the process.updatepri()โ is used to adjust the priority of a process that has been asleep. It retroactively decays the estimated CPU time of the process for eachschedcpu()event that the process was asleep. Finally, it callsresetpriority()to adjust the priority of the process.
SEE ALSO ๐
mi_switch(9), runqueue(9), sleepqueue(9), tsleep(9)
BUGS ๐
- The
curpriorityvariable really should be per-CPU. In addition,maybe_resched()should compare the priority ofchkwith that of each CPU, and then send an IPI to the processor with the lowest priority to trigger a reschedule if needed. - Priority propagation is broken and is thus disabled by default. The
p_nativeprivariable is only updated if a process does not obtain a sleep mutex on the first try. Also, if a process obtains more than one sleep mutex in this manner, and had its priority bumped in between, thenp_nativepriwill be clobbered.
made with ๐ from the Debian flavor of this page, dated November 3, 2000 โ and itโs filed under SCHEDULER(9) in the Kernel Developerโs Manual. periodt ๐โจ
๐ฆ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2000-2001 John H. Baldwin <jhb@FreeBSD.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms