Compiling Java for Real-Time Systems Anders Nilsson - - PowerPoint PPT Presentation

compiling java for real time systems
SMART_READER_LITE
LIVE PREVIEW

Compiling Java for Real-Time Systems Anders Nilsson - - PowerPoint PPT Presentation

Compiling Java for Real-Time Systems Anders Nilsson andersn@cs.lth.se Department of Computer Science, Lund University, Sweden Compiling Java for Real-Time Systems p.1 Outline Introduction Approach Real-Time Execution Platform


slide-1
SLIDE 1

Compiling Java for Real-Time Systems

Anders Nilsson

andersn@cs.lth.se

Department of Computer Science, Lund University, Sweden

Compiling Java for Real-Time Systems – p.1

slide-2
SLIDE 2

Outline

  • Introduction
  • Approach
  • Real-Time Execution Platform
  • A Compiler for Real-Time Java
  • Experimental Verification
  • Future Work
  • Conclusions

Compiling Java for Real-Time Systems – p.2

slide-3
SLIDE 3

Introduction

  • Most computers in the world are embedded,

with various RT demands

  • Software complexity is increasing drastically
  • Programming language problems:
  • Type casts, as in C
  • Pointer arithmetics
  • No array bounds checking
  • Manual memory management, malloc/free
  • Lack of encapsulation

Bugs are easily created, but hard to find

Compiling Java for Real-Time Systems – p.3

slide-4
SLIDE 4

Hypothesis

Safe OO programming languages proved beneficial in other software development areas

  • Encapsulation
  • Strict type safety
  • Many errors caught by compiler. Remaining

errors caught and handled by run-time checks

  • Automatic memory management

All possible results of the execution are expressed by the source code

Compiling Java for Real-Time Systems – p.4

slide-5
SLIDE 5

