sep 22-25, 2003gpce'03, erfurt, germany1 a selective, just-in-time aspect weaver yoshiki sato...
TRANSCRIPT
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 1
A Selective, Just-in-Time Aspect Weaver
Yoshiki Sato (T.I.Tech, Japan)Shigeru Chiba (T.I.Tech, Japan)Michiaki Tatsubori (IBM TRL)
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 2
About This Talk…
Dynamic AOP (Aspect-Oriented Programming) What is dynamic AOP? Why do we need it?
• Scenario1 : Performance profiling• Scenario2 : Web response cache
Our goal Efficient dynamic AOP system
I will introduce Wool : a dynamic aspect weaver for Java
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 3
Dynamic AOP ?
Aspects are dynamically woven into programsA woven aspect can be composed, updated
and removed at runtimeBenefits
Aspects can be woven without restarting application software
Changing the behavior of applications on runtime contexts while separating concerns.
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 4
An Example Scenario(1)
Performance profiling Typical profiling iteration
• Stop the software• Write a profiling aspect• Weave it into the program• Restart the software• Collect and analyze the profile data
This iteration is annoying• Restarting some large software(e.g. Websphere) is
really slow
Focus attention onsuspicious operations
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 5
Dynamic Aspect Weaving
Allows us to weave a new profiling aspect interactivelywithout shutting down/restarting the
application software
This improves the efficiency of software development
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 6
An Example Scenario(2)
Adaptable response cacheEnables a web application to adapt the
caching policy for runtime contexts• E.g. current client, hit ratio, CPU load,
memory usage, time zone…
The implementation would be tangled• Design patterns are not helpful• AspectJ? NO! Because…
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 7
Static Weaving Aspect is Not Enough Static aspect weaver has
No mechanism to switch aspects by using runtime contexts Many policies would be still tangled in an aspect
Dynamic aspect weaver allows Switching aspects according to runtime contexts Separating concerns relying on runtime contexts into aspects
web applicationprogram
stilltangling
switch (context) {case A: policyAcase B: policyB. . . . .
aspectTangling code seemedto be separated
. . . .switch (context) {case conditionA: cachepolicyAcase conditionB: cachepolicyB}. . . . .. . . .if (. . .) {. . . .switch (context) {
switch (context) {case . . . .. . . . if (. . .) {
CachepolicyA
completelyseparated
CachepolicyC
CachepolicyB
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 8
Our Motivation and Goal
Dynamic AOP is usefulAn ordinary implementation for it is
very slow
Our goal is to address the performance problem in dynamic AOP
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 9
Hook
aspect
Typical Implementation with Inserting “Hooks” Hooks connect a base program with aspects
Intercept execution of a program at the identified join point and the associated advice is executed
Compilers or preprocessors insert hook code statically
Base ProgramBase Program
joinpoint
pointcut
insertedhook code
do_before_advice();do_after_advice();
do_before_advice();do_after_advice();
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 10
Static Code Translation
Inserts hooks into all join points in advance Join points required to hook aren’t known
before weaving All are conditional hooks that do not always
execute advice
need_advice();need_advice();
need_advice();need_advice();
need_advice();
need_advice();need_advice();
need_advice();need_advice();need_advice();need_advice();need_advice();
Each join point is checked on every execution whether it is activated or notInvolves large overheads in normaloperations with no woven aspectIt causes code bloat
identifiedjoin points
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 11
Wool
A Java-based dynamic, just-in-time aspect weaver Provides Java APIs to write aspects, and
is a pure Java weaver Inserts hooks on demand
Vast conditional hooks are not inserted, because they are inserted after aspects are woven
proposal
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 12
Mixing Two Types of Hooks
Users can choose a suitable hook at each join point considering the whole cost They are inserted through two steps1. All are inserted as a breakpoint by a debugger2. Some are executed by a debugger, others are
embedded as a method call using dynamic code translation
debugger
pointcut
proposal
do_advice();debugger
or do_advice();do_advice();
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 13
As a Breakpoint A debugger process sets it through the JPDA
at runtime If the thread of control reaches one of the breakpoints, it
switches to the debugger process and the advice code associated with that breakpoint is run
Full-speed debug mode execution with HotSpot (3~6% down)
Pros and cons Since hooks inserted in advance, unnecessary hooks are
not inserted Advice execution is slow
sets breakpointsdebugger
do_advice();debugger
A debuggerruns advice
(Java PlatformDebugger
Architecture)
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 14
do_advice();
Tons of Context Switches !!
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 15
As a Method CallDynamic Code Translation Embeds hooks into the base program by
reloading a modified class The runtime replacement of bytecode is done using the
hotswap mechanism of the JPDA (J2SDK1.4)• Allows loaded classes to be updated at runtime under
the control of a debugger Pros and cons
Since this hook is just a method call, advice execution is quite fast
Hook insertion is slow
JPDAJVM
do_advice();do_advice();
do_advice();
do_advice();do_advice();
Hooks embeddedclass
reload
LoadedClasses
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 16
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();do_advice();do_advice();do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();do_advice();do_advice();do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
Vast runtime compilation time !!
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 17
do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();do_advice();do_advice();do_advice();do_advice();
do_advice();do_advice();do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();do_advice();do_advice();do_advice();do_advice();
do_advice();do_advice();do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();
do_advice();do_advice();do_advice();do_advice();
Well-balanced!!
Breakpoints for rare adviceAvoiding wasteful compilations
Method calls for frequent adviceFast advice execution
do_advice();do_advice();
do_advice();do_advice();
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 18
The Way of Hybrid
An aspect in Woolclass LoggingAspect extends WlAspect { Pointcut log = Pointcut.methodCall(….);
public void weave(Wool wl) throws Exception { wl.insert(new BeforeAdvice(log) { public void advice(Joinpoint jp) { System.out.println(“IN ” + jp); }}); }
public void hook(…) ... }
pointcut
advice
aspect
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 19
The Way of Hybrid
Users can override the hook() method to describe which hooks are selectedThey can decide a suitable type of hooks
at every hooked join point public void hook(Wool wool, Joinpoint jp) throws WoolException { wool.advice(jp); // <- executed by a debugger Class cl = jp.method().declaringType(); if (map.increment(cl) >= 20) // <- checks the count wool.embedHook(jp, cl.getName()); // <- dynamic code translation }
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 20
Deal with Active Methods
An active method is a method currently being executed at weaving time Advice should be executed in it? It depends on the situation
Users can override hook() to decide it They can decide whether dynamic code translation is
immediately performed or delayed, why? Because the class definition for an old class and a new
class are different after HotSwap is done
Foo#hoge()
Foo#bar()
Foo#foo()
do_advice();do_advice();
Currently beingexecuted
at weaving time
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 21
Experimental Results Jess benchmarks (SPECjvm98) monkey banana
Over 10,000 lines of code and 140 classes Null before advice into all the public methods If using AspectJ, the aspect is woven into 76 classes, the program is
hooked 163 places and the hooks are totally called 87,457 times
Sun Java 2 SDK v1.4.0 HotSpotTM Client VMArch : Sun Blade1000, OS : Solaris8CPU:UltraSPARC-III dual 750MHz, RAM:1GB
Static code translation
Wool (all breakpoint)
Wool (all method call)
Wool(hybrid)
The number of hooks
2,815(1727%)
163(100%)
The intercepted count
1,077,338(1231%)
87,457(100%)
Translated classes
149 (196%) 0 76 (100%) 15 (20%)
Inserting unnecessary hooks are avoided
Minimum necessarycompilations are done
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 22
Experimental Results
0
5000
10000
15000
20000400714
1093810058 8138
26%
19%
98%
conditional hooksare removed
Favorite hooksare inlined
Compilation timeis paid off
Ideal : 1013ms(AspectJ)
2428
11964514
3553398,286
4077
24282428
Ela
pse
d tim
e
[ms]
Static codetranslation
All-breakpoint All-method-call Hybrid
execution
hook insertion
pointcut
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 23
Discussion - Performance Tuning of Wool Pure execution time using Wool was 4 times
slower than the ideal result using AspectJ
Wool reifies the runtime context at every join points whereas AspectJ does only if explicitly required with thisJoinPoint-> Wool should analyze which join point requires the runtime context
Wool calls an advice method indirectly because advice is represented as a closure-> An advice invocation should be represented as just a method invocation
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 24
Concluding Remarks
Wool – a Java-based dynamic aspect weaver Just-in-time hook insertionHybrid of two types of hooks
Future workPerformance tuning
• Now on enhancing the new aspect API in Wool
Experiments on a practical application
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 25
The End
Thank you for your attentions!If any questions, please ask me “clearly”.
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 26
Additional Slide
An Implementation Overview of WoolThe new aspect API Ideal hook insertionExamples of Active frame
Executing Advice in Active MethodsNot Executing..
Using Design Patterns JIT Aspects
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 27
An Implementation Overview of Wool Just-in-time aspect weaver
Sets breakpoints and redefines classes through the JPDA
Runtime bytecode translation using Javassist
JVM
Wool
ClassReloading
aspects
+Javassist
Bytecodetranslation
do_advice();do_advice();
JPDA
BreakpointsAdvice execution
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 28
Ideal Hook Selection
Firstly, all hooks are inserted as breakpoints
Only favorite join points are inlined
Hook insertion
method calls
breakpointsAd
vice
execu
tion
For rarely executed adviceAvoiding wasteful compilations is important
For frequently executed adviceFast advice execution is important
2
1hybrid
slow
slow
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 29
New Aspect Designclass ProfileAspect extends WlAspect { Timer timer = new Timer();
public void pointcut_timedentry() { Pointcut timedentry = Pointcut.methodCall(“*",“FigureElement","paint","*"); associate(pointcut, “entry", WlAdvice.BEFORE); associate(pointcut, “exit", WlAdvice.AFTER); }
public void entry(FigureElement $target) { timer.start(); } public void exit(FigureElement $target) { timer.stop(); }
Passes runtime contextsOnly if explicitly needed
pointcut
advice
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 30
The Case of Executing Advice in Active Methods The consistency of advice is not kept
against time line
hoge() hoge()bar()
hoge()bar()foo()
hoge()bar()
hoge()
aspectaspect
before_advice()before_advice()before_advice()after_advice()after_advice()
void hoge() { bar();}
void bar() { foo();}
void foo() {}
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 31
The Case of Not Executing Advice in Active Methods A pair of before and after advice woven at the
same join point For example, measuring the elapsed time of method
execution If only after advice are executed in active methods, the
resulting elapsed time would be wrongpublic aspect ProfileAspect { Time timer = new Time(); pointcut operation() : call(public * XMLParser.*(..)); before() : operation() { timer.start(); } after() : operation() { timer.stop(); }}
A pair of advice
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 32
Strategy pattern ? Allows algorisms
switching at runtimeCache (invalidation) operations scatters over concrete classes
Strategy pattern + AOPCan separate only appearanceCannot switch at runtime
Class bloat to prepare for every situations
PortalSiteServlet
CacheStrategy
User2 User3User1 ・・・
Cache operations
aspect
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 33
JIT Aspects
Inserts minimum hooks into all join points on native code generated by a JIT compilerNative hooks are smaller than Java’s
one Implemented in Jalapeno VM
• Compile only approach• Baseline compiler• Many shadow hooks
need_advice();need_advice();
need_advice();need_advice();
need_advice();
need_advice();need_advice();
need_advice();need_advice();need_advice();need_advice();need_advice();
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 34
Join point model
・ field access
・ method call・ object creation
・・
・
Execute advice at identified join point a piece of code a point in a program
Dynamic Join Point Model(AspectJ[*1])
pointcut identifying join pointsadvice describing operations to execute at the points
Aspect
join point
*1 Xerox Corporation. “The AspectJ Programming Guide. Online Documentation”, 2001. http://www.aspectj.org/
do_cache()invalidate()
ShoppingCart.getAmount()
ShoppingCart.update()
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 35
Goals of Dynamic AO
requires:Small footprint
• Low overhead under normal operations• No modification of runtime systems (JVM)
Quick weaving (hook insertion)• Short suspending time
Fast advice execution
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 36
Adaptable Response Cache
An aspect can be parameterized by the runtime environment
The optimal cache aspect is instantiated and composed
programA heavyauctioneer
A stock marketwatcher
StockMarketCacheAspect
CacheAspectAuction
CacheAspect
instantiate
motivation
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 37
Mixing Two Types of Hooks Wool inserts hooks through two steps
1. Inserts debugger breakpoints all the join points2. Lets users choose a suitable implementation of hook
from two types for each join point Two types of Hooks
Remaining as a breakpoint and executed by a debugger Embedded as a method call using dynamic code
translation
debugger
pointcut
proposal
do_advice();do_advice();
do_advice();debugger
or
Sep 22-25, 2003 GPCE'03, Erfurt, Germany 38
注意 Advice Insert Embed Dynamic technique in advance performance problem overhead
On demand Serious Hook