Title: ?????%20???????%20?-Linux
1????? ??????? ?-Linux
- ??? ???
- (????? ?? ???? ?????? "?????? ?????" ???????)
2????
- ???????? ????? ???????? ?-Linux
- ???? ???? ????? ????? ????? ??? ??? ?????
3???? ??????
- ???????? ?????? ????? ?????? ?? ??? ?????????
???? ?? Linux ?????? ????? 2.5.X ????? - ???????? ?? ???? ????? ????? ????? ???? ?????
???? Understanding the Linux Kernel - ????? ???? ???????? ?? ????????? ????, ???? ????
?? ?? ???? ??? ?????? ??????? ?????? ??????? ???
?? ?????? ?????? 2.5.X - ?????? ???????? include/linux/sched.h
?-kernel/sched.c - ???? ???? ?? ??? ?? ???? ????????? ???? Ingo
Molnar
4????? ??????? ?-Linux (1)
- Linux ?????? ????? ??????? ???? "?? ?????" ??
????? ??????? ????? ??? ????? ?????? (time
sharing) - ????? ????? ?????? ??? ???? ??????? ????? ????
- ???? ??????? "??????", ????? ???? ????? ???????
????? ??????, ?? ?? ??????? ?? ??????? ?????? ??
????? - Preemptive multitasking
- ??????? ????? ???????? ?"??????" ?-Linux ??????
SCHED_OTHER - ???? ???????? ????? ?????? ??? ????? ???
- ??? ????? ????? ??? ??????? ?????? ?? ??? ????
- ????? ????? ???????? ?? ????
5????? ??????? ?-Linux (2)
- Linux ?????, ????? ???????? "??????", ???????
???-??? (real-time) - ??????? ??????? ????? ?? ??????? ????? ??????
???? ??? ????? ??? ????? - ??????? ?????? ?????????, ???? ???????? ???'
- ?????? ???-??? ??????? ????? ??-??? ???????
?????? - ??????? ?????? ???? ????? ???? ?? ?? ??????
???-??? ?????? ????? (??? TASK_RUNNING) - ???? ?????? ???-??? ?????? ??? ????? ?????? ??
??????? ????? - SCHED_FIFO ????? ????? (non-preemptive) ??
???????? ????? ????? ?? ???? ?????? ????? ?????
???? ?? ???? ????? ???? ???? ???? ?????. ?????
????? ???? ???????? ??????? ????? ??????? ????? - SCHED_RR ????? ??? ???? (Round Robin) ???
????? ????? ????? ???? ??? ?? ???????? ???????
????? ??????? ?????
6???????? ?????? (SCHED_OTHER)
- ??? ????? ????? ?"??????" (epochs)
- ??? ????? ????? ??? ??? ?????? ????? time slice
- ????? ?-time slice ???? ???? ?-runqueue ?????
????? ????? - ?????? ????? ????? ??? ????? ???? ?????
(TASK_RUNNING) ??? ??????? ?????? ????? ????? ??
??? ???? ???? ?-time slice ??? - ??? ?????, ?? ???????? ??????? ????? ???????
????? ?? ??? ????? ???? ?? ?-time slice ??? ??
???? ?????? - ????? ??????? ???? ?? ????? ?????? ???? ?????
???? ?? ?-time slice ??? - ???? ???? ????? ?????? ????? ???? ?? ??? ?????
????? ????? ????? time slice ???
7????? ???????? ??????
- ???????? ?????? ????? ?????? Linux ?"? ????? ????
????? (????? kernel/sched.c) - ?????? ???????? (scheduler) ?????? ?? ?????
?????? ??? ????? ????? ??? ????? ?????? - ???????? schedule()
- ?????? ????? ????? (??? ????? ????) ?? ?????
????? ?? ?????? ?????? ??? ????? (??????,
time_slice, ???') ?????? ??????? ??? ????? - ???????? scheduler_tick()
- ??? ??, ??? ????? ?????, ?????? ?????? ??????
- ???????? ????? ?????? ?? ?????? ?????/????
- ????? ??????? ??? ????? ???? ????? ????? ?????
- ????????? (de)activate_task()
8????? ???
- ???? ???? ????? ??????? ?????? ???? ???? ?????
???? - ????? ??? ???? ????? ?????? ?????
- ?? ????? ???? ????? ????? ?????
- ?-Linux ????? ?????? ???? ?? HZ100Hz
- ???? ??? ????? ????? (??????? ????) ???? ?-Linux
???? ???? ????? jiffies
9?????? ????? (1)
- ????? ?????? ??? ?????? ????? ?????? ?? ??????
?????? (process priority) - ?????? ????? ????? ????? ????? ????? ????
- ??? ????? ???? - ?????? ?????
- ????? ?? ???? ?? ??????? ?????? ?????
- ???? ????? ?????? ????? ??-??? API (?????? ??????
nice()) - ????? ????? ???? ????? ????? ??????? ??????
- ?????? "?????" ????? ????? ?????? ???? ?????
???????/???? ??-??? ????? ??????? ????? ?????
???? - ????? ????, ??????? ??? ????? ?/? (IO-BOUND)
??????? ??-??? ??????? ??? ????? ?????
(CPU-BOUND) ???? ???? ?????? ?????
10?????? ????? (2)
- ??????? ?? ????? ????? ??????
- ????? ??????? ?? 0..139 (MAX_PRIO-1)
- ???????? 0..99 (MAX_RT_PRIO-1) ??????? ???????
???-??? - ???????? 100..139 ??????? ???????? ??????
- ??? ?????? ????? ???? ????? ?????? ?????, ?????
- ????? ???? ????? ????-??? ?? ?????? 120
- ?????? ??????? ????? 120
- ????? ??? ??????? ?????? ?? ????? ?-120 ?????
?-niceness (??? nice) ?? ?????? - ???? ????? ????, ??? ?-nice ?? ??? -20 ?-19
- ?????? ????? ?????? TASK_NICE
- define TASK_NICE(p) ((p)-gtstatic_prio
MAX_RT_PRIO 20)
11??? ????? ?? ?-runqueue (1)
- ?-runqueue ??? ???? ?????? ?????? ?? ?? ?????
???????? (process descriptors) ?? ???????? ?????
?? ?????? ????? ?? ???? ????? ("????? ????") - ?? runqueue (?? ?? ????) ???? ?? ??????? ?????
- nr_running ???? ???????? ?-runqueue (?? ???? ??
?-swapper) - curr ????? ????? ?????? ??? ????
- idle ????? ????? ????? ?-swapper
12??? ????? ?? ?-runqueue (2)
- active ????? ????? ???? ????????? ?? ????????
???????, ????? ??????? ?????? ????? (?????? ????
TASK_RUNNING) ????? ??? ??? ???? ?????? ??????
??????? - ????? ????? ?????? ????? ??????? ????? ?????
????? ???? ?? ??? ??????? ????????
activate_task() - expired ????? ????? ???? ????????? ?? ????????
??????? ????? ????? ?? ?-time slice ???? ??????
??????? - expired_timestamp ??? ??? ?????? ??????
?-active ?-expired ?????? ???????
13??? ????? ?? ?-runqueue (3)
- ???? ???? ???????? (struct prio_array) ???? ??
??????? ????? - nr_active ???? ???????? ??????? ????? ??
- bitmap ????? ????? ????? ???? ????? ???????
(140 ?????) - ??? i ???? (1) ?? ?? ??????? ???? ?????? ???????
i - ?????? ??? ?????? ??? ?????? ?????? ???? ???????
????? ????? ?-active ????? ??? ???? ?????
?-O(1) - queue ???? ?????? ????, ?? ????? ??? ????
?????? (140 ??????). ?? ????? ????? ??? ??? ????
list_t.
14??? ????? ?? ?-runqueue (4)
runqueue
nr_running
curr
???? ????? swapper
idle
active
prio_array_t
nr_active
expired
bitmap
queue
expired_ts
..
..
...
58
0
..
..
112
prio_array_t
1
..
..
120
1
..
..
????? ???????
15????? ????? ??????? ?????? (1)
- ???? ????? ???? ?? ??????? ????? ??????? ??????
- policy ??? ??????? ?????? ?? ??????
- need_resched ??? ??? ?????? ???? ????? ??
????? - ?????? set_tsk_need_resched ????? ?? ????
- ?????? clear_tsk_need_resched ???? ?? ????
16????? ????? ??????? ?????? (2)
- prio ?????? ??????
- static_prio ??????? ?????? ?? ?????? ????
????? - sleep_timestamp ??? ??????? ????? ????? ????
??????? (????? ?????? ??"? ??? ?? ?????? ??
?????) - sleep_avg ??? ????? "?????" ?? ??????
- ???? ?????? ??????? ?????? ????? ?????
- time_slice ???? ???? (??????? ????) ??????
?????? ????? ????? ?????? (epoch) ???????
17?????? ???????? ?-scheduler (1)
- ?????? ???????? ??? ???? ????? ?????? Linux
?????? ?? ????? ?????? ??? ????? - ??? ???? ???????? schedule()
- ???? ???? ?? ?????? ??? ??? ????? ?? ???????
????? ????? context_switch() ????? ????? - ???? ????? ????? ????? ?????????
- ?????? ?? ????? ???? ???? ?????? ???? ?? ?-time
slice ??? (?????? ????? scheduler_tick()) - ?????? ?????? ????? ?????? ?????? ????? ?????
??????, ?? ?????? ????? ????? ????? ??? (?????
?????? sleep_on()) - ????? ?? ????? ?????? ?? ???? ?? ??????
?-runqueue ?????? ?? ?????? ??? ?? ???? ?????
???? (????? ??? wake_up()) - ???? ????? ????? ????? ?? ????? ?????? ??????
????? ???? sched_yield()
18?????? ???????? ?-scheduler (2)
- ??? ???????? ????? ???? ?????? ?????? ??????
- ?? ????? ????? ?????? ????? ?? ???? ?????? ?????
???? - ????? ???? ????? ???? (direct invocation) ??????
??-??? ????? ?-schedule() - ???? ???? ???????? ?????
- ???? ????? ?? ????? ???? (lazy invocation) ??-???
????? ??? need_resched ????? ?????? ???? ????
?-kernel mode ?-user mode - ??? ???? ?????? ???? ???? ?? ???? ??"? ???? ???
?? ????? ?? schedule() - ????? ???????? ????? ????? ?????? ?????, ?? ????
?-time slice ?? ??????, ????? scheduler_tick()
?????? ?? ???? need_resched ????? ??????
19???????? schedule() (1)
prev ????? ????? ?????? ?????? (?????? ??
?????) rq ???? ?? ?-runqueue ?? ????? ??????
- prev current
- rq this_rq()
- ..
- spin_lock_irq(rq-gtlock)
- switch(prev-gtstate)
- case TASK_INTERRUPTIBLE
- if (signal_pending(prev))
- prev-gtstate TASK_RUNNING
- break
-
- default
- deactivate_task(prev, rq)
- case TASK_RUNNING
-
????? ???????
?? ????? ???? ???? ????? ???? ???? ?-runqueue
???? ??? ?? ????? ??????
20???????? schedule() (2)
- if (!rq-gtnr_running)
- next rq-gtidle
- rq-gtexpired_timestamp 0
- goto switch_tasks
-
- array rq-gtactive
- if (!array-gtnr_active)
- rq-gtactive rq-gtexpired
- rq-gtexpired array
- array rq-gtactive
- rq-gtexpired_timestamp 0
?? ??? ???? ??????? ?????? ???? ?-runqueue, ?????
????? ?-swapper
?? ?? ????? ??????? ?-active array, ??????? ???
?-active ??-expired (???? ????? ?????? ????? ????)
21???????? schedule() (3)
- idx sched_find_first_bit(array-gtbitmap)
- queue array-gtqueue idx
- next list_entry(queue-gtnext, task_t, run_list)
- switch_tasks
- clear_tsk_need_resched(prev)
- if (prev ! next)
- ..
- rq-gtcurr next
- prev context_switch(prev, next)
- ..
- spin_unlock_irq(rq-gtlock)
-
- else
- spin_unlock_irq(rq-gtlock)
?????? ??? ?????? ????? (next) ??? ?????? ??????
???? ?-active array ??? ??????? ?????? ?????
????? ????? ????? ?"? ????? ?-context_switch()
???? ?? ???? ?? ????? ??????? ???? ???? ??????
?-context_switch()?
????? ?????? next
22????? ????? ?? ????? ?????
- ???????? scheduler_tick() ?????? ??? ????? ????
- ?????? ?? ????? ?????? ?? ?????? ?????? ???
????? - ????? ??????? ?????? ??????? ????? ?????????
- "??? ????? ?????"
- ????? ?-time_slice, ???????
- ????? ???? ?? ??????? ??????? ????? ????? ??????
- ????? time_slice ???
- ????? ????? ??????????? ?????? ?????? ?????
?-expired ?? ??? ?-active - ???? ?????? ??????? need_resched
23????? ??????? ????? (1)
- Linux ????? ?? "??? ?????? ??????" ?? ????? ????
sleep_avg ????? ?????? - "??? ????? ?????" ?? ?? ??? ?????? ?????
??????? ???? ?? ?? ??? ????? - ??? ??? ?????? ????? ?? ?????, ??? ????? ????
(??????? schedule()) - p-gtsleep_timestamp jiffies
- ???? ????? ???? ?????? ???? ??? ?????? ??????
(??????? activate_task()) ?? ????? ???????
MAX_SLEEP_AVG - define MAX_SLEEP_AVG (2HZ)
- sleep_time jiffies p-gtsleep_timestamp
- p-gtsleep_avg sleep_time
- if (p-gtsleep_avg gt MAX_SLEEP_AVG)
- p-gtsleep_avg MAX_SLEEP_AVG
24????? ??????? ????? (2)
- ?? ????? ???? ?? ?????? ?? ?????? ???????
(???????? sheduler_tick()) ?? ???????? 0 - if (p-gtsleep_avg)
- p-gtsleep_avg--
- ??-?? ???? ????? ??
- ????? ???? ????? ???? ????? ?"??? ????? ?????"
???? - ????? ???? ?/? ???? ????? ?"??? ????? ?????" ????
25????? ????? ?? ?????? ????? (1)
- Linux ?????? ?? ??????? ?? ?? ????? "????" ?????
????? ????? ?"??? ?????? ??????" ?? ??????
sleep_avg - ???????? ?????? ???-??? ??????? ???? ?????
- ????? ??????? ??????? ????? ????????
effective_prio() ??? ????????? ???
prio static_prio bonus if (prio lt
MAX_RT_PRIO) prio MAX_RT_PRIO if (prio gt
MAX_PRIO - 1) prio MAX_PRIO 1
26????? ????? ?? ?????? ????? (2)
- ????? ???? ?-bonus ?-5 ????? ????? ??? ??? ?????
????? ???????? ??? ??????? ???????????? ????????
(???????) ?????? ?? ??? - ????, ?????? ??? nice 19 (?????? 139) ????? ?????
???? ?????? ??? nice 0 (?????? 120) - ???? ????? ?? ????? ?? ??????? ?? ????? ?"?????
????" (???? ???????? ????? ?/?) ????? ?? ???????
?? ????? ?"????? ???" (???? ???????? ????? ?????) - ???? ?? ????? MAX_SLEEP_AVG, ????? HZ ?????? ??
???? ???, ???? ???? ??? ??? "????? ?????" ?"?????
?????"
27????? ????? ?? ?????? ????? (3)
- ????? ???? ?? ??????? ??????? ????? ?? ???? ?????
???? time slice ?? ???? ????? ???? ?????? - ????? ????? ?????? ????? ?????? ????? ???? ????
?? ???? ???? ?????? ?? ??????? ?????
28????? time slice ?????? (1)
- time slice ????? ?????? ?????? TASK_TIMESLICE
define MIN_TIMESLICE (10 HZ / 1000) / 10
msec / define MAX_TIMESLICE (300 HZ /
1000) / 300 msec / define TASK_TIMESLICE(p)
\ MIN_TIMESLICE (MAX_TIMESLICE
MIN_TIMESLICE) (MAX_PRIO 1
(p)-gtstatic_prio)/39
- ?? ????? ???? ????? 10 msec ??? ???? ????
???????? ??????? ?????? ?? ?? ???? ?-300 msec - ????? ??????? "?????" (120) ???? ??? ?????? 150
msec - ????? time slice ???? ????? ?? ??? ?? ???? ?-time
slice ?????? ??? ????? ???????? scheduler_tick()
????? - ?????, ?? ????? ???? ?????? ????? ??????? ?????
????? ????, ???? ????? ????? ?? ????? ?-time
slice ??? ???? ????? ????? ????
29????? time slice ?????? (2)
- ?????? ????? ???, ????? ??? ???? ????? ??-time
slice ??? ????? ????? ??? - ????? ????? ??? ?? ????? ???? ??????? ??? ????
???? ???? ?????, ???? ????? ??? - ???? ???? ?-time slice, ?????? ????? ????? ??
?????? ????? ?? ???? ???? time slice ???? ?????
????? - ???? ??-time slice ?? ??? ??????, ??? ????? ???,
??? ????.. - ????? ??????? ???????? do_fork() ?????? ?? fork()
- ???? ????? kernel/fork.c
- p-gttime_slice (current-gttime_slice 1) gtgt 1
- current-gttime_slice gtgt 1
30??????? ????????????? (1)
- ??????? ????????????? ?????? ?-Linux ?????
?????? ???? ???? time slices ?????? ??????
??????? - ??????? ????????????? ?? ??????? ???????? ????
???????, ?????? shell, ????? ??????? ???'
?????? ?????? ?????? - ????? ?????? ??????? ????????????? ??? ????? ???
????? ????? ????? ?? ?????? ?????? ????? ????
?????? ?????? ??? ??????? - ????? ??????? ????????????? ??? ????? ????? ???
???????? ??-????????????? ?????? ?? ?-time slice
?????? ??????? "??????", ????? ?? ????? ??? ????
???????
31??????? ????????????? (2)
- ??? ????? ?? ????? ??????, ????? "?? ?????" ??
??? ?????? ?? ???????? ?-expired - define EXPIRED_STARVING(rq) \
- ((rq)-gtexpired_timestamp \
- (jiffies (rq)-gtexpired_timestamp gt \
- STARVATION_LIMIT ((rq)-gtnr_running 1))
- ???? ???????? ?-expired ??????? ???? ???, ??????
????? time slices ?????? ???????? ?????????????
?????? ??????? - ??? ?????? ??????????? ????? ???????? ?-runqueue,
?? ???? ??? ???? ??????? ?????? ?????, ??? ????
???? - ??? ?????? ?????? ???? ????, ???????
????????????? ?????? ???? ????? ?? ??? ????? ??
?????? ????? ????? ??????? ??????
32??????? ????????????? (3)
- ????? ????? ???????????? ????? ??????
TASK_INETRACTIVE - define TASK_INTERACTIVE(p) \
- ((p)-gtprio lt (p)-gtstatic_prio DELTA(p))
- ???? DELTA(p) ????? ?????? ??????
- ????? ????? ???????????? ?? ??????? (???????) ???
"?????" ???????? ?????? ??? - ????? ?? ??? ????? ????? ???? ????? ????? ??????
??????? ??????? ?? ??????
33??????? ????????????? (4)
- ??? ????? ??????? ?????? ?? ??????, "?? ????"
?????? ??????? ???????????? - "?? ????" ?????? ???? ????? ??? ????? ????? ????
???? ??-??? ?????? ???????????? - ?????? ????? ?? nice -20 ???? ????? "????"
?????? ????? ?????? ??????? ?????? ?? ????? ?????
-3 ?????? ??????? ????????????. ????? ???, ?????
??nice 19 ?? ???? ??? ??????? ???????????? ??
???? ????? ?? 7 ??????? ???????? ??? 5 - ?????? ??????? ???? ?????? ????? ????? ??
????-??? ?????? ????? (batch) ????? ??????
?????????? ?? ?????? - ??????? ??????? ??????, ????? ???????, ???'
34???????? scheduler_tick() (1)
- ??????? ?? ?????? ??? ????? ???? ??????? ?????
????? ?? ??????? - ?? ??????? ?? ?????? ????????? ?????? ??? ?????
????? ?????? ?"? ????? ???????? ?????? - ???????? ????? ?????? schedule() ?? ?????? ??????
???? ?? ?-time slice ??? - ???????? ?????? ????????? enqueue_task()
?-dequeue_task() ?????? ?????? ?? ????? ????
?????? ?????? (active ?? expired) - task_t p current
- runqueue_t rq this_rq()
- ..
- if (p-gtsleep_avg)
- p-gtsleep_avg--
????? ?-sleep_avg ?? ?????? ??????
35???????? scheduler_tick() (2)
- if (!--p-gttime_slice)
- dequeue_task(p, rq-gtactive)
- set_tsk_need_resched(p)
- p-gtprio effective_prio(p)
- p-gttime_slice TASK_TIMESLICE(p)
- if (!TASK_INTERACTIVE(p) EXPIRED_STARVING(rq))
- if (!rq-gtexpired_timestamp)
- rq-gtexpired_timestamp jiffies
- enqueue_task(p, rq-gtexpired)
- else
- enqueue_task(p, rq-gtactive)
?? ?????? ?????? ???? ?? ?-time slice ?? ????,
??? ???? ??-active, ??????? ??????? ??-time slice
??? ??? ??????? ????, ?????? ?? schedule() ?????
???? ?????? ?????? ?????? ??????? ???? ?-expired,
?????? expired_timestamp
????? ??????????? ????? ?-active ???? time slice
???? ?????? ??????? ???? ??? ?????