Java or C#

  • Both are safe (except explicit unsafe in C#)

OO programming languages

  • Built-in concurrency and synchronization
  • Exception handling
  • Platform “independent”

But, Java is more mature, available on more de- velopment platforms, and there is an open-source class library available

Compiling Java for Real-Time Systems – p.5

slide-6
SLIDE 6

Problem Statement

Can standard Java be used as a programming language on arbitrary hardware platforms with varying degrees

  • f real-time-, memory footprint-, and

performance demands?

  • r

Write once run anywhere, for severely resource-constrained real-time systems?

Compiling Java for Real-Time Systems – p.6

slide-7
SLIDE 7

Standards

Two RT Java standards; RTJ and JConsortium None complies with Real (J2SE) Java:

  • Assuming (hard) RTGC not feasible
  • Numerous memory types:

Immortal,Scoped, Raw, Heap

  • Effectively return memory management to the

programmer Several Java benefits are lost. There must be a better way!

Compiling Java for Real-Time Systems – p.7

slide-8
SLIDE 8

Key Concepts

Considerations for Real Real-Time Java in embedded systems.

  • Portability
  • Scalability
  • Real-time execution and performance
  • Real-time communication
  • Applicability

Utilize the language, adopt run-time to embedded needs

Compiling Java for Real-Time Systems – p.8

slide-9
SLIDE 9

Approach

  • 1. Small memory footprint and high

performance ⇓ Natively compiled Java (no JVM)

  • 2. Any hardware comes with a C compiler

⇓ Use ANSI C as intermediate (high-level assembly) language

Compiling Java for Real-Time Systems – p.9

slide-10
SLIDE 10

External Code

Need to link with external (not GC-aware) code.

  • Hardware device drivers.
  • Code libraries.
  • Legacy software.
  • Automatically generated code from high-level

tools (Matlab/Real-Time Workshop). Typically, no (usable) source code available.

Compiling Java for Real-Time Systems – p.10

slide-11
SLIDE 11

RT Memory Mangement

  • Incremental GC in a medium priority thread.
  • High priority threads pay no overhead penalty

during allocation.

  • Low priority threads pay overhead for

themselves AND the high priority threads. Compacting GC ⇒ Shorter maximum latencies than malloc/free.

Compiling Java for Real-Time Systems – p.11

slide-12
SLIDE 12

Compacting GC

  • Schedule so as not to disturb high-priority

threads

  • Read-barrier needed; objects relocate
  • Calls to external functions become critical

sections

  • No fragmentation
  • Average performance normally decreases

Compiling Java for Real-Time Systems – p.12

slide-13
SLIDE 13

Non-Moving GC

  • Schedule so as not to disturb high-priority

threads

  • Fixed size memory blocks ⇒ large objects

are split in several memory blocks. Problematic with external code

  • No read barrier

Less overhead than Compacting GC

Compiling Java for Real-Time Systems – p.13

slide-14
SLIDE 14

C/C++ compatibility option

Non-moving GC with variable memory block sizes and a good memory allocator

  • Johnstone et al. 1998. Memory fragmentation

is not a serious problem in real applications

  • No read barrier
  • As deterministic as using malloc() and

free() in C

  • Can call external code, that uses Java objects

Compiling Java for Real-Time Systems – p.14

slide-15
SLIDE 15

Latency and Preemption

  • Native preemption promotes short latency

and allows external code, but may introduce (external) fragmentation ⇒ deficient predictability (as in C++)

  • 100% Java and appropriate run-time

⇒ Hard RT Java. To improve average performance:

  • Preemption points ⇒ higher latency
  • Block-based GC ⇒ internal fragmentation

Hence a tradeoff: Latency ↔ Fragmentation

Compiling Java for Real-Time Systems – p.15

slide-16
SLIDE 16

Real-Time Execution Environment

Frenchmen, I die guiltless of the countless crimes imputed to me. Pray God my blood fall not on France! Lois XVI, 1793

Compiling Java for Real-Time Systems – p.16

slide-17
SLIDE 17

Real-Time Execution Environment

  • Garbage Collector Interface
  • Different strategies require different code
  • Class Library
  • Native methods using GCI. Domain-specific I/O
  • Threads and Synchronization
  • Map thread primitives on native OS
  • RTThread classes @CS
  • Exceptions
  • Only one active exception per thread
  • Implemented with setjmp/longjmp

Compiling Java for Real-Time Systems – p.17

slide-18
SLIDE 18

Garbage Collector Interface

class MyClass { void foo() { String foo = new String("Hello World!"); System.out.println(foo); } } GC_PROC_BEGIN(_MyClass_foo, GC_PARAM(MyClass,this)) GC_PARAM_REF(MyClass,this); GC_PUSH_PARAM(this); GC_ENTER GC_REF(String,foo); GC_PUSH_ROOT(foo) GC_NEW(String,foo,"Hello World!"); GC_PROC_CALL(System_out_println,foo); GC_POP_ROOT(foo); GC_LEAVE GC_POP_PARAM(this); GC_PROC_END(_MyClass_foo)

Compiling Java for Real-Time Systems – p.18

slide-19
SLIDE 19

Java Compiler

  • Java based compiler-compiler, generating

Java (to C) translator, in Java

  • Based on Reference Attributed Grammars,

JastAdd

  • AOP for modular semantics, optimization and

code generating

Compiling Java for Real-Time Systems – p.19

slide-20
SLIDE 20

Compiler Overview

Compiling Java for Real-Time Systems – p.20

slide-21
SLIDE 21

Name Transformations

a.b = c ⇒ GC_SET(a,b,c)

a.b = c.d; ⇓ tmp_1 = c.d; a.b = tmp_1;

= b a c d = tmp_1 d c = b a tmp_1

Compiling Java for Real-Time Systems – p.21

slide-22
SLIDE 22

Code Generation

Compiling Java for Real-Time Systems – p.22

slide-23
SLIDE 23

Evaluation

Lines of code Parser and AST Abstract Grammar 181 Concrete Grammar 1044 Semantic Analysis Name- and Type Analysis 1458 Transformations and Optimizations Simplifications 901 Dead Code Optimization 154 Code Generation C code generation 5745 TOTAL 9473

Compiling Java for Real-Time Systems – p.23

slide-24
SLIDE 24

Compiler Performance

Our compiler gcj javac HelloWorld Memory usage (MB) 14 <5 21 Time (s) 26 0.65 3 RobotController Memory usage (MB) 34

  • 30

Time (s) 160

  • 9

Compiling Java for Real-Time Systems – p.24

slide-25
SLIDE 25

Experimental Verification

To which extent are the key concept requirements fulfilled?

  • Portability
  • Scalability
  • Real-time execution and performance
  • Real-time communication
  • Applicability

Compiling Java for Real-Time Systems – p.25

slide-26
SLIDE 26

Portability

Current supported platforms AVR PPC i386 SPARC CSRTK X STORK X Linux RTAI(k) X X Linux RTAI(u) X X Posix X X X

Compiling Java for Real-Time Systems – p.26

slide-27
SLIDE 27

Scalability

Low end prototype

  • Atmel AVR ATmega 103
  • 8 bit RISC Architecture,

6 MHz ⇒ 6 MIPS

  • 32 Registers, 128 KB Flash,

4KB RAM

  • Real-Time

Clock, UART, Timers, 8-channel 10-bit ADC

  • LCD Display, Summer, 6 buttons
  • Tiny in-house RTOS

Multi(3)-threaded application in less than 62 KB ROM and 32 KB RAM, including run-time

Compiling Java for Real-Time Systems – p.27

slide-28
SLIDE 28

Real-Time Execution

T1 T2 T3 GC1 Period (µs) 100 300 500 NA Workload (µs) 30 50 90 NA

50 100 150 200 250 300 350 20 40 60 Latency and response times for three periodic threads High priority number of samples (10 KHz) us 20 40 60 80 100 120 50 100 150 200 Medium priority number of samples (3.3 KHz) us 10 20 30 40 50 60 70 100 200 300 Low priority number of samples (2 KHz) us

Compiling Java for Real-Time Systems – p.28

slide-29
SLIDE 29

General Performance

fibonacci (virtual) fibonacci (static) scalar Our compiler (ms) mark-compact GC 10050 7012 146400 mark-sweep GC 7002 6904 7760 no GC 753 586 5402 Other (ms) Sun JVM 271 251 5085 Sun JVM -server 270 245 3910 Sun JVM -Xint 3302 3120 52500 GCJ 360 567 10098 GCJ -O3 328 504 2249 Hand-written C GCC NA 280 6810 GCC -O3 NA 293 761

Compiling Java for Real-Time Systems – p.29

slide-30
SLIDE 30

Real-Time Communication

  • Real-time network protocol available:

ThrottleNet (@control.lth.se)

  • Successful experiments with compiled Java

and RTAI: Patrycja Grudziecka and Daniel Nyberg (2004)

Compiling Java for Real-Time Systems – p.30

slide-31
SLIDE 31

Applicability

Tested on many platforms with different levels of real-time requirements.

  • Atmel AVR, Hard real-time
  • Motorola PPC G4, Hard real-time
  • RTAI Linux, Hard real-time
  • Posix, No real-time

Compiling Java for Real-Time Systems – p.31

slide-32
SLIDE 32

Conclusions – General

  • Java (safe & portable) highly desirable for

flexible RT systems

  • Use the language (no JVM) for embedded

systems!

  • Real (based on J2SE) Real-Time Java is

feasible

  • Standard memory model to be kept (RTGC is
  • k)

Compiling Java for Real-Time Systems – p.32

slide-33
SLIDE 33

Conclusions – tradeoffs

Non-moving GC + Can link with external binary code that can use Java objects + Latency as good as C++

  • Predictability as bad as with C++

Compacting GC

  • 100% Java (or open source) re-

quired (no ext. code)

  • Decreased average performance

+ Hard RT Java!

Trade latency for:

  • predictability (using compacting GC)
  • average performance (using compacting GC and

preemption points)

Compiling Java for Real-Time Systems – p.33

slide-34
SLIDE 34

Contributions – Real-Time Java

  • A prototype implementation of hard

Real-Time Java

  • The Garbage Collector Interface
  • A Real-Time Exception implementation
  • Latency ⇔ Predictability tradeoff

Compiling Java for Real-Time Systems – p.34

slide-35
SLIDE 35

Conclusions – CC

  • OO AST, RAGs and AOP renders a very

compact, yet clear compiler implementation

  • Code analysis, refactorings and optimizations

are conveniently described as aspects, possibly performing transformations, on the AST

  • Current implementation is substantially slower

than other compilers, but still fast enough.

Compiling Java for Real-Time Systems – p.35

slide-36
SLIDE 36

Contributions – CC

  • A compiler for a complete real-world OO

language, based on RAGs and AOP

  • A new way of implementing high-level
  • ptimizations as a set of AST transformations
  • Use AST transformations to simplify

expressions makes code generation easier

Compiling Java for Real-Time Systems – p.36

slide-37
SLIDE 37

Future Work

  • Full-scale applications
  • Improve general performance
  • GC synchronization, memory allocation and OO
  • ptimizations
  • Networking
  • Dynamic class loading
  • Klas Nilsson et al. 1998
  • WCET analysis
  • Patrik Persson 2000
  • Hybrid execution environment
  • Compiled Java ⇔ JVM

Compiling Java for Real-Time Systems – p.37

slide-38
SLIDE 38

The End

  • Portability – OK
  • Scalability – OK
  • Real-time execution – OK
  • Real-time communication – OK
  • Applicability – OK
  • Performance – Needs more work

Write once run anywhere, for severely resource-constrained real-time systems!

Compiling Java for Real-Time Systems – p.38