Английская Википедия:Brain Fuck Scheduler

Материал из Онлайн справочника
Перейти к навигацииПерейти к поиску

Шаблон:Short description Шаблон:Confused Шаблон:Update Шаблон:Infobox software

Файл:Simplified Structure of the Linux Kernel.svg
The location of process schedulers in a simplified structure of the Linux kernel

The Brain Fuck Scheduler (BFS) is a process scheduler designed for the Linux kernel in August 2009 based on earliest eligible virtual deadline first scheduling (EEVDF),[1] as an alternative to the Completely Fair Scheduler (CFS) and the O(1) scheduler.[2] BFS was created by an experienced kernel programmer Con Kolivas.[3]

The objective of BFS, compared to other schedulers, is to provide a scheduler with a simpler algorithm, that does not require adjustment of heuristics or tuning parameters to tailor performance to a specific type of computational workload. Kolivas asserted that these tunable parameters were difficult for the average user to understand, especially in terms of interactions of multiple parameters with each other, and claimed that the use of such tuning parameters could often result in improved performance in a specific targeted type of computation, at the cost of worse performance in the general case.[3] BFS has been reported to improve responsiveness on Linux desktop computers with fewer than 16 cores.[4]

Shortly following its introduction, the new scheduler made headlines within the Linux community, appearing on Slashdot, with reviews in Linux Magazine and Linux Pro Magazine.[2][5][6] Although there have been varied reviews of improved performance and responsiveness, Con Kolivas did not intend for BFS to be integrated into the mainline kernel.[3]

Theoretical design and efficiency

In 2009, BFS was introduced and had originally used a doubly linked list data structure,[7][8] but the data structure is treated like a queue. Task insertion is Шаблон:Tmath.[8]Шаблон:Rp Task search for next task to execute is Шаблон:Tmath worst case.[8]Шаблон:Rp It uses a single global run queue which all CPUs use. Tasks with higher scheduling priorities get executed first.[8]Шаблон:Rp Tasks are ordered (or distributed) and chosen based on the virtual deadline formula in all policies except for the realtime and Isochronous priority classes.

The execution behavior is still a weighted variation of the Round-Robin Scheduler especially when tasks have the same priority below the Isochronous policy.[8]Шаблон:Rp The user tuneable round robin interval (time slice) is 6 milliseconds by default which was chosen as the minimal jitter just below detectable by humans.[8]Шаблон:Rp Kolivas claimed that anything below the 6 ms was pointless and anything above 300 ms for the round robin timeslice is fruitless in terms of throughput.[8]Шаблон:Rp This important tuneable can tailor the round robin scheduler as a trade off between throughput and latency.[8]Шаблон:Rp All tasks get the same time slice with the exception of realtime FIFO which is assumed to have infinite time slice.[8]Шаблон:Rp

Kolivas explained the reason why he choose to go with the doubly linked list mono-runqueue than the multi-runqueue (round robin[9]Шаблон:Rp) priority array[10][9] per CPU that was used in his RDSL scheduler was to put to ease fairness among the multiple CPU scenario and remove complexity that each runqueue in a multi-runqueue scenario had to maintain its own latencies and [task] fairness.[8]Шаблон:Rp He claimed that deterministic latencies was guaranteed with BFS in his later iteration of MuQSS.[11]Шаблон:Rp He also recognized possible lock contention problem (related to the altering, removal, creation of task node data)[11]Шаблон:Rp with increasing CPUs and the overhead of Шаблон:Tmath next task for execution lookup.[11]Шаблон:Rp MuQSS tried to resolve those problems.

Kolivas later changed the design to a skip list in the v0.480 release of BFS in 2016.[12] This time this altered the efficiency of the scheduler. He noted Шаблон:Tmath task insertion, Шаблон:Tmath task lookup; Шаблон:Tmath, with Шаблон:Tmath, for task removal.[12]Шаблон:Rp

Virtual deadline

