111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

30
1 G53SRP: RTSJ Memory Areas Chris Greenhalgh Chris Greenhalgh School of Computer School of Computer Science Science

Transcript of 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

Page 1: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

111

G53SRP: RTSJ Memory Areas

Chris GreenhalghChris Greenhalgh

School of Computer ScienceSchool of Computer Science

Page 2: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

222

Contents• IntroductionIntroduction

– Heap and garbage collectionHeap and garbage collection• Memory area classesMemory area classes

– HeapMemory, ImmortalMemory, ScopedMemoryHeapMemory, ImmortalMemory, ScopedMemory• Estimating sizesEstimating sizes• Threads and memory usageThreads and memory usage

– Memory Parameters, No heap threads and handlersMemory Parameters, No heap threads and handlers• Physical memory usePhysical memory use

– Raw memory accessRaw memory access• SummarySummary

• Book: Wellings 7.3, 8 (part), 15.1 & 15.2 (part)Book: Wellings 7.3, 8 (part), 15.1 & 15.2 (part)

Page 3: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

33

IntroductionIntroduction• In regular JavaIn regular Java

– All objects are allocated on the heapAll objects are allocated on the heap• A single expandable area of memoryA single expandable area of memory

• Local variables and parameters are on the thread’s Local variables and parameters are on the thread’s stack stack

– In Java these can only be primitive types and references!In Java these can only be primitive types and references!

– A garbage collection algorithm runs in the A garbage collection algorithm runs in the backgroundbackground

• E.g. when heap is full, periodically or incrementallyE.g. when heap is full, periodically or incrementally

• Releases/recycles objects which are no longer Releases/recycles objects which are no longer reachablereachable

Page 4: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

44

Realtime issues• Memory may be limited

– May need special management

• Garbage collection may interfere with processes– Introducing additional delays– Causing missed deadlines

Page 5: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

55

RTSJ responses• Allows objects to be allocated in non-heap

“memory areas”– Avoiding garbage collection for those areas

• And/or recycling the whole area as one (i.e. scoped memory)

– Allowing more explicit management • E.g. monitoring space used and free

Page 6: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

66

Memory area classesMemory area classes<<abstract>>

javax.realtime.MemoryArea

javax.realtime.HeapMemory

extends

<<abstract>>javax.realtime.ScopedMemory

javax.realtime.ImmortalMemory

javax.realtime.LTMemory

javax.realtime.VTMemory

Page 7: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

77

Memory area types• HeapMemory

– The regular Java heap• ImmortalMemory

– Single area of non-GC memory• ScopedMemory

– Block(s) of memory which are reference counted and allocated/freed as a whole

– LTMemory – ScopedMemory with allocation time linearly proportional to size

– VTMemory – ScopedMemory with variable allocation time

Page 8: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

88

MemoryArea class[optional detail]

package javax.realtime;

