Dynamic Assignment of Scoped Memory Regions in the ...mdeters/doc/slides/deters__ms_thesis-defense.pdf · Morgan Deters Dynamic Assignment of Scoped Memory Regions 1 10 March 2003
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Distributed Object Computing LaboratoryWashington University
St. Louis, MO
Dynamic Assignment of Scoped Memory Regions in the Translation
Research funded by DARPA under contract F33615-00-C-1697
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
110 March 2003
Overview
Dynamic AnalysisMemory Regions
JavaReal-Time
Garbage Collection Avoidance
Scoped Memory
Aspect-Oriented Programming
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
210 March 2003
Big Picture
Java
JVM
OS
RT-Java
RTJVM
RTOS
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
310 March 2003
Java vs. Real-Time
• High-level• Popular• Object-oriented• Garbage collection• Reusable components• Flexible, adaptable• Scalable• Portable• Optimized average-case• Memory use varies• Coarse control of
concurrency
• Low-level• Specialized• Manually-optimized• Specialized allocators• Tailored components• Hard-coded behavior• May not scale• Hardware-dependent• Reasonable worst-case• Footprint, latency critical• Fine control over
concurrency
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
410 March 2003
Real-Time Java ?
• Real-Time Specification for Java (RTSJ, JSR001) brings infrastructure for real-time accountability to Java§ Threads with real-time guarantees§ Real-time schedulability§ High-precision timers§ Asynchronous transfer of control§ “Physical” memory access§ Choice of memory allocators and garbage collection§ Memory reclamation without garbage collection
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
510 March 2003
Real-Time Java Memory Hierarchy
MemoryArea
HeapMemoryScopedMemoryImmortalMemory
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
610 March 2003
Rules for Scope References
From the Real-Time Specification for Java v1.0
Thou shalt not reference any object whose lifetime could be shorter than thine own.
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
710 March 2003
Inter-Object Reference Rules
• Objects may only refer to objects in§ ancestor memory regions§ garbage-collected heap
Child Region
Parent Region
An object cannot refer to an objectfrom a shorter-lived memory region!
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
810 March 2003
Nested ScopeMemory References
X YZ
A
B
C
FGD
E
Threads create scopes and objectsThreads can enter existing memory scopesMore scope and object creationObject references must point outwardReferences pointing inward are illegalThreads recede causing collection en masseInward-pointing references can “dangle”
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
910 March 2003
A Stack of Regions
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
1010 March 2003
Using Real-Time Java Scoped Memory
• Consider class C:
class C {void foo() {bar();// ...
}void bar() {// generate lots of garbage
}}
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
1110 March 2003
Using Real-Time Java Scoped Memory
• foo() calls bar() in a separate regionclass C {void foo() {ScopedMemory sc = new ScopedMemory(1024,1024);sc.enter(new Runnable() {
public void run() {bar();
}});
}void bar() {// generate lots of garbage
}}
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
1210 March 2003
Using Real-Time Java Scoped Memoryclass C {Object barResult; // assume single-threadedvoid foo() {ScopedMemory sc = new ScopedMemory(1024,1024);sc.enter(new Runnable() {
public void run() {barResult = bar();
}});
// now use the value in barResult...}Object bar() {// generate lots of garbage// ... new MyObject() ...
}}
Illegalreference
Allocated inscoped memory lt
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
1310 March 2003
RT-Java Memory Summary
• Assign memory regions to execution scopes• new operations allocate from current region
• Introduces memory management dependencies between caller and target
• Hurts reusability of code• Integrates concerns of program• Introduces interdependence of libraries
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
1410 March 2003
High-Level View
PP + = PPReal-Time JavaJava
DynamicAnalysisDynamicAnalysis
ProgramInstrumentation
ProgramInstrumentation
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
1510 March 2003
Valley ofStatic Analysis
Valley ofDynamic Analysis
THE TRUTHTHE TRUTH
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
1610 March 2003
Stack of Regions == Call Stack
M()
N()
O()
P()
Q()
R()
S()
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
1710 March 2003
Goal:AP : OP ? FP
OP: objects of program PFP: call-stack frames of program P
Given program P …
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
1810 March 2003
Approach
• Run program P…• Observe referencing behavior• Observe object lifetimes• Come up with scoping hierarchy that does this
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
1910 March 2003
Determine Referencing andObject Lifetimes
• Keep a doesReference graph
• Annotate it with object death frames
A C
D E
B
F
G1
2
0 2
222Represents all legal scoping hierarchies
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
2010 March 2003
Solving the doesReference graph
• Decompose into strongly-connected components
A C
D E
B
F
G1
2
0 2
222A B C
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
2110 March 2003
Solution to doesReference graph
• Propagate smaller numbers through the graph
D
A B C
F
G1
2
0 2
20
0
E
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
2210 March 2003
How to recognize objects from one run
to another?Q:
A:Use an object’s
allocation site as a discriminator
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
2310 March 2003
Object Behavior Categories
• How to map objects observed in previous analyses to objects observed in current program execution?§ Class-based behavior
• All instances of a class behave similarly
§ “New site”-based behavior• All instances of a class instantiated at the same source
code location behave similarly
§ Instance-based behavior• Instances of a class behave differently
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
2410 March 2003
Impact of Simplification
• Consider a method that acts as a Factory:class LinkedList {public Iterator iterator() {return new LLIterator();
}}
Iterator i = list.iterator();while(i.hasNext()) {// ...
}
list.iterator
()
list.iterator
()
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
2510 March 2003
Revised Goal:
AP : SP ? ?FP
SP: allocation sites of program PFP : call-stack frames of program P?FP : object lifetimes in # of frames
Given program P …
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
2610 March 2003
Determine Referencing andObject Lifetimes
A C
D E
B
F
G2,1
2,2
0,0 2,2
2,22,22,2
birth frame, collection frame
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
2710 March 2003
Solving the Graph
• Liveness constraints:For each vertex vScope(v) = max oi – oc
• Reference constraints:For each edge (u,v)Scope(v) = Scope(u) + max oi – min oi
o ? Objects(v) o ? Objects(u)
o ? Objects(v)
oi = object allocation frameoc = object collection frame
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
2810 March 2003
Solving the Graph
D E F
G2,1
2,2
0,0 2,2
A B C2,2
0
0
01
0 ?
Scope(ABC) = Scope(E) + max oi – min oiABC E
Reference Constraint:
birth frame, collection frame
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
2910 March 2003
Solution to doesReference graph
D E F
G
A B C
2,1
2,2
0,0 2,2
2,2
0
0
01
2
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
3010 March 2003
Deriving the Reference Constraint
• Scope(v) = Scope(u) + max oi – min oio ? Objects(v) o ? Objects(u)
In absolute depth model using individual objects:Suppose we know that object A references object BWe want to instantiate A – where does it go ?
B
A
Scope(B) = Scope(A)
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
3110 March 2003
Deriving the Reference Constraint
• Scope(v) = Scope(u) + max oi – min oio ? Objects(v) o ? Objects(u)
In relative depth model using instantiation sites:Suppose we know that u references vWe want to create something at u – where does it go ?
B ? u
Scope(v) = Scope(u)
but there’s another case…
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
3210 March 2003
Deriving the Reference Constraint
• Scope(v) = Scope(u) + max oi – min oio ? Objects(v) o ? Objects(u)
In relative depth model using instantiation sites:Suppose we know that u references vWe want to create something at u – where does it go ?
A ? v
B ? u
Scope(v) = Scope(u)
but there’s another case…
Scope(v) = Scope(u) + max difference
A ? v
difference ininstantiation
requestframes
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
3310 March 2003
High-Level View
PP + = PPReal-Time JavaJava
DynamicAnalysisDynamicAnalysis
ProgramInstrumentation
ProgramInstrumentation
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
3410 March 2003
Aspect-Oriented Programming
• Advanced Separation of Concerns (ASoC)• quantification§ Our object analysis and program instrumentation
can be expressed in modular pieces of code
• obliviousness§ Target code is oblivious to this extra processing
• it need not be changed at all
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
3510 March 2003
Reference Probe
1 Determine references between objectsa. Track objects’ references to other objectsb. Build a doesReference graph
2 Discover legal scoping hierarchiesa. Collapse strongly connected componentsb. The resulting DAG is a representation of legal
scoping hierarchies
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
3610 March 2003
Effect of Reference Probeclass C {MyObject anObject;
void foo() {
anObject = new MyObject();
// do other stuff...}
}
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
3710 March 2003
Effect of Reference Probeclass C {MyObject anObject;
void foo() {MyObject o = new MyObject();ReferenceProbe.newObject(o, getSourceLocation());
out = new ObjectOutputStream(sock.getOutputStream());out.writeObject(localUser);in = new ObjectInputStream(sock.getInputStream());System.out.println("BACK HERE");try {System.out.println("reading ...");remoteUser = (User)in.readObject();System.out.println("read it!");
} catch(ClassNotFoundException e) {throw new UnrecognizedObjectException(e );
}}
public User getLocalUser() {return localUser;
}
public User getRemoteUser () {return remoteUser ;
}
public long getDuration() {return closed ? duration : System.currentTimeMillis() - duration;
}
public Socket getSocket() {return sock;
}
public InetAddress getRemoteAddress() {return sock.getInetAddress();
}
public boolean isClosed() {return closed;
}
public void write(Object o) throws IOException {out.writeObject(o );
}
public Object read() throws IOException {try {return in.readObject();
} catch(ClassNotFoundException e) {throw new UnrecognizedObjectException(e );
}}
public void update() {NetworkedMainFrame.instance().updateConnection(this);
}
public void close() throws IOException {if(closed)return;
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
5810 March 2003
Combining Static and Dynamic Analyses
• Static analysis used for§ Eliminating RTSJ-mandated reference checks§ Verifying scope assignments, flagging others for
programmer input
• Dynamic analysis used for§ Initial scope assignment§ Debugging information
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
5910 March 2003
Related Work
• Regions§ Tofte & Talpin§ Gay & Aiken
• Real-Time Java analysis§ Pointer and escape analysis for Java§ Salcianu & Rinard
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
6010 March 2003
Remarks
• Garbage collection is great, but…• Stack-based region-allocated objects• RTSJ scoped memory breaks modularity• Compose with results of static analysis• Extension to non-RTSJ systems
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
6110 March 2003
Contributions
• Use of aspects in dynamic analysis§ Deters, Leidenfrost, and Cytron. Translation of Java to Real-Time
Java, International Workshop on Aspect-Oriented Programming and Separation of Concerns, August 2001
• A Real-Time Java translation system, implemented in AspectJ, that requires no program annotation and transforms Java programs into ScopeMemory-aware RT-Java programs modularly§ Deters and Cytron. Introduction of Program Instrumentation using
Aspects, OOPSLA Workshop on Advanced Separation of Concerns in Object-Oriented Systems, October 2001
• Automation of scope detection and selection§ Deters and Cytron. Automated Discovery of Scoped Memory Regions
for Real-Time Java, International Symposium on Memory Management, June 2002
Washington UniversitySt. Louis
Dynamic Assignment of Scoped Memory RegionsMorgan Deters
6210 March 2003
Acknowledgments
• Dr. Ron K. Cytron• Committee§ Dr. Chris Gill§ Dr. Aaron Stump
• JVM hacking§ Nick Leidenfrost§ Dante Cannarozzi§ Matt Hampton