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
.LazyArrayListStore
;
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
.statesystem
.core
.statevalue
.ITmfStateValue
.Type
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.callstack
.CallStackAnalysis
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
43 import com
.google
.common
.annotations
.VisibleForTesting
;
44 import com
.google
.common
.collect
.ImmutableList
;
47 * Call stack analysis used to create a segment for each call function from an
48 * entry/exit event. It builds a segment tree from the state system. An example
49 * taken from the Fibonacci trace's callStack shows the structure of the segment
50 * tree given by this analysis:
62 * @author Sonia Farrah
64 public abstract class CallGraphAnalysis
extends TmfAbstractAnalysisModule
implements ISegmentStoreProvider
{
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
73 private final ISegmentStore
<@NonNull ISegment
> fStore
= new LazyArrayListStore
<>();
78 private final ListenerList fListeners
= new ListenerList(ListenerList
.IDENTITY
);
81 * The Trace's root functions list
83 private final List
<ICalledFunction
> fRootFunctions
= new ArrayList
<>();
86 * The sub attributes of a certain thread
88 private List
<Integer
> fCurrentQuarks
= Collections
.emptyList();
90 * The List of thread nodes. Each thread has a virtual node having the root
91 * function as children
93 private List
<ThreadNode
> fThreadNodes
= new ArrayList
<>();
98 public CallGraphAnalysis() {
103 public @NonNull String
getHelpText() {
104 String msg
= Messages
.CallGraphAnalysis_Description
;
105 return (msg
!= null) ? msg
: super.getHelpText();
109 public @NonNull String
getHelpText(@NonNull ITmfTrace trace
) {
110 return getHelpText();
114 public boolean canExecute(ITmfTrace trace
) {
116 * FIXME: change to !Iterables.isEmpty(getDependentAnalyses()) when
117 * analysis dependencies work better
123 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
124 return TmfTraceManager
.getTraceSet(getTrace()).stream()
125 .flatMap(trace
-> StreamUtils
.getStream(TmfTraceUtils
.getAnalysisModulesOfClass(trace
, CallStackAnalysis
.class)))
126 .distinct().collect(Collectors
.toList());
130 protected boolean executeAnalysis(@Nullable IProgressMonitor monitor
) {
131 ITmfTrace trace
= getTrace();
132 if (monitor
== null || trace
== null) {
135 Iterable
<IAnalysisModule
> dependentAnalyses
= getDependentAnalyses();
136 for (IAnalysisModule module
: dependentAnalyses
) {
137 if (!(module
instanceof CallStackAnalysis
)) {
142 // TODO:Look at updates while the state system's being built
143 dependentAnalyses
.forEach((t
) -> t
.waitForCompletion(monitor
));
144 for (IAnalysisModule module
: dependentAnalyses
) {
145 CallStackAnalysis callstackModule
= (CallStackAnalysis
) module
;
146 String
[] threadsPattern
= callstackModule
.getThreadsPattern();
147 String
[] processesPattern
= callstackModule
.getProcessesPattern();
148 String
[] callStackPath
= callstackModule
.getCallStackPath();
149 ITmfStateSystem ss
= callstackModule
.getStateSystem();
150 if (!iterateOverStateSystem(ss
, threadsPattern
, processesPattern
, callStackPath
, monitor
)) {
161 * Iterate over the process of the state system,then iterate over the
162 * different threads of each process.
166 * @param threadsPattern
167 * The threads pattern
168 * @param processesPattern
169 * The processes pattern
170 * @param callStackPath
171 * The call stack path
177 protected boolean iterateOverStateSystem(@Nullable ITmfStateSystem ss
, String
[] threadsPattern
, String
[] processesPattern
, String
[] callStackPath
, IProgressMonitor monitor
) {
181 List
<Integer
> processQuarks
= ss
.getQuarks(processesPattern
);
182 for (int processQuark
: processQuarks
) {
183 for (int threadQuark
: ss
.getQuarks(processQuark
, threadsPattern
)) {
184 if (!iterateOverQuark(ss
, threadQuark
, callStackPath
, monitor
)) {
194 * Iterate over functions with the same quark,search for their callees then
195 * add them to the segment store
201 * @param subAttributePath
202 * sub-Attributes path
207 private boolean iterateOverQuark(ITmfStateSystem stateSystem
, int quark
, String
[] subAttributePath
, IProgressMonitor monitor
) {
208 String threadName
= stateSystem
.getAttributeName(quark
);
210 ITmfStateInterval interval
= null;
212 interval
= stateSystem
.querySingleState(stateSystem
.getStartTime(), quark
);
213 ITmfStateValue threadStateValue
= interval
.getStateValue();
214 if (threadStateValue
.getType() == Type
.LONG
|| threadStateValue
.getType() == Type
.INTEGER
) {
215 threadId
= threadStateValue
.unboxLong();
218 threadId
= Long
.parseLong(threadName
);
219 } catch (NumberFormatException e
) {
220 /* use default threadId */
223 } catch (StateSystemDisposedException error
) {
224 Activator
.getInstance().logError(Messages
.QueringStateSystemError
, error
);
227 long curTime
= stateSystem
.getStartTime();
228 long limit
= stateSystem
.getCurrentEndTime();
229 AbstractCalledFunction initSegment
= CalledFunctionFactory
.create(0, 0, 0, threadName
, null);
230 ThreadNode init
= new ThreadNode(initSegment
, 0, threadId
);
231 while (curTime
< limit
) {
232 if (monitor
.isCanceled()) {
235 int callStackQuark
= stateSystem
.getQuarkRelative(quark
, subAttributePath
);
236 fCurrentQuarks
= stateSystem
.getSubAttributes(callStackQuark
, false);
237 if (fCurrentQuarks
.isEmpty()) {
241 int quarkParent
= fCurrentQuarks
.get(depth
);
242 interval
= stateSystem
.querySingleState(curTime
, quarkParent
);
243 ITmfStateValue stateValue
= interval
.getStateValue();
245 if (!stateValue
.isNull()) {
246 long intervalStart
= interval
.getStartTime();
247 long intervalEnd
= interval
.getEndTime();
248 // Create the segment for the first call event.
249 AbstractCalledFunction segment
= CalledFunctionFactory
.create(intervalStart
, intervalEnd
+ 1, depth
, stateValue
, null);
250 fRootFunctions
.add(segment
);
251 AggregatedCalledFunction firstNode
= new AggregatedCalledFunction(segment
, fCurrentQuarks
.size());
252 if (!findChildren(segment
, depth
, stateSystem
, fCurrentQuarks
.size() + fCurrentQuarks
.get(depth
), firstNode
, monitor
)) {
255 init
.addChild(firstNode
);
258 curTime
= interval
.getEndTime() + 1;
260 fThreadNodes
.add(init
);
261 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
262 Activator
.getInstance().logError(Messages
.QueringStateSystemError
, e
);
269 * Find the functions called by a parent function in a call stack then add
270 * segments for each child, updating the self times of each node
274 * The segment of the stack call event(the parent) callStackQuark
276 * The depth of the parent function
278 * The quark of the segment parent ss The actual state system
280 * The last quark in the state system
281 * @param AggregatedCalledFunction
282 * A node in the aggregation tree
284 * The progress monitor The progress monitor TODO: if stack size
285 * is an issue, convert to a stack instead of recursive function
287 private boolean findChildren(AbstractCalledFunction node
, int depth
, ITmfStateSystem ss
, int maxQuark
, AggregatedCalledFunction aggregatedCalledFunction
, IProgressMonitor monitor
) {
289 long curTime
= node
.getStart();
290 long limit
= node
.getEnd();
291 ITmfStateInterval interval
= null;
292 while (curTime
< limit
) {
293 if (monitor
.isCanceled()) {
297 if (depth
+ 1 < fCurrentQuarks
.size()) {
298 interval
= ss
.querySingleState(curTime
, fCurrentQuarks
.get(depth
+ 1));
302 } catch (StateSystemDisposedException e
) {
303 Activator
.getInstance().logError(Messages
.QueringStateSystemError
, e
);
306 ITmfStateValue stateValue
= interval
.getStateValue();
307 if (!stateValue
.isNull()) {
308 long intervalStart
= interval
.getStartTime();
309 long intervalEnd
= interval
.getEndTime();
310 if (intervalStart
< node
.getStart() || intervalEnd
> limit
) {
313 AbstractCalledFunction segment
= CalledFunctionFactory
.create(intervalStart
, intervalEnd
+ 1, node
.getDepth() + 1, stateValue
, node
);
314 AggregatedCalledFunction childNode
= new AggregatedCalledFunction(segment
, aggregatedCalledFunction
);
315 // Search for the children with the next quark.
316 findChildren(segment
, depth
+ 1, ss
, maxQuark
, childNode
, monitor
);
317 aggregatedCalledFunction
.addChild(childNode
);
318 node
.addChild(segment
);
320 curTime
= interval
.getEndTime() + 1;
326 public void addListener(@NonNull IAnalysisProgressListener listener
) {
327 fListeners
.add(listener
);
331 public void removeListener(@NonNull IAnalysisProgressListener listener
) {
332 fListeners
.remove(listener
);
336 protected void canceling() {
341 public @Nullable ISegmentStore
<@NonNull ISegment
> getSegmentStore() {
351 protected void sendUpdate(final ISegmentStore
<@NonNull ISegment
> store
) {
352 getListeners().forEach(listener
-> listener
.onComplete(this, store
));
358 * @return The listeners
360 protected Iterable
<IAnalysisProgressListener
> getListeners() {
361 return Arrays
.stream(fListeners
.getListeners())
362 .filter(listener
-> listener
instanceof IAnalysisProgressListener
)
363 .map(listener
-> (IAnalysisProgressListener
) listener
)
364 .collect(Collectors
.toList());
368 * The functions of the first level
370 * @return Functions of the first level
372 public List
<ICalledFunction
> getRootFunctions() {
373 return ImmutableList
.copyOf(fRootFunctions
);
377 * List of thread nodes. Each thread has a virtual node having the root
378 * functions called as children.
380 * @return The thread nodes
382 public List
<ThreadNode
> getThreadNodes() {
383 return ImmutableList
.copyOf(fThreadNodes
);