280 likes | 409 Views
Monitoring Design Pattern Contracts. Neelam Soundarajan Ohio State University. Jason O. Hallstrom Clemson University. Benjamin Tyler (Presenter) Ohio State University. SAVCBS ’04 Workshop at ACM SIGSOFT 2004/FSE-12. Design Patterns and Monitoring. Patterns capture key system behaviors
E N D
Monitoring Design Pattern Contracts Neelam Soundarajan Ohio State University Jason O. Hallstrom Clemson University Benjamin Tyler (Presenter) Ohio State University SAVCBS ’04 Workshop at ACM SIGSOFT 2004/FSE-12
Design Patterns and Monitoring • Patterns capture key system behaviors • Behaviors often cross-cutting • Usually, specified informally • To monitor and test behaviors, informal specs are not enough
Our Approach • AOP aspect – Language construct, similar to a class, that implements cross-cutting behaviors for classes in an application • Aspects can be used to capture design pattern contracts • These aspects can be used to monitor system behavior to see if these contracts are respected
Methodology Overview • The contract aspect contains the main checking logic for a pattern Contract Aspect for P (abstract)
Methodology Overview • The contract aspect contains the main checking logic for a pattern System Code Purportedly uses pattern P Contract Aspect for P (abstract)
Methodology Overview • The subcontract aspect contains definitions specific to the application System Code Purportedly uses pattern P Contract Aspect for P (abstract) Subcontract Aspect Specific to System
Methodology Overview System Code Purportedly uses pattern P Contract Aspect for P (abstract) Subcontract Aspect Specific to System Compile/ Weave Compiled System Monitors P
Methodology Overview Another System Also uses P Contract Aspect for P (abstract) Subcontract Aspect Specific to System
Methodology Overview Another System Also uses P Contract Aspect for P (abstract) Another Subct. Aspect Specific to System
Methodology Overview Another System Also uses P Contract Aspect for P (abstract) Another Subct. Aspect Specific to System Compile/ Weave Compiled System Also monitors P
Benefits of Our Approach • No instrumentation of source code • Monitoring code separate from application code • Contract aspect needs to be created only once for a given design pattern • Only need to create new subcontract aspects for each system to be monitored
Aspect Terminology • Pointcut – A set of places in the class code where an aspect can insert new behavior • Advice – Code associated with a pointcut that implements this new behavior
class Calc { int st; void Sqrt() { int i = 1; while(i*i<=st) i++; st = i-1; } } Monitoring with Aspects aspect CalcMonitor { int st_old; pointcut SqPc(Calc c): exec(Sqrt()) && targ(c); before(Calc c): SqPc(c) { } assert(c.st >= 0); st_old = c.st; after(Calc c): SqPc(c) { } assert(c.st^2 <= st_old && st_old <(c.st+1)^2); }
Pattern Example: Observer Subject +Attach(in Observer) +Notify() +Detach(in Observer) Observer + Update() observers 1 * for all o in observers o.Update() ConcreteSubject ConcreteObserver + Update() 1 * - subjectState - observerState subject
The Contract Aspect • Formalizes the behavior intended by the pattern • Advice provides the main monitoring code for the pattern • However, method names/signatures and concrete definitions for relations vary from application to application • Use abstract pointcuts and methods for these in the contract aspect
Part of the Contract Aspect for Observer • Property: After call to Update(), the Observer should be “consistent” with its subject after(Subj s, Obs o): UpdatePc(s, o) { assert(Consistent(s, o)); } abstract pointcut UpdatePc(Subj s, Obs o) ; abstract boolean Consistent(Subj s, Obs o) ;
Part of the Contract Aspect for Observer • Another property: If a call to a Subject method results in a change in state, Notify() should have been called after(Subj s): SubjMethPc(s) { assert(! Modified(s_old, s)); } before(Subj s): NotifyPc(s) { s_old = copyObject(s); … } abstract pointcut SubjMethPc(Subj s) ; abstract pointcut NotifyPc(Subj s) ; abstract boolean Modified(Subj s1, Subj s2) ;
In the Subcontract Aspect… • Associate concrete classes with pattern roles • Insert dummy interfaces in the class hierarchy (AspectJ feature) • Pointcut definitions that associate role methods with actual methods in the application • Definitions for relations specific to the given application
Hospital Example • Patients play the Subject role • State includes temp and heart rate • Nurses observe Patients • Keep track if Patients have a fever or not • Updates done with Nrs.TempUpd(Pat) • Doctors also observe Patients • Record whether or not patients are in stable condition • Updates done with Doc.CondUpd(Pat)
Pointcuts in the Subcontract Aspect • In the subcontract aspect, we give definitions for the abstract pointcuts mentioned in the contract aspect • Examples: pointcut UpdatePc(Subjsubj, Obsobs) : ( exec(Nrs.TempUpd(Pat)) || exec(Doc.CondUpd(Pat)) ) && targ(obs) && arg(subj) ; pointcut SubjMethPc(Subjsubj) : exec(Pat.*(..)) && targ(subj) ;
Defining Relations • Need to provide definitions of application-specific relations in the subcontract aspect • Example: Modified boolean Modified(Subjs1, Subjs2) { return s1.temp != s2.temp || s1.hrtRate != s2.hrtRate; }
Defining Relations • Another example: Consistent boolean Consistent(Subjsubj, Obsobs) { if (obs instanceOf Nrs) { return subj.temp > 104 == obs.isFeverish(subj); } else if (obs instanceOf Doc) { return (55 < subj.hrtRate < 100 && 92 < subj.temp < 105) == obs.isStable(subj); } else { /* Error! */} }
Discussion • Contract aspects generally harder to write than the subcontract aspects • Only have to write them once! • Information such as method call sequences can be tracked using auxiliary variables in the aspect • Pointcuts can encompass more than just method calls • Current and future work • MonGen and PCL
Presented Methodology System Code Purportedly uses pattern P Contract Aspect for P (abstract) Subcontract Aspect Specific to System Compile/ Weave Compiled System Monitors P Monitoring Code
System Code Purportedly uses pattern P Monitoring Aspect for System Compile/ Weave Compiled System Monitors P MonGen Methodology Contract Spec for P MonGen Subcontract Spec Specific to System Specs written in PCL
Conclusion • Design patterns often describe behaviors that encompass multiple classes • Aspects are a natural way to monitor such behaviors • Separating the essence of the pattern from the details of the application saves time and effort
Questions? • e-mail • tyler@cse.ohio-state.edu • MonGen Web page: • www.cse.ohio-state.edu/~tyler/MonGen