basic of contiki process
DESCRIPTION
Basic of Contiki ProcessTRANSCRIPT
Basic of Contiki:Process and Protothread, KAE WON CHOI
Outline
Process
Events
Process Scheduler
Context Switching
Protothread
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
Process Structure
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
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)
Process Thread
Process thread contains the code of processes.
Process thread is single protothread that is invoked from the processes scheduler.
Example:
Process ProgrammingPROCESS macro
PROCESS_THREAD macro
PROCESS_BEGIN macro
PROCESS_END macro
Macros for Processcore/sys/process.cPROCESS macro
PROCESS_THREAD macro
PROCESS_BEGIN macro
PT_THREAD: char
PROCESS_END macro
process_start
Put the process on the process list
Run the process
core/sys/process.c
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
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
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]
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
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
process_post_synch
core/sys/process.c
The process is called
process_postcore/sys/process.c
Put the event into the event queue
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
do_event (1)core/sys/process.c
Take out an event out of the event queue
Move the pointer in the event queue
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
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.
process_poll core/sys/process.c
Set needspoll
do_pollcore/sys/process.c
Call the process if needs poll is set
process_run
This function is executed indefinitely
core/sys/process.c
Call do_poll if there is any polled process
Call do_event
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
Context Switching (2) Illustration Context Switch in General[1]
[1] http://clinuxpro.com/context-switch
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)
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)
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)
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]
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‡∗
Protothread Example
Stack in Protothread
Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels† , Oliver Schmidt, Thiemo Voigt† , Muneeb Ali‡∗
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
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‡∗
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‡∗
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
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
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‡∗
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‡∗
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‡∗
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
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
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
Protothread Implementation (6)core/sys/process.c
PROCESS_THREAD macro
PROCESS_BEGIN macro
PROCESS_END macro
Protothread Implementation (7)core/sys/process.c
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
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‡∗
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‡∗
Hierarchical Protothread (3) Implementation of PT_SPAWN
core/sys/pt.hpt struct of a child protothread
processthread (function) of a child protothread