Adam Dunkels*, Oliver Schmidt, Thiemo Voigt*, Muneeb Ali**

Slides:



Advertisements
Similar presentations
Spatial Computation Thesis committee: Seth Goldstein Peter Lee Todd Mowry Babak Falsafi Nevin Heintze Ph.D. Thesis defense, December 8, 2003 SCS Mihai.
Advertisements

Lazy Asynchronous I/O For Event-Driven Servers Khaled Elmeleegy, Anupam Chanda and Alan L. Cox Department of Computer Science Rice University, Houston,
Shared-Memory Model and Threads Intel Software College Introduction to Parallel Programming – Part 2.
Process Description and Control
1 Concurrency: Deadlock and Starvation Chapter 6.
1 Vorlesung Informatik 2 Algorithmen und Datenstrukturen (Parallel Algorithms) Robin Pomplun.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 1.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 4 Computing Platforms.
Processes and Operating Systems
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 1 Embedded Computing.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 3 CPUs.
Chung for Robofest 05 1 Introduction to RoboLab CJ Chung Lawrence Technological University.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
FIGURE 8.1 Process and controller.
Arithmetic and Geometric Means
1 Processes and Threads Creation and Termination States Usage Implementations.
Chapter 5 Input/Output 5.1 Principles of I/O hardware
Chapter 6 File Systems 6.1 Files 6.2 Directories
Week 2 The Object-Oriented Approach to Requirements
Real Time Versions of Linux Operating System Present by Tr n Duy Th nh Quách Phát Tài 1.
Figure 12–1 Basic computer block diagram.
Chapter 11: Models of Computation
Turing Machines.
PP Test Review Sections 6-1 to 6-6
Chapter 24 Lists, Stacks, and Queues
Project 5: Virtual Memory
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
The Modular Structure of Complex Systems Team 3 Nupur Choudhary Aparna Nanjappa Mark Zeits.
Outline Minimum Spanning Tree Maximal Flow Algorithm LP formulation 1.
CS 6143 COMPUTER ARCHITECTURE II SPRING 2014 ACM Principles and Practice of Parallel Programming, PPoPP, 2006 Panel Presentations Parallel Processing is.
Operating Systems Operating Systems - Winter 2011 Dr. Melanie Rieback Design and Implementation.
Operating Systems Operating Systems - Winter 2012 Dr. Melanie Rieback Design and Implementation.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Chapter 6 File Systems 6.1 Files 6.2 Directories
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Chapter 3: Processes.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
1 of 32 Images from Africa. 2 of 32 My little Haitian friend Antoine (1985)
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
Chapter Three Arithmetic Expressions and Assignment Statements
Chapter 10: The Traditional Approach to Design
Analyzing Genes and Genomes
Systems Analysis and Design in a Changing World, Fifth Edition
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Types of selection structures
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Pointers and Arrays Chapter 12
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Abstraction, Modularity, Interfaces and Pointers Original slides by Noah Mendelsohn, including content from Mark Sheldon, Noah Daniels, Norman Ramsey COMP.
Run-Time Dynamic Linking for Reprogramming Wireless Sensor Networks
Contiki A Lightweight and Flexible Operating System for Tiny Networked Sensors Presented by: Jeremy Schiff.
Threading Abstractions for Embedded Operating Systems on Memory Constrained Devices Andrew Barton-Sweeney September 21, 2006.
Programming Memory-Constrained Networked Embedded Systems
Presentation transcript:

Protothreads – Simplifying Programming of Memory-Constrained Embedded Systems Adam Dunkels*, Oliver Schmidt, Thiemo Voigt*, Muneeb Ali** * Swedish Institute of Computer Science ** TU Delft ACM SenSys 2006

What this talk is about Memory-constrained networked embedded systems 2k RAM, 60k ROM; 10k RAM, 48K ROM “Artificial” limitations – based on economy, not mother nature More memory = higher per-unit cost Concurrent programming Multithreading – requires “lots” of memory for stacks 100 bytes is ~5% of 2k! Event-driven – less memory

Why use the event-driven model? “In TinyOS, we have chosen an event model so that high levels of concurrency can be handled in a very small amount of space. A stack-based threaded approach would require that stack space be reserved for each execution context.” J. Hill, R. Szewczyk, A. Woo, S. Hollar, D. Culler, and K. Pister. System architecture directions for networked sensors. [ASPLOS 2000]

