Real-Time Software. Exceptions and Low-level Programming. René Rydhof Hansen. 2 November 2010

Similar documents
The Real-time Specification for Java

Safety Critical Java. Language Standards and Analysis Techniques for Java in Safety Critical applications. aicas GmbH

Real-Time Java David Holmes

Real-Time Java. Martin Schöberl

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

Empirical Analysis of Real-Time Java Performance and Predictability

Concurrent and Real-Time Programming in Java

A Real-Time RMI Framework for the RTSJ

The Design and Performance of Real-time Java Middleware

Programming Languages for Real-Time Systems. LS 12, TU Dortmund

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

Concurrent Programming. Alexandre David

Control Abstraction. Hwansoo Han

Concurrent and Real-Time Programming in Java

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

Concurrent Programming using Threads

Asynchronous Event Handling and Safety Critical Java

Run-time Environments

Run-time Environments

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

The Procedure Abstraction

G Programming Languages - Fall 2012

C++ for System Developers with Design Pattern

UNIT 3

CPS221 Lecture: Threads

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru Department of Electronics and Communication Engineering

EECS 3221 Operating System Fundamentals

EECS 3221 Operating System Fundamentals

Green Hills Software, Inc.

Volume II, Section 5 Table of Contents

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Real-Time and Embedded Systems (M) Lecture 19

In Java we have the keyword null, which is the value of an uninitialized reference type

Recommendations for a CORBA Language Mapping for RTSJ

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Exception handling. Exceptions can be created by the hardware or by software: Examples. Printer out of paper End of page Divide by 0

Hardware OS & OS- Application interface

Chapter 3 Processes. Process Concept. Process Concept. Process Concept (Cont.) Process Concept (Cont.) Process Concept (Cont.)

Processes. Johan Montelius KTH

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 11

A process. the stack

GUJARAT TECHNOLOGICAL UNIVERSITY MASTER OF COMPUTER APPLICATION SEMESTER: III

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

Embedded Software Programming

National University. Faculty of Computer Since and Technology Object Oriented Programming

MAKING JAVA HARD REAL-TIME

Run-Time Environments/Garbage Collection

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 11

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Lecture Notes on Garbage Collection

Programming Languages Third Edition. Chapter 7 Basic Semantics

CS370 Operating Systems

Asynchronism. Asynchronism. Asynchronism. Interrupts. Asynchronism. Interrupts. Asynchronism close to hardware

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

Operating Systems. Steven Hand. Michaelmas / Lent Term 2008/ lectures for CST IA. Handout 4. Operating Systems

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

CS370 Operating Systems

Lecture 23: Object Lifetime and Garbage Collection

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Chapter 4: Multithreaded Programming

Diagram of Process State Process Control Block (PCB)

Introduction. CS3026 Operating Systems Lecture 01

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Memory Management for Safety-Critical Java

CHAPTER 3 - PROCESS CONCEPT

Lecture Notes on Programming Languages

Real-time Support in Operating Systems

A Hardware Abstraction Layer in Java

REAL-TIME CONVERGENCE OF ADA AND JAVA

Programming with Non-Heap Memory in the Real Time Specification for Java

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

OVERVIEW. Last Week: But if frequency of high priority task increases temporarily, system may encounter overload: Today: Slide 1. Slide 3.

Computer System Overview. Chapter 1

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Outline. Introduction. Survey of Device Driver Management in Real-Time Operating Systems

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Chapter 4: Threads. Operating System Concepts 9 th Edition

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Chapter 14. Exception Handling and Event Handling ISBN

Top-Level View of Computer Organization

CMSC 330: Organization of Programming Languages

Time Handling in Programming Language

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

The control of I/O devices is a major concern for OS designers

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Chapter 3: Processes. Operating System Concepts 9 th Edition

Chapter 3: Processes

Process Concept. Chapter 4: Processes. Diagram of Process State. Process State. Process Control Block (PCB) Process Control Block (PCB)

Chapter 4: Processes

Research on the Novel and Efficient Mechanism of Exception Handling Techniques for Java. Xiaoqing Lv 1 1 huihua College Of Hebei Normal University,

Lecture 2 Process Management

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Embedded Systems Programming

Q.1 Explain Computer s Basic Elements

Transcription:

Real-Time Software Exceptions and Low-level Programming René Rydhof Hansen 2 November 2010 TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 1 / 35

Today s Goals Exceptions and Exception Handling (briefly) Low-level programming Input/output Language support Memory management TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 2 / 35

