Exceptional Actors Implementing Exception Handling for Encore
Total Page:16
File Type:pdf, Size:1020Kb
IT 17 050 Examensarbete 15 hp Juli 2017 Exceptional Actors Implementing Exception Handling for Encore Sahand Shamal Taher Institutionen för informationsteknologi Department of Information Technology This page intentionally left blank i Abstract Exceptional Actors: Implementing Exception Handling for Encore Sahand Shamal Taher Teknisk- naturvetenskaplig fakultet UTH-enheten Encore is an object-oriented programming language which uses the actor model as its concurrency model, and is specifically aimed at Besöksadress: implementing concurrent and parallel systems. Communication between Ångströmlaboratoriet Lägerhyddsvägen 1 actors is done via asynchronous method calls, which store their results Hus 4, Plan 0 in futures, placeholder objects for later storing the result of an computation. Encore currently lacks an exception handling mechanism, Postadress: which is an important part of programming languages, and helps Box 536 751 21 Uppsala programmers create more fault tolerant and robust software. This thesis presents an exception handling implementation for Encore, in which Telefon: futures are used to propagate errors between actors. The implementation 018 – 471 30 03 uses a modified version of the open-source library Exceptions4C as a Telefax: basis, and enables basic exception handling through try-catch-finally 018 – 471 30 00 expressions. It uses type-based matching that also considers subtyping, and allows the programmer to define and throw custom exception types. A Hemsida: pull-model is used to enable inter-process communication of exceptions, http://www.teknat.uu.se/student where receiving actors can choose to handle exceptions at any point by consuming associated futures. The implementation is a good first step, but there are Encore features such as streams which it does not yet support. Furthermore, it brings an overhead to programs, which can be reduced by redesigning parts of the exception handling model. Handledare: Dave Clarke Ämnesgranskare: Tobias Wrigstad Examinator: Olle Gällmo IT 17 050 Tryckt av: Reprocentralen ITC This page intentionally left blank iii Contents 1 Introduction 1 2 Background 3 2.1 Encore ................................3 2.1.1 Message Passing and Futures . .3 2.1.2 Cooperative Scheduling Primitives . .4 2.1.3 Threads and Scheduling . .5 2.2 Exceptions . .6 2.2.1 Exception Handling in C ..................7 2.2.2 The Exceptions4C Library . .8 2.3 Related Work . 10 3 Exception Handling in Encore 17 3.1 Synchronous Exceptions . 17 3.2 Asynchronous Exceptions . 19 4 Implementation 21 4.1 Language Constructs . 21 4.2 Exception Objects . 23 4.3 Communicating Exceptions Between Actors . 25 4.4 Protecting the Runtime and Future Fulfills . 25 4.4.1 Encore's Dispatch Mechanism . 26 4.4.2 Modifying the Dispatch Mechanism . 26 4.5 Scope of Exception Contexts . 27 5 Evaluation 29 5.1 Expressiveness . 29 5.1.1 Synchronous exceptions . 29 5.1.2 Asynchronous Exceptions . 31 5.2 Performance . 32 5.2.1 Runtime Overhead Experiments . 32 5.2.2 Scalability Experiments . 33 5.2.3 Results . 33 5.3 Limitations . 36 iv 5.3.1 Compiler Level Issues and Refactoring . 37 5.3.2 Streaming Methods and Future Chaining . 37 5.3.3 Exception Frame Limit . 38 6 Conclusion 39 6.1 Future Work . 40 6.1.1 Redundant Allocation of Exception Objects . 40 6.1.2 Custom Data in Exceptions . 41 6.1.3 The Exception Handling Model . 42 Appendices 47 A Savina Tests 48 A.1 Pingpong . 48 A.2 Counting . 49 A.3 Fib . 50 A.4 Chameneos . 52 A.5 Big . 58 A.6 BndBuffer . 61 v 1. Introduction Exception handling is an important feature of programming languages and helps programmers create more fault tolerant and robust software. As distributed pro- cessing has gained increased attention, new concurrency models and exception handling models have emerged. The actor model is a concurrency model that is gaining increased attention [1, 4, 10, 14, 19{21, 36], and has been implemented for languages such as Java [18] and Scala [30], and in the .NET framework [7]. Some languages, such as Dart [8] and Erlang [3, 4], are even built with the actor model as the default concurrency model. Encore, developed by the programming language group at Uppsala Univer- sity, is an object-oriented programming language which uses the actor model as its concurrency model, and is specifically aimed at implementing concurrent and parallel systems [9]. Communication between actors is done via asynchronous method calls, which store their results in futures, placeholder objects for later storing the result of an computation [5, 24]. The Encore compiler is written in Haskell and translates Encore code to C code, which links with a runtime system written in C. Goal The Encore language currently lacks an exception handling mecha- nism, providing the programmer no help in recovering from errors. To solve this, Encore needs an exception handling mechanism which fits in with the concurrency model of Encore and preserves the semantics of existing language while using a reasonable amount of resources. As a starting point for Encore's exception handling, the Encore team has suggested a Java-inspired model to handle exceptions in synchronous settings, and to use futures to communicate exceptions asynchronously, inspired by the paper Fault in the Future [22]. The goal of this thesis is to implement an exception handling mechanism for Encore, based on the suggestions of the Encore team. A Java-inspired model for exception handling in synchronous settings will be provided, and futures be utilized to communicate exceptions in asynchronous settings. Contributions Concretely, this thesis makes the following contributions to the design and implementation of the Encore programming language: 1. It extends Encore with a new mechanism for raising and handling ex- ceptions using a throw and try-catch-finally mechanism. The imple- 1 mented try-catch-finally are expressions which return values. 2. It extends Encore with a family of exceptions which can be extended by a programmer. This allows distinguishing between different types of exceptions. 3. It extends Encore with an exception, representing an instance of a thrown exception. The exception objects give the programmer access to exception data, and a method for rethrowing the exception. 4. It provides exception handling in asynchronous settings by extending En- core's futures and the get mechanism. The futures are modified to store exceptions, and the get operation to rethrow exceptions in futures. The context swapping mechanism is extended to support exception handling contexts, which contain message level exception handling information. 5. It provides Encore with support to deal with exceptions in functional settings. An Either-object encapsulates a value, and either contains a normal value or an exception. Programs can distinguish between the nor- mal and exceptional value through pattern matching. In Chapter 2 relevant background information is presented, providing a ba- sic understanding of Encore, exception handling and exception handling in existing languages. The exception handling model implemented in this work is presented in Chapter 3, where new language constructs are introduced, and the use of futures during exception handling is explained. Implementation details for exception handling model are outlined in Chapter 4, where code generation for new language constructs is shown, an exception class is introduced, and changes to the runtime and context swapping is explained. In Chapter 5, the exception handling implementation is evaluated in terms of expressiveness, per- formance, and limitations. A conclusion of the work is given in Chapter 6, along with a discussion about the results and future work. 2 2. Background This chapter introduces Encore and its concurrency model (Section 2.1) and basic exception handling (Section 2.2). Basic exception handling in C is dis- cussed in Section 2.2.1, and a C library for exception handling used in this work presented in Section 2.2.2. Exception handling in related programming languages and works is discussed in (Section 2.3). 2.1 Encore Encore is an object-oriented programming language which uses the actor model as its concurrency model, and is specifically aimed at implementing concurrent and parallel systems [9]. Like regular objects, Encore's actors have encap- sulated fields and methods that operate on those fields. Unlike objects, how- ever, a thread of control is associated with each actor, which is the only thread able to access the actor's fields [9]. Communication between actors is done via asynchronous method calls, which store their results in futures (Section 2.1.1), placeholders for later storing the result of a method call [5, 24]. The Encore compiler is written in Haskell and translates Encore code to C code, which links with a runtime system written in C. Encore's runtime runs on top of the runtime of the Pony programming language [25], which is an actor-based, object-oriented programming language. The Pony runtime provides an im- plementation of the actor model and garbage collection, which Encore builds on. The rest of this section details different parts of Encore's concurrency model that are relevant when implementing exception handling for Encore. Message passing and futures, which provide a means for asynchronous commu- nication, are explained in Section 2.1.1, and cooperative scheduling primitives and their affect on actors in Section 2.1.2. Section 2.1.3 discusses Encore's threads and how they are scheduled. 2.1.1 Message Passing and Futures Communication between actors is done via asynchronous method calls, or mes- sage sends, by using the \!" operator. Asynchronous method calls immediately return, optionally with a future, which is used for synchronization and make 3 blocking operations possible (Figure 2.2). Upon finishing a method, the called actor fulfills or resolves a future with the computed result, which the caller can later retrieve from the future. Each actor has its own inbox, where messages to the actor are placed, each message corresponding to running some method.