Problems with the event-driven model? “This approach is natural for reactive processing and for interfacing with hardware, but complicates sequencing high-level operations, as a logically blocking sequence must be written in a state- machine style.” 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]

Enter protothreads Protothreads – a new programming abstraction For memory-constrained embedded systems A design point 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

An example protothread int a_protothread(struct pt *pt) { PT_BEGIN(pt); PT_WAIT_UNTIL(pt, condition1); if(something) { PT_WAIT_UNTIL(pt, condition2); } PT_END(pt); /* … */ /* … */ /* … */ /* … */

Implementation Proof-of-concept implementation in pure ANSI C No changes to compiler No special preprocessor No assembly language Two deviations: automatic variables not saved across a blocked wait, restrictions on switch() statements Six-line implementation will be shown on slide! Very low memory overhead Two bytes of RAM per protothread No per-thread stacks

Evaluation, conclusions We can replace explicit state machines with protothreads Protothreads let programs use if and while statements instead of state machines 16% - 49% reduction in lines of code for rewritten programs Most explicit state machines completely replaced Code size increase/decrease depends on program Run-time overhead small (3-15 cycles) Useful even in time-critical code; interrupt handlers Protothreads have been adopted, used by others

The details…

Example: A hypothetical sensor network MAC protocol

Radio sleep cycle twait_max tsleep tawake t0 Communication left… Radio on Radio off t0

Five-step specification Radio on t0 tawake twait_max tsleep Radio off Communication left… Turn radio on. Wait until t = t_0 + t_awake. If communication has not completed, wait until it has completed or t = t_0 + t_awake + t_wait_max. Turn the radio off. Wait until t = t_0 + t_awake + t_sleep. Repeat from step 1. No blocking wait! Problem: with events, we cannot implement this as a five-step program!

The event-based implementation: a state machine Radio on t0 tawake twait_max tsleep Radio off Communication left… ON WAITING Timer expires Timer expires Timer expires Communication completes, timer expires OFF

Event-driven state machine implementation: messy enum {ON, WAITING, OFF} state; void eventhandler() { if(state == ON) { if(expired(timer)) { timer = t_sleep; if(!comm_complete()) { state = WAITING; wait_timer = t_wait_max; } else { radio_off(); state = OFF; } } else if(state == WAITING) { if(comm_complete() || expired(wait_timer)) { } else if(state == OFF) { radio_on(); state = ON; timer = t_awake; Radio on t0 tawake twait_max tsleep Radio off Communication left… ON OFF WAITING Timer expires Communication completes, timer expires

Protothreads makes implementation easier Radio on t0 tawake twait_max tsleep Radio off Communication left… Protothreads – conditional blocking wait: PT_WAIT_UNTIL() No need for an explicit state machine Sequential code flow

Protothreads-based implementation is shorter int protothread(struct pt *pt) { PT_BEGIN(pt); while(1) { radio_on(); timer = t_awake; PT_WAIT_UNTIL(pt, expired(timer)); timer = t_sleep; if(!comm_complete()) { wait_timer = t_wait_max; PT_WAIT_UNTIL(pt, comm_complete() || expired(wait_timer)); } radio off(); PT_END(pt); Radio on t0 tawake twait_max tsleep Radio off Communication left… Code shorter than the event-driven version Code uses structured programming (if and while statements) Mechanism evident from the code

Protothread scheduling A protothread runs in a C function We schedule a protothread by invoking its function We can invoke the protothread from an event handler Protothreads as blocking event handlers We can let the operating system invoke our protothreads Contiki Protothreads can invoke other protothreads Can wait until a child protothread completes Hierarchical protothreads

What’s wrong with using state machines? There is nothing wrong with state machines! State machines are a powerful tool Amenable to formal analysis, proofs But: state machines typically used to control the logical progam flow in many event-driven programs Like using gotos instead of structured programming The state machines not formally specified Must be infered from reading the code These state machines typically look like flow charts anyway We’re not the first to see this Protothreads: use language constructs for flow control

Why not just use multithreading? Multithreading the basis of (almost) all embedded OS/RTOSes! WSN community: Mantis, BTNut (based on multithreading); Contiki (multithreading on a per-application basis) Nothing wrong with multithreading Multiple stacks require more memory Networked = more concurrency than traditional embedded Can lead to more expensive hardware Preemption Threads: explicit locking; Protothreads: implicit locking Protothreads are a new point in the design space Between event-driven and multithreaded

How do we implement protothreads?

Implementing protothreads Modify the compiler? There are many compilers to modify… (IAR, Keil, ICC, Microchip, GCC, …) Special preprocessor? Requires us to maintain the preprocessor software on all development platforms Within the C language? The best solution, if language is expressive enough Possible?

Proof-of-concept implementation of protothreads in ANSI C Slightly limited version of protothreads in pure ANSI C Uses the C preprocessor Does not need a special preprocessor No assembly language Very portable Nothing is changed between platforms, C compilers Two approaches Using GCCs C extension computed goto Not ANSI C: works only with GCC Using the C switch statement ANSI C: works on every C compiler

Six-line implementation Protothreads implemented using the C switch statement struct pt { unsigned short lc; }; #define PT_INIT(pt) pt->lc = 0 #define PT_BEGIN(pt) switch(pt->lc) { case 0: #define PT_EXIT(pt) pt->lc = 0; return 2 #define PT_WAIT_UNTIL(pt, c) pt->lc = __LINE__; case __LINE__: \ if(!(c)) return 0 #define PT_END(pt) } pt->lc = 0; return 1

C-switch expansion Line numbers 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

Limitations of the proof-of-concept implementation Automatic variables not stored across a blocking wait Compiler does produce a warning Workaround: use static local variables instead Ericsson solution: enforce static locals through LINT scripts Constraints on the use of switch() constructs in programs No warning produced by the compiler Workaround: don’t use switches The limitations are due to the implementation, not protothreads as such

How well do protothreads work? Quantitative: reduction in code complexity over state machines Rewritten seven state machine-based programs with protothreads Four by applying a rewriting method, three by rewriting from scratch Measure states, state transitions, lines of code Quantitative: code size Quantitative: execution time overhead Qualitative: useful in practice?

Reduction of complexity States before States after Transitions before Transitions after Reduction in lines of code XNP 25 20 32% TinyDB DBBufferC 23 24 24% Mantis CC1000 driver 15 19 23% SOS CC1000 driver 26 9 32 14 16% Contiki TR1001 driver 12 3 22 49% uIP SMTP client 10 45% Contiki codeprop 6 4 11 29% Found state machine-related bugs in the Contiki TR1001 driver and the Contiki codeprop code when rewriting with protothreads

Code footprint Average increase ~200 bytes Inconclusive

Execution time overhead is a few cycles Switch/computed goto jump incurs small fixed size preamble State machine Protothreads, switch statement Protothreads, computed gotos gcc -Os 92 98 107 gcc –O1 91 94 103 Contiki TR1001 radio driver average execution time (CPU cycles)

Are protothreads useful in practice? We know that at least thirteen different embedded developers have adopted them AVR, PIC, MSP430, ARM, x86 Portable: no changes when crossing platforms, compilers MPEG decoding equipment, real-time systems Others have ported protothreads to C++, Objective C Probably many more From mailing lists, forums, email questions Protothreads recommended twice in embedded “guru” Jack Ganssle’s Embedded Muse newsletter

Conclusions Protothreads can reduce the complexity of event- driven programs by removing flow-control state machines ~33% reduction in lines of code Memory requirements very low Two bytes of RAM per protothread, no stacks Seems to be a slight code footprint increase (~ 200 bytes) Performance hit is small (~ 10 cycles) Protothreads have been adopted by and are recommended by others

Questions? http://www.sics.se/~adam/pt/

Hierarchical protothreads int a_protothread(struct pt *pt) { static struct pt child_pt; PT_BEGIN(pt); PT_INIT(&child_pt); PT_WAIT_UNTIL(pt2(&child_pt) != 0); PT_END(pt); } int pt2(struct pt *pt) { PT_BEGIN(pt); PT_WAIT_UNTIL(pt, condition); PT_END(pt); }

Threads vs events… No blocking wait! Threads: sequential code flow Events: unstructured code flow No blocking wait!

Threads require per-thread stack memory Four threads, each with its own stack Thread 1 Thread 2 Thread 3 Thread 4

Events require one stack Threads require per-thread stack memory Four event handlers, one stack Four threads, each with its own stack Thread 1 Thread 2 Thread 3 Thread 4 Stack is reused for every event handler Eventhandler 1 Eventhandler 4 Eventhandler 2 Eventhandler 3

Protothreads require one stack Threads require per-thread stack memory Four protothreads, one stack Four threads, each with its own stack Thread 1 Thread 2 Thread 3 Just like events Thread 4 Events require one stack Four event handlers, one stack Protothread 1 Protothread 4 Protothread 3 Protothread 2

Trickle, T-MAC