Patterns for Safety-Critical Java Memory Usage Juan Rios 1 Kelvin - - PowerPoint PPT Presentation

patterns for safety critical java memory usage
SMART_READER_LITE
LIVE PREVIEW

Patterns for Safety-Critical Java Memory Usage Juan Rios 1 Kelvin - - PowerPoint PPT Presentation

Motivation Contribution Summary Patterns for Safety-Critical Java Memory Usage Juan Rios 1 Kelvin Nilsen 2 Martin Schoeberl 1 1 Department of Informatics and Mathematical Modelling Technical University of Denmark 2 Atego Systems, Inc. Java


slide-1
SLIDE 1

Motivation Contribution Summary

Patterns for Safety-Critical Java Memory Usage

Juan Rios1 Kelvin Nilsen2 Martin Schoeberl1

1Department of Informatics and Mathematical Modelling

Technical University of Denmark

2Atego Systems, Inc.

Java Technologies for Real-time and Embedded Systems, 2012

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 1/14

slide-2
SLIDE 2

Motivation Contribution Summary

Outline

1

Motivation Problem Outline and Motivation Previous Work

2

Contribution Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 2/14

slide-3
SLIDE 3

Motivation Contribution Summary Problem Outline and Motivation Previous Work

Outline

1

Motivation Problem Outline and Motivation Previous Work

2

Contribution Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 3/14

slide-4
SLIDE 4

Motivation Contribution Summary Problem Outline and Motivation Previous Work

Problem and Motivation

Real-time Java profiles make use of scoped memory regions Explicit scoping requires care from programmers

Deal with temporary objects Pass scope-allocated objects as arguments to methods Returning scope-allocated objects from methods

Look into the expressiveness of the SCJ memory model Explore patterns on how to use it

Focus on how to pass arguments and return objects between private memories

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 4/14

slide-5
SLIDE 5

Motivation Contribution Summary Problem Outline and Motivation Previous Work

Problem and Motivation

Real-time Java profiles make use of scoped memory regions Explicit scoping requires care from programmers

Deal with temporary objects Pass scope-allocated objects as arguments to methods Returning scope-allocated objects from methods

Look into the expressiveness of the SCJ memory model Explore patterns on how to use it

Focus on how to pass arguments and return objects between private memories

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 4/14

slide-6
SLIDE 6

Motivation Contribution Summary Problem Outline and Motivation Previous Work

Problem and Motivation

Real-time Java profiles make use of scoped memory regions Explicit scoping requires care from programmers

Deal with temporary objects Pass scope-allocated objects as arguments to methods Returning scope-allocated objects from methods

Look into the expressiveness of the SCJ memory model Explore patterns on how to use it

Focus on how to pass arguments and return objects between private memories

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 4/14

slide-7
SLIDE 7

Motivation Contribution Summary Problem Outline and Motivation Previous Work

Problem and Motivation

Real-time Java profiles make use of scoped memory regions Explicit scoping requires care from programmers

Deal with temporary objects Pass scope-allocated objects as arguments to methods Returning scope-allocated objects from methods

Look into the expressiveness of the SCJ memory model Explore patterns on how to use it

Focus on how to pass arguments and return objects between private memories

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 4/14

slide-8
SLIDE 8

Motivation Contribution Summary Problem Outline and Motivation Previous Work

Outline

1

Motivation Problem Outline and Motivation Previous Work

2

Contribution Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 5/14

slide-9
SLIDE 9

Motivation Contribution Summary Problem Outline and Motivation Previous Work

Previous Work

Memory pools and memory blocks in Immortal and scoped memories Collections of design patterns for RTSJ

RTSJ specific Features not in SCJ Violations to reference assignment rules

Mapping between memory areas and methods

Reduce the need for reference assignment checks Restricted use of RTSJ memory API very similar to SCJ private memories

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 6/14

slide-10
SLIDE 10

Motivation Contribution Summary Problem Outline and Motivation Previous Work

Previous Work

