Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Memory AreasMemory AreasReal Time Scheduling 15/04/10 G53SRPG53SRPG53SRP Memory AreasReal Time Scheduling G53SRPG53SRP Real Time Scheduling 15/04/10 G53SRP
  • Memory AreasMemory AreasReal Time Scheduling 15/04/10 G53SRPG53SRPG53SRP Memory AreasReal Time Scheduling G53SRPG53SRP
  • Memory AreasMemory AreasReal Time Scheduling 15/04/10 G53SRPG53SRPG53SRP
  • memoryareas.ppt

    1. 1. G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science
    2. 2. Contents <ul><li>Introduction </li></ul><ul><ul><li>Heap and garbage collection </li></ul></ul><ul><li>Memory area classes </li></ul><ul><ul><li>HeapMemory, ImmortalMemory, ScopedMemory </li></ul></ul><ul><li>Estimating sizes </li></ul><ul><li>Threads and memory usage </li></ul><ul><ul><li>Memory Parameters, No heap threads and handlers </li></ul></ul><ul><li>Physical memory use </li></ul><ul><ul><li>Raw memory access </li></ul></ul><ul><li>Summary </li></ul><ul><li>Book: Wellings 7.3, 8 (part), 15.1 & 15.2 (part) </li></ul>
    3. 3. Introduction <ul><li>In regular Java </li></ul><ul><ul><li>All objects are allocated on the heap </li></ul></ul><ul><ul><ul><li>A single expandable area of memory </li></ul></ul></ul><ul><ul><ul><li>Local variables and parameters are on the thread’s stack </li></ul></ul></ul><ul><ul><ul><ul><li>In Java these can only be primitive types and references! </li></ul></ul></ul></ul><ul><ul><li>A garbage collection algorithm runs in the background </li></ul></ul><ul><ul><ul><li>E.g. when heap is full, periodically or incrementally </li></ul></ul></ul><ul><ul><ul><li>Releases/recycles objects which are no longer reachable </li></ul></ul></ul>
    4. 4. Realtime issues <ul><li>Memory may be limited </li></ul><ul><ul><li>May need special management </li></ul></ul><ul><li>Garbage collection may interfere with processes </li></ul><ul><ul><li>Introducing additional delays </li></ul></ul><ul><ul><li>Causing missed deadlines </li></ul></ul>
    5. 5. RTSJ responses <ul><li>Allows objects to be allocated in non-heap “memory areas” </li></ul><ul><ul><li>Avoiding garbage collection for those areas </li></ul></ul><ul><ul><ul><li>And/or recycling the whole area as one (i.e. scoped memory) </li></ul></ul></ul><ul><ul><li>Allowing more explicit management </li></ul></ul><ul><ul><ul><li>E.g. monitoring space used and free </li></ul></ul></ul>
    6. 6. Memory area classes <<abstract>> javax.realtime. MemoryArea javax.realtime. HeapMemory extends <<abstract>> javax.realtime. ScopedMemory javax.realtime. ImmortalMemory javax.realtime. LTMemory javax.realtime. VTMemory
    7. 7. Memory area types <ul><li>HeapMemory </li></ul><ul><ul><li>The regular Java heap </li></ul></ul><ul><li>ImmortalMemory </li></ul><ul><ul><li>Single area of non-GC memory </li></ul></ul><ul><li>ScopedMemory </li></ul><ul><ul><li>Block(s) of memory which are reference counted and allocated/freed as a whole </li></ul></ul><ul><ul><li>LTMemory – ScopedMemory with allocation time linearly proportional to size </li></ul></ul><ul><ul><li>VTMemory – ScopedMemory with variable allocation time </li></ul></ul>
    8. 8. MemoryArea class [optional detail] <ul><li>package javax.realtime; </li></ul><ul><li>public abstract class MemoryArea { </li></ul><ul><li>… </li></ul><ul><li>public long size(); </li></ul><ul><li>public long memoryConsumed(); </li></ul><ul><li>public long memoryRemaining(); </li></ul><ul><li>… </li></ul>Monitor/manage usage
    9. 9. <ul><li>… </li></ul><ul><li>public void enter(Runnable logic); </li></ul><ul><li>public void executeInArea(Runnable logic); </li></ul><ul><li>… </li></ul><ul><li>public Object newInstance(Class clazz); </li></ul><ul><li>public Object newArray(Class type, </li></ul><ul><li>int number); </li></ul><ul><li>… </li></ul><ul><li>} </li></ul>[optional detail] Explicit allocation within currently valid memory area Manipulate default memory area used for realtime thread’s memory allocation
    10. 10. ImmortalMemory & HeapMemory [optional detail] <ul><li>package javax.realtime; </li></ul><ul><li>public abstract class ImmortalMemory </li></ul><ul><li>extends MemoryArea { </li></ul><ul><li>public static ImmortalMemory instance(); </li></ul><ul><li>} </li></ul><ul><li>public abstract class HeapMemory </li></ul><ul><li>extends MemoryArea { </li></ul><ul><li>public static HeapMemory instance(); </li></ul><ul><li>} </li></ul>Singletons
    11. 11. Example [optional detail] <ul><li>… </li></ul><ul><li>ImmortalMemory.instance().executeInArea( </li></ul><ul><li>new Runnable() { </li></ul><ul><li>public void run() { </li></ul><ul><li>int a[] = new int[1000]; </li></ul><ul><li>… </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul><ul><li>… </li></ul><ul><li>int a[] = (int[])ImmortalMemory.instance(). </li></ul><ul><li>newArray(Integer.TYPE, 1000); </li></ul><ul><li>… </li></ul>In immortal memory: never garbage collected
    12. 12. Scoped Memory <ul><li>Block of memory managed as a whole </li></ul><ul><li>Accessible only to realtime threads (and asynchronous event handlers) </li></ul><ul><li>Accessible only to a realtime thread that has enter ed it </li></ul><ul><li>Released (and objects finalised) when all realtime threads leave it </li></ul><ul><ul><li>i.e. “garbage collected” as a single unit </li></ul></ul><ul><ul><li>References to it are not allowed where they might “outlive” it, e.g. on the heap or immortal memory </li></ul></ul>
    13. 13. LTMemory & VTMemory [optional detail] <ul><li>package javax.realtime; </li></ul><ul><li>public abstract class LTMemory </li></ul><ul><li>extends ScopedMemory { </li></ul><ul><li>public LTMemory(long size); </li></ul><ul><li>public LTMemory(long initial, long maximum); </li></ul><ul><li>… </li></ul><ul><li>} </li></ul><ul><li>public abstract class VTMemory </li></ul><ul><li>extends ScopedMemory { </li></ul><ul><li>public VTMemory(long size); </li></ul><ul><li>public VTMemory(long initial, long maximum); </li></ul><ul><li>… </li></ul><ul><li>} </li></ul>
    14. 14. Example [optional detail] <ul><li>… </li></ul><ul><li>LTMemory mem = new LTMemory(size); </li></ul><ul><li>mem.enter( </li></ul><ul><li>new Runnable() { </li></ul><ul><li>public void run() { </li></ul><ul><li>int a[] = new int[1000]; </li></ul><ul><li>… </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul><ul><li>… </li></ul>In scoped memory ‘mem’ Needs to be big enough! Scoped memory backing store released when method finishes
    15. 15. Scoped memory notes [optional detail] <ul><li>References to objects in scoped memory </li></ul><ul><ul><li>Cannot be on heap </li></ul></ul><ul><ul><li>Cannot be in immortal memory </li></ul></ul><ul><ul><li>Cannot be in a scoped memory that is “below” the scoped memory containing the object </li></ul></ul><ul><li>Each scoped memory has one parent scoped memory </li></ul><ul><ul><li>= the scoped memory from which it was entered </li></ul></ul>
    16. 16. Scoped memory portals [optional detail] <ul><li>Each ScopedMemory can have one “portal” </li></ul><ul><ul><li>An object, typically in the scoped memory, that can be accessed from the ScopedMemory object </li></ul></ul><ul><ul><ul><li>Note that the ScopedMemory object itself may be on the Heap, in immortal memory, etc.; its backing store is separate from the object </li></ul></ul></ul><ul><ul><li>Allows different threads to find an entry to object(s) in a common ScopedMemory </li></ul></ul><ul><ul><ul><li>The reference rules make this difficult otherwise </li></ul></ul></ul><ul><ul><li>… public Object getPortal(); public void setPortal(Object o); … </li></ul></ul>
    17. 17. Memory area stack <ul><li>RealtimeThread and AsyncEventHandler have a MemoryArea parameter </li></ul><ul><ul><li>Initial memory area in which to execute </li></ul></ul><ul><ul><li>Available via getMemoryArea() </li></ul></ul><ul><li>Memory areas are also visible as a memory area stack </li></ul><ul><ul><li>See RealtimeThread static methods e.g. getCurrentMemoryArea() , getOuterMemoryArea(int index) </li></ul></ul>
    18. 18. Estimating sizes <ul><li>Scoped memory areas need to have a size specified </li></ul><ul><ul><li>Supported by SizeEstimator … </li></ul></ul>
    19. 19. SizeEstimator class [optional detail] <ul><li>package javax.realtime; </li></ul><ul><li>public class SizeEstimator { </li></ul><ul><li>public SizeEstimator(); </li></ul><ul><li>// call as required </li></ul><ul><li>public void reserve(Class c, int number); </li></ul><ul><li>public void reserve(SizeEstimator s, </li></ul><ul><li>int number); </li></ul><ul><li>public void reserveArray(int dimensions, </li></ul><ul><li>Class type); </li></ul><ul><li>… </li></ul><ul><li>public long getEstimate(); </li></ul><ul><li>} </li></ul>Adds to running total Current estimate (sufficient)
    20. 20. Example [optional detail] <ul><li>… </li></ul><ul><li>SizeEstimator s = new SizeEstimator(); </li></ul><ul><li>s.reserve(MyClass.class, 10); </li></ul><ul><li>… </li></ul><ul><li>long size = s.getEstimate(); </li></ul><ul><li>… </li></ul>Enough for 10 instances of MyClass (excluding any member reference Values!)
    21. 21. Memory Parameters <ul><li>Parameter to RealtimeThread and AsyncEventHandler </li></ul><ul><ul><li>Control memory use of the task </li></ul></ul><ul><li>Specifies: </li></ul><ul><ul><li>maxMemoryArea – maximum amount of memory allocation allowed in the task’s (initial) memory area </li></ul></ul><ul><ul><li>maxImmortal – maximum amount of memory allocation allowed in immortal memory </li></ul></ul><ul><ul><li>allocationRate – maximum rate at which heap memory can be allocated [optionally enforced] </li></ul></ul>
    22. 22. MemoryParameters class <ul><li>package javax.realtime; </li></ul><ul><li>public class MemoryParameters { </li></ul><ul><li>public static final long NO_MAX; </li></ul><ul><li>public MemoryParameters( </li></ul><ul><li>long maxMemoryArea, </li></ul><ul><li>long maxImmortal, </li></ul><ul><li>long allocationRate); </li></ul><ul><li>public long getMaxMemoryArea(); </li></ul><ul><li>public long SetMaxMemoryAreaIfFeasible(); </li></ul><ul><li>… </li></ul><ul><li>} </li></ul>
    23. 23. Memory Parameter checking <ul><li>May be used by Scheduler for admission control (feasibility checking) </li></ul><ul><ul><li>E.g. total available memory </li></ul></ul><ul><li>Checked/enforced by new </li></ul><ul><ul><li>Throws OutOfMemoryError if exceeded </li></ul></ul><ul><li>E.g. </li></ul><ul><ul><li>too much (immortal or scoped) memory used </li></ul></ul><ul><ul><li>Heap memory allocated too rapidly </li></ul></ul><ul><ul><ul><li>May impact need for garbage collection </li></ul></ul></ul><ul><ul><ul><li>Implementation dependent whether this is checked </li></ul></ul></ul>
    24. 24. No heap threads and handlers <ul><li>To completely avoid interference from the garbage collector a Schedulable must </li></ul><ul><ul><li>Not use heap memory at all </li></ul></ul><ul><ul><ul><li>Indicated to JVM by using NoHeapRealtimeThread class or AsyncEventHandler with noheap = true </li></ul></ul></ul><ul><ul><ul><li>Throws exception at run-time if heap memory used </li></ul></ul></ul><ul><ul><li>Have a higher priority than any heap using thread </li></ul></ul><ul><ul><ul><li>Garbage collection may occur on behalf of any heap using thread – at its priority </li></ul></ul></ul>
    25. 25. Physical memory access <ul><li>An embedded device may have several different kinds of memory with different characteristics </li></ul><ul><ul><li>Fast volatile DRAM/SRAM </li></ul></ul><ul><ul><li>Fast readonly ROM/EPROM </li></ul></ul><ul><ul><li>Slower non-volatile EEPROM/FLASH </li></ul></ul><ul><ul><li>Removable memory, e.g. SD/MMC/… </li></ul></ul><ul><li>The program may need to use the right kind for the right things… </li></ul>
    26. 26. Example Physical Memory map From Wellings Figure 15.2 © Wellings 2004 IO Memory Shared memory Removable Memory RAM ROM High address Low address
    27. 27. Raw Memory Access <ul><li>Can access raw bytes, e.g. </li></ul><ul><ul><li>Interacting with an IO device (see later notes) </li></ul></ul><ul><ul><li>Accessing memory shared with another (esp. non-Java) application </li></ul></ul><ul><li>Via RawMemoryAccess … </li></ul>
    28. 28. RawMemoryAccess class <ul><li>package javax.realtime; </li></ul><ul><li>public class RawMemoryAccess { </li></ul><ul><li>public RawMemoryAccess( </li></ul><ul><li>Object type, </li></ul><ul><li>long base, </li></ul><ul><li>long size); </li></ul><ul><li>… </li></ul><ul><li>public byte getByte(long offset); </li></ul><ul><li>public int getInt(long offset); </li></ul><ul><li>public void setByte(long offset, byte value); </li></ul><ul><li>public void setInt(long offset, int value); </li></ul><ul><li>… </li></ul><ul><li>} </li></ul>Types: ALIGNED BYTESWAP DMA IO_PAGE SHARED … (see PhysicalMemoryManager) Raw memory bytes Byte order platform dependent…
    29. 29. Checking byte order <ul><li>package javax.realtime; </li></ul><ul><li>public class RealtimeSystem { </li></ul><ul><li>… </li></ul><ul><li>public static final byte BIG_ENDIAN; </li></ul><ul><li>public static final byte LITTLE_ENDIAN; </li></ul><ul><li>public static final byte BYTE_ORDER; </li></ul><ul><li>… </li></ul><ul><li>} </li></ul>LITTLE_ENDIAN or BIG_ENDIAN as appropriate i.e. start at the “big” or “little” end of the number
    30. 30. Physical memory areas <ul><li>Can also create and use MemoryAreas that are “locked” to particular parts of physical memory </li></ul><ul><ul><li>to ensure correct physical memory is used </li></ul></ul><ul><ul><ul><li>E.g. EEPROM/FLASH vs RAM vs DMA </li></ul></ul></ul><ul><li>Extends previous MemoryArea classes… </li></ul><ul><ul><li>Inherits basic characteristics and API </li></ul></ul><ul><ul><li>Needs extra information about physical “backing” </li></ul></ul>
    31. 31. Physical Memory classes <<abstract>> javax.realtime. MemoryArea <<abstract>> javax.realtime. ScopedMemory extends javax.realtime. ImmortalPhysicalMemory javax.realtime. LTPhysicalMemory javax.realtime. VTPhysicalMemory
    32. 32. E.g. ImmortalPhysicalMemory <ul><li>package javax.realtime; </li></ul><ul><li>public class ImmortalPhysicalMemory </li></ul><ul><li>extends MemoryArea { </li></ul><ul><li>public ImmortalPhysicalMemory( Object type, </li></ul><ul><li>long base, </li></ul><ul><li>long size); </li></ul><ul><li>… </li></ul><ul><li>} </li></ul>Types: ALIGNED BYTESWAP DMA IO_PAGE SHARED … (see PhysicalMemoryManager)
    33. 33. Summary <ul><li>Normal Java object allocated on the heap </li></ul><ul><ul><li>Released when unreachable by garbage collector </li></ul></ul><ul><li>Realtime and embedded systems may </li></ul><ul><ul><li>Need more explicit management of space </li></ul></ul><ul><ul><li>Be delayed unpredictably by garbage collection </li></ul></ul><ul><li>RTSJ allows realtime threads to use different memory areas: </li></ul><ul><ul><li>HeapMemory – normal heap </li></ul></ul><ul><ul><li>ImmortalMemory – non-garbage collected area </li></ul></ul><ul><ul><li>ScopedMemory … </li></ul></ul>
    34. 34. Summary (2) <ul><li>Scoped memory </li></ul><ul><ul><li>LTMemory or VTMemory </li></ul></ul><ul><ul><li>memory areas which are released as a whole when no realtime threads are using them </li></ul></ul><ul><ul><li>May be nested </li></ul></ul><ul><ul><li>Cannot have references to object in scoped memory on heap, immortal memory or parent/ancestor scoped memory </li></ul></ul><ul><li>Estimating sizes for scoped memory areas – using SizeEstimator </li></ul>
    35. 35. Summary (3) <ul><li>Realtime thread and asycn event handler memory usage limits specified by MemoryParameters : </li></ul><ul><ul><li>Memory area and immortal memory size limites </li></ul></ul><ul><ul><li>Heap allocation rate limit (optionally checked) </li></ul></ul><ul><li>Can avoid interference from GC using NoHeapRealtimeThread or noheap AsyncEventHandler </li></ul><ul><ul><li>Iff priority > any heap-using thread / handler </li></ul></ul>
    36. 36. Summary (4) <ul><li>Access to raw physical memory </li></ul><ul><ul><li>E.g. for device IO or inter-program communication </li></ul></ul><ul><ul><li>via RawMemoryAccess </li></ul></ul><ul><li>Use of specific areas of physical memory </li></ul><ul><ul><li>E.g. to reflect type(s) of memory present </li></ul></ul><ul><ul><li>via PhysicalImmortalMemory , LTPhysicalMemory & VTPhysicalMemory </li></ul></ul>