basic of contiki process

50
Basic of Contiki: Process and Protothread, KAE WON CHOI

Upload: hoangconganh

Post on 22-Dec-2015

64 views

Category:

Documents


8 download

DESCRIPTION

Basic of Contiki Process

TRANSCRIPT

Page 1: Basic of Contiki Process

Basic of Contiki:Process and Protothread, KAE WON CHOI

Page 2: Basic of Contiki Process

Outline

Process

Events

Process Scheduler

Context Switching

Protothread

Page 3: Basic of Contiki Process

Process

All programs running in Contiki is called Process

Process structure Process Control Block

Process Thread

Process is switched to another process Preemptive

Cooperative

Process inside process => subprocess

Page 4: Basic of Contiki Process

Process Structure

Page 5: Basic of Contiki Process

thread

Process Control Block in Memory

Process 1

next

Process 2

next

Process 3

next

process_list

thread thread

Process Thread

Function pointer

Process Thread

Function pointer

Process Thread

Function pointer

Page 6: Basic of Contiki Process

Process Control Block core/sys/process.c

Refer to next PCB in linked list of active processes

Textual name of processes

Thread field, function pointer, points to the processes thread of the processes

State of protothread

Process statePoll is needed (flag)

Page 7: Basic of Contiki Process

Process Thread

Process thread contains the code of processes.

Process thread is single protothread that is invoked from the processes scheduler.

Example:

Page 8: Basic of Contiki Process

Process ProgrammingPROCESS macro

PROCESS_THREAD macro

PROCESS_BEGIN macro

PROCESS_END macro

Page 9: Basic of Contiki Process

Macros for Processcore/sys/process.cPROCESS macro

PROCESS_THREAD macro

PROCESS_BEGIN macro

PT_THREAD: char

PROCESS_END macro

Page 10: Basic of Contiki Process

process_start

Put the process on the process list

Run the process

core/sys/process.c

Page 11: Basic of Contiki Process

exit_processInform all processes that this process will exit

Remove the process from the process list

core/sys/process.c

Post the exit event to the process exited

Page 12: Basic of Contiki Process

call_process

If the process is running….

Run the process thread

core/sys/process.c

If the process is ended or exited, execute exit_process

Page 13: Basic of Contiki Process

Preemptive and Cooperative Execution

Cooperative Run sequentially

Example: piece of code execute regularly by Contiki system

Preemptive Can run anytime

Interrupt cooperative code

Example: interrupt and real time timer

[1] https://github.com/contiki-os/contiki/wiki/Processes

Illustration [1]

Page 14: Basic of Contiki Process

Event

Process runs when it receives an event

Two types of events: Synchronous Event (A)

a) Delivered directly when they are posted

b) Equivalent to a function call

c) Process that posted the event is blockeduntil the receiving process has finished

Asynchronous Event (B)

a) Delivered to the receiving process some timeafter they have been posted

(A)

(B)

https://github.com/contiki-os/contiki/wiki/Processes#The_Process_Scheduler

Page 15: Basic of Contiki Process

Event Structure & Queuecore/sys/process.c

Type of event (e.g., PROCESS_EVENT_INIT, PROCESS_EVENT_MSG)

Data handed over to the receiving process (void *)

Receiving process

Event queue

Page 16: Basic of Contiki Process

process_post_synch

core/sys/process.c

The process is called

Page 17: Basic of Contiki Process

process_postcore/sys/process.c

Put the event into the event queue

Page 18: Basic of Contiki Process

Process Scheduler

Purpose to invoke processes when it is their time to run

How? Invokes processes by calling function that implements process thread

When? In response to an event being posted to a process

Argument Event identifier

Data (opaque pointer to data)

https://github.com/contiki-os/contiki/wiki/Processes#The_Process_Scheduler

Page 19: Basic of Contiki Process

do_event (1)core/sys/process.c

Take out an event out of the event queue

Move the pointer in the event queue

Page 20: Basic of Contiki Process

do_event (2) core/sys/process.c

