public abstract class MethodTraceAspect
extends java.lang.Object
aop.xml
file. That concrete aspect must define a pointcut for the abstract scope()
method.
Although the tracing advice implementation is designed to introduce as little overhead as possible, the overhead is quite noticeable while data collection is active (less so while it is disabled). This overhead will be reflected in the collected data, and it should be taken into account when analyzing the results. Part of the overhead is due to the load-time weaving of this aspect with the classes it is measuring. This weaving is done once, when the target class is first loaded into the JVM. To minimize the impact of this one-time weave on the trace results, it is recommended to perform the work flow to be measured once before starting data collection, then perform it again with data collection turned on. However, this is not always possible, if what is being measured is the first-time execution of a code path, where future executions will be short-circuited by caching of various resources by the runtime.
Due to the aforementioned overhead which this feature introduces to running code, it is recommended not to enable this feature in production deployments.
Tracing is controlled by the enableDataCollection(boolean)
method. When an active
tracing session is disabled, all data which has been collected up to that point is
post-processed and written to a CSV file, then discarded.
Modifier and Type | Class and Description |
---|---|
private static class |
MethodTraceAspect.BucketData
A class which organizes method trace data into specific buckets.
|
private static class |
MethodTraceAspect.ByTotalTime
A comparator which sorts trace data objects into descending order by total elapsed time.
|
private static class |
MethodTraceAspect.Call
Representation of a method invocation which identifies the caller and the callee methods,
optimized for use as a hash map key.
|
private static interface |
MethodTraceAspect.ElapsedTimeable
An interface with one method to return elapsed time.
|
private static class |
MethodTraceAspect.MethodIdentifier
A wrapper for a method, optimized for use as a hash map key.
|
private static class |
MethodTraceAspect.TraceBucket
Trace data for a single analysis bucket, including the trace data itself and an aggregate
of all the traces' own times.
|
private static class |
MethodTraceAspect.TraceData
Data structure containing invocation count and aggregate elapsed time for a method call.
|
Modifier and Type | Field and Description |
---|---|
private static java.lang.String[] |
buckets
Package "buckets" for data post-processing
|
private java.lang.ThreadLocal<java.util.Deque<MethodTraceAspect.TraceData>> |
callStack
Thread-local stack of trace data since entering the first join point in the traced code
|
private static java.lang.String |
CSV_FILE
Fixed name of CSV output file (written to current directory)
|
private static boolean |
enabled
Flag which controls data collection
|
private static java.util.logging.Logger |
log
Logger
|
private static java.util.Map<MethodTraceAspect.Call,MethodTraceAspect.TraceData> |
traceData
Map of profile data by method signature
|
private static long |
traceStart
Start time of a trace session, in milliseconds
|
Constructor and Description |
---|
MethodTraceAspect() |
Modifier and Type | Method and Description |
---|---|
static void |
enableDataCollection(boolean enable)
Enable or disable data collection.
|
private static java.lang.String |
matchBucket(MethodTraceAspect.MethodIdentifier ident)
Attempt to match a method's declaring class with a data bucket identifier.
|
private static void |
processData()
Post-process the collected trace data and write the result to a CSV file.
|
abstract void |
scope()
An abstract pointcut which should be defined for load-time weaving inside a
concrete-aspect element within META-INF/aop.xml . |
java.lang.Object |
trace(org.aspectj.lang.ProceedingJoinPoint joinPoint)
The primary tracing advice which collects trace information about calls to the target
methods.
|
private static final java.util.logging.Logger log
private static java.lang.String CSV_FILE
private static volatile boolean enabled
private static long traceStart
private static final java.util.Map<MethodTraceAspect.Call,MethodTraceAspect.TraceData> traceData
private final java.lang.ThreadLocal<java.util.Deque<MethodTraceAspect.TraceData>> callStack
private static final java.lang.String[] buckets
public static void enableDataCollection(boolean enable)
enable
- true
to start data collection; false
to stop it and process the
results.private static void processData()
private static java.lang.String matchBucket(MethodTraceAspect.MethodIdentifier ident)
ident
- Method identifier.null
.public abstract void scope()
concrete-aspect
element within META-INF/aop.xml
.public java.lang.Object trace(org.aspectj.lang.ProceedingJoinPoint joinPoint) throws java.lang.Throwable
Data is collected safely across all threads on which calls to the traced methods are made.
The caller method is determined using a thread-local stack, which is cleared any time this advice is entered after data collection has been halted. Note that the relationship between caller and callee may represent an actual, direct invocation if both methods are crosscut by the defined pointcut. Otherwise, if intermediate method invocations are not crosscut, the recorded "caller" may actually represent some "ancestor" method further up the actual call stack. It is assumed that this potential "gap" between caller and callee is understood by and acceptable to the definer of the pointcut.
joinPoint
- Join point which represents the method being traced.java.lang.Throwable
- if an exception is thrown by the method being traced.