+ All Categories
Home > Documents > Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and...

Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and...

Date post: 17-Jan-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
25
state-machine.com 1 Beyond the RTOS Modern Embedded Software Architecture Miro Samek, Ph.D. Miro Samek, Ph.D. Quantum Leaps, LLC Quantum Leaps, LLC
Transcript
Page 1: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 1

Beyond the RTOSModern Embedded Software Architecture

Miro Samek, Ph.D.Miro Samek, Ph.D.Quantum Leaps, LLCQuantum Leaps, LLC

Page 2: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 2

Presentation Outline

● A quick introduction to RTOSand the perils of blocking

● Active objects

● State machines

● Active object frameworks fordeeply embedded systems

● Demonstrations

● Q&A

~40 min

~10 min

~10 min

Page 3: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 3

In the beginning was the “Superloop”

// adapted from the Arduino Blink Tutorial (*)void main() { pinMode(LED_PIN, OUTPUT); // setup: set the LED pin as output while (1) { // endless loop digitalWrite(LED_PIN, HIGH); // turn LED on delay(1000); // wait for 1000ms digitalWrite(LED_PIN, LOW); // turn LED off delay(1000); // wait for 1000ms }}

(*) Arduino Blink Tutorial: http://www.arduino.cc/en/Tutorial/Blink

Page 4: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 4