If the event is a broadcast event, it is delivered to all processes

If the event is not a broadcast event, it is delivered only to a receiver

Page 21: Basic of Contiki Process

Polling

A poll request is a special type of event.

A process is polled by calling the function process_poll().

Calling this function on a process causes the process to be scheduled as quickly as possible.

The process is passed a special event that informs the process that it has been polled.

Polling is the way to make a process run from an interrupt. The process_poll() function is the only function in the process module that is safe to call from preemptive mode.

Page 22: Basic of Contiki Process

process_poll core/sys/process.c

Set needspoll

Page 23: Basic of Contiki Process

do_pollcore/sys/process.c

Call the process if needs poll is set

Page 24: Basic of Contiki Process

process_run

This function is executed indefinitely

core/sys/process.c

Call do_poll if there is any polled process

Call do_event

Page 25: Basic of Contiki Process

Context Switching (1)

Context [1]:

“the contents of a CPU’s registers and program counter at any point in time”

Context switching [2] Saved all register value may using by process (included Program

Counter and Process Control Block)

Stored first PCB in a stack

Load another PCB (second process) from queues

After second process completed

Load Program Counter, continue first process

[1] http://clinuxpro.com/context-switch[2] http://en.wikipedia.org/wiki/Context_switch

Page 26: Basic of Contiki Process

Context Switching (2) Illustration Context Switch in General[1]

[1] http://clinuxpro.com/context-switch

Page 27: Basic of Contiki Process

Context Switching in FreeRTOS (1)

[1] http://www.freertos.org/implementation/a00020.html[2] http://www.freertos.org/implementation/a00021.html[3] http://www.freertos.org/implementation/a00022.html[4] http://www.freertos.org/implementation/a00024.html

1 2

3 4FreeRTOS context switchMemory View (1)

Page 28: Basic of Contiki Process

Context Switching in FreeRTOS (2)

[5] http://www.freertos.org/implementation/a00025.html[6] http://www.freertos.org/implementation/a00026.html

5

6

FreeRTOS context switchMemory View (2)

Page 29: Basic of Contiki Process

Problem of thread in embedded systems

Constraint in network embedded systems 2k RAM, 60k ROM / 10k RAM, 48

More memory, higher per unit cost

Problem of thread requires a large memory space for

keeping stacks for each thread.

Programming models for sensor networks Earlier : TinyOS (event driven)

Currently: ContikiOS (protothreads)

Page 30: Basic of Contiki Process

Event-Driven Model Event-driven model

Implemented in TinyOS

Using event driven models: TinyOS can handle high-level concurrency in a very small amount of space [1]

Shortcomings of event-driven model Useful for reactive processing and

interfacing with HW, complicates sequencing high-level operations

Blocking sequence must be written in a state-machine style [2]

[1] J. Hill, R. Szewczyk, A. Woo, S. Hollar, D. Culler, and K. Pister. System architecture directions for networked sensors. [ASPLOS 2000][2] P. Levis, S. Madden, D. Gay, J. Polastre, R. Szewczyk, A. Woo, E. Brewer, and D. Culler. The Emergence of Networking Abstractions and Techniques in TinyOS. [NSDI 2004]

Page 31: Basic of Contiki Process

Protothread Protothread

Implemented in ContikiOS Introduce Protothreads [1]: new programming abstraction

A design between events and threads

Very simple, yet powerful idea Programming primitive: conditional blocking wait

PT_WAIT_UNTIL(condition) Sequential flow of control

Programming language helps us: if and while

Protothreads run on a single stack, like the event-driven model Memory requirements (almost) same as for event-driven

Can be seen as combination of events and thread From thread: inherited the blocking wait semantics (Compare to traditional

thread, protothreads are very lightweight)

