Running Real-Time Tasks in Linux (x86 and ARM) - - PowerPoint PPT Presentation
Running Real-Time Tasks in Linux (x86 and ARM) - - PowerPoint PPT Presentation
Running Real-Time Tasks in Linux (x86 and ARM) Sisu Xi CSE 467S, Spring, 04/14/2014 Real-Time Tasks Task: a sequence of jobs
Real-‑Time ¡Tasks ¡
Ø Task: ¡a ¡sequence ¡of ¡jobs ¡
q
Period ¡
q
Worst ¡case ¡execuEon ¡Eme ¡
q
Deadline ¡(usually ¡equals ¡period) ¡
Ø Scheduler ¡
q
StaEc ¡priority: ¡RM ¡
q
Dynamic ¡priority: ¡EDF ¡
q
PreempEve ¡vs. ¡non-‑preempEve ¡
2 ¡
Time 5 10 15
A real time task with period of 5, execution time of 2, and deadline of 4
schedulable deadline miss
- 1. ¡Periodically ¡triggered ¡tasks? ¡
- 2. ¡Worst ¡case ¡execu7on ¡7me? ¡
- 3. ¡Scheduler ¡setup? ¡
- 4. ¡Deadline ¡miss? ¡
- 1. ¡Periodically ¡Triggered ¡Task ¡
Ø Video ¡decoding, ¡sensor ¡processing, ¡etc. ¡
Ø hTp://www.cse.wustl.edu/~xis/programs/period_task.c ¡
Ø Many ¡other ¡approaches, ¡see ¡pointers ¡
3 ¡
struct sigaction sa; … sa.sa_sigaction = work; sigaction(SIGRTMIN, &sa, NULL); … struct sigevent timer_event; timer_event.sigev_signo = SIGRTMIN; … timer_create(CLOCK_REALTIME, &timer_event, &timer); timer_settime(timer, TIMER_ABSTIME, &timerspec, NULL); …
- 2. ¡Workload ¡for ¡Tasks ¡– ¡Setup ¡ ¡
Ø Minimize ¡interference/uncertainEes ¡
q
Turn ¡off ¡unnecessary ¡services ¡
- Bluetooth, ¡network, ¡graphic, ¡etc ¡
q
Fix ¡CPU ¡frequency ¡
- CPU ¡frequency ¡scaling ¡
hTps://wiki.archlinux.org/index.php/CPU_Frequency_Scaling ¡
- CPU ¡frequency ¡governors ¡
hTp://www.mjmwired.net/kernel/DocumentaEon/cpu-‑freq/ governors.txt ¡ ¡
- Disable ¡in ¡BIOS ¡(SpeedStep ¡for ¡Intel, ¡PowerNow ¡for ¡AMD) ¡
- Set ¡to ¡Performance/Custom/PowerSave ¡
§
ls ¡/sys/devices/system/cpu/cpu0/cpufreq/ ¡
§
echo ¡performance ¡> ¡/sys/devices/system/cpu/cpu0/cpufreq/ governors ¡
4 ¡
- 2. ¡Workload ¡for ¡Tasks ¡– ¡Programs ¡ ¡
Ø Simulated ¡workload ¡
q
WCET ¡benchmarks ¡ hTp://www.mrtc.mdh.se/projects/wcet/benchmarks.html ¡
q
SNU ¡real-‑Eme ¡benchmarks ¡ hTp://www.cprover.org/goto-‑cc/examples/snu.html ¡
q
MiBench ¡benchmarks ¡hTp://www.eecs.umich.edu/mibench/ ¡
q
Video ¡player: ¡“Sod ¡Real-‑Time ¡on ¡MulEprocessors: ¡Are ¡Analysis-‑ Based ¡Scheduler ¡Really ¡Worth ¡It?” ¡[RTSS’11] ¡ hTps://wiki.litmus-‑rt.org/litmus/PublicaEons ¡
q
CPU ¡intensive ¡workload ¡
- Measure ¡workload ¡for ¡1 ¡ms, ¡then ¡scale ¡to ¡any ¡milliseconds ¡workload ¡
- Note: ¡need ¡to ¡tune ¡for ¡each ¡individual ¡machine ¡
5 ¡
- 3. ¡Scheduler ¡Setup ¡– ¡Basic ¡ ¡
Ø ParEEoned ¡run ¡queue, ¡holds ¡pending ¡jobs ¡ hTp://oreilly.com/catalog/linuxkernel/chapter/ch10.html ¡ ¡ Ø Two ¡classes, ¡would ¡always ¡schedule ¡RT ¡class ¡first ¡
q
RT ¡class: ¡staEc ¡priority, ¡1 ¡(lowest) ¡to ¡99 ¡(highest) ¡
- SCHED_FIFO, ¡SCHED_RR ¡
- Can ¡be ¡used ¡to ¡implement ¡staEc ¡priority ¡(like ¡rate ¡monotonic) ¡
- SCHED_DEADLINE ¡since ¡Linux ¡3.14 ¡
- Reserve ¡5% ¡for ¡other ¡classes ¡
§
/proc/sys/kernel/sched_rt_period_us ¡ ¡ ¡ ¡ ¡1000000 ¡
§
/proc/sys/kernel/sched_rt_runEme_us ¡ ¡ ¡ ¡950000 ¡
q
Non-‑RT ¡class: ¡SCHED_OTHER ¡with ¡Complete ¡Fair ¡Scheduler ¡
6 ¡
- 3. ¡Scheduler ¡Setup ¡– ¡Preemptive ¡ ¡
Ø Scheduler ¡is ¡triggered ¡every ¡HZ ¡quantum ¡ Ø cat ¡/boot/config-‑* ¡| ¡grep ¡CONFIG_HZ ¡
q
For ¡most ¡desktops, ¡value ¡is ¡1000. ¡Ecked ¡every ¡1ms ¡
q
For ¡arm ¡processors, ¡value ¡is ¡usually ¡100. ¡Ecked ¡every ¡10 ¡ms ¡
Ø CONFIG_NO_HZ ¡= ¡y ¡ ¡ Ø CONFIG_HIGH_RES_TIMERS ¡= ¡y ¡ hTp://elinux.org/High_ResoluEon_Timers ¡ ¡ Ø Can ¡recompile ¡kernel ¡to ¡change ¡these ¡values ¡
7 ¡
- 3. ¡Scheduler ¡Setup ¡– ¡Priorities ¡ ¡ ¡
Ø chrt ¡command ¡(can ¡also ¡check ¡task ¡prioriEes) ¡[link] ¡
q
sudo ¡chrt ¡–f ¡–p ¡99 ¡4800 ¡ ¡ ¡ ¡ ¡# ¡pid ¡4800 ¡with ¡priority ¡99 ¡and ¡fifo ¡
Ø sched_scheduler ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡[link] ¡ ¡ ¡
8 ¡
#include <sched.h> int main() { … struct sched_param sched; sched.sched_priority = 99; if (sched_setscheduler(getpid(), SCHED_FIFO, &sched) < 0) { exit(EXIT_FAILURE); } … }
- 3. ¡Scheduler ¡Setup ¡– ¡AfJinities ¡
Ø taskset ¡command ¡(can ¡also ¡check ¡task ¡affiniEes) ¡ ¡[link] ¡
q
sudo ¡taskset ¡-‑c ¡2,3 ¡4800 ¡ ¡ ¡ ¡ ¡# ¡pid ¡4800 ¡runs ¡on ¡cores ¡2-‑3 ¡
Ø sched_setaffinity ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡[link] ¡
9 ¡
#include <sched.h> int main() { … unsigned long mask = 1; if (sched_setaffinity(getpid(), sizeof(mask), &mask) < 0) { exit(EXIT_FAILURE); } … }
- 4. ¡Measure ¡Deadline ¡Miss ¡
Ø geymeofday() ¡ ¡ ¡[link] ¡
q
return ¡struct ¡Emeval, ¡includes ¡tv_sec ¡and ¡tv_usec ¡
q
not ¡ok ¡for ¡measuring ¡overhead ¡on ¡standard ¡kernel ¡configuraEon ¡
Ø rdtsc ¡ ¡ ¡ ¡[link] ¡
q
read ¡CPU ¡cycles ¡directly ¡(need ¡to ¡fix ¡CPU ¡frequency) ¡
q
cat ¡/proc/cpuinfo ¡to ¡get ¡CPU ¡frequency ¡
q
- n ¡a ¡3.33GHZ ¡CPU, ¡Ecks ¡3,330,000,000 ¡Emes ¡per ¡second ¡
- 3 ¡Eck ¡equals ¡1ns ¡
- if ¡you ¡use ¡rdtsc ¡to ¡record ¡Eme, ¡pay ¡aTenEon ¡to ¡this ¡value ¡
- cat ¡/proc/cpuinfo ¡ ¡ ¡ ¡ ¡# ¡get ¡CPU ¡frequency ¡
- cat ¡/sys/devices/system/clocksource/clocksource0 ¡# ¡get ¡current ¡clock ¡
10 ¡
Demo ¡
Ø /proc/cpuinfo ¡ Ø /proc/sys/kernel/sched_rt_* ¡ Ø /sys/devices/system/clocksource/clocksource0/ ¡ Ø /sys/devices/system/cpu/cpufreq ¡ Ø /sys/devices/system/cpu/cache/ ¡ Ø /boot/config* ¡ Ø /dev/shm ¡ Ø Compile ¡demo ¡task ¡(g++ ¡period_task.c ¡-‑lrt) ¡
11 ¡
Raspberry ¡Pi ¡
Ø ARM ¡based ¡Linux ¡box ¡ ¡ Ø Model ¡B ¡($35) ¡
q
700 ¡MHz, ¡single ¡core ¡
q
512 ¡MB ¡RAM ¡
12 ¡
Raspberry ¡Pi ¡– ¡CPU ¡ ¡
Ø cat ¡/proc/cpuinfo ¡
q
ARMv6 ¡compaEble ¡processor ¡rev ¡7 ¡(v61) ¡
Ø ls ¡/sys/devices/system/cpu/cpu0/cpufreq/ ¡
q
cur, ¡max, ¡min ¡freq ¡
q
scaling_governor ¡
Ø uname ¡–a ¡
q
Linux ¡raspberrypi ¡3.6.11+ ¡#371 ¡PREEMPT ¡Thu ¡Feb ¡7 ¡16:31:35 ¡GMT ¡ 2013 ¡armv6l ¡GNU/Linux ¡
q
Linux ¡Preempt-‑RT ¡patch: ¡hTps://rt.wiki.kernel.org ¡
- Minimize ¡scheduling ¡latency ¡
13 ¡
Raspberry ¡Pi ¡– ¡Clock ¡
Ø /sys/devices/system/clocksource/clocksource0/ available_clocksource ¡
q
stc, ¡sodware ¡simulated ¡clock, ¡Ecked ¡every ¡1 ¡microseconds ¡
Ø cat ¡/proc/config.gz ¡| ¡gzip ¡–d ¡| ¡grep ¡HZ ¡
q
CONFIG_NO_HZ ¡= ¡y ¡ ¡ ¡ ¡ ¡# ¡when ¡idle, ¡Eckles ¡to ¡save ¡energy ¡
q
CONFIG_HZ ¡= ¡100 ¡# ¡Eck ¡every ¡10 ¡ms ¡
14 ¡
Raspberry ¡Pi ¡– ¡Summary ¡
Ø Single ¡core ¡processor: ¡no ¡need ¡to ¡set ¡cpu_masks ¡ Ø Fixed ¡frequency: ¡no ¡need ¡to ¡change ¡frequency ¡governor ¡ Ø No ¡rdtsc ¡
q
Need ¡to ¡record ¡Eme ¡using ¡other ¡funcEons ¡calls ¡like ¡geymeofday, ¡ clock_geyme(), ¡etc ¡
q
Minimum ¡Eme ¡resoluEon ¡provided ¡is ¡1 ¡microseconds ¡via ¡stc ¡
q
Default ¡scheduling ¡Eck ¡is ¡10 ¡ms ¡(compared ¡to ¡1 ¡ms ¡on ¡Desktop) ¡
Ø Comes ¡with ¡Preempt-‑RT ¡patch ¡by ¡default ¡
q
Less ¡scheduling ¡latency, ¡more ¡responsive ¡to ¡I/O ¡tasks ¡
15 ¡
Demo ¡ ¡
Ø /sys/devices/system/clocksource/clocksource0/ available_clocksource ¡ Ø /proc/config.gz ¡| ¡gzip ¡–d ¡| ¡grep ¡HZ ¡ Ø /sys/devices/system/cpu/cpu0/cpufreq/ ¡ Ø /proc/cpuinfo ¡ Ø uname ¡–a ¡ Ø Compile ¡demo ¡task ¡ ¡
q
No ¡rdtsc, ¡need ¡to ¡replace ¡with ¡geymeofday() ¡
16 ¡
Note: ¡Be ¡careful ¡with ¡serial ¡cable! ¡
17 ¡
Pointers ¡
Ø Periodically ¡running ¡a ¡task ¡
q
hTp://www.cse.wustl.edu/~xis/programs/period_task.c ¡
q
hTp://man7.org/linux/man-‑pages/man2/Emer_create.2.html ¡
q
hTp://courses.cs.vt.edu/~cs5565/spring2012/projects/project1/posix-‑Emers.c ¡
q
hTp://www.embedded-‑linux.co.uk/tutorial/periodic_threads ¡
Ø Video ¡players ¡
q
hTps://wiki.litmus-‑rt.org/litmus/PublicaEons ¡
Ø Get ¡Eme ¡in ¡Linux ¡
q
geymeofday: ¡hTp://linux.die.net/man/2/geymeofday ¡
q
rdtsc: ¡hTp://www.mcs.anl.gov/~kazutomo/rdtsc.html ¡
Ø Fix ¡CPU ¡frequencies ¡
q
hTp://www.mjmwired.net/kernel/DocumentaEon/cpu-‑freq/governors.txt ¡
q
hTps://wiki.archlinux.org/index.php/CPU_Frequency_Scaling ¡
18 ¡
Pointers ¡
Ø Linux ¡schedulers ¡
q
hTps://www.kernel.org/doc/DocumentaEon/scheduler/ ¡
q
hTp://oreilly.com/catalog/linuxkernel/chapter/ch10.html ¡
Ø Set ¡priority ¡
q
chrt: ¡ hTp://www.cyberciE.biz/faq/howto-‑set-‑real-‑Eme-‑scheduling-‑priority-‑process/ ¡
q
sched_scheduler: ¡hTp://linux.die.net/man/2/sched_setscheduler ¡
Ø Set ¡CPU ¡affinity ¡on ¡mulE-‑core: ¡
q
taskset: ¡hTp://linux.die.net/man/1/taskset ¡
q
Check ¡freq: ¡hTp://www.advenage.com/topics/linux-‑Emer-‑interrupt-‑frequency.php ¡
Ø Linux ¡real-‑Eme ¡patches: ¡
q
Linux ¡Preempt ¡RT: ¡hTps://rt.wiki.kernel.org/index.php/Main_Page ¡
q
LITMUS-‑RT: ¡hTp://www.litmus-‑rt.org/ ¡
q
RTAI: ¡hTps://www.rtai.org/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
q
SCHED_DEADLINE: ¡hTp://gitorious.org/sched_deadline ¡
19 ¡
Pointers ¡
Ø Raspberry ¡Pi ¡
q
hTp://www.raspberrypi.org/ ¡
q
tutorials: ¡hTp://www.rpitodo.com/ ¡
q
with ¡sensors: ¡hTp://www.raspberrypi.org/archives/tag/sensors ¡
q
clock: ¡ hTp://blog.remibergsma.com/2013/05/12/how-‑accurately-‑can-‑the-‑ raspberry-‑pi-‑keep-‑Eme/ ¡
q
source ¡code: ¡hTps://github.com/raspberrypi/linux ¡
q
recompile ¡kernel: ¡hTp://elinux.org/RPi_Kernel_CompilaEon ¡
20 ¡