What is an RTOS and Why Use One Embedded.com
description
Transcript of What is an RTOS and Why Use One Embedded.com
-
What Is an RTOSand
Why Use One?Why Use One?May, 2013May, 2013
-
What is an Embedded System? Dedicated to a specific purpose Components:
Microprocessor Application program
Real Time Operating S stem (RTOS) Real-Time Operating System (RTOS) RTOS and application programs usually stored
in ROMO Deterministic
2 2013, Express Logic, Inc.
-
The First Embedded System?The first embedded system was probably developed in 1971 b th I t l1971 by the Intel Corporation which produced the 4004 microprocessor e 00 c op ocessochip for a variety of business calculators. The same chip was used for allsame chip was used for all the calculators, but software in ROM provided unique
Maximum clock speed was 740 kHz Instruction cycle time: 10.8 s[11] (8 clock cycles / instruction cycle) Instruction execution time 1 or 2 instruction cycles (10.8 or 21.6 s), 46300 to 92600 instructions per second Separate program and data storage. Contrary to Harvard architecture designs, however, which use separate buses, the 4004, with its need to keep pin count down, used a single multiplexed 4-bit bus for t f i
p qfunctionality for each calculator.
transferring: 12-bit addresses 8-bit instructions 4-bit data words
Instruction set contained 46 instructions (of which 41 were 8 bits wide and 5 were 16 bits wide) Register set contained 16 registers of 4 bits each Internal subroutine stack 3 levels deep.
3 2013, Express Logic, Inc.
-
Real Time Systems
Must respond to inputs or events within prescribed time limitsp
Must operate within specified time constraints Important subclasses of Real Time:
Hard Real Time Must meet deadlines 100% of the time Generally true of safety-critical systems Generally true of safety-critical systems
Soft Real Time Must meet deadlines under normal conditions
G f Generally true of consumer electronics
4 2013, Express Logic, Inc.
-
Determinism Time required to complete any function must be
finite and predictable
Maximum response time must be calculable and guaranteed
Number of cycles required to execute a given operation must always be the same
Execution can be interrupted, but interrupt latency and processing time must be bounded
N t ll t i it Not all systems require it
Not all RTOSes deliver it
5 2013, Express Logic, Inc.
-
Real-Time Operating System
RTOS What is an RTOS?
Kernel File System RTOS
File System
USB, etc.
File System Networking USB Graphics System Graphics
Kernel Components Scheduler
Kernel
Graphics Networking
Thread Management Message Queues Semaphoresp Mutexes Timers Memory Poolsy
2013, Express Logic, Inc.
-
What Does An RTOS Do? Simplifies use of hardware resources
Allocate memoryoca e e o y Service interrupts Interface to devicesInterface to devices
Provides library of services Schedule application (activate/suspend/resume) Send/Receive messages Get/Release semaphores React to events
2013, Express Logic, Inc.
-
Why Use An RTOS?
RTOS Benefits1 Better Responsiveness and Lower Overhead1. Better Responsiveness and Lower Overhead2. Simplified Resource Sharing3 E i D l t d D b i3. Easier Development and Debugging4. Enabled Use of Layered Products5 I d P t bilit d M i t5. Increased Portability and Maintenance6. Faster Time To Market
2013, Express Logic, Inc.
-
Better Responsiveness and Lower Overhead
Non-RTOS application program must loop or poll to check for need to perform a function ie: process a received message Number of application functions determines time to poll Number of application functions determines time to poll Response to need for service depends on polling time Looping, checking, polling, state machine tracking all consume processor
cycles and add to overheadcycles and add to overhead
RTOS can context switch processor to required function and back RTOS performs context switch transparently to application
RTOS makes use of processor when application is waiting MultithreadingMultithreading
RTOS enables processor to spend more time in application Less time inefficiently managing application
2013, Express Logic, Inc.
-
Simplified Resource Sharing Some processor resources must be shared among functions
MemoryI/O Ports I/O Ports
Critical Sections of code
RTOS provides centralized mechanisms for arbitrating requests p g qfor resources Memory allocation/de-allocation at run-time Semaphores and Mutexes to control single-use hardware or critical p g
sections of software Preemption-Threshold to help manage access to Critical Sections
2013, Express Logic, Inc.
-
Easier Development and Debugging
Development team members can operate asynchronously
Application can be maintained more easily
Debugger can display RTOS kernel objects for increased visibility into application behavior
Applications can call service functions to perform operations rather than write and debug new code
Application developers can avoid dealing with many interrupt details, timers, and other hardware resources
2013, Express Logic, Inc.
-
Enabled Use of Layered Products Layered products often depend on RTOS services for
their operation File System TCP/IP Network Stack USB StackUSB Stack Graphics 3rd Party Products
2013, Express Logic, Inc.
-
Increased Portability and Easier Maintenance
Application talks to RTOS API, not specific hardware
A li ti h RTOS Application runs wherever RTOS runs
Modular applications easily expanded and modified
Commercial support for RTOS service functions
2013, Express Logic, Inc.
-
Who Doesnt Need An RTOS? Single-purpose applications
Si l l i lli li ti Simple looping or polling applications
Foreground-background applications
Typically,
-
Example: Non-RTOS Simple application with two functions
One sends a message to a buffer and checks for reply Second one checks for message and if found, replies
Main loop to sequence back and forth between the functions to see if they have any messages to send or have received a message
Each function must remember where it left off last time it ran (sent or received a message?)
Nothing else can be done while routines are working or waiting
2013, Express Logic, Inc.
-
Non-RTOS Example
Yes A No
Function1
No A No
Function2Main
Check for Message?
No
Yes Any message in queue?
Yes
Send Message?
Yes
No Any message in queue?
Yes
Call Function1
Send MessageGet message from queue Send Message
Get message from queue
Call Function2
Change state to check on
next run
Change state to send on next run
Change state to check on
next run
Change state to send on next run
Exit Exit
2013, Express Logic, Inc.
-
Example: RTOS Routines become RTOS threads
M t t t d t i d f Messages get sent to, and retrieved from a queue managed by the RTOS
Threads suspend if queue is empty
Processor is free to do other work while queue remains qempty
Threads automatically resumed at point ofThreads automatically resumed at point of suspension when message arrives in queue
2013, Express Logic, Inc.
-
RTOS Example
Thread_0Main Thread_1
Suspend Suspend
Send MessageStart Thread_0
Get message from queue
Suspend if queue
full Resume
when room
Suspend Suspend
Suspend if queue empty Resume
when msg
RTO
Get message from queue Send Message
if queue empty Resume
when msg
if queue full
Resume when room
SStart
Thread_1
Note no return
2013, Express Logic, Inc.
-
Processes, Tasks, and Threads
Process/Task Independent executable program with its own memory p p g y
space Multitasking: running several tasks/processes
concurrentlyconcurrently A Process can have multiple threads
Threadead Semi-independent program segment; multiple threads
can share the same memory spaceM ltith di i l th d tl Multithreading: running several threads concurrently
Some RTOSes use task to mean thread
19 2013, Express Logic, Inc.
-
Threads and Priorities Threads
What is a thread? Semi-independent program
segment
Process Process memory space
Share same memory space Run concurrently
How are threads used? Modularize a program
Mi i i ll Minimize stalls
Thread Services Create, Suspend, Relinquish,
Terminate, Exit, Prioritize
Thread States
ThreadThreadThread Thread States
READY, RUNNING, SUSPENDED, TERMINATED
Thread Priorities Often 0-n, with 0 highest
Highest
Priority01 Dynamic or Static
Equal priorities Multiple threads at same priority
Unique priorities Each thread has unique priority Lowest
12n Each thread has unique priority
2013, Express Logic, Inc.
-
Multithreading Enables one part of an application to use the CPU while another
part must waitM k ffi i t f CPU th iti Makes more efficient use of CPU than waiting
Foreground/Background or Multiple threads
Thread A Thread AI/O Start I/O Finish
TimeThread A
Waits
Thread B
Thread A has to wait for I/O
When I/O is done, Thread A can continue
While Thread A is waiting for I/O, Thread B can use CPU
2013, Express Logic, Inc.
-
Context Switch Thread Context
Information critical to threads operation Register Contents, Program Counter, Stack Registers
PCg g
Pointer Saved when thread is preempted Restored when thread is resumed
RegistersSP
Context Switch Interrupt running thread
and do something else
Step Operation Cycles
1 Save the current threads context (ie: GP and FP register values and PC) on the stack. 20 - 100
2 Save the current stack pointer in the thread's control block. 2 - 20
3 S it h t th t t k i t 2 20 Result of preemption, interrupt, or cooperative service
Whats involved in a
3 Switch to the system stack pointer. 2 - 20
4 Return to the scheduler. 2 - 20
5 Find the highest priority thread that is ready to run. 2 - 50
6 Switch to the new thread's stack. 2 - 50 What s involved in a context switch? See
7 Recover the new thread's context. 20 - 100
8 Return to the new thread at its previous PC. 2 - 40
9 Other processing 0 - 100
TOTAL 50 - 500TOTAL 50 500
2013, Express Logic, Inc.
-
Types of Schedulers Big Loop Scheduling
Each thread is polled to see if it needs to run Polling proceeds sequentially, or in priority order
Thread-1
? ?
?
g p q y, p y Inefficient, lacks responsiveness
Round-Robin Scheduling Cycle through multiple READY threads
Thread-2 Thread-4Big Loop Scheduler
?
?
?
Cycle through multiple READY threads Threads run to completion or blockage May impose time-slice for each thread
Preemptive Scheduling
Thread-3
Preemptive Scheduling Based on priority Performs context switches Manages thread states
Thread-1
Thread-2 Thread-4Round-Robin Manages thread states
Ready/Running Suspended (Blocked/Sleeping/Relinquished) Terminated
Thread-3
Scheduling
2013, Express Logic, Inc.
-
Preemptive Scheduling Preemption
Interruption for higher-priority activity Interrupt Thread
Preemptive Scheduling
Thread-2 Runs
P
r
i
o
r
i
t
y
Context Switch
Context Switch
Always run highest priority thread that is READY to run
Maximum
Thread-1 Begins
Thread-1 Resumes
P
Maximum responsiveness
No Polling, so more efficient
Time Always results in a
context switch
2013, Express Logic, Inc.
-
Preemptive Problems Thread Starvation
If a higher-priority thread is always ready the loweralways ready, the lower priority threads never execute Thread-2 Preempts
Thread-1 and Runs
r
i
t
y
Excessive Overhead From context switching See example
Thread-1 Begins
P
r
i
o
r
Thread-1 may never get to run again
Priority Inversion Higher-priority thread is
Time
Higher priority thread is suspended because a lower-priority thread has a needed resource
2013, Express Logic, Inc.
-
Priority Inversion Occurs when higher priority thread is
suspended because a lower priority thread has a needed resource
May be necessary for 2 threads of different May be necessary for 2 threads of different priorities to share a common resource
Priority inversion time may become un-deterministic and lead to application failure
26 2013, Express Logic, Inc.
-
Undeterministic Priority Inversion
Thread-3 Preempts Thread-2, But
Suspends For Mutex-M
y
Thread-3 Blocked!
Thread-2 Preempts Thread-1
Th d 1 Obt i
P
r
i
o
r
i
t
y
Thread-2 Resumes
Thread-1 Obtains Mutex-M
TimeTime
Even though Thread-3 has the highest priority, it must wait for Thread-2.
2013, Express Logic, Inc.
-
Preventing Priority Inversion Proper design of application run-time
behavior and appropriate priority selectionspp p p y
Lower priority threads can use Preemption-Threshold to block preemption fromThreshold to block preemption from intermediate threads while sharing resources with higher priority threadsresources with higher priority threads
Threads using mutex objects may utilize i it i h it t li i tpriority inheritance to eliminate un-
deterministic priority inversion
28 2013, Express Logic, Inc.
-
Priority Inheritance
Thread-3 Preempts Thread-2, But
Suspends For Mutex-M
y
Thread-1 Thread-3 Resumes
Thread-2 Preempts Thread-1
P
r
i
o
r
i
t
y
Thread-1 Assumes Thread-3 Priority and releases the mutex
Thread-1 Obtains Mutex-M
Time
Thread-1
TimeThread-1 assumes the priority of Thread-3
until it is finished with Mutex-M
2013, Express Logic, Inc.
-
Preemption-Threshold Another technique to avoid priority-inversion and reduce
context switches Preemption-Threshold establishes a priority ceiling for
disabling preemption preemption requires a priority higher (lower number) than the ceiling
Priority Comment For example, assume a Priority Comment0
Preemption allowed for threads with prioritiesfrom 0 to 14 (inclusive):
14
threads priority is 20, and its preemption threshold is set to 15Threads with priority
15 Thread is assigned Preemption-threshold = 15 [this has the effect of disabling preemption for threads with priority values from 15 to 19
(inclusive)]:
19
Threads with priority lower than (larger number) 14 , even if higher than (smaller
Preemption-Threshold
20
Thread is assigned Priority = 20:
31
g (number) the running threads priority (20), will not preempt the running thread
Priority
thread
2013, Express Logic, Inc.
-
Message Queues
messages inserted at rear of queue
messages removed from front of queue
msg_n || || msg_3 || msg_2 || msg_1
What is a Message Queue? Data structure that holds messages Means of message-passing among threadsg p g g Messages usually are inserted at rear of queue (FIFO) but can be
inserted at front of queue if desired (LIFO) Messages are removed from front of queue Public resourceany thread can access any queue Threads will suspend on queue full and queue empty
2013, Express Logic, Inc.
-
Semaphore Efficient means of inter-thread communication
Binary Semaphores (0 or 1) Only one occurrence (single-use resource)
Counting Semaphoresg (0 - 0xFFFFFFFF) Many occurrences (multiple-use resource)
Very Low OverheadVery Low Overhead Suspension on 0
Suspended Threads Resumed in FIFO Manner Optional Time-out on Suspension
No Maximum Number of Semaphores
32 2013, Express Logic, Inc.
-
Semaphore Management API
Semaphore Create UINT tx_semaphore_create (TX_SEMAPHORE *semaphore_ptr,
CHAR *name_ptr, ULONG initial_count);
Semaphore GetSemaphore Get UINT tx_semaphore_get (TX_SEMAPHORE *semaphore_ptr,
ULONG wait_option);
Semaphore Put UINT tx_semaphore_put (TX_SEMAPHORE *semaphore_ptr);
33 2013, Express Logic, Inc.
-
Mutex Used to control thread access
To critical sections Or exclusive-use resources Prevents interference with exclusive use
Si il t bi h b t d l l f Similar to binary semaphore, but used solely for mutual exclusion, and not for event notification
G t ti bt i t t d b Get operation obtains mutex not owned by another thread Suspension if already owned by another thread
Put operation releases previously obtained mutex
34 2013, Express Logic, Inc.
-
Mutex Management API
Mutex Create UINT tx_mutex_create (TX_MUTEX *mutex_ptr, CHAR *name_ptr,
UINT priority_inherit);
Mutex Get UINT tx_mutex_get (TX_MUTEX *mutex_ptr, ULONG wait_option);
Mutex Put Mutex Put UINT tx_mutex_put (TX_MUTEX mutex_put);
35 2013, Express Logic, Inc.
-
Summary And Conclusions What Is An RTOS
Facility for managing application threads Why Use An RTOS? Why Use An RTOS?
Achieve more efficient use of CPU through multithreading Modularize application development and maintenance Simplify application porting Simplify application porting
2013, Express Logic, Inc.