The virtual deadline formula is a future deadline time that is the scaled round robin timeslice based on the nice level offset by the current time (in niffy units or nanosecond jiffies, an internal kernel time counter).[8]Шаблон:Rp The virtual deadline only suggests the order but does not guarantee that a task will run exactly on the future scheduled niffy.[8]Шаблон:Rp

First a prio ratios lookup table is created.[8]Шаблон:Rp It is based on a recursive sequence. It increases 10% each nice level.[8]Шаблон:Rp It follows a parabolic pattern if graphed, and the niced tasks are distributed as a moving squared function from 0 to 39 (corresponding from highest to lowest nice priority) as the domain and 128 to 5089 as the range.[8]Шаблон:Rp The moving part comes from the Шаблон:Mono variable in the virtual deadline formula that Kolivas hinted.

Шаблон:Pre

Index Numerator
0 128
1 140
2 154
3 169
4 185
5 203
6 223
7 245
8 269
9 295
10 324
11 356
12 391
13 430
14 473
15 520
16 572
17 629
18 691
19 760
20 836
21 919
22 1010
23 1111
24 1222
25 1344
26 1478
27 1625
28 1787
29 1965
30 2161
31 2377
32 2614
33 2875
34 3162
35 3478
36 3825
37 4207
38 4627
39 5089

The task's nice-to-index mapping function Шаблон:Mono is mapped from nice −20...19 to index 0...39 to be used as the input to the prio ratio lookup table. This mapping function is the Шаблон:Code macro in sched.h in the kernel header. The internal kernel implementation slightly differs with range between 100 and 140 static priority but users will see it as −20...19 nice.

Nice Index
−20 0
−19 1
−18 2
−17 3
−16 4
−15 5
−14 6
−13 7
−12 8
−11 9
−10 10
−9 11
−8 12
−7 13
−6 14
−5 15
−4 16
−3 17
−2 18
−1 19
0 20
1 21
2 22
3 23
4 24
5 25
6 26
7 27
8 28
9 29
10 30
11 31
12 32
13 33
14 34
15 35
16 36
17 37
18 38
19 39

The virtual deadline is based on this exact formula:[8]Шаблон:Rp

Шаблон:Pre

Alternatively,

Шаблон:Block indent

where Шаблон:Mono is the virtual deadline in u64 integer nanoseconds as a function of nice Шаблон:Mono and Шаблон:Mono which is the current time in niffies, Шаблон:Mono is the prio ratio table lookup as a function of index, Шаблон:Mono is the task's nice-to-index mapping function, Шаблон:Mono is the round robin timeslice in milliseconds, Шаблон:Mono is a constant of 1 millisecond in terms of nanoseconds as a latency reducing approximation of the conversion factor of <math>\mathrm\frac{10^9 ns}{10^3 ms}</math> but Kolivas uses a base 2 constant Шаблон:Mono with approximately that scale.[8]Шаблон:Rp Smaller values of Шаблон:Mono mean that the virtual deadline is earlier corresponding to negative nice values. Larger values of Шаблон:Mono indicate the virtual deadline is pushed back later corresponding to positive nice values. It uses this formula whenever the timeslice expires.[8]Шаблон:Rp

128 in base 2 corresponds to 100 in base 10 and possibly a "pseudo 100".[8]Шаблон:Rp 115 in base 2 corresponds to 90 in base 10. Kolivas uses 128 for "fast shifts",[8]Шаблон:Rp as in division is right shift base 2.

Nice Virtual deadline in timeslices relative to Шаблон:Mono Virtual deadline in exact seconds relative to Шаблон:Mono
−20 1.0 0.006
−19 1.09 0.006562
−18 1.2 0.007219
−17 1.3 0.007922
−16 1.4 0.008672
−15 1.5 0.009516
−14 1.7 0.010453
−13 1.9 0.011484
−12 2.1 0.012609
−11 2.3 0.013828
−10 2.5 0.015187
−9 2.7 0.016688
−8 3.0 0.018328
−7 3.3 0.020156
−6 3.6 0.022172
−5 4.0 0.024375
−4 4.4 0.026812
−3 4.9 0.029484
−2 5.3 0.032391
−1 5.9 0.035625
0 6.5 0.039188
1 7.1 0.043078
2 7.8 0.047344
3 8.6 0.052078
4 9.5 0.057281
5 10.5 0.063000
6 11.5 0.069281
7 12.6 0.076172
8 13.9 0.083766
9 15.3 0.092109
10 16.8 0.101297
11 18.5 0.111422
12 20.4 0.122531
13 22.4 0.134766
14 24.7 0.148219
15 27.1 0.163031
16 29.8 0.179297
17 32.8 0.197203
18 36.1 0.216891
19 39.7 0.238547

