Challenges and Solutions in the Design of a Java Virtual Machine for Resource Constrained Microcontrollers
Total Page:16
File Type:pdf, Size:1020Kb
Challenges and Solutions in the Design of a Java Virtual Machine for Resource Constrained Microcontrollers Faisal Aslam Dissertation Submitted to the University of Freiburg for Doctorate Degree in Computer Scinece Computer Networks and Telematics Department of Computer Science Faculty of Applied Sciences University of Freiburg, Germany March 2011 Dissertation Defense Date: 17.03.2011 Thesis committee: Chair: Prof. Dr. Georg Lausen Examiner: PD Dr. Cyrill Stachniss Referees: Prof. Dr. Christian Schindelhauer Prof. Dr. Peter Thiemann To Abbu, Ammi and Ayesha Zusammenfassung Diese Arbeit beschreibt die TakaTuka Java Virtual Machine (JVM), die für Microcon- troller mit geringen Kapazitäten an RAM, Speicher und Prozessor entwickelt wurde. TakaTuka optimiert die Speicheranforderungen sowohl des compilierten Java Codes als auch die des JVM Interpreters, die beide auf einem eingebetteten System abgespeichert werden müssen. Im Durchschnitt lässt sich die Größe des compilierten Java Codes der TakaTuka JVM um über 92% der Größe des Standard-Java-Codes eines Programms re- duzieren. Desweiteren kann die Interpreter-Größe für ein gegebenes Programm auf nicht mehr als 25 KB verkleinert werden. Auf diese Weise führt ein Sensorknoten mit geringem Flash Speicher einen hoch optimierten Java Binärcode und Java Interpreter aus. Die Fähigkeit komplexe Java-Programme auszuführen, wird durch den auf den Bauteilen vorhandenen RAM-Speicher beschränkt. Das Speicher-Management für ein Java Pro- gramm besteht aus zwei Teilen: Erstens wird der Speicher, den jedes Objekt konsumiert, durch die Garbage Collection (GC) geregelt, die zwischendurch ausgeführt wird um Spe- icher wieder freizugeben. Zweitens wird der Speicher, den der Frame einer Funktion beansprucht hat, automatisch zurückgefordert, sobald eine aufgerufene Funktion been- det ist. TakaTuka verfügt über eine Offline Garbage Collection, um den von Objekten während der Dauer einer Programmausführung beanspruchten Speicher zu reduzieren. Die Offline Garbage Collection erlaubt die Freigabe eines Objekts, das zwar noch erreich- bar ist, aber allerdings garantiert nicht mehr im Programm benutzt wird. Es wird gezeigt, dass die Offline GC den RAM-Speicher, das dem Programm zur Verfügung steht, im Ver- gleich zum üblichen online Garbage Collector um bis zu 66% erhöhen kann. Eine weitere wichtige Eigenschaft der TakaTuka JVM ist das Variable Slot Size (VSS) Schema, das die RAM Anforderungen an den Funktionen-Frame verringert. Die durch TakaTuka bereit gestellten Speicher- und RAM-Optimierungen erhöhen außer- dem die Ausführungsgeschwindigkeit eines Programms und erhöht auf diese Weise die Lebensdauer eines Sensorknotens. Die Eigenschaften der TakaTuka JVM bezüglich Spe- icher, Leistungs und RAM-Konsum machen sie zu einer attraktiven Plattform, um An- wendungen für drahtlose Sensornetzwerke zu entwickeln, die Microcontroller und einge- bettete Systeme beinhalten. v Abstract This thesis describes the TakaTuka Java virtual machine (JVM), which has been devel- oped for microcontrollers with small RAM, storage and processing power. The TakaTuka optimizes storage requirements for the Java binary as well as the JVM interpreter, both of which are to be stored on an embedded device. On the average, Java binary size of the TakaTuka JVM can have a percentage reduction of more than 92 % of the size of tra- ditional Java binary of a program. Furthermore, the interpreter size for a given program may be reduced to as small as 25 KB. Thus a mote with small flash storage executes a highly optimized Java binary and Java interpreter. The ability of tiny motes to run large, feature-rich Java programs is constrained by the amount of RAM installed on such devices. The memory management for a Java program may be divided into two parts. First, the memory consumed by each object is managed by the Garbage Collection (GC), which is executed intermittently to reclaim that memory. Second, the memory consumed by the frame of a function is automatically reclaimed on the completion of function’s invocation. TakaTuka has an Offline GC to reduce the aggregated memory requirements of objects during execution of a program. The Offline GC allows freeing an object that is still reachable but is guaranteed not to be used again in the program. The Offline GC is shown to increase the amount of RAM available to a program by up to 66 % as compared with a typical online garbage collector. Another important feature of the TakaTuka JVM is variable Slot Size (VSS) scheme that reduces RAM requirements of the function’s frame. TakaTuka storage and RAM optimizations also increase the speed of a program ex- ecution thus increasing the lifetime of the mote. The storage, performance and RAM consumption features of the TakaTuka JVM makes it an attractive platform for develop- ing applications for wireless sensor networks employing microcontrollers and embedded devices. vii Acknowledgments I am very grateful to my supervisor Professor Christian Schindelhauer for his encourage- ment, help and inspiring guidance throughout the course of this work. He always believed in me and supported me in creating a team for the development of TakaTuka JVM. I could rely on him for all my problems including the personal ones. The formal and informal exchange of ideas with him and the influence of his personality helped me in becoming a better person. I am also obliged to my co-supervisor Professor Peter Thiemann for his continued guidance and counseling during this work. His qualified suggestions and vi- sion of the programming techniques proved extremely useful in corroborating my ideas and avoiding possible dead ends. I am greatly indebted to Professor Zartash Afzal Uzmi for stimulating my research interests and giving publishable shape to most of my work. I wish to thank two of my young colleagues, Luminous (Lu) Fennel and Gidon Ernst for their assistance in realizing the TakaTuka project. Gidon wrote the early version of the TakaTuka interpreter to help establish its feasibility. Lu transformed the TakaTuka JVM into a fully functional and usable form. I want to express my gratitude to Oliver Maye, of IHP-microelectronics, for certain improvements in the TakaTuka JVM and emancipating it from bugs. I would also like to acknowledge Elmar Haussmann for implementing Variable Slot Size (VSS) functionality in TakaTuka JVM, Jet Tang for porting TakaTuka to MSP430 family of processors, Omer Salem Al-Khanbushi for writing Apache-Ant script, Zhongjie Cai for improving TakaTuka multi-threads support and Shou-Yu Chao for providing Java debugger (JDB) functionality on motes and porting CLDC library to the TakaTuka JVM. I am grateful to all my friends and colleagues, especially Saqib Raza, Fahad Rafique Dogar, Arne Vater, Stefan Rührup, Wasim Malik, Asif M. Siddiqui and Professor Sohail Aslam, for continued support and encouragement I have received from them. I am hugely obligated to my parents and also to my wife and children who were always there for me and patiently waited to see me successful. My thanks are also due to my uncle Dr. I. A. Azhar who reviewed the manuscript to improve the language of the thesis. Financial support of Higher Education Commission of Pakistan for carrying out my Ph.D. program is gratefully acknowledged. ix Contents Acknowledgements ix List of Tables xiii List of Figures xv Abbreviations xvii 1 Introduction 1 1.1 Existing JVMs for Microcontrollers .................... 3 1.2 Wireless Sensor Network .......................... 3 1.3 Programming Paradigm ........................... 5 1.4 Fundamentals of a Java ........................... 6 1.5 Challenges and Contributions ........................ 10 2 Offline Garbage Collection 17 2.1 Motivating Example ............................ 19 2.2 Offline GC Data-Flow Analyzer ...................... 20 2.3 Offline Garbage Identifier .......................... 25 2.4 Runtime Implementation .......................... 33 2.5 Related Work ................................ 35 2.6 Results and Discussion ........................... 36 3 Variable Slot Size 47 3.1 VSS Instruction Design ........................... 49 3.2 Changing Bytecode for Supporting VSS .................. 52 3.3 Results and Discussion ........................... 54 xi xii Contents 4 Bytecode Compaction 57 4.1 Available Opcodes ............................. 59 4.2 Single Instruction Compaction ....................... 60 4.3 Multiple Instruction Compaction (MIC) .................. 60 4.4 Interpreter Support for Bytecode Compaction ............... 67 4.5 Results and Discussion ........................... 70 5 Class File Optimizations 83 5.1 TakaTuka Constant Pool Optimization ................... 84 5.2 Dead Code Removal ............................ 86 5.3 TUK File Format .............................. 86 5.4 Results and Discussion ........................... 89 6 Interpreter Design 91 6.1 Runtime Architecture ............................ 92 6.2 Customized JVM .............................. 92 6.3 Hardware and Operating System Abstraction Layer ............ 93 6.4 TakaTuka Linkage With TinyOS ...................... 93 6.5 Tiny Java Debugger ............................. 94 6.6 Results and Discussion ........................... 96 7 Summary and Conclusions 99 References 105 List of Tables 2.1 Benchmarks used for the evaluation of the Offline GC. .......... 39 2.2 The number of times online GC is called by the benchmark programs, with and without Offline GC. ........................ 41 3.1 Set of additional bytecode instructions in the TakaTuka VSS Extension. 51 4.2 Detailed