Memory pools and memory blocks in Immortal and scoped memories Collections of design patterns for RTSJ

RTSJ specific Features not in SCJ Violations to reference assignment rules

Mapping between memory areas and methods

Reduce the need for reference assignment checks Restricted use of RTSJ memory API very similar to SCJ private memories

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 6/14

slide-11
SLIDE 11

Motivation Contribution Summary Problem Outline and Motivation Previous Work

Previous Work

Memory pools and memory blocks in Immortal and scoped memories Collections of design patterns for RTSJ

RTSJ specific Features not in SCJ Violations to reference assignment rules

Mapping between memory areas and methods

Reduce the need for reference assignment checks Restricted use of RTSJ memory API very similar to SCJ private memories

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 6/14

slide-12
SLIDE 12

Motivation Contribution Summary Problem Outline and Motivation Previous Work

Previous Work

IBM’s “Lifecycle Memory Managed Periodic Worker Threads" pattern, with focus on object lifetimes and not on understanding scopes

Defines four categories of object lifetimes analogous to the lifetime of objects in SCJ scopes

RETAIN_FOREVER = Immortal Memory RETAIN_THREAD_GROUP = Mission Memory RETAIN_THREAD = Private Memory RETAIN_ITERATION = Nested Private Memory

Relies on java.lang.reflect package, not part of SCJ

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 7/14

slide-13
SLIDE 13

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Outline

1

Motivation Problem Outline and Motivation Previous Work

2

Contribution Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 8/14

slide-14
SLIDE 14

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

SCJ Memory API

Brief description

Nested Memory Private Memory 1 PM2 PEH2 NM Object representing memory area MM PM1 PEH1 Private Memory 2 Immortal Memory Mission Memory

Memory area objects represent allocation contexts Memory areas are entered with:

enterPrivateMemory() executeInArea()

Creation of objects in an allocation context other than the current is done with:

newInstance() newArray()

It is possible to get references to memory areas with:

getMemoryArea(Object O) getCurrentManagedMemory()

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 9/14

slide-15
SLIDE 15

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

SCJ Memory API

Brief description

Nested Memory Private Memory 1 PM2 PEH2 NM Object representing memory area MM PM1 PEH1 Private Memory 2 Immortal Memory Mission Memory

Memory area objects represent allocation contexts Memory areas are entered with:

enterPrivateMemory() executeInArea()

Creation of objects in an allocation context other than the current is done with:

newInstance() newArray()

It is possible to get references to memory areas with:

getMemoryArea(Object O) getCurrentManagedMemory()

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 9/14

slide-16
SLIDE 16

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

SCJ Memory API

Brief description

Nested Memory Private Memory 1 PM2 PEH2 NM Object representing memory area MM PM1 PEH1 Private Memory 2 Immortal Memory Mission Memory

Memory area objects represent allocation contexts Memory areas are entered with:

enterPrivateMemory() executeInArea()

Creation of objects in an allocation context other than the current is done with:

newInstance() newArray()

It is possible to get references to memory areas with:

getMemoryArea(Object O) getCurrentManagedMemory()

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 9/14

slide-17
SLIDE 17

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

SCJ Memory API

Brief description

Nested Memory Private Memory 1 PM2 PEH2 NM Object representing memory area MM PM1 PEH1 Private Memory 2 Immortal Memory Mission Memory

Memory area objects represent allocation contexts Memory areas are entered with:

enterPrivateMemory() executeInArea()

Creation of objects in an allocation context other than the current is done with:

newInstance() newArray()

It is possible to get references to memory areas with:

getMemoryArea(Object O) getCurrentManagedMemory()

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 9/14

slide-18
SLIDE 18

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Loop Pattern

Description

Mission Memory PM2 PM1 PEH2 PEH1 Immortal Memory MM

T0

. . .

Temporary

  • bject

Periodic Thread execu- ting in memory region

Tn Ti

Private Memory 1

T0

. . .

Tm

Private Memory 2 Nested Memory NM Loop N times Object representing memory area