Scheduling policies

BFS uses scheduling policies to determine how much of the CPU tasks may use. BFS uses 4 scheduling tiers (called scheduling policies or scheduling classes) ordered from best to worst which determines how tasks are selected[8]Шаблон:Rp with the ones on top being executed first.

Each task has a special value called a prio. In the v0.462 edition (used in the -ck 4.0 kernel patchset), there are total of 103 "priority queues" (aka prio) or allowed values that it can take. No actual special data structure was used as the priority queue but only the doubly linked list runqueue itself. The lower prio value means it is more important and gets executed first.

Realtime policy

The realtime policy was designed for realtime tasks. This policy implies that the running tasks cannot be interrupted (i.e. preempted) by the lower prio-ed task or lower priority policy tiers. Priority classes considered under the realtime policy by the scheduler are those marked SCHED_RR and SCHED_FIFO.[8]Шаблон:Rp The scheduler treats realtime round robin (SCHED_RR) and realtime FIFO (SCHED_FIFO) differently.[8]Шаблон:Rp

The design laid out first 100 static priority queues.[8]Шаблон:Rp

The task that will get chosen for execution is based on task availability of the lowest value of prio of the 100 queues and FIFO scheduling.[8]Шаблон:Rp

On forks, the process priority will be demoted to normal policy.[8]Шаблон:Rp

On unprivileged use (i.e. non-root user) of sched_setscheduler called with a request for realtime policy class, the scheduler will demote the task to Isochronous policy.[8]Шаблон:Rp

Isochronous policy

The Isochronous policy was designed for near realtime performance for non-root users.[8]Шаблон:Rp

The design laid out 1 priority queue that by default ran as pseudo-realtime tasks, but can be tuned as a degree of realtime.[8]Шаблон:Rp

The behavior of the policy can allow a task can be demoted to normal policy[8]Шаблон:Rp when it exceeds a tuneable resource handling percentage (70% by default[8]Шаблон:Rp) of 5 seconds scaled to the number of online CPUs and the timer resolution plus 1 tick.[8]Шаблон:Rp[11]Шаблон:Rp The formula was altered in MuQSS due to the multi-runqueue design. The exact formulas are:

Шаблон:Block indent Шаблон:Block indent

where Шаблон:Mono is the total number of isochronous ticks, Шаблон:Mono is the timer frequency, Шаблон:Mono is the number of online CPUs, Шаблон:Mono is the tuneable resource handling percentage not in decimal but as a whole number. The timer frequency is set to 250 by default and editable in the kernel, but usually tuned to 100 Hz for servers and 1000 Hz for interactive desktops. 250 is the balanced value. Setting Шаблон:Mono to 100 made tasks behave as realtime and 0 made it not pseudo-realtime and anything in the middle was pseudo-realtime.[8]Шаблон:Rp

The task that had an earliest virtual deadline was chosen for execution, but when multiple Isochronous tasks are in existence, they schedule as round robin allowing tasks to run the tuneable round robin value (with 6 ms as the default) one after another in a fair equal chance without considering the nice level.[8]Шаблон:Rp

This behavior of the Isochronous policy is unique to only BFS and MuQSS and may not be implemented in other CPU schedulers.[8]Шаблон:Rp[11]Шаблон:Rp

Normal policy

The normal policy was designed for regular use and is the default policy. Newly created tasks are typically marked normal.[8]Шаблон:Rp

The design laid out one priority queue and tasks are chosen to be executed first based on earliest virtual deadline.

Idle priority policy

