exploiting static application knowledge in a java compiler ... · exploiting static application...

28
Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael Stilkerich, Daniel Lohmann, Wolfgang Schr¨ oder-Preikschat http://www4.cs.fau.de/ ~ erhardt/ [email protected] KESO Case Study (JTRES 2011) 1 – 16

Upload: others

Post on 27-Jun-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Exploiting Static Application Knowledge in a JavaCompiler for Embedded Systems

A Case Study

Christoph Erhardt, Michael Stilkerich, Daniel Lohmann,Wolfgang Schroder-Preikschat

http://www4.cs.fau.de/~erhardt/

[email protected] KESO Case Study (JTRES 2011) 1 – 16

Page 2: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Motivation

Automotive industry

High robustness requirements

But: mass production, immense cost pressure

{Computational, memory} efficiency is key!

Challenge for the deployment of Java applications

System model of the OSEK/AUTOSAR OS

OS tailoring: only enable features needed by the application

Completely static configuration of the application world:

Fixed number of tasksNo dynamic code loadingFixed-priority scheduling

Let’s build a Java VM for this system model!

[email protected] KESO Case Study (JTRES 2011) Introduction 2 – 16

Page 3: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Motivation

Automotive industry

High robustness requirements

But: mass production, immense cost pressure

{Computational, memory} efficiency is key!

Challenge for the deployment of Java applications

System model of the OSEK/AUTOSAR OS

OS tailoring: only enable features needed by the application

Completely static configuration of the application world:

Fixed number of tasksNo dynamic code loadingFixed-priority scheduling

Let’s build a Java VM for this system model!

[email protected] KESO Case Study (JTRES 2011) Introduction 2 – 16

Page 4: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

The KESO Multi-JVM System architecture

Java-to-C ahead-of-time compilerVM tailoring, static configuration

Peripheral DeviceAccess(KNI)

DomainA

Static Fields

Heap

SystemObjects

TaskA1 TaskA2 Alarm1 Resource

Microcontroller

OSEK /AUTOSAR OS

Domain BServicePortal

OSEK API(KNI)

SharedMemory

Domain Zero (TCB)

GC Task

Control Flows

TaskA1 ISR1TaskA2

[email protected] KESO Case Study (JTRES 2011) Introduction 3 – 16

Page 5: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

The KESO Multi-JVM System architecture

Java-to-C ahead-of-time compilerVM tailoring, static configuration

Peripheral DeviceAccess(KNI)

DomainA

Static Fields

Heap

SystemObjects

TaskA1 TaskA2 Alarm1 Resource

Microcontroller

OSEK /AUTOSAR OS

Domain BServicePortal

OSEK API(KNI)

SharedMemory

Domain Zero (TCB)

GC Task

Control Flows

TaskA1 ISR1TaskA2

[email protected] KESO Case Study (JTRES 2011) Introduction 3 – 16

Page 6: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

The KESO Multi-JVM System architecture

Java-to-C ahead-of-time compilerVM tailoring, static configuration

Peripheral DeviceAccess(KNI)

DomainA

Static Fields

Heap

SystemObjects

TaskA1 TaskA2 Alarm1 Resource

Microcontroller

OSEK /AUTOSAR OS

Domain BServicePortal

OSEK API(KNI)

SharedMemory

Domain Zero (TCB)

GC Task

Control Flows

TaskA1 ISR1TaskA2

[email protected] KESO Case Study (JTRES 2011) Introduction 3 – 16

Page 7: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

The KESO Multi-JVM System architecture

Java-to-C ahead-of-time compilerVM tailoring, static configuration

Peripheral DeviceAccess(KNI)

DomainA

Static Fields

Heap

SystemObjects

TaskA1 TaskA2 Alarm1 Resource

Microcontroller

OSEK /AUTOSAR OS

Domain BServicePortal

OSEK API(KNI)

SharedMemory

Domain Zero (TCB)

GC Task

Control Flows

TaskA1 ISR1TaskA2

[email protected] KESO Case Study (JTRES 2011) Introduction 3 – 16

Page 8: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

The KESO Multi-JVM System architecture

Java-to-C ahead-of-time compilerVM tailoring, static configuration

Peripheral DeviceAccess(KNI)

DomainA

Static Fields

Heap

SystemObjects

TaskA1 TaskA2 Alarm1 Resource

Microcontroller

OSEK /AUTOSAR OS

Domain BServicePortal

OSEK API(KNI)

SharedMemory

Domain Zero (TCB)

GC Task

Control Flows

TaskA1 ISR1TaskA2

[email protected] KESO Case Study (JTRES 2011) Introduction 3 – 16

Page 9: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Static knowledge The basis for tailoring and optimizations in KESO

KESO relies upon the availability of extensive application knowledgeat compile-time.

Sources of knowledge

1. System model

Abandon the aspects of the Java programming model that don’t fitthe OSEK/AUTOSAR system model!“Everything is static”

2. Static application system configuration

Isolation domainsEntry pointsSystem objects

