380 likes | 507 Views
A Selective, Just-in-Time Aspect Weaver. Yoshiki Sato (T.I.Tech, Japan) Shigeru Chiba (T.I.Tech, Japan) Michiaki Tatsubori (IBM TRL). About This Talk…. Dynamic AOP (Aspect-Oriented Programming) What is dynamic AOP? Why do we need it? Scenario1 : Performance profiling
E N D
A Selective, Just-in-Time Aspect Weaver Yoshiki Sato (T.I.Tech, Japan) Shigeru Chiba (T.I.Tech, Japan) Michiaki Tatsubori (IBM TRL) GPCE'03, Erfurt, Germany
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 GPCE'03, Erfurt, Germany
Dynamic AOP ? • Aspects are dynamically woven into programs • A woven aspect can be composed, updated and removed at runtime • Benefits • Aspects can be woven without restarting application software • Changing the behavior of applications on runtime contexts while separating concerns. GPCE'03, Erfurt, Germany
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 profiledata • This iteration is annoying • Restarting some large software(e.g. Websphere) is really slow Focus attention on suspicious operations GPCE'03, Erfurt, Germany
Dynamic Aspect Weaving • Allows us to weave a new profiling aspect interactively • without shutting down/restarting the application software • This improves the efficiency of software development GPCE'03, Erfurt, Germany
An Example Scenario(2) • Adaptable response cache • Enables 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… GPCE'03, Erfurt, Germany
aspect Tangling code seemed to be separated switch (context) { case A: policyA case B: policyB . . . . . still tangling completely separated Cache policyA Cache policyB Cache policyC 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 . . . . switch (context) { case conditionA: cachepolicyA case conditionB: cachepolicyB } . . . . . . . . . if (. . .) { . . . . switch (context) { switch (context) { case . . . . . . . . if (. . .) { web application program GPCE'03, Erfurt, Germany
Our Motivation and Goal • Dynamic AOP is useful • An ordinary implementation for it is very slow • Our goal is to address the performance problem in dynamic AOP GPCE'03, Erfurt, Germany
Hook inserted hook code do_before_advice(); do_after_advice(); aspect do_before_advice(); do_after_advice(); 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 Program join point pointcut GPCE'03, Erfurt, Germany
Each join point is checked on every execution whether it is activated or not • Involves large overheads in normaloperations with no woven aspect • It causes code bloat 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(); identified join points need_advice(); need_advice(); need_advice(); need_advice(); need_advice(); need_advice(); need_advice(); need_advice(); need_advice(); GPCE'03, Erfurt, Germany
proposal 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 GPCE'03, Erfurt, Germany
or debugger do_advice(); do_advice(); do_advice(); debugger proposal Mixing Two Types of Hooks • Users can choose a suitable hook at each join point considering the whole cost • They are inserted through two steps • All are inserted as a breakpoint by a debugger • Some are executed by a debugger, others are embedded as a method call using dynamic code translation pointcut GPCE'03, Erfurt, Germany
As a Breakpoint (Java Platform Debugger Architecture) • A debugger process setsit 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 A debugger runs advice sets breakpoints GPCE'03, Erfurt, Germany debugger debugger do_advice();
Tons of Context Switches !! do_advice(); GPCE'03, Erfurt, Germany
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 LoadedClasses JVM Hooks embedded class JPDA reload do_advice(); do_advice(); do_advice(); do_advice(); do_advice(); GPCE'03, Erfurt, Germany
Vast runtime compilation time !! 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(); GPCE'03, Erfurt, Germany
Breakpoints for rare advice • Avoiding wasteful compilations • Method calls for frequent advice • Fast advice execution Well-balanced!! 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(); GPCE'03, Erfurt, Germany
The Way of Hybrid • An aspect in Wool aspect class 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 GPCE'03, Erfurt, Germany
The Way of Hybrid • Users can override the hook() method to describe which hooks are selected • They 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 } GPCE'03, Erfurt, Germany
do_advice(); do_advice(); 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 Currently being executed at weaving time Foo#foo() Foo#bar() GPCE'03, Erfurt, Germany Foo#hoge()
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 HotSpotTMClient VM Arch : Sun Blade1000, OS : Solaris8 CPU:UltraSPARC-III dual 750MHz, RAM:1GB Inserting unnecessary hooks are avoided Minimum necessary compilations are done GPCE'03, Erfurt, Germany
26% 98% 19% Experimental Results Ideal : 1013ms (AspectJ) execution 400714 hook insertion pointcut Elapsed time [ms] 10938 10058 8138 4077 Favorite hooks are inlined Compilation time is paid off 4514 398,286 conditional hooks are removed 3553 1196 2428 2428 2428 Static code translation All-breakpoint All-method-call Hybrid GPCE'03, Erfurt, Germany
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 GPCE'03, Erfurt, Germany
Concluding Remarks • Wool – a Java-based dynamic aspect weaver • Just-in-time hook insertion • Hybrid of two types of hooks • Future work • Performance tuning • Now on enhancing the new aspect API in Wool • Experiments on a practical application GPCE'03, Erfurt, Germany
The End Thank you for your attentions! If any questions, please ask me “clearly”. GPCE'03, Erfurt, Germany
Additional Slide • An Implementation Overview of Wool • The new aspect API • Ideal hook insertion • Examples of Active frame • Executing Advice in Active Methods • Not Executing.. • Using Design Patterns • JIT Aspects GPCE'03, Erfurt, Germany
Class Reloading Javassist do_advice(); do_advice(); JPDA Bytecode translation Breakpoints An Implementation Overview of Wool • Just-in-time aspect weaver • Sets breakpoints and redefines classes through the JPDA • Runtime bytecode translation using Javassist aspects + JVM Wool Advice execution GPCE'03, Erfurt, Germany
Ideal Hook Selection • Firstly, all hooks are inserted as breakpoints • Only favorite join points are inlined slow • For rarely executed advice • Avoiding wasteful compilations is important breakpoints Advice execution 1 hybrid • For frequently executed advice • Fast advice execution is important 2 method calls Hook insertion slow GPCE'03, Erfurt, Germany
New Aspect Design class 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(); } pointcut Passes runtime contexts Only if explicitly needed advice GPCE'03, Erfurt, Germany
The Case of Executing Advice in Active Methods • The consistency of advice is not kept against time line void bar() { foo(); } void foo() {} void hoge() { bar(); } aspect foo() bar() bar() bar() hoge() hoge() hoge() hoge() hoge() before_advice() before_advice() after_advice() after_advice() before_advice() GPCE'03, Erfurt, Germany
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 wrong public aspect ProfileAspect { Time timer = new Time(); pointcut operation() : call(public * XMLParser.*(..)); before() : operation() { timer.start(); } after() : operation() { timer.stop(); } } A pair of advice GPCE'03, Erfurt, Germany
Strategy pattern? • Allows algorisms switching at runtime • Cache (invalidation) operations scatters over concrete classes • Strategy pattern + AOP • Can separate only appearance • Cannot switch at runtime • Class bloat to prepare for every situations PortalSiteServlet CacheStrategy aspect User1 User2 User3 ・・・ Cache operations GPCE'03, Erfurt, Germany
JIT Aspects • Inserts minimum hooks into all join points on native code generated by a JIT compiler • Native 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(); GPCE'03, Erfurt, Germany need_advice();
Join point model ・field access Execute advice at identified join point a piece of code a point in a program ・method call ・object creation Dynamic Join Point Model (AspectJ[*1]) ・・・ Aspect ShoppingCart.getAmount() pointcut identifying join points ShoppingCart.update() advice describing operations to execute at the points join point do_cache() invalidate() *1 Xerox Corporation. “The AspectJ Programming Guide. Online Documentation”, 2001. http://www.aspectj.org/ GPCE'03, Erfurt, Germany
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 GPCE'03, Erfurt, Germany
instantiate Auction CacheAspect StockMarket CacheAspect motivation Adaptable Response Cache • An aspect can be parameterized by the runtime environment • The optimal cache aspect is instantiated and composed Cache Aspect A heavy auctioneer program A stock market watcher GPCE'03, Erfurt, Germany
do_advice(); do_advice(); or debugger debugger do_advice(); proposal Mixing Two Types of Hooks • Wool inserts hooks through two steps • Inserts debugger breakpoints all the join points • 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 pointcut GPCE'03, Erfurt, Germany
注意 • Advice • Insert • Embed • Dynamic • technique • in advance • performance problem • overhead • On demand • Serious • Hook GPCE'03, Erfurt, Germany