The idle priority was designed for background processes such as distributed programs and transcoders so that foreground processes or those above this scheduling policy can run uninterrupted.[8]Шаблон:Rp

The design laid out 1 priority queue and tasks can be promoted to normal policy automatically to prevent indefinite resource hold.[8]Шаблон:Rp

The next executed task with Idle priority with others residing in the same priority policy is selected by the earliest virtual deadline.[8]Шаблон:Rp

Preemption

Preemption can occur when a newly ready task with a higher priority policy (i.e. higher prio) has an earlier virtual deadline than the currently running task - which will be descheduled and put at the back of the queue.[8]Шаблон:Rp Descheduled means that its virtual deadline is updated.[8]Шаблон:Rp The task's time gets refilled to max round robin quantum when it has used up all its time.[8]Шаблон:Rp If the scheduler found the task at the higher prio with the earliest virtual deadline, it will execute in place of the less important currently running task only if all logical CPUs (including hyperthreaded cores / SMT threads) are busy. The scheduler will delay preemption as long as possible if there are unused logical CPUs.

If a task is marked idle priority policy, it cannot preempt at all even other idle policy marked tasks but rather use cooperative multitasking.[8]Шаблон:Rp

Task placement, multiple cores

When the scheduler discovers a waking task on a non-unicore system, it will need to determine which logical CPU to run the task on. The scheduler favors most the idle hyperthreaded cores (or idle SMT threads) first on the same CPU that the task executed on,[8]Шаблон:Rp then the other idle core of a multicore CPU,[8]Шаблон:Rp then the other CPUs on the same NUMA node,[8]Шаблон:Rp then all busy hyperthreaded cores / SMT threads / logical CPUs to be preempted on the same NUMA node,[8]Шаблон:Rp then the other (remote) NUMA node[8]Шаблон:Rp and is ranked on a preference list.[8]Шаблон:Rp This special scan exists to minimize latency overhead resulting of migrating the task.[8]Шаблон:Rp

The preemption order is similar to the above paragraph. The preemption order is hyperthreaded core / SMT units on the same multicore first, then the other core in the multicore, then the other CPU on the same NUMA node.[8]Шаблон:Rp When it goes scanning for a task to preempt in the other remote NUMA node, the preemption is just any busy threads with lower to equal prio or later virtual deadline assuming that all logical CPUs (including hyperthreaded core / SMT threads) in the machine are all busy.[8]Шаблон:Rp The scheduler will have to scan for a suitable task with a lower or maybe equal priority policy task (with a later virtual deadline if necessary) to preempt and avoid logical CPUs with a task with a higher priority policy which it cannot preempt. Local preemption has a higher rank than scanning for a remote idle NUMA unit.[8]Шаблон:Rp

When a task is involuntary preempted at the time the CPU is slowed down as a result of kernel mediated CPU frequency scaling (aka CPU frequency governor), the task is specially marked "sticky" except those marked as realtime policy.[8]Шаблон:Rp Marked sticky indicates that the task still has unused time and the task is restricted executing to the same CPU.[8]Шаблон:Rp The task will be marked sticky whenever the CPU scaling governor has scaled the CPU at a slower speed.[8]Шаблон:Rp The idled stickied task will return to either executing at full Ghz speed by chance or to be rescheduled to execute on the best idle CPU that is not the same CPU that the task ran on.[8]Шаблон:Rp It is not desirable to migrate the task to other places but make it idle instead because of increased latency brought about of overhead to migrating the task to another CPU or NUMA node.[8]Шаблон:Rp This sticky feature was removed in the last iteration of BFS (v0.512) corresponding to Kolivas' patchset 4.8-ck1 and did not exist in MuQSS.

schedtool

A privileged user can change the priority policy of a process with the schedtool program[8]Шаблон:Rp or it is done by a program itself.[8]Шаблон:Rp The priority class can be manipulated at the code level with a syscall like sched_setscheduler only available to root,[13] which schedtool uses.[14]

Benchmarks

