HW1 Graded • Ready in your pendaflexes • If you forget to include his name, e-mail Brandon. • Average: 92 • Good job overall! Race Conditions • Unit of energy • Watt vs. Joule • micro Joule vs. milli Joule • Pop loop end address and counter from stacks when loop counter reaches 0 • Indicate top of stack Chenyang Lu CSE 467S 1 Chenyang Lu CSE 467S 2 TinyOS Two-level Scheduling Example 1: Race Condition Tasks do intensive computations • • Unpreemptable FIFO scheduling module SurgeM { ... } • Bounded number of pending tasks implementation { Events handle interrupts • bool busy; • Interrupts trigger lowest level events uint16_t sensorReading; • Events can signal events, call commands, or post tasks • Two priorities event result_t Timer.fired() { • Interrupt if (!busy) Tasks Preempt • Tasks POST FIFO busy = TRUE; Asynchronous Code events call ADC.getData(); commands } commands return SUCCESS; } Interrupts Time Hardware Chenyang Lu CSE 467S 3 Chenyang Lu CSE 467S 4 Example 2: Race Condition Causes of race conditions In a command handler reachable from an interrupt: • Different event/command handlers and /* Contains a race: */ tasks access shared variables if (state == IDLE) { • Preemption at a “wrong” time state = SENDING; count++; // send a packet } Chenyang Lu CSE 467S 5 Chenyang Lu CSE 467S 6
Concurrency in TinyOS Solution • Asynchronous code (AC): code that is • Race-Free Invariant : Any update to shared state is either not a potential race condition (SC only), or reachable from at least one interrupt handler occurs within an atomic section. • Synchronous code (SC): code that is only • Compiler check: identifies all shared states and reachable from tasks return errors if the above invariant is violated • Key properties • Fix code • Any update to shared state (variable) from AC is a • Make the access to all shared states with potential race condition potential race conditions atomic • Any update to shared state from SC that is also • Move access to SC updated from AC is a potential race condition Chenyang Lu CSE 467S 7 Chenyang Lu CSE 467S 8 module SurgeM { ... } implementation { Atomic Sections bool busy; norace uint16_t sensorReading; event result_t Timer.fired() { atomic { disable bool localBusy; interrupt atomic { <Statement list> localBusy = busy; test-and-set } busy = TRUE; } enable • Disable interrupt when atomic code is being executed if (!localBusy) interrupt call ADC.getData(); • But cannot disable interrupt for long! � restrictions return SUCCESS; • No loops } • No commands/events task void sendData() { // send sensorReading • Calls OK, but callee must meet restrictions too adcPacket.data = sensorReading; call Send.send(&adcPacket, sizeof adcPacket.data); return SUCCESS; } event result_t ADC.dataReady(uint16_t data) { sensorReading = data; post sendData(); return SUCCESS; } Chenyang Lu CSE 467S 9 Chenyang Lu CSE 467S 10 Example 2 /* Contains a race: */ /* Fixed version: */ if (state == IDLE) { uint8_t oldState; state = SENDING; atomic { count++; oldState = state; // send a packet if (state == IDLE) { } state = SENDING; } Results } if (oldState == IDLE) { count++; // send a packet } Chenyang Lu CSE 467S 11 Chenyang Lu CSE 467S 12
Results • Tested on full TinyOS tree, plus applications • 186 modules (121 modules, 65 configurations) • 20-69 modules/app, 35 average • 17 tasks, 75 events on average (per app) • Lots of concurrency! • Found 156 races: 103 real (!) • About 6 per 1000 lines of code • 53 false positives • Fixing races: • Add atomic sections • Post tasks (move code to task context) Chenyang Lu CSE 467S 13 Chenyang Lu CSE 467S 14 Optimization: inlining Principles Revisited • Support TinyOS components: interface, modules, configuration • Whole-program analysis • Enforce global properties (e.g. no races) • Inlining reduces code size AND improves • Optimization: inlining performance! • “Static language” • no malloc, no function pointers Chenyang Lu CSE 467S 15 Chenyang Lu CSE 467S 16 Space Breakdown… Power Breakdown… Active Idle Sleep Code size for ad hoc networking CPU 5 mA 2 mA 5 µA application Radio 7 mA (TX) 4.5 mA (RX) 5 µA EE-Prom 3 mA 0 0 3500 Interrupts Panasonic LED’s 4 mA 0 0 Message Dispatch CR2354 3000 Initilization Photo Diode 200 µA 0 0 560 mAh C-Runtime Scheduler: 144 Bytes code 2500 Temperature 200 µA 0 0 Light Sensor Totals: 3430 Bytes code Clock 226 Bytes data 2000 Bytes • Lithium Battery runs for 35 hours at peak load and years Scheduler Led Control at minimum load! 1500 Messaging Layer • That’s three orders of magnitude difference! Packet Layer 1000 Radio Interface • A one byte transmission uses the same energy as approx Routing Application 500 11000 cycles of computation. Radio Byte Encoder 0 D. Culler et. Al., TinyOS boot camp presentation, Feb 2001 Chenyang Lu CSE 467S 17 Chenyang Lu CSE 467S 18
Time Breakdown… Reading • Textbook 6.1-6.4 Packet reception Components work breakdown CPU Utilization Energy (nj/Bit) • Required: D. Gay, P. Levis, R. von Behren, M. Welsh, E. 0.05% 0.20% 0.33 AM Brewer, and D. Culler. The nesC Language: A Holistic 1.12% 0.51% 7.58 Packet Approach to Networked Embedded Systems, PLDI 26.87% 12.16% 182.38 Ratio handler 2003. 5.48% 2.48% 37.2 Radio decode thread 66.48% 30.08% 451.17 RFM • Optional: J. Hill, R. Szewczyk, A. Woo, S. Hollar, D. - - 1350 Radio Reception Culler, and K. Pister, System Architecture Directions Idle - 54.75% - for Network Sensors, ASPLOS 2000. Total 100.00% 100.00% 2028.66 • 50 cycle thread overhead (6 byte copies) • 10 cycle event overhead (1.25 byte copies) Chenyang Lu CSE 467S 19 Chenyang Lu CSE 467S 20
Recommend
More recommend