Intent: Reuse of private memory space by several sub computations in a single handler release Condition: No need for returning results

  • r to pass arguments

Implementation: The run() method of Runnable instances are executed in private nested memory areas by calling

enterPrivateMemory()

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 10/14

slide-19
SLIDE 19

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Loop Pattern

Example

class Worker implements Runnable { public void run() { // Your code here } } class MyHandler extends PeriodicEventHandler { public void handleAsyncEvent() { Worker w = new Worker(); for(int i = 0; i < BLOCK_SIZE; i++){ ManagedMemory.enterPrivateMemory(256, w); } } }

Intent: Reuse of private memory space by several sub computations in a single handler release Condition: No need for returning results

  • r to pass arguments

Implementation: The run() method of Runnable instances are executed in private nested memory areas by calling

enterPrivateMemory()

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 10/14

slide-20
SLIDE 20

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Return a Newly Allocated Object

Description

Mission Memory

W

Nested Memory Private Memory PM1 PEH1

T0

. . .

Tm

NM Temporary

  • bject

Periodic Thread execu- ting in memory region

Ti

Object representing memory area Reference to object

Intent: Pass parameters and preserve results within different (nested) private memory activations without using static fields The condition of returning only primitive values is relaxed Implementation: The returned object is created in a different memory area with the help of a memory reference

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 11/14

slide-21
SLIDE 21

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Return a Newly Allocated Object

Description

Mission Memory

W

Nested Memory Private Memory 1 PM1 PEH1

T0

. . .

Tm

NM Temporary

  • bject

Periodic Thread execu- ting in memory region

Ti

Object representing memory area Reference to object Get a reference to memory area

Intent: Pass parameters and preserve results within different (nested) private memory activations without using static fields The condition of returning only primitive values is relaxed Implementation: The returned object is created in a different memory area with the help of a memory reference

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 11/14

slide-22
SLIDE 22

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Return a Newly Allocated Object

Description

Mission Memory

W

Nested Memory Private Memory Use reference to create return object 2 1 Get a reference to memory area PM1 PEH1 rObj

T0

. . .

Tm

newInstance()

NM Temporary

  • bject

Periodic Thread execu- ting in memory region

Ti

Object representing memory area Reference to object

Intent: Pass parameters and preserve results within different (nested) private memory activations without using static fields The condition of returning only primitive values is relaxed Implementation: The returned object is created in a different memory area with the help of a memory reference

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 11/14

slide-23
SLIDE 23

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Return a Newly Allocated Object

Description

Mission Memory

W

Nested Memory Private Memory Use reference to create return object 2 1 Get a reference to memory area PM1 PEH1 rObj

T0

. . .

Tm

newInstance()

NM Return object can be used when handler returns 3 Temporary

