Frodo could see them, small black figures in rank upon rank, marching swiftly and silently, passing outwards in an endless stream.
—The Two Towers, Book IV, Chapter 8
Linux's real-time scheduling classes are for processes that require a great deal of control over how they execute. They can be used to define programs that execute in very specific ways, and are even used preempt the operating system kernel.
In this studio, you will:
Please complete the required exercises below, as well as any optional enrichment exercises that you wish to complete.
As you work through these exercises, please record your answers, and when finished email your results to dferry@email.wustl.edu with the phrase Real-Time Scheduler in the subject line.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
NOTE: Modern compilers are smart enough to optimize your loop away.
Turn off optimizations for this exercise. With no optimization,
on the 900 MHz Raspberry Pi 2, the instructor's program
runs for about five seconds. For -O1 it runs for about a half second. For
any higher optimization level the program returns immediately. Use the
time
command to ensure that your program runs for around five
seconds.
trace-cmd
command to record sched_switch
events during an execution of your program. Recall that the synatx for
this is "sudo trace-cmd -e sched_switch ./your_program arg1 arg2 ...
". Then use Kernelshark to inspect the trace.
Examine how your program executes. List three processes that interefere
with the execution of your program. Make a copy of your trace to inspect later-
next time we run trace-cmd
it will overwrite this one.
To do so, you should use the function sched_setscheduler()
that is documented at man sched_setscheduler
. The required
data structure, sched_param
, is also documented at that page.
Be sure to check the return value from this function for success, and print
out an appropriate error message in the event of failure.
trace-cmd
with a real-time
priority of one.
Inspect the output in Kernelshark. What do you notice? Do any processes
preempt your program? If so, which ones?
Filter
menu and select
list CPUs
. How many sched_switch
events were
recorded on this CPU?
Filter your list to a different CPU. How many sched_switch
events were recorded there?
ps -e -o cmd,rtprio
to get a list of
all processes on the system and their real-time priorities. A dash in the
priority column means that this process does not have a real-time priority.
Which real-time priorities do you see used? Give two processes with a real-time priority and speculate why they deserve a real-time priority.
sched_switch
events occur on your program's processor? Is your
program ever preempted? If so, when and where is it preempted?
fork()
function. This function should be called
AFTER you have set the program's
real-time priority, but BEFORE you start executing your work loop.
SCHED_FIFO
and (much more recently) SCHED_DEADLINE
.
Under the former policy, tasks are allowed to run to completion or until they
give up the processor with sched_yield()
. Under the latter policy,
tasks are given deadlines for completion, and scheduled in order to best meet
those deadlines. Try them out!