Programming Memory-Constrained Networked Embedded Systems
Total Page:16
File Type:pdf, Size:1020Kb
Swedish Institute of Computer Science Doctoral Thesis SICS Dissertation Series 47 Programming Memory-Constrained Networked Embedded Systems Adam Dunkels February 2007 Swedish Institute of Computer Science Stockholm, Sweden Copyright c Adam Dunkels, 2007 ISRN SICS-D–47–SE SICS Dissertation Series 47 ISSN 1101-1335 Printed by Arkitektkopia, V¨aster˚as, Sweden 3 Abstract Ten years after the Internet revolution are we standing on the brink of another revolution: networked embedded systems that connect the physical world with the computers, enabling new applications ranging from environmental moni- toring and wildlife tracking to improvements in health care and medicine. Only 2% of all microprocessors that are sold today are used in PCs; the re- maining 98% of all microprocessorsare used in embeddedsystems. The micro- processors used in embedded systems have much smaller amounts of memory than PC computers. An embedded system may have as little has a few hundred bytes of memory, which is thousands of millions times less than the memory in a modern PC. The memory constraints make programming embedded systems a challenge. This thesis focus on three topics pertaining to programming memory- constrained networked embedded systems: the use of the TCP/IP protocol suite even in memory-constrained networked embedded systems; simplifying event- driven programming of memory-constrained systems; and dynamic loading of program modules in an operating system for memory-constrained devices. I show that the TCP/IP protocol stack can, contrary to previous belief, be used in memory-constrainedembedded systems but that a small implementation has a lower network throughput. I present a novel programming mechanism called protothreads that is intended to replace state machine-based event-driven pro- grams. Protothreads provide a conditional blocked wait mechanism on top of event-driven systems with a much smaller memory overhead than full multi- threading; each protothread requires only two bytes of memory. I show that protothreads significantly reduce the complexity of event-driven programming for memory-constrained systems. Of seven state machine-based programs rewritten with protothreads, almost all explicit states and state transitions could be removed. Protothreads also reduced the number of lines of code with 31% on the average. The execution time overhead of protothreads is on the order i ii of a few processor cycles which is small enough to make protothreads usable even in time-critical programs. Finally, I show that dynamic linking of native code in standard ELF object code format is doable and feasible for wireless sensor networks by implementing a dynamic loading and linking mechanism for my Contiki operating system. I measure and quantify the energy consump- tion of the dynamic linker and compare the energy consumption of native code with that of virtual machine code for two virtual machines, including the Java virtual machine. The results show that the energy overhead of dynamic linking of ELF files mainly is due to the ELF file format and not due to the dynamic linking mechanism as such. The results also suggest that combinations of na- tive code and virtual machine code are more energy efficient than pure native code or pure virtual machine code. The impact of the research in this thesis has been and continues to be large. The software I have developedas part of this thesis, lwIP,uIP,protothreads, and Contiki, is currently used by hundreds of companies in embedded devices in such diverse systems as car engines, oil boring equipment, satellites, and con- tainer security systems. The software is also used both in academic research projects and in university project courses on embedded systems throughout the world. Articles have been written, by others, in professional embedded soft- ware developer magazines about the software developed as part of this thesis. The papers in this thesis are used as required reading in advanced university courses on networked embedded systems and wireless sensor networks. Sammanfattning Tio ˚ar efter Internet-revolutionen st˚ar vi nu inf¨or n¨asta revolution: kommu- nicerande inbyggda system som kopplas ihop med varandra och d¨arigenom m¨ojligg¨or helt nya till¨ampningar inom ett stort antal omr˚aden, bland annat sjukv˚ard, milj¨o¨overvakning och energim¨atning. Endast 2% av alla mikroprocessorer som s¨aljs idag anv¨ands f¨or att bygga PC-datorer; resterande 98% g˚ar till inbyggda system. Det stora flertalet av dessa system har avsev¨art mycket mindre minne ¨an en modern PC. Inbygga system har ofta endast ett par hundra bytes minne, att j¨amf¨ora med de tusen- tals miljoner bytes minne en modern PC har. Minnesbegr¨ansningarna hos de inbyggda systemen g¨or dem till en utmaning att programmera. Avhandlingen behandlar programmering av minnesbegr¨ansade kommu- nicerande inbyggda system ur tre synvinklar: m¨ojligheten f¨or mycket sm˚a minnesbegr¨ansade inbyggda system att kommunicera med hj¨alp av Internet-protokollen; f¨orenkling av h¨andelsestyrd programmering f¨or minnes- begr¨ansade system; och dynamisk programladdning i ett operativsystem f¨or kommunicerande inbyggda system. F¨or att ett inbyggt system ska kunna kommnunicera i ett n¨atverk kr¨avs att systemet kan prata n¨atverkets spr˚ak, n¨atverksprotokollet. Avhandlingen visar att det ¨ar m¨ojligt ¨aven f¨or mycket sm˚asystem att anv¨anda Internet-protokollen, TCP/IP, utan att beh¨ova g¨ora avsteg fr˚an g¨allande Internet-standarder. Dock inneb¨ar minnesbegr¨ansningarna en avsev¨ard prestandaminskning. Min pro- gramvara lwIP och uIP visar att det ¨ar m¨ojligt att koppla ihop mycket enkla inbyggda system, v¨asentligt mycket mindre ¨an man tidigare trott, med n¨atverk som anv¨ander TCP/IP-protokollen. M˚anga program f¨or minnesbegr¨ansade inbyggda system bygger p˚aen programmeringsmetod som kallas h¨andelsestyrd programmering. Med h¨andelsestyrd programmering kan man skriva program som kr¨aver mycket lite minne, men programmen blir ofta sv˚ara att b˚ade utveckla och underh˚alla. F¨or iii iv att underl¨atta programmeringen av s˚adana system har jag utvecklat en pro- grammeringsteknik som jag kallar protothreads, prototr˚adar. Protothreads g¨or h¨andelsestyrda program mindre komplexa utan att n¨amnv¨art ¨oka minnesutnyt- tjandet och med mycket sm˚aprestandaf¨orluster. Operativsystemet Contiki, som jag har utvecklat under avhandlingsarbetet, kan under drift ladda nya programmoduler,n˚agot som inte st¨ods av andra oper- ativsystem f¨or sm˚ainbyggda system. Att kunna ladda programvara under drift underl¨attar b˚ade utveckling av ny programvara och korrigering av felaktig pro- gramvara. Contiki visar att det trots resursbegr¨ansningarna ¨ar m¨ojligt att ladda programmoduler i standardformatet ELF. Jag kvantifierar energi˚atg˚angen b˚ade f¨or att ladda program med dynamisk l¨ankning och f¨or att exekvera de laddade programmen, samt j¨amf¨or energi˚atg˚angen med den f¨or motsvarande program skrivet f¨or tv˚avirtuell maskiner, bland annat en Java-maskin. Programvaran har f˚att mycket stor spridning och anv¨ands idag av hun- dratals f¨oretag i ett stort antal produkter s˚asom bilmotorer, satellitsystem, olje- borrar, TV-utrustning och l˚assystem fr˚an f¨oretag s˚asom BMW, NASA och HP. Programvaran anv¨ands i projektkurser p˚auniversitetsniv˚av¨arlden ¨over. Ar- tiklar i branchtidskrifter har skrivits, av utomst˚aende, om hur man anpassar programvaran f¨or nya mikroprocessorer. Ledande experter inom programvaru- utveckling f¨or inbyggda system har ett flertal g˚anger rekommenderat program- varan i nyhetsbrev. B˚ade forskningsartiklarna i avhandlingen och program- varan anv¨ands i undervisningen vid universitet v¨arldenover. ¨ To Castor, Morgan, Maria, and the little one we have not yet met Preface I have always loved programming. When I was a kid, my father sometimes brought home a computer that he used in teaching computer programming to mathematic teacher students at the university. The computer was an ABC80, a Swedish Z80-based computer with a BASIC interpreter and 16 kilobytes of RAM. I learned programming BASIC from modifying my father’s programs and by typing in BASIC programs from the manual. The programs were very small and I was never limited by the small amount of memory. A few years later I got my first own computer, a Commodore 64 with 64 kilobytes of RAM. I was so eager to start programming that I had already learned programming the assembly language of its 6510 processor by reading books on the subject before I got the actual computer. Over the years, I wrote a large number of assembly language programs for it and frequently felt that its memory was a limitation. Some six or seven years later I bought my first PC, with a 486 microprocessor and 16 megabytes of RAM. I quickly learned x86 assembly language but never came anywhere near writing a program that used the entire memory of the machine. In 2000 I did my master’s thesis at the Swedish Institute of Computer Sci- ence in Kista, Sweden. As part of my thesis I developed a TCP/IP stack, which I named lwIP, for transmitting vital statistics from wireless sensors on ice hockey-playersto people in the audience with laptop computers. The wire- less sensors were equipped with Mitsubishi M16c CPUs with 20 kilobytes of RAM and 100 kilobytes of ROM. I was almost back where I started! While this thesis officially was done over the past four years at the Swedish Institute of Computer Science, the real work started almost 20 years earlier. Adam Dunkels Stockholm, January 8 2007 vii Acknowledgements I first and foremost thank my colleague Thiemo Voigt, who has also been the co-adviser for this thesis, for all the moral support over the past few years, for being the committed person that he is, and for being genuinely fun to work with.