From events: inherited the stacklessness and the low memory overhead[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Page 32: Basic of Contiki Process

Protothread Example

Page 33: Basic of Contiki Process

Stack in Protothread

Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Page 34: Basic of Contiki Process

Advantages of Protothreads

We can replace explicit state machines with protothreads Use if and while statements instead of state machines 16%-49% reduction of lines of code for rewritten programs Most explicit state machines replaced Run-time overhead is small

Page 35: Basic of Contiki Process

Radio Sleep Cycle Radio Sleep Cycle [1]

Hypothetical Sensor Network Protocol State Machine

[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Page 36: Basic of Contiki Process

Event-Based Implementation of RSC

State machine-based implementation Messy

[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Page 37: Basic of Contiki Process

Protothread Implementation of RSC

[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Code shorter than the event-driven version

Code uses structured programming (if and while statements)

Mechanism evident from the code

Page 38: Basic of Contiki Process

Primitive State Machine

Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Primitive state machines and each implementation in pseudocode using protothread sequence iteration selection

Page 39: Basic of Contiki Process

State machine of Radio Sleep Cycle

Example radio sleep cycle state machine and related primitive state machine pattern identified

Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Page 40: Basic of Contiki Process

Protothread Implementation (1)

Local Continuations Low level mechanism that underpins protothreads When a protothread(pt) blocks, the state of pt is stored in local

continuation Similar to ordinary continuation but does not capture the

program stack Only captures the current state execution inside a single function

(not include the history) Operations

SET : state of execution is stored in the local continuationRESUME : restore current state later time

Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Page 41: Basic of Contiki Process

Protothread Implementation (2) Local Continuations: C preprocessor implementation main operations

Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Page 42: Basic of Contiki Process

Protothread Implementation (3) Local Continuations: Implemented with the GCC labels as values C extension

Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Local Continuations: Implemented with the C switch statement

Only works with GCC

GCC and ANSI C protothread memory overhead = 2 bytes

Page 43: Basic of Contiki Process

Protothread Implementation (4)int a_protothread(struct pt *pt) { PT_BEGIN(pt);

PT_WAIT_UNTIL(pt, condition1);

if(something) {

PT_WAIT_UNTIL(pt, condition2);

}

PT_END(pt);}

int a_protothread(struct pt *pt) { switch(pt->lc) { case 0:

pt->lc = 5; case 5: if(!condition1) return 0;

if(something) {

pt->lc = 10; case 10: if(!condition2) return 0;

}

} return 1;}

Line numbers

Page 44: Basic of Contiki Process

Protothread Implementation (5)

process_list

Process Thread

Function pointer

Process Thread

Function pointer

Process Thread

Function pointer

pt

thread

Process 1

next

pt

thread

Process 2

next

pt

thread

Process 3

next

Page 45: Basic of Contiki Process

Protothread Implementation (6)core/sys/process.c

PROCESS_THREAD macro

PROCESS_BEGIN macro

PROCESS_END macro

Page 46: Basic of Contiki Process

Protothread Implementation (7)core/sys/process.c

Page 47: Basic of Contiki Process

Yielding in Protothreads The importance of unconditional blocking wait, PT_YIELD() PT_YIELD() performs an single unconditional blocking wait that

temporarily blocks the protothread until the next time the protothread is invoked

Next invocation the protothread continues executing the code following PT_YIELD() statement (perform similar to stackless coroutines)

[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

core/sys/pt.h

Page 48: Basic of Contiki Process

Hierarchical Protothread (1) Simple program readily expressed with a single protothread More complex operations may need to be decomposed in

hierarchical fashion PT_SPAWN() initializes a child protothread and blocks the current

protothread until the child has either ended with PT_END or exit with PT_EXIT

Child is scheduled by the parent protothread Each time the parent protothread is invoked by the underlying

system, the child protothread is invoked through the PT_SPAWN() statement

Memory state of child protothread is allocated in a local variable of the parent protothread

[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Page 49: Basic of Contiki Process

Hierarchical Protothread (2) Pseudocode for

hierarchical Protothread example

[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗

Page 50: Basic of Contiki Process

Hierarchical Protothread (3) Implementation of PT_SPAWN

core/sys/pt.hpt struct of a child protothread

processthread (function) of a child protothread