ContikiContiki
A Lightweight and Flexible Operating A Lightweight and Flexible Operating
System for Tiny Networked SensorsSystem for Tiny Networked Sensors
Presented by: Jeremy SchiffPresented by: Jeremy Schiff
ObjectivesObjectives
LightweightLightweight
Event Driven ModelEvent Driven Model
Has Multi-Threading Support as LibraryHas Multi-Threading Support as Library
Dynamic Loading and Replacement of Dynamic Loading and Replacement of
Individual ServicesIndividual Services
Contiki MotivationsContiki Motivations
No memory protection between appsNo memory protection between apps
Kernel very minimalKernel very minimal
CPU multiplexingCPU multiplexing
Loading ProgramsLoading Programs
All other abstractions by librariesAll other abstractions by libraries
Custom ApplicationsCustom Applications
Why Loadable ApplicationsWhy Loadable Applications
Smaller file to uploadSmaller file to upload
Less EnergyLess Energy
Less Dissemination Time Less Dissemination Time
Why No Threads?Why No Threads?
Thread stacks must be allocated at creation timeThread stacks must be allocated at creation time
Stack Memory Over-ProvisionedStack Memory Over-Provisioned
No Virtual Memory SystemNo Virtual Memory System
No Memory Protection Mechanisms No Memory Protection Mechanisms
Locking for state exclusionLocking for state exclusion
Stack memory is inaccessible to other threadsStack memory is inaccessible to other threads
EventsEvents
No lockingNo locking
Only one running at a timeOnly one running at a time
Only one stackOnly one stack
Locking rareLocking rare
Hard to express some things as state machineHard to express some things as state machine
Problem: Cryptography takes 2 seconds, everything Problem: Cryptography takes 2 seconds, everything
else blocked.else blocked.
Solution: Preemptive ThreadsSolution: Preemptive Threads
System OverviewSystem Overview
Service – Something that implements Service – Something that implements
functionality for more than one applicationfunctionality for more than one application
Apps have direct access to hardwareApps have direct access to hardware
Single Address SpaceSingle Address Space
Inter-process communication via event postingInter-process communication via event posting
Always Through KernelAlways Through Kernel
Core vs. Loaded ProgramCore vs. Loaded Program
Partitioned at compile timePartitioned at compile time
CoreCore
Single Binary Single Binary
Ideally never modifiedIdeally never modified
ProgramProgram
Easily upgradedEasily upgraded
KernelKernel
Execution via: Kernel Events or Polling EventsExecution via: Kernel Events or Polling Events
No preemption of scheduled events No preemption of scheduled events
Synchronous vs Asynchronous EventsSynchronous vs Asynchronous Events
Synch: Like Function CallSynch: Like Function Call
Asynch: Like posting new eventAsynch: Like posting new event
Asynch reduces stack spaceAsynch reduces stack space
Debugging issuesDebugging issues
Two Level SchedulingTwo Level Scheduling
Events Events
Can’t preempt one anotherCan’t preempt one another
Interrupts Interrupts
Can preempt eventsCan preempt events
Can use “underyling real-time executive” Can use “underyling real-time executive”
Provides realtime guaranteesProvides realtime guarantees
Non-hardwareNon-hardware
Can’t post eventsCan’t post events
Use polling flag insteadUse polling flag instead
Prevent race conditionsPrevent race conditions
Loading ProgramsLoading Programs
Relocation Information in BinaryRelocation Information in Binary
Check for space before loadingCheck for space before loading
Call Initialization FunctionCall Initialization Function
Replace or Starts new ProgramReplace or Starts new Program
Power Save ModePower Save Mode
No explicit Kernel assistanceNo explicit Kernel assistance
Access to event queue size Access to event queue size
ServicesServices
ApplicationApplication
Service LayerService Layer
Service InterfaceService Interface
Service ProcessService Process
ApplicationApplication
Must dynamically linkMust dynamically link
Interact via Service StubInteract via Service Stub
Compiled inCompiled in
Version numberVersion number
Caches Service ProcessIDCaches Service ProcessID
Service LayerService Layer
Works with KernelWorks with Kernel
Provides lookup for specific serviceProvides lookup for specific service
Returns a Service InterfaceReturns a Service Interface
Has pointers to all functions service providesHas pointers to all functions service provides
Contains a version NumberContains a version Number
Interface description implemented by Service ProcessInterface description implemented by Service Process
Version numbers must matchVersion numbers must match
Failure Support? MANTIS thinks about this better.Failure Support? MANTIS thinks about this better.
Service ExampleService Example
Service ReplacementService Replacement
Process ID must be preservedProcess ID must be preserved
Kernel supportedKernel supported
Kernel instructs service to remove itselfKernel instructs service to remove itself
Could lead to problemsCould lead to problems
Kernel provides mechanism to transfer state Kernel provides mechanism to transfer state
Service state also versionedService state also versioned
Service state must be stored in a shared space during Service state must be stored in a shared space during
swap due to reallocations of old version’s spaceswap due to reallocations of old version’s space
Is there a better way to do this - SOS?Is there a better way to do this - SOS?
LibrariesLibraries
Application optionsApplication options
Static link with coreStatic link with core
Static link with librariesStatic link with libraries
Single binarySingle binary
Call a serviceCall a service
Memcpy() vs Atoi()Memcpy() vs Atoi()
MemCpy in CoreMemCpy in Core
CommunicationCommunication
Architecture makes easy to replace Communication Architecture makes easy to replace Communication
Stack Stack
A service like any otherA service like any other
Enables multiple communication stack for comparisonEnables multiple communication stack for comparison
Comm Services use Service Mechanism to call one Comm Services use Service Mechanism to call one
anotheranother
Comm Services use Synchronous events to Comm Services use Synchronous events to
communicates with applicationscommunicates with applications
No copying of buffers because Synch events can’t be No copying of buffers because Synch events can’t be
preemptedpreempted
Over the air ProgrammingOver the air Programming
1 node took 30 seconds1 node took 30 seconds
40 nodes took 30 minutes40 nodes took 30 minutes
1 component for 40 nodes took 2 minutes1 component for 40 nodes took 2 minutes
Used naïve protocolUsed naïve protocol
Code SizeCode Size
Bigger than TinyOS, Smaller than MantisBigger than TinyOS, Smaller than Mantis
Polling Handlers and Increase FlexibilityPolling Handlers and Increase Flexibility
Less compiler optimization possibleLess compiler optimization possible
Preemption DemoPreemption Demo
Start 8 second Start 8 second
process at 5 Seconds process at 5 Seconds
and continually pingand continually ping
~.1 ms latency ~.1 ms latency
increaseincrease
Poll handler caused Poll handler caused
spikesspikes
Mainly Radio Packet Mainly Radio Packet
driverdriver
PortabilityPortability
Custom Code in a portCustom Code in a port
Boot up codeBoot up code
Device DriversDevice Drivers
Architecture specific parts of program loaderArchitecture specific parts of program loader
Stack switching code of the multithreading libraryStack switching code of the multithreading library
Kernel and Service layer need no changesKernel and Service layer need no changes