3. Application bytecode

Analyze to gain additional knowledgeOptimize aggressively using the aggregated knowledge

[email protected] KESO Case Study (JTRES 2011) Introduction 4 – 16

Page 10: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Static knowledge The basis for tailoring and optimizations in KESO

KESO relies upon the availability of extensive application knowledgeat compile-time.

Sources of knowledge

1. System model

Abandon the aspects of the Java programming model that don’t fitthe OSEK/AUTOSAR system model!“Everything is static”

2. Static application system configuration

Isolation domainsEntry pointsSystem objects

3. Application bytecode

Analyze to gain additional knowledgeOptimize aggressively using the aggregated knowledge

[email protected] KESO Case Study (JTRES 2011) Introduction 4 – 16

Page 11: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Static knowledge The basis for tailoring and optimizations in KESO

KESO relies upon the availability of extensive application knowledgeat compile-time.

Sources of knowledge

1. System model

Abandon the aspects of the Java programming model that don’t fitthe OSEK/AUTOSAR system model!“Everything is static”

2. Static application system configuration

Isolation domainsEntry pointsSystem objects

3. Application bytecode

Analyze to gain additional knowledgeOptimize aggressively using the aggregated knowledge

[email protected] KESO Case Study (JTRES 2011) Introduction 4 – 16

Page 12: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Exploiting static knowledge in the compiler

Introduction

Exploiting static knowledge in the compiler

Results

Conclusion

[email protected] KESO Case Study (JTRES 2011) Exploiting static knowledge in the compiler 5 – 16

Page 13: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Optimizations

Static whole-program (or rather: whole-domain) analysis

Starting at each domain’s entry points

Combined control flow, data flow and class hierarchy analysis

Since the application’s static nature enables us to collect extensiveinformation ahead of time, we can apply aggressive optimizations.

Improved standard optimizations

Method inlining

Constant propagation & folding

Escape analysis & stack allocation

Runtime check elimination (null-, bounds checks)

Dead-code elimination

Devirtualization

[email protected] KESO Case Study (JTRES 2011) Exploiting static knowledge in the compiler 6 – 16

Page 14: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Optimizations

Static whole-program (or rather: whole-domain) analysis

Starting at each domain’s entry points

Combined control flow, data flow and class hierarchy analysis

Since the application’s static nature enables us to collect extensiveinformation ahead of time, we can apply aggressive optimizations.

Improved standard optimizations

Method inlining

Constant propagation & folding

Escape analysis & stack allocation

Runtime check elimination (null-, bounds checks)

Dead-code elimination

Devirtualization

[email protected] KESO Case Study (JTRES 2011) Exploiting static knowledge in the compiler 6 – 16

Page 15: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Selective use of linked stack frames “Henderson frames”

Local references stored as compounds, stack frames linked in order toenable GC scanning

Induces significant overhead!

But: GC will only run in slack time

Optimization: Only enable Henderson frames in methods that canpossibly block – i.e., methods directly or indirectly invoking theWaitEvent() OS function

[email protected] KESO Case Study (JTRES 2011) Exploiting static knowledge in the compiler 7 – 16

Page 16: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Variant-specific constants Proposed optimization

The final qualifier for field variables allows better optimizations

However...

... programmers may be lazy or unaware

... declaring a field as final is impossible in some cases:

public final class Constants {

public static int MAX_FRAMES = 1000;

// ... more similar constants...

}

public class Main {

private static void parse(final String[] v) {

// ...

if (v[i].equals("MAX_FRAMES"))

Constants.MAX_FRAMES = Integer.parseInt(v[i + 1]);

// ...

}

}

→ constant

unreac

hable

Idea: deduce final qualifiers where possible and legal

[email protected] KESO Case Study (JTRES 2011) Exploiting static knowledge in the compiler 8 – 16

Page 17: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Variant-specific constants Proposed optimization

The final qualifier for field variables allows better optimizations

However...

... programmers may be lazy or unaware

... declaring a field as final is impossible in some cases:

public final class Constants {

public static int MAX_FRAMES = 1000;

// ... more similar constants...

}

public class Main {

private static void parse(final String[] v) {

// ...

if (v[i].equals("MAX_FRAMES"))

Constants.MAX_FRAMES = Integer.parseInt(v[i + 1]);

// ...

}

}

→ constant

unreac

hable

Idea: deduce final qualifiers where possible and legal

[email protected] KESO Case Study (JTRES 2011) Exploiting static knowledge in the compiler 8 – 16

Page 18: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Variant-specific constants Proposed optimization

The final qualifier for field variables allows better optimizations

However...

... programmers may be lazy or unaware

... declaring a field as final is impossible in some cases:

public final class Constants {

public static int MAX_FRAMES = 1000;

// ... more similar constants...

}

public class Main {

private static void parse(final String[] v) {

// ...

if (v[i].equals("MAX_FRAMES"))

Constants.MAX_FRAMES = Integer.parseInt(v[i + 1]);

// ...

}

}

