Embedded Systems: a Contemporary Design Tool James Peckol
Total Page:16
File Type:pdf, Size:1020Kb
OS? Based on Embedded Systems: A Contemporary Design Tool James Peckol and EE472 Lecture Notes Pack Blake Hannaford, James Peckol, Shwetak Patel CSE 466 Tasks And Scheduling 1 Why would anyone want an OS? Goal: run multiple programs on the same HW “simultaneously” i.e. multi-tasking…it means more than surfing Facebook during lecture Problem: how to share resources & avoid interference To be shared: processor, memory, GPIOs, PWM, timers, counters, ADCs, etc In embedded case, we may need to do the sharing while respecting “real time” constraints OS is responsible for scheduling the various jobs Also: OS provides abstractions of HW (e.g. device drivers) that make code more portable & re-usable, as well as enabling sharing Code re-use a key goal of ROS (“meta-operating system”) Power: maintain state across power loss, power aware scheduling CSE 466 Tasks and Scheduling 2 Tasks / Processes, Threads Task or process Unit of code and data… a program running in its own memory space Thread Smaller than a process A single process can contain several threads Memory is shared across threads but not across processes Task 1 Task 2 Task 3 Ready Running Waiting With just 1 task, it is either Running or Ready Waiting CSE 466 Tasks and Scheduling 3 Types of tasks Periodic --- Hard real time Control: sense, compute, & generate new motor cmd every 10ms Multimedia: sample audio, compute filter, generate DAC output every 22.73 uS Characterized by P, Period C, Compute time (may differ from instance to instance, but C<=P) D, Deadline (useful if start time of task is variable) C < D < P Intermittent Characterized by C and D, but no P Background Soft realtime or non-realtime Characterized by C only Complex Examples MS Word, Web server Continuous need for CPU Requests for IO or user input free CPU CSE 466 Tasks and Scheduling 4 Scheduling strategies Multiprogramming Running task continues until a stopping point (e.g. waiting for an IO event) Real-time Tasks must be completed before deadline Time sharing Running task gives up CPU Cooperative multitasking App voluntarily gives up control Old versions of Windows & Mac OS Badly behaved apps hang the system Preemptive multitasking HW timer preempts currently executing task, returns control to OS All versions of Unix Power aware Research topic CSE 466 Tasks and Scheduling 5 Context State must be saved / restored to switch between tasks Program Counter (PC) Register values Processor status flags (Status Register) Stack Pointer (SP) Memory state Peripheral configurations Etc CSE 466 Tasks and Scheduling 6 Task states in a time-sharing system Blocked/ Waiting Enter Ready Running Waiting Exit CSE 466 Tasks and Scheduling 7 Memory resource management Address space Each process has a range of addresses it’s allowed to use Privilege level Supervisory / kernel mode User mode Interrupt generated when a user process tries to operate outside its Supervisory Mode address space Address Space “General protection fault” in x86 Supervisor User Mode Mode access Address Space User mode access CSE 466 Tasks and Scheduling 8 Task Control Block (TCB) Task Control Block Pointer State Process ID Program Counter Register contents Memory limits Open Files Etc. Also: scheduling information, memory management information, I/O status info CSE 466 Tasks and Scheduling 9 Task Control Block (TCB) // The task control block struct TCB { void (*taskPtr)(void* taskDataPtr); void* taskDataPtr; void* stackPtr; unsigned short priority; struct TCB* nextPtr; struct TCB* prevPtr; }; taskPtr is a pointer to a function The function’s param list has one arg, of type void* stackPtr: each task has its own stack Priority: what is the priority level of this task? nextPtr & prevPtr: pointers to other TCBs CSE 466 Tasks and Scheduling 10 Scheduling CSE 466 Tasks and Scheduling 11 Time (for RTOS) Time slice T, Ticks Pmin, shortest period of all tasks in system T < Pmin, sometimes T << Pmin CSE 466 Tasks and Scheduling 12 Scheduling goals CPU Utilization UCPU = 1 – idle / period In mainframe, 100% is best, but 100% not safe for realtime systems Goal: 40% low load, 90% high load Throughput Turnaround time Waiting time Response time CSE 466 Tasks and Scheduling 13 Scheduler types Infinite loop, aka non-preemptive Round Robin while(1) { task1_fn(); task2_fn(); task3_fn(); } taskN_fn() { compute a little bit; return(); } CSE 466 Tasks and Scheduling 14 Scheduler types Synchronized Infinite loop Top of loop waits for a HW clock while(1) { wait(CLOCK_PULSE); task1_fn(); task2_fn(); task3_fn(); } CSE 466 Tasks and Scheduling 15 Scheduler types Preemptive round robin AKA cyclic executive All processes handled without priority Starvation free CSE 466 Tasks and Scheduling 16 Scheduler types Preemptive priority based Goal in non-RT OS is to allocate resources equitably…no process should perpetually lack necessary resources Attach priorities to each process Problem: priority inversion A is highest priority process. It is blocked waiting for a result from C B is 2nd highest priority. It never blocks C is 3rd highest priority Now B runs all the time and A never gets to…their priorities are effectively inverted…A is starved Problem: deadlock Catch 22 / Chicken - Egg: A is waiting for B, but B is waiting for A One person has the pencil but needs the rule, the other has ruler but needs pencil You can’t make coffee until you’re alert…but you’re not alert until you’ve had coffee Ways to avoid priority inversion Make sure every job gets a minimum time slice Priority inheritance Does not prevent deadlock when there are circular dependencies CSE 466 Tasks and Scheduling 17 Scheduler types Preemptive priority based Rate monotonic scheduling (RMS), for RTOS Static priorities set based on job cycle duration---shorter job gets scheduled more often Provide deterministic guarantees about response times (show using Rate Monotonic Analysis) Where Ci is compute time Ti is release period n is # processes to be scheduled Roughly, RMS can meet deadlines when CPU < 69% used CSE 466 Tasks and Scheduling 18 End CSE 466 Tasks and Scheduling 19 Real-Time OSes and their communities Linux Sensor networks RTLinux TinyOS RTAI Contiki Xenomai Computational RFID Commercial Dewdrop LynxOS MementOS QNX Robotics [“meta OSes,” VxWorks on top of Linux] Windows CE ROS iRMX for Windows Player / Stage OSE Carmen Embedded systems FreeRTOS C/OS-II CSE 466 Tasks and Scheduling 20 RTLinux Hard realtime RTOS microkernal runs entire Linux OS as a preemptive process Real time OS is virtual machine “host OS” …Linux kernal runs as “guest OS” Interrupts for realtime processing handled by realtime core Other interrupts forwarded to Linux, handled at lower priority than realtime interrupts Acquired by WindRiver, sold as Wind River Real-Time Core for Wind-River Linux CSE 466 Tasks and Scheduling 21 RTAI & Xenomai (Real time Linux) RTAI==Real Time Application Interface Provides deterministic response to interrupts Kernel patch allows RT system to take over key interrupts, leaves ordinary Linux to handle others No patent restrictions (vs RTLinux) Lowest feasible latencies Xenomai Emphasizes extensibility rather than lowest latency CSE 466 Tasks and Scheduling 22 C/OS-II www.ucos-ii.com Kernal only…supports Scheduling Message passing (mailboxes) Synchronization (semaphores) Memory management Supports 64 priority levels…runs highest priority first Does not support: IO devices, Files, networking Versions mC/GUI mC/USB-Bulk mC/USB-MSD [for Mass Storage Devices] CSE 466 Tasks and Scheduling 23 FreeRTOS http://www.freertos.org/ Another realtime kernal Many features similar to C/OS-II Supports both tasks and co-routines A co-routine does not have its own stack Smaller memory footprint, more efficient Restrictions on how/when to call etc required Versions OpenRTOS Commercial, supported SafeRTOS Documented for safety critical applications CSE 466 Tasks and Scheduling 24 Contiki and TinyOS See Contiki slides More info: http://www.sics.se/contiki/wiki/index.php/Main_Page CSE 466 Tasks and Scheduling 25 DewDrop Energy-aware runtime (scheduler) for computational RFID Interesting to compare power aware scheduling to RTOS (“time-aware scheduling”) CSE 466 Tasks and Scheduling 26 ROS Robot Operating System Meta-operating system See ros_overview.pdf ros_tutorial.pdf CSE 466 Tasks and Scheduling 27 CSE 466 Tasks and Scheduling 28 Inter-task communication Shared variables Global variable Shared buffer: producer & consumer Task T0 Task T1 Problems: mismatch in filling & emptying rates can lead to over- or underflow Solution: always check empty / full before reading / writing CSE 466 Tasks and Scheduling 29 Inter-task communication Shared variables Shared double buffer (ping pong buffer) Task T0 Task T1 One buffer is being filled while the other is being emptied (also used for displays / graphics!) Can generalize to n buffers…may be useful when producer generates data in fast short bursts CSE 466 Tasks and Scheduling 30 Inter-task communication Shared variables Task T0 Ring buffer head An implementation of a queue, used xx D0 to let 2 processes communicate xx D1 FIFO (First In First Out) xx D2 Need to avoid under/overflow xx D3 tail Task T1 D0 – D3: valid data xx: junk CSE 466 Tasks and Scheduling 31 Inter-task communication Shared variables Mailbox Interface post(mailbox, data) // post to mailbox pend(mailbox, data) // pend on mailbox Task T0 Task T1 pend post