In a contemporary study,[4] the author compared the BFS to the CFS using the Linux kernel v3.6.2 and several performance-based endpoints. The purpose of this study was to evaluate the Completely Fair Scheduler (CFS) in the vanilla Linux kernel and the BFS in the corresponding kernel patched with the ck1 patchset. Seven different machines were used to see if differences exist and, to what degree they scale using performance based metrics. Number of logical CPUs ranged from 1 to 16. These end-points were never factors in the primary design goals of the BFS. The results were encouraging.

Kernels patched with the ck1 patch set including the BFS outperformed the vanilla kernel using the CFS at nearly all the performance-based benchmarks tested.[4] Further study with a larger test set could be conducted, but based on the small test set of 7 PCs evaluated, these increases in process queuing, efficiency/speed are, on the whole, independent of CPU type (mono, dual, quad, hyperthreaded, etc.), CPU architecture (32-bit and 64-bit) and of CPU multiplicity (mono or dual socket).

Moreover, on several "modern" CPUs, such as the Intel Core 2 Duo and Core i7, that represent common workstations and laptops, BFS consistently outperformed the CFS in the vanilla kernel at all benchmarks. Efficiency and speed gains were small to moderate.

Adoption

BFS is the default scheduler for the following desktop Linux distributions:

Additionally, BFS has been added to an experimental branch of Google's Android development repository.[19] It was not included in the Froyo release after blind testing did not show an improved user experience.[20]

MuQSS

BFS has been retired in favour of MuQSS, known formally as the Multiple Queue Skiplist Scheduler, a rewritten implementation of the same concept.[21][22] The primary author abandoned[23] work on MuQSS by the end of August 2021.

Theoretical design and efficiency

MuQSS uses a bidirectional static arrayed 8 level skip list and tasks are ordered by static priority [queues] (referring to the scheduling policy) and a virtual deadline.[11]Шаблон:Rp 8 was chosen to fit the array in the cacheline.[11]Шаблон:Rp Doubly linked data structure design was chosen to speed up task removal. Removing a task takes only O(1) with a doubly skip list versus the original design by William Pugh which takes Шаблон:Tmath worst case.[11]Шаблон:Rp

Task insertion is Шаблон:Tmath.[11]Шаблон:Rp The next task for execution lookup is Шаблон:Tmath, where Шаблон:Mvar is the number of CPUs.[11]Шаблон:Rp The next task for execution is Шаблон:Tmath per runqueue,[11]Шаблон:Rp but the scheduler examines every other runqueues to maintain task fairness among CPUs, for latency or balancing (to maximize CPU usage and cache coherency on the same NUMA node over those that access across NUMA nodes), so ultimately Шаблон:Tmath.[11]Шаблон:Rp The max number of tasks it can handle are 64k tasks per runqueue per CPU.[11]Шаблон:Rp It uses multiple task runqueues in some configurations one runqueue per CPU, whereas its predecessor BFS only used one task runqueue for all CPUs.

Tasks are ordered as a gradient in the skip list in a way that realtime policy priority comes first and idle policy priority comes last.[11]Шаблон:Rp Normal and idle priority policy still get sorted by virtual deadline which uses nice values.[11]Шаблон:Rp Realtime and Isochronous policy tasks are run in FIFO order ignoring nice values.[11]Шаблон:Rp New tasks with same key are placed in FIFO order meaning that newer tasks get placed at the end of the list (i.e. top most node vertically), and tasks at 0th level or at the front-bottom get execution first before those at nearest to the top vertically and those furthest away from the head node.[11]Шаблон:Rp The key used for inserted sorting is either the static priority[11]Шаблон:Rp or the virtual deadline.[11]Шаблон:Rp

The user can choose to share runqueues among multicore or have a runqueue per logical CPU.[11]Шаблон:Rp The speculation of sharing runqueues design was to reduce latency with a tradeoff of throughput.[11]Шаблон:Rp

A new behavior introduced by MuQSS was the use of the high resolution timer for below millisecond accuracy when timeslices were used up resulting in rescheduling tasks.[11]Шаблон:Rp

See also

Шаблон:Portal

References

Шаблон:Reflist

External links

Шаблон:Linux kernel