→ constant

unreac

hable

Idea: deduce final qualifiers where possible and legal

[email protected] KESO Case Study (JTRES 2011) Exploiting static knowledge in the compiler 8 – 16

Page 19: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Bug detection at compile-time A bonus feature

System configuration

Does a task have run-to-completion semanticsaccording to the configuration, but invokesWaitEvent() anyway?

Service protection: Does a task or ISR access systemobjects other than those specified in the configuration?

Used raw-memory areas

Which address ranges are accessed from a specific domain?

Would a raw-memory area intersect with the regularapplication memory?

Which memory-mapped I/O ports are used by whichdomains?

[email protected] KESO Case Study (JTRES 2011) Exploiting static knowledge in the compiler 9 – 16

Page 20: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Bug detection at compile-time A bonus feature

System configuration

Does a task have run-to-completion semanticsaccording to the configuration, but invokesWaitEvent() anyway?

Service protection: Does a task or ISR access systemobjects other than those specified in the configuration?

Used raw-memory areas

Which address ranges are accessed from a specific domain?

Would a raw-memory area intersect with the regularapplication memory?

Which memory-mapped I/O ports are used by whichdomains?

[email protected] KESO Case Study (JTRES 2011) Exploiting static knowledge in the compiler 9 – 16

Page 21: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Results

Introduction

Exploiting static knowledge in the compiler

Results

Conclusion

[email protected] KESO Case Study (JTRES 2011) Results 10 – 16

Page 22: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Test system

Software

CDj 1.2, ported to KESO:

Real-time air traffic simulator and collisiondetectorCommunity-accepted benchmark

CiAO OS (AUTOSAR programming interface)

Hardware

TriCore TC1796 @ 150 MHz

2 MiB ROM

1 MiB SRAM

[email protected] KESO Case Study (JTRES 2011) Results 11 – 16

Page 23: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Dead code elimination & devirtualization

0

500

1000

1500

2000

2500

3000

Unoptimized DCE + devirtualization + inlining

Num

ber o

f met

hod

invo

catio

nsNon-virtual

20161832

Virtual2557

1116

More than half of all virtual method invocations are either removedor bound statically.

[email protected] KESO Case Study (JTRES 2011) Results 12 – 16

Page 24: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

How many checks are performed during execution?

CDj detector, 10,000 radar frames:

0

500

1000

1500

2000

Null checks Bounds checks

Che

cks

eval

uate

d du

ring

exec

utio

n (x

100

0) Original2000

1540

Optimized

1130

870

Over 40 % of all runtime checks are elided.

[email protected] KESO Case Study (JTRES 2011) Results 13 – 16

Page 25: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Effectiveness of advanced optimizations

Default + Optimized frames1 + Variant-specific constants

Exec. time2 23.9 ms 15.9 ms (−33.5 %) 15.8 ms (−33.9 %)Code size 67.8 KiB 55.2 KiB (−18.6 %) 46.5 KiB (−31.4 %)Data size 4.86 KiB 4.86 KiB (±0 %) 3.08 KiB (−36.6 %)

Using Henderson frames selectively improves both execution time andcode size significantly

Inferring the final qualifier for CDj ’s Configuration fields woulddrastically reduce the memory footprint

1Use of Henderson frames only where necessary2For detector run with 10,000 radar frames; median

[email protected] KESO Case Study (JTRES 2011) Results 14 – 16

Page 26: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Conclusion

Introduction

Exploiting static knowledge in the compiler

Results

Conclusion

[email protected] KESO Case Study (JTRES 2011) Conclusion 15 – 16

Page 27: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Conclusion

Summary

KESO abandons the aspects of the Javaprogramming model that don’t fit the staticOSEK/AUTOSAR system model...

... permitting aggressive whole-programoptimizations... bringing the benefits of Java to deeplyembedded systems

Our smallest system to date: Robertino

Autonomous robot navigating around obstaclesControl software running on ATmega8535 (8-bitAVR, 8 KiB Flash, 512 B SRAM)

Future work

Implementation of more advanced optimizations

Extensive evaluation of real-life embedded applications

[email protected] KESO Case Study (JTRES 2011) Conclusion 16 – 16

Page 28: Exploiting Static Application Knowledge in a Java Compiler ... · Exploiting Static Application Knowledge in a Java Compiler for Embedded Systems A Case Study Christoph Erhardt, Michael

Conclusion

Summary

KESO abandons the aspects of the Javaprogramming model that don’t fit the staticOSEK/AUTOSAR system model...

... permitting aggressive whole-programoptimizations... bringing the benefits of Java to deeplyembedded systems

Our smallest system to date: Robertino

Autonomous robot navigating around obstaclesControl software running on ATmega8535 (8-bitAVR, 8 KiB Flash, 512 B SRAM)

Future work

Implementation of more advanced optimizations

Extensive evaluation of real-life embedded applications

[email protected] KESO Case Study (JTRES 2011) Conclusion 16 – 16