1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.analysis
.timing
.core
.callgraph
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.Collections
;
15 import java
.util
.List
;
16 import java
.util
.stream
.Collectors
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.ListenerList
;
20 import org
.eclipse
.jdt
.annotation
.NonNull
;
21 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.IAnalysisProgressListener
;
23 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.ISegmentStoreProvider
;
24 import org
.eclipse
.tracecompass
.common
.core
.StreamUtils
;
25 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.core
.Activator
;
26 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.core
.store
.ArrayListStore
;
27 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
28 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegmentStore
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.callstack
.CallStackAnalysis
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
42 import com
.google
.common
.annotations
.VisibleForTesting
;
43 import com
.google
.common
.collect
.ImmutableList
;
46 * Call stack analysis used to create a segment for each call function from an
47 * entry/exit event. It builds a segment tree from the state system. An example
48 * taken from the Fibonacci trace's callStack shows the structure of the segment
49 * tree given by this analysis:
61 * @author Sonia Farrah
63 public abstract class CallGraphAnalysis
extends TmfAbstractAnalysisModule
implements ISegmentStoreProvider
{
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
72 private final ISegmentStore
<@NonNull ISegment
> fStore
= new ArrayListStore
<>();
77 private final ListenerList fListeners
= new ListenerList(ListenerList
.IDENTITY
);
80 * The Trace's root functions list
82 private final List
<ICalledFunction
> fRootFunctions
= new ArrayList
<>();
85 * The sub attributes of a certain thread
87 private List
<Integer
> fCurrentQuarks
= Collections
.emptyList();
89 * The List of thread nodes. Each thread has a virtual node having the root
90 * function as children
92 private List
<AggregatedCalledFunction
> fThreadNodes
= new ArrayList
<>();
97 public CallGraphAnalysis() {
102 public @NonNull String
getHelpText() {
103 String msg
= Messages
.CallGraphAnalysis_Description
;
104 return (msg
!= null) ? msg
: super.getHelpText();
108 public @NonNull String
getHelpText(@NonNull ITmfTrace trace
) {
109 return getHelpText();
113 public boolean canExecute(ITmfTrace trace
) {
115 * FIXME: change to !Iterables.isEmpty(getDependentAnalyses()) when
116 * analysis dependencies work better
122 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
123 return TmfTraceManager
.getTraceSet(getTrace()).stream()
124 .flatMap(trace
-> StreamUtils
.getStream(TmfTraceUtils
.getAnalysisModulesOfClass(trace
, CallStackAnalysis
.class)))
125 .distinct().collect(Collectors
.toList());
129 protected boolean executeAnalysis(@Nullable IProgressMonitor monitor
) {
130 ITmfTrace trace
= getTrace();
131 if (monitor
== null || trace
== null) {
134 Iterable
<IAnalysisModule
> dependentAnalyses
= getDependentAnalyses();
135 for (IAnalysisModule module
: dependentAnalyses
) {
136 if (!(module
instanceof CallStackAnalysis
)) {
141 // TODO:Look at updates while the state system's being built
142 dependentAnalyses
.forEach((t
) -> t
.waitForCompletion(monitor
));
143 for (IAnalysisModule module
: dependentAnalyses
) {
144 CallStackAnalysis callstackModule
= (CallStackAnalysis
) module
;
145 String
[] threadsPattern
= callstackModule
.getThreadsPattern();
146 String
[] processesPattern
= callstackModule
.getProcessesPattern();
147 String
[] callStackPath
= callstackModule
.getCallStackPath();
148 ITmfStateSystem ss
= callstackModule
.getStateSystem();
149 if (!iterateOverStateSystem(ss
, threadsPattern
, processesPattern
, callStackPath
, monitor
)) {
160 * Iterate over the process of the state system,then iterate over the
161 * different threads of each process.
165 * @param threadsPattern
166 * The threads pattern
167 * @param processesPattern
168 * The processes pattern
169 * @param callStackPath
170 * The call stack path
176 protected boolean iterateOverStateSystem(@Nullable ITmfStateSystem ss
, String
[] threadsPattern
, String
[] processesPattern
, String
[] callStackPath
, IProgressMonitor monitor
) {
180 List
<Integer
> processQuarks
= ss
.getQuarks(processesPattern
);
181 for (int processQuark
: processQuarks
) {
182 for (int threadQuark
: ss
.getQuarks(processQuark
, threadsPattern
)) {
183 if (!iterateOverQuark(ss
, threadQuark
, callStackPath
, monitor
)) {
193 * Iterate over functions with the same quark,search for their callees then
194 * add them to the segment store
200 * @param subAttributePath
201 * sub-Attributes path
206 private boolean iterateOverQuark(ITmfStateSystem stateSystem
, int quark
, String
[] subAttributePath
, IProgressMonitor monitor
) {
207 String threadName
= stateSystem
.getAttributeName(quark
);
208 AggregatedCalledFunction init
= new AggregatedCalledFunction(threadName
, 0L, 0, 0, null);
210 long curTime
= stateSystem
.getStartTime();
211 long limit
= stateSystem
.getCurrentEndTime();
212 while (curTime
< limit
) {
213 if (monitor
.isCanceled()) {
216 int callStackQuark
= stateSystem
.getQuarkRelative(quark
, subAttributePath
);
217 fCurrentQuarks
= stateSystem
.getSubAttributes(callStackQuark
, false);
218 if (fCurrentQuarks
.isEmpty()) {
222 int quarkParent
= fCurrentQuarks
.get(depth
);
223 ITmfStateInterval interval
= stateSystem
.querySingleState(curTime
, quarkParent
);
224 ITmfStateValue stateValue
= interval
.getStateValue();
226 if (!stateValue
.isNull()) {
227 long intervalStart
= interval
.getStartTime();
228 long intervalEnd
= interval
.getEndTime();
229 // Create the segment for the first call event.
230 AbstractCalledFunction segment
= CalledFunctionFactory
.create(intervalStart
, intervalEnd
+ 1, depth
, stateValue
, null);
231 fRootFunctions
.add(segment
);
232 AggregatedCalledFunction firstNode
= new AggregatedCalledFunction(stateValue
, intervalEnd
- intervalStart
+ 1, segment
.getDepth(), fCurrentQuarks
.size(), null);
233 if (!findChildren(segment
, depth
, stateSystem
, fCurrentQuarks
.size() + fCurrentQuarks
.get(depth
), firstNode
, monitor
)) {
236 init
.addChild(firstNode
);
239 curTime
= interval
.getEndTime() + 1;
242 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
243 Activator
.getInstance().logError(Messages
.QueringStateSystemError
, e
);
246 fThreadNodes
.add(init
);
251 * Find the functions called by a parent function in a call stack then add
252 * segments for each child, updating the self times of each node
256 * The segment of the stack call event(the parent) callStackQuark
258 * The depth of the parent function
260 * The quark of the segment parent ss The actual state system
262 * The last quark in the state system
263 * @param AggregatedCalledFunction
264 * A node in the aggregation tree
266 * The progress monitor The progress monitor TODO: if stack size
267 * is an issue, convert to a stack instead of recursive function
269 private boolean findChildren(AbstractCalledFunction node
, int depth
, ITmfStateSystem ss
, int maxQuark
, AggregatedCalledFunction aggregatedCalledFunction
, IProgressMonitor monitor
) {
271 long curTime
= node
.getStart();
272 long limit
= node
.getEnd();
273 ITmfStateInterval interval
= null;
274 while (curTime
< limit
) {
275 if (monitor
.isCanceled()) {
279 if (depth
+ 1 < fCurrentQuarks
.size()) {
280 interval
= ss
.querySingleState(curTime
, fCurrentQuarks
.get(depth
+ 1));
284 } catch (StateSystemDisposedException e
) {
285 Activator
.getInstance().logError(Messages
.QueringStateSystemError
, e
);
288 ITmfStateValue stateValue
= interval
.getStateValue();
289 if (!stateValue
.isNull()) {
290 long intervalStart
= interval
.getStartTime();
291 long intervalEnd
= interval
.getEndTime();
292 if (intervalStart
< node
.getStart() || intervalEnd
> limit
) {
295 AbstractCalledFunction segment
= CalledFunctionFactory
.create(intervalStart
, intervalEnd
+ 1, node
.getDepth() + 1, stateValue
, node
);
296 AggregatedCalledFunction childNode
= new AggregatedCalledFunction(stateValue
, segment
.getLength(), segment
.getDepth(), aggregatedCalledFunction
.getMaxDepth(), aggregatedCalledFunction
);
297 // Search for the children with the next quark.
298 findChildren(segment
, depth
+ 1, ss
, maxQuark
, childNode
, monitor
);
299 aggregatedCalledFunction
.addChild(childNode
);
300 node
.addChild(segment
);
302 curTime
= interval
.getEndTime() + 1;
308 public void addListener(@NonNull IAnalysisProgressListener listener
) {
309 fListeners
.add(listener
);
313 public void removeListener(@NonNull IAnalysisProgressListener listener
) {
314 fListeners
.remove(listener
);
318 protected void canceling() {
323 public @Nullable ISegmentStore
<@NonNull ISegment
> getSegmentStore() {
333 protected void sendUpdate(final ISegmentStore
<@NonNull ISegment
> store
) {
334 getListeners().forEach(listener
-> listener
.onComplete(this, store
));
340 * @return The listeners
342 protected Iterable
<IAnalysisProgressListener
> getListeners() {
343 return Arrays
.stream(fListeners
.getListeners())
344 .filter(listener
-> listener
instanceof IAnalysisProgressListener
)
345 .map(listener
-> (IAnalysisProgressListener
) listener
)
346 .collect(Collectors
.toList());
350 * The functions of the first level
352 * @return Functions of the first level
354 public List
<ICalledFunction
> getRootFunctions() {
355 return ImmutableList
.copyOf(fRootFunctions
);
359 * List of thread nodes. Each thread has a virtual node having the root
360 * functions called as children.
362 * @return The thread nodes
364 public List
<AggregatedCalledFunction
> getThreadNodes() {
365 return ImmutableList
.copyOf(fThreadNodes
);