Part II Exceptions and Exception Handling TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 3 / 35

General Requirements for Exception Handling R1 The facility must be simple to understand and use R2 The code for exception handling should not obscure understanding of the program s normal error-free operation R3 The mechanism should be designed so that run-time overheads are incurred only when handling an exception R4 The mechanism should allow the uniform treatment of exceptions detected both by the environment and by the program R5 The exception mechanism should allow recovery actions to be programmed TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 4 / 35

Exception Handling Error return Functions return a value outside normal range, indicating success/failure Classic C/UNIX/POSIX Alternative: use global/shared error variable for error indication Non-local goto In C: setjmp and longjmp Requires more clean-up than local goto, e.g., call stack High-level exceptions TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 5 / 35

Exception Handling in Real-Time Java Exceptions in Java are integrated into the OO model In Java, all exceptions are subclasses of the predefined class java.lang.throwable The language also defines other classes, for example: Error, Exception, and RuntimeException Not all blocks can have exception handlers Domain exception handlers must be explicitly indicated using try/catch t r y { // statements which may raise exceptions } catch ( ExceptionType e) { // handler for e } Real-Time Java supports termination model TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 6 / 35

Resumption versus termination model Should the invoker of the exception continue its execution after the exception has been handled If the invoker can continue, then it may be possible for the handler to cure the problem that caused the exception to be raised and for the invoker to resume as if nothing has happened This is referred to as the resumption or notify model The model where control is not returned to the invoker is called termination or escape Clearly it is possible to have a model in which the handler can decide whether to resume the operation which caused the exception, or to terminate the operation; this is called the hybrid model TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 7 / 35

The Resumption Model Problem: it is difficult to repair errors raised by the RTS Eg, an arithmetic overflow in the middle of a sequence of complex expressions results in registers containing partial evaluations; calling the handler overwrites these registers Implementing a strict resumption model is difficult, a compromise is to re-execute the block associated with the exception handler; Eiffel provides such a facility. Note that for such a scheme to work, the local variables of the block must not be re-initialised on a retry The advantage of the resumption model comes when the exception has been raised asynchronously and, therefore, has little to do with the current process execution TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 8 / 35

Exception Propagation If there is no handler associated with the block or procedure regard it as a programmer error which is reported at compile time but an exception raised in a procedure can only be handled within the context from which the procedure was called eg, an exception raised in a procedure as a result of a failed assertion involving the parameters CHILL requires that a procedure specifies which exceptions it may raise (that is, not handle locally); the compiler can then check the calling context for an appropriate handler Java allows a function to define which exceptions it can raise; however, unlike CHILL, it does not require a handler to be available in the calling context TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 9 / 35

Alternative Approach Propagating the exception: Look for handlers up the chain of invokers; the Ada and Java approach A problem occurs where exceptions have scope; an exception may be propagated outside its scope, thereby making it impossible for a handler to be found Most languages provide a catch all exception handler An unhandled exception causes a sequential program to be aborted If the program contains more than one process and a particular process does not handle an exception it has raised, then usually that process is aborted However, it is not clear whether the exception should be propagated to the parent process TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 10 / 35

Part III Low-level Programming TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 11 / 35

Hardware Input/Ouput Mechanisms Input/output performed through device registers Two approaches to I/O: port mapped and memory mapped Port mapped I/O Requires special I/O bus and I/O address space (ports) Special machine instructions Better (high granularity) control (potentially) More explicit, easier to detect automatically (program analysis) Memory mapped I/O Special addresses in normal memory address space Uses same (logical) bus as normal data traffic Only uses normal memory access instructions TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 12 / 35

Controlling Input/Output Polling (aka. status driven) Active polling of device status Historically cheaper than interrupt driven More deterministic than interrupt driven Three kinds of instructions (typically) needed 1 Test operations 2 Control operations 3 I/O operations Interrupt-driven Device coomunicates by requesting an interrupt More efficient/better utilisation (typically) than polling Several variations 1 Interrupt-driven program-controlled 2 Interrupt-driven program-initiated (I/O controllsers, DMA) 3 Interrupt-driven channel-program controlled (I/O coprocessor) TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 13 / 35

Controlling I/O: Interrupt-driven I/O Interrupt-driven program-controlled I/O Device requests interrupt when data is ready Running task suspended I/O performed Suspended task resumed More non-deterministic than polling, esp. when interrupts are unbounded Interrupt-driven program-initiated I/O Program asks DMA to perform I/O Data transfer handled by DMA DMA requests interrupt when transfer is complete DMA uses memory cycles: cycle stealing May add to non-determinism TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 14 / 35

Controlling I/O: Interrupt-driven I/O Interrupt-driven channel-program controlled Used by I/O co-processor Three major components 1 The hardware channel 2 The channel program 3 I/O instructions May increase non-determinism TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 15 / 35

Interrupt-driven I/O: Requirements Context-switching mechanism Interrupt device identification Interrupt identification Interrupt control Priority control Hardware support for context switch Three levels (commonly): 1 Basic: only program counter (PC) 2 Partial (most common): PC and program status word (PSW) 3 Complete: full context (rare) Basic and partial often requires extra support in software Complete may be too specialised TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 16 / 35

Interrupt-driven I/O: Requirements Interrupt device identification What device generated an interrupt? Especially problem for multiplexed devices Interrupt vector (IV) table: each device (or device function) is assigned separate entry in IV table Status: necessary information stored in shared memory or accesible through status instructions; useful for generalised ISR Polling: when interrupt occurs, ask everyone who did it High-level language primitive: automatic translation (often using above techniques at a lower level) to high-level concept, e.g., message or event. TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 17 / 35

Interrupt-driven I/O: Requirements Interrupt identification Why was an interrupt generated? Information communicated through (shared memory) status word or by binding a device to multiple interrupts Interrupt Control Disabling/enabling interrupts form a particular device May be controlled individually by interrupt masking May be organised in a hierarchy Devices (currently) running at a lower logical level cannot interrupt when the processor (currently) runs at a higher logical level Priority Control Prioritising devices, often controlled using the interrupt control facility. TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 18 / 35

Language Support for Low-level Programming Modularity and encapsulation In particular: ways to encapsulate non-portable parts of the program; typically the low-level parts Not only relevant for RTSs! Abstract models of device handling Manipulating device registers Representation of interrupts Procedure call Sporadic task invocation Asynchronous notification Shared-variable condition synchronization Message based synchronization TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 19 / 35

Language Support: Real-Time Java Manipulating device registers Direct access to memory through the RawMemoryAccess Can only be used for simple types, i.e., not for user defined objects p u b l i c c l a s s ControlAndStatusRegister { RawMemoryClass rawmemory ; p u b l i c ControlAndStatusRegister ( long base, long size ) rawmemory = RawMemoryAccess. create ( IO_Page,base, size ); } p u b l i c v o i d setcontrolword ( s h o r t value ) { rawmemory. setshort (0, value ); }... channel = 6; shadow = ( channel << 8) start enable ; csr. setcontrolword ( shadow ); TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 20 / 35

Memory Management in RTSJ: RawMemoryAccess p u b l i c c l a s s RawMemoryAccess { p r o t e c t e d RawMemoryAccess ( RawMemoryAccess memory, long base, long size ); p u b l i c s t a t i c RawMemoryAccess create ( java. lang. Object type, long size ) throws SecurityException, OffsetOutOfBoundsException, SizeOutOfBoundsException, UnsupportedPhysicalMemoryException ; p u b l i c s t a t i c RawMemoryAccess create ( java. lang. Object type, long base, long size ) throws SecurityException, OffsetOutOfBoundsException, SizeOutOfBoundsException, UnsupportedPhysicalMemoryExc p u b l i c byte getbyte ( long offset ) throws SizeOutOfBoundsException, OffsetOutOfBoundsException ; } p u b l i c v o i d setbyte ( long offset, byte value ) throwssizeoutofboundsexception, OffsetOutOfBoundsException ; TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 21 / 35

Language Support: Real-Time Java Interrupt Handling RTSJ views an interrupt as an asynchronous event The interrupt is equivalent to a call of the fire method The association between the interrupt and the event is achieved via the bindto method in the AsyncEvent class The parameter is of string type, and this is used in an implementation-dependent manner one approach might be to pass the address of the interrupt vector When the interrupt occurs, the appropriate handler s fire method is called Now, it is possible to associate the handler with a schedulable object and give it an appropriate priority and release parameters TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 22 / 35

Language Support: Real-Time Java Interrupt Handling AsyncEvent Interrupt = new AsyncEvent (); AsyncEventHandler InterruptHandler = new BoundAsyncEventHandler ( priparams, releaseparams, n u l l, n u l l, n u l l ); Interrupt. addhandler ( InterruptHandler ); Interrupt. bindto (" 0177760 "); TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 23 / 35

Language Support: C/Real-Time POSIX Low-level Programming in C By design. TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 24 / 35

Memory Management Memory often very limited in RTSs Heap must be managed Manually, e.g., C: malloc and free Hard to get right Garbage collected, e.g., Java May lead to unpredictable timing behaviour Future: real-time garbage collection Stack must be managed Often through worst case memory consumption (WCMC) analysis Similar to WCET analysis TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 25 / 35

Memory Management in Real-Time Java 1 MemoryArea 1 HeapMemory (singleton) 2 ImmortalMemory 3 ImmortalPhysicalMemory 4 ScopedMemory 1 LTMemory 2 VTMemory TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 26 / 35

Memory Management in Real-Time Java: MemoryArea p u b l i c a b s t r a c t c l a s s MemoryArea { p r o t e c t e d MemoryArea ( long sizeinbytes ); p u b l i c v o i d enter ( java. lang. Runnable logic ); // associate this memory area to the current thread // for the duration of the logic. run method p u b l i c s t a t i c MemoryArea getmemoryarea ( java. lang. Object object ); // get the memory area associated with the object p u b l i c long memoryconsumed (); // number of bytes consumed in this memory area p u b l i c long memoryremaining (); // number of bytes remaining... p u b l i c s y n c h r o n i z e d java. lang. Object newinstance ( java. lang. Class type ) throws IllegalAccessException, InstantiationException, OutOfMemoryError ; // allocate an object } p u b l i c long size (); // the size of the memory area TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 27 / 35

ImmortalMemory Immortal memory is shared among all threads in an application Objects created in immortal memory are never subject to garbage collection and are freed only when the program terminates p u b l i c f i n a l c l a s s ImmortalMemory extends MemoryArea { p u b l i c s t a t i c ImmortalMemory instance (); } ImmortalPhysicalMemory has the same characteristics as immortal memory but allows objects to be allocated from within a range of physical addresses TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 28 / 35

ScopedMemory A memory area where the objects have a well-defined lifetime May be entered explicitly (through the use of the enter method) or implicitly by attaching it to a RealtimeThread at thread creation time Associated with each scoped memory is a reference counter which is incremented for every call to enter and at every associated thread creation It is decremented when the enter method returns and at every associaated thread exit When the reference counter reaches zero, all objects residing in the scoped memory have their finalization method executed and the memory is reclaimed Scoped memory can be nested by nested calls to enter TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 29 / 35

ScopedMemory p u b l i c a b s t r a c t c l a s s ScopedMemory extends MemoryArea { p u b l i c ScopedMemory ( long size ); p u b l i c v o i d enter ( java. lang. Runnable logic ); p u b l i c i n t getmaximumsize (); p u b l i c MemoryArea getouterscope (); } TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 30 / 35

ScopedMemory To avoid the possibility of dangling pointers, a set of access restrictions are placed on the use of the various memory areas Heap objects can reference other heap objects and objects in immortal memory only (i.e. it cannot access scoped memory) Immortal objects can reference heap objects and immortal memory objects only; Scoped objects can reference heaped objects, immortal objects and objects in the same scope or an outer scope only TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 31 / 35

ScopedMemory: Example import javax. realtime.*; p u b l i c c l a s s ThreadCode implements Runnable { p r i v a t e v o i d computation () { f i n a l i n t min = 1*1024; f i n a l i n t max = 1*1024; f i n a l LTMemory mymem = new LTMemory (min, max ); } mymem. enter (new Runnable () { p u b l i c v o i d run () { // code here which requires access // to temporary memory } } ); TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 32 / 35

ScopeMemory: Example (continued) The thread can now be created; note, no parameters other than the memory area and the Runnable are given ThreadCode code = new ThreadCode (); RealtimeThread mythread = new RealtimeThread ( n u l l, n u l l, n u l l, ImmortalMemory. instance (), n u l l, code ); TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 33 / 35

Stack Management Embedded programmers also have to be concerned with stack size Specifying the stack size of a task/thread requires trivial support (for example, in Ada it is via the Storage Size attribute applied to a task; in POSIX it is via pthread attributes) Calculating the stack size is more difficult; as tasks enter blocks and execute procedures their stacks grow To estimate the maximum extent of this growth requires knowledge of the execution behaviour of each task This knowledge is similar to that required to undertake WCET analysis WCET and worst-case stack usage bounds can be obtained from control flow analysis of the task s code TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 34 / 35

Summary Exceptions and Exception Handling (briefly) Low-level programming Input/output Language support Memory management TSW (2010e) (Lecture 13) Real-Time Software 2 November 2010 35 / 35