  • bject

Periodic Thread execu- ting in memory region

Ti

Object representing memory area Reference to object

Intent: Pass parameters and preserve results within different (nested) private memory activations without using static fields The condition of returning only primitive values is relaxed Implementation: The returned object is created in a different memory area with the help of a memory reference

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 11/14

slide-24
SLIDE 24

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Return a Newly Allocated Object

Example

class Worker implements Runnable { RetObject rObj; public void run() { // Do some work... MemoryArea mem = MemoryArea.getMemoryArea(this); rObj = mem.newInstance(RetObject.class); } }; class MyHandler extends PeriodicEventHandler { public void handleAsyncEvent(){ Worker w = new Worker(); ManagedMemory.enterPrivateMemory(256,w); // Use returned object and fields w.rObj ... } }

Intent: Pass parameters and preserve results within different (nested) private memory activations without using static fields The condition of returning only primitive values is relaxed Implementation: The returned object is created in a different memory area with the help of a memory reference

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 11/14

slide-25
SLIDE 25

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Outline

1

Motivation Problem Outline and Motivation Previous Work

2

Contribution Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 12/14

slide-26
SLIDE 26

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Simplifying Allocation Context Change

Leaking memory references between handlers

Mission Memory PM2 PM1 PEH2 PEH1 sObj Private Memory 1 Immortal Memory Private Memory 2 MM

A handler can store memory references in shared objects allocated in Mission Memory

By using getCurrentManagedMemory()

Get a private memory reference from the handler object itself

By using getMemoryArea(Object)

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 13/14

slide-27
SLIDE 27

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Simplifying Allocation Context Change

Runtime checks to prevent execution in different private scopes

Mission Memory PM2 PM1 PEH2 PEH1 sObj Private Memory 1 Immortal Memory Private Memory 2 MM

With an arbitrary memory reference, a handler can try to execute code in a different private memory

executeInArea(), newInstance() and newArray()

RTSJ requires checking at runtime that the target memory is in the current thread scope stack

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 13/14

slide-28
SLIDE 28

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Simplifying Allocation Context Change

Proposed changes in the current API

Mission Memory PM2 PM1 PEH2 PEH1 sObj Private Memory 1 Immortal Memory Private Memory 2 MM

Hide the following methods from the public API:

getCurrentManagedMemory() getMemoryArea() executeInArea() newInstance() newArray()

Replace executeInArea() with a new static method:

executeInOuter(object, runnable) executeInOuter(runnable)

New API will be semantically equivalent to the old

No memory reference leaks No runtime checks

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 13/14

slide-29
SLIDE 29

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Simplifying Allocation Context Change

Proposed changes in the current API

Mission Memory PM2 PM1 PEH2 PEH1 sObj Private Memory 1 Immortal Memory Private Memory 2 MM

Hide the following methods from the public API:

getCurrentManagedMemory() getMemoryArea() executeInArea() newInstance() newArray()

Replace executeInArea() with a new static method:

executeInOuter(object, runnable) executeInOuter(runnable)

New API will be semantically equivalent to the old

No memory reference leaks No runtime checks

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 13/14

slide-30
SLIDE 30

Motivation Contribution Summary Scoped Memory Usage Patterns Proposed Modifications to SCJ API

Simplifying Allocation Context Change

Proposed changes in the current API

Mission Memory PM2 PM1 PEH2 PEH1 sObj Private Memory 1 Immortal Memory Private Memory 2 MM

Hide the following methods from the public API:

getCurrentManagedMemory() getMemoryArea() executeInArea() newInstance() newArray()

Replace executeInArea() with a new static method:

executeInOuter(object, runnable) executeInOuter(runnable)

New API will be semantically equivalent to the old

No memory reference leaks No runtime checks

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 13/14

slide-31
SLIDE 31

Motivation Contribution Summary

Summary

Scoped memory use patterns are useful to develop SCJ applications

Having arguments and returning results from inner scopes is not so obvious

An explicit change of allocation context into an outer memory eliminates:

Run time checks (target memory need to be in thread’s scope stack) The possibility to leak memory references

Future work

Is it possible to obtain re-usable code by rewriting portions of Java’s standard libraries to be scope-safe?

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 14/14

slide-32
SLIDE 32

Appendix Leaking Memory References The Scope Stack in SCJ

Leaking Memory References

An example

public class MyMission extends Mission { public void initialize () { ... PeriodicEventHandler PEH1 = new PeriodicEventHandler(...); PeriodicEventHandler PEH2 = new PeriodicEventHandler(..., PEH1); ... } } public class PEH2 extends PeriodicEventHandler { PeriodicEventHandler PEH1; public PEH2(..., PeriodicEventHandler peh) { super (...); PEH1 = peh; } public void handleAsyncEvent() { ... MemoryArea mem = MemoryArea.getMemoryArea(PEH1); mem.executeInArea(...); ... } } Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 1/2

slide-33
SLIDE 33

Appendix Leaking Memory References The Scope Stack in SCJ

The Scope Stack in SCJ

Juan Rios, Kelvin Nilsen, Martin Schoeberl Patterns for Safety-Critical Java Memory Usage 2/2