void thread_blink() { // RTOS thread routine pinMode(LED_PIN, OUTPUT); // setup: set the LED pin while (1) { // endless loop digitalWrite(LED_PIN, HIGH); // turn the LED on RTOS_delay(1000); // wait for 1000ms digitalWrite(LED_PIN, LOW); // turn the LED off RTOS_delay(1000); // wait for 1000ms }}

RTOS Multithreading: Multiple “Superloops”

void thread_alarm() { // RTOS thread routine pinMode(SW_PIN, INPUT); // setup: set the Switch pin as input while (1) { // endless loop if (digitalRead(SW_PIN) == HIGH) { // is the switch depressed? digitalWrite(ALARM_PIN, HIGH); // start the alarm } else { digitalWrite(ALARM_PIN, LOW); // stop the alarm } RTOS_delay(100); // wait for 100ms }}

void thread_blink() { // RTOS thread routine pinMode(LED_PIN, OUTPUT); // setup: set pin as output while (1) { // endless loop digitalWrite(LED_PIN, HIGH); // turn the LED on RTOS_delay(1000); // wait for 1000ms digitalWrite(LED_PIN, LOW); // turn the LED off RTOS_delay(1000); // wait for 1000ms }}

Page 5: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 5

void thread_blink() { pinMode(LED_PIN, OUTPUT); While (1) { // endless loop digitalWrite(LED_PIN, HIGH); RTOS_delay(1000); digitalWrite(LED_PIN, LOW); RTOS_delay(1000); }}

Thread Context & Context Switch

. . .

Memory

CPU registers

Thread ControlBlocks (TCBs)

per-thread stacksstack stack

sr

. . .

sp

pc

TCB

. . .

spTCB

. . .

sp

context restore context save

program counter

stack pointerstatus register

pc

pc

Page 6: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 6

Thread Blocking

time

context switch

context switch

(6b)

prio

rity

Thread-B blocked

Thread-A blockedThread-A runs Thread-A runsK

Thread-B blocked

RTOS kernel

Thread makes a blocking call, e.g,RTOS_delay()

Clock tick interrupt

ISR K RTOS kernel

Thread-B runs

Page 7: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 7

RTOS Benefits

1) Divide and conquer strategy

→ Multiple threads are easier to develop than one “kitchen sink” superloop

2) More efficient CPU use

→ Threads that are efficiently blocked don't consume CPU cycles

3) Threads can be decoupled in the time domain

→ Under a preemptive, priority-based scheduler, changes in low-priority threads have no impact on the timing of high-priority threads (Rate Monotonic Analysis (RMA))

Page 8: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 8

Perils of Blocking

Synchronizationby blocking

Deadlock

Priorityinversion

Misseddeadlines

Starvation

Unresponsivethreads

Architecturaldecay

Failure

Blocking

Morethreads

MutualExclusion

Shared-state concurrency

Raceconditions

Page 9: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 9

Best Practices of Concurrent Programming(*)

● Don't block inside your code

→ Communicate and synchronize threads asynchronouslyvia event objects

● Don't share data or resources among threads

→ Keep data isolated and bound to threads (strict encapsulation)

● Structure your threads as “message pumps”

(*) Herb Sutter “Prefer Using Active Objects Instead of Naked Threads”

Page 10: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 10

EventqueuePrivate thread

Best Practices: RTOS Implementation

start

Waitfor event

Processevent

Eventqueue

Privatedataand

resources

Eventqueue

Event objectEvent object

Event object ISR

void thread_handler(AO_Type *ao) { // AO thread routine ... // setup while (1) { // event loop // pend on the event queue (BLOCKING!) Event e = RTOS_queuePend(ao->queue); ao->handle(e); // handle event (NON-BLOCKING!) }}

Eventloop

NOBLOCKING

BLOCKING

Page 11: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 11

Active Object (Actor) Design Pattern

● Active Objects (Actors) are event-driven, strictly encapsulated software objects running in their own threads and communicating asynchronously by means of events.

● Not a novelty. Carl Hewitt's actors 1970s. ROOM actors 1990s.

● Adapted from ROOM into UML as active objects

→ ROOM actors and UML active objects use hierarchical state machines (UML statecharts) to specify the behavior of event-driven active objects.

Page 12: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 12

Active Object Framework● Implement the Active Object pattern as a framework

● Inversion of control (main difference from RTOS)

→ automates and enforces the best practices (safer design)

→ brings conceptual integrity and consistency to the applications

void thread_handler(AO_Type *ao) { // AO thread routine ... // setup while (1) { // event loop // pend on the event queue (BLOCKING!) Event e = RTOS_queuePend(ao->queue); ao->handle(e); // handle event (NON-BLOCKING!) }}

Page 13: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 13

void thread_blink() { pinMode(LED_PIN, OUTPUT); while (1) { digitalWrite(LED_PIN, HIGH); RTOS_delay(1000); // NOT allowed! ... }}

Paradigm Shift: Sequential → Event-Driven

● No blocking

→ No use for most RTOSmechanisms!

delay()

Sequentialprogramming

with RTOS

Event-drivenactive objectframework

Semaphores

EventFlags

CallbackTimers

MessageQueues*

Threads

TimeEvents

ActiveObjects

Events

Publish/Subscribe

MemoryPools

Paradigm Shift

StateMachines

EventPosting

Mutexes

Page 14: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 14

Reduce “Spaghetti Code” with State Machines● Finite State Machines—the best known “spaghetti reducers”

→ “State” captures only the relevant aspects of the system's history

→ Natural fit for event-driven programming, where the code cannot block and must return to the event-loop after each event

→ Minimal context (a single state-variable) instead of the whole call stack

ANY_KEY / send_lower_case_scan_code();default

ANY_KEY / send_upper_case_scan_code();caps_locked

CAPS_LOCK CAPS_LOCK

trigger list of actions

internaltransitions

Page 15: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 15

State Machines are not Flowcharts

s1

(a)

s2

s3

do X

do Y do Z

(b)

E1 / action1();

E2 / action2();

E3 / action3(); do W

Statechart (event-driven)→ represents all states of a system→ driven by explicit events→ processing happens on arcs (transitions)→ no notion of “progression”

Flowchart (sequential)→ represents stages of processing in a system→ gets from node to node upon completion→ processing happens in nodes→ progresses from start to finish

Page 16: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 16

Input-driven state machines are NOT driven by events:→ combination of polling for events and state machine logic→ often called from “superloops” (while(1) loops)→ transitions have only guard conditions ( if(guard) statements in the code)

Event-Driven vs Input-Driven State Machines

Source: http://users.ece.cmu.edu/~koopman/lectures/ece642/04_modalstatechart.pdf

Page 17: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 17

Hierarchical State Machines

Traditional FSMs “explode”due to repetitions

(a)

operand1

OPER

opEntered operand2

DIGIT_0_9, POINT

EQUALS

result

DIGIT_0_9, POINT

OPER

C

CC

C

on(b)

operand1

OPER

opEntered

operand2

DIGIT_0_9, POINT

EQUALS

result

DIGIT_0_9, POINTOPER

C

OFF

OFF

OFF

OFFOFF

State hierarchy eliminates repetitions→ programming-by-difference

Page 18: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 18

AO Frameworks for Deeply Embedded Systems

Page 19: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 19

AO Frameworks vs. RTOS kernels

ROM(code)1KB

10B10KB 100KB 1MB 10MB

100B

1KB

10KB

100KB

1MB

RA

M(d

ata)

2KB

QP-nano

QP/C,QP/C++

A typicalsmall preemptive RTOS

VxWorks

Linux,Windows XP

Windows CE

AO Frameworks can be smaller than RTOS kernels, because they don't need blocking

Page 20: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 20

AO Framework – “Software Bus”

ISR_1() ISR_2()

Active Object 1

Active Object 2

Active Object N

directevent posting

publish-subscribe “software bus”

. . .

multicasting a published event

Page 21: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 21

Coding Hierarchical State MachinesQState Calc_on(Calc * const me, QEvt const *e) { QState status; switch (e->sig) { case Q_ENTRY_SIG: /* entry action */ . . . status = Q_HANDLED(); break; case Q_EXIT_SIG: /* exit action */ . . . status = Q_HANDLED(); break; case Q_INIT_SIG: /* initial transition */ status = Q_TRAN(&Calc_ready); break; case C_SIG: /* state transition */ BSP_clear(); /* clear the display */ status = Q_TRAN(&Calc_on); break; case OFF_SIG: /* state transition */ status = Q_TRAN(&Calc_final); break; default: status = Q_SUPER(&QHsm_top); /* superstate */ break; } return status;}

top

entry /exit /

on

Cready

OFF

Page 22: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 22

Cooperative Kernel (QV)

“vanilla” scheduler

. . .

. . .dispatch(e);

e = queue.get();

dispatch(e);

e = queue.get();

dispatch(e);

e = queue.get();. . .

find highest-priority non-empty queue

all queues empty (idle condition)

idleprocessing

priority = 1priority = n-1priority = n priority = 0

Page 23: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 23

Preemptive, Non-Blocking Kernel (QK)

0

low pr

iority

task

time5 10 15 20 25

high p

riority

task

(3)

(4)

priority

task preempted(1) (5)

functioncall

interrupt entry/exit

RTC scheduler

(2)

Synchronous Preemption

0

low pr

iority

task

time5 10 15 20 25

high p

riority

task

(8)

(7)

priority

task preempted(1) (11)

(2)

interrupt call

interrupt return

functioncall

(4)(3)

interrupt entry/exit

RTC scheduler

(10)

(6)

(9)

(5)

interr

upt Asynchronous Preemption

Page 24: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 24

Graphical Modeling and Code Generation● Active Objects enable you to effectively apply UML modeling ● A modeling tool needs an AO framework as a target for automatic

code generation

Page 25: Beyond the RTOS - QP · state-machine.com 2 Presentation Outline A quick introduction to RTOS and the perils of blocking Active objects State machines Active object frameworks for

state-machine.com 25

Summary

● Experts use the Active Object design pattern instead of naked RTOS● AO framework is an ideal fit for deeply embedded real-time systems● AO framework requires a paradigm shift (sequential→event-driven)● Compared to RTOS, AO framework opens new possibilities:

→ Safer architecture and state-machine design method (functional safety)

→ Simpler, more efficient kernels (lower-power applications)

→ Easier unit testing and software tracing (V&V)

→ Higher level of abstraction suitable for modeling and code generation

● Welcome to the 21st century!


Recommended