public abstract class MemoryArea {

public long size();

public long memoryConsumed();

public long memoryRemaining();

Monitor/manage usage

Page 9: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

1111

Example[optional detail]

…ImmortalMemory.instance().executeInArea( new Runnable() { public void run() { int a[] = new int[1000]; … } });…int a[] = (int[])ImmortalMemory.instance(). newArray(Integer.TYPE, 1000);…

In immortal memory:never garbage collected

Page 10: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

1212

Scoped Memory• Block of memory managed as a whole• Accessible only to realtime threads (and

asynchronous event handlers)• Accessible only to a realtime thread that has entered it

• Released (and objects finalised) when all realtime threads leave it– i.e. “garbage collected” as a single unit

– References to it are not allowed where they might “outlive” it, e.g. on the heap or immortal memory

Page 11: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

1313

LTMemory & VTMemory[optional detail]

package javax.realtime;public abstract class LTMemory extends ScopedMemory { public LTMemory(long size); public LTMemory(long initial, long maximum); …}public abstract class VTMemory extends ScopedMemory { public VTMemory(long size); public VTMemory(long initial, long maximum); …}

Page 12: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

1717

Memory area stack• RealtimeThread and AsyncEventHandler have a MemoryArea parameter– Initial memory area in which to execute

– Available via getMemoryArea()

• Memory areas are also visible as a memory area stack– See RealtimeThread static methods e.g. getCurrentMemoryArea(), getOuterMemoryArea(int index)

Page 13: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

1818

Estimating sizesEstimating sizes• Scoped memory areas need to have a size Scoped memory areas need to have a size

specifiedspecified– Supported by Supported by SizeEstimatorSizeEstimator……

Page 14: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

2020

Example[optional detail]

SizeEstimator s = new SizeEstimator();

s.reserve(MyClass.class, 10);

long size = s.getEstimate();

… Enough for 10instances of MyClass

(excluding any member reference

Values!)

Page 15: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

21

Memory ParametersMemory Parameters• Parameter to Parameter to RealtimeThreadRealtimeThread and and AsyncEventHandlerAsyncEventHandler– Control memory use of the taskControl memory use of the task

• Specifies:Specifies:– maxMemoryAreamaxMemoryArea – maximum amount of memory – maximum amount of memory

allocation allowed in the task’s (initial) memory areaallocation allowed in the task’s (initial) memory area– maxImmortalmaxImmortal – maximum amount of memory – maximum amount of memory

allocation allowed in immortal memoryallocation allowed in immortal memory– allocationRateallocationRate – maximum rate at which heap – maximum rate at which heap

memory can be allocated [optionally enforced]memory can be allocated [optionally enforced]

21

Page 16: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

22

MemoryParameters classpackage javax.realtime;public class MemoryParameters { public static final long NO_MAX; public MemoryParameters( long maxMemoryArea, long maxImmortal, long allocationRate); public long getMaxMemoryArea(); public long SetMaxMemoryAreaIfFeasible(); …}

Page 17: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

23

Memory Parameter checking• May be used by Scheduler for admission

control (feasibility checking)– E.g. total available memory

• Checked/enforced by new– Throws OutOfMemoryError if exceeded

• E.g. – too much (immortal or scoped) memory used– Heap memory allocated too rapidly

• May impact need for garbage collection• Implementation dependent whether this is checkedImplementation dependent whether this is checked

Page 18: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

24

No heap threads and handlersNo heap threads and handlers• To completely avoid interference from the To completely avoid interference from the

garbage collector a garbage collector a SchedulableSchedulable must must – Not use heap memory at allNot use heap memory at all

• Indicated to JVM by using Indicated to JVM by using NoHeapRealtimeThreadNoHeapRealtimeThread class or class or AsyncEventHandlerAsyncEventHandler with with noheapnoheap = = truetrue

• Throws exception at run-time if heap memory usedThrows exception at run-time if heap memory used

– Have a higher priority than any heap using threadHave a higher priority than any heap using thread• Garbage collection may occur on behalf of any heap Garbage collection may occur on behalf of any heap

using thread – at its priorityusing thread – at its priority

24

Page 19: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

2525

Physical memory access• An embedded device may have several

different kinds of memory with different characteristics– Fast volatile DRAM/SRAM– Fast readonly ROM/EPROM– Slower non-volatile EEPROM/FLASH– Removable memory, e.g. SD/MMC/…

• The program may need to use the right kind for the right things…

Page 20: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

2626

Example Physical Memory map

From Wellings Figure 15.2 © Wellings 2004

IO Memory

Shared memory

Removable Memory

RAM

ROM

High address

Low address

Page 21: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

2727

Raw Memory Access• Can access raw bytes, e.g.

– Interacting with an IO device (see later notes)– Accessing memory shared with another (esp.

non-Java) application

• Via RawMemoryAccess…

Page 22: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

2828

RawMemoryAccess class

package javax.realtime;

public class RawMemoryAccess {

public RawMemoryAccess(

Object type,

long base,

long size);

public byte getByte(long offset);

public int getInt(long offset);

public void setByte(long offset, byte value);

public void setInt(long offset, int value);

}

Raw memory bytes

Byte order platform dependent…

Types: ALIGNEDBYTESWAPDMAIO_PAGESHARED…

(see PhysicalMemoryManager)

Page 23: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

2929

Checking byte orderpackage javax.realtime;

public class RealtimeSystem {

public static final byte BIG_ENDIAN;

public static final byte LITTLE_ENDIAN;

public static final byte BYTE_ORDER;

} LITTLE_ENDIAN orBIG_ENDIAN as appropriatei.e. start at the “big” or “little”

end of the number

Page 24: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

3030

Physical memory areas• Can also create and use MemoryAreas that

are “locked” to particular parts of physical memory– to ensure correct physical memory is used

• E.g. EEPROM/FLASH vs RAM vs DMA

• Extends previous MemoryArea classes…– Inherits basic characteristics and API– Needs extra information about physical

“backing”

Page 25: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

3131

Physical Memory classesPhysical Memory classes<<abstract>>

javax.realtime.MemoryArea

<<abstract>>javax.realtime.ScopedMemory

extends

javax.realtime.ImmortalPhysicalMemory

javax.realtime.LTPhysicalMemory

javax.realtime.VTPhysicalMemory

Page 26: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

3232

E.g. ImmortalPhysicalMemorypackage javax.realtime;

public class ImmortalPhysicalMemory

extends MemoryArea {

public ImmortalPhysicalMemory( Object type,

long base,

long size);

}

Types: ALIGNEDBYTESWAPDMAIO_PAGESHARED…

(see PhysicalMemoryManager)

Page 27: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

3333

SummarySummary

• Normal Java object allocated on the heapNormal Java object allocated on the heap– Released when unreachable by garbage collectorReleased when unreachable by garbage collector

• Realtime and embedded systems mayRealtime and embedded systems may– Need more explicit management of spaceNeed more explicit management of space– Be delayed unpredictably by garbage collectionBe delayed unpredictably by garbage collection

• RTSJ allows realtime threads to use different RTSJ allows realtime threads to use different memory areas:memory areas:– HeapMemoryHeapMemory – normal heap – normal heap– ImmortalMemoryImmortalMemory – non-garbage collected area – non-garbage collected area– ScopedMemoryScopedMemory……

Page 28: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

3434

Summary (2)Summary (2)• Scoped memoryScoped memory

– LTMemory LTMemory or or VTMemoryVTMemory– memory areas which are released as a whole when no memory areas which are released as a whole when no

realtime threads are using themrealtime threads are using them

– May be nestedMay be nested

– Cannot have references to object in scoped memory on Cannot have references to object in scoped memory on heap, immortal memory or parent/ancestor scoped heap, immortal memory or parent/ancestor scoped memorymemory

• Estimating sizes for scoped memory areas – using Estimating sizes for scoped memory areas – using SizeEstimatorSizeEstimator

Page 29: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

35

Summary (3)• Realtime thread and asycn event handler

memory usage limits specified by MemoryParameters:– Memory area and immortal memory size

limites– Heap allocation rate limit (optionally checked)

• Can avoid interference from GC using NoHeapRealtimeThread or noheap AsyncEventHandler– Iff priority > any heap-using thread / handler

Page 30: 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

3636

Summary (4)• Access to raw physical memory

– E.g. for device IO or inter-program communication

– via RawMemoryAccess

• Use of specific areas of physical memory– E.g. to reflect type(s) of memory present– via PhysicalImmortalMemory, LTPhysicalMemory & VTPhysicalMemory