-
Notifications
You must be signed in to change notification settings - Fork 11
/
ProfilingCallGraphAnalysisModule.java
265 lines (237 loc) · 9.85 KB
/
ProfilingCallGraphAnalysisModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*******************************************************************************
* Copyright (c) 2017 École Polytechnique de Montréal
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.incubator.callstack.core.sampled.callgraph;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.incubator.analysis.core.concepts.AggregatedCallSite;
import org.eclipse.tracecompass.incubator.analysis.core.concepts.ICallStackSymbol;
import org.eclipse.tracecompass.incubator.callstack.core.base.ICallStackElement;
import org.eclipse.tracecompass.incubator.callstack.core.callgraph.CallGraph;
import org.eclipse.tracecompass.incubator.callstack.core.callgraph.ICallGraphProvider;
import org.eclipse.tracecompass.incubator.callstack.core.flamechart.IEventCallStackProvider;
import org.eclipse.tracecompass.incubator.callstack.core.symbol.CallStackSymbolFactory;
import org.eclipse.tracecompass.incubator.internal.callstack.core.Activator;
import org.eclipse.tracecompass.incubator.internal.callstack.core.instrumented.callgraph.Messages;
import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
import org.eclipse.tracecompass.tmf.core.symbols.ISymbolProvider;
import org.eclipse.tracecompass.tmf.core.symbols.SymbolProviderManager;
import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
import org.eclipse.tracecompass.tmf.core.util.Pair;
/**
* The callgraph analysis module
*
* @author Geneviève Bastien
*/
@Deprecated(since="0.10.0", forRemoval=true)
public abstract class ProfilingCallGraphAnalysisModule extends TmfAbstractAnalysisModule implements ICallGraphProvider, IEventCallStackProvider {
private static final MetricType WEIGHT_METRIC = new MetricType(Objects.requireNonNull(Messages.CallGraphStats_NbCalls), DataType.NUMBER, null);
private @Nullable ITmfEventRequest fRequest;
private final Set<ICallStackElement> fRootElements = new HashSet<>();
private @Nullable CallGraph fFullRangeCallGraph;
private @Nullable Collection<ISymbolProvider> fSymbolProviders = null;
/**
* Get the root elements from this call graph hierarchy
*
* @return The root elements
*/
protected Collection<ICallStackElement> getRootElements() {
return fRootElements;
}
/**
* Add a root element to call graph
*
* @param element
* The root element to add
*/
public void addRootElement(ICallStackElement element) {
fRootElements.add(element);
}
@Override
public CallGraph getCallGraph(ITmfTimestamp start, ITmfTimestamp end) {
CallGraph cg = executeForRange(new TmfTimeRange(start, end));
if (cg == null) {
return CallGraph.EMPTY_GRAPH;
}
return cg;
}
@Override
public CallGraph getCallGraph() {
CallGraph cg = fFullRangeCallGraph;
if (cg == null) {
return CallGraph.EMPTY_GRAPH;
}
return cg;
}
@Override
public @NonNull MetricType getWeightType() {
return WEIGHT_METRIC;
}
@Override
public String getTitle() {
return "Call Graph"; //$NON-NLS-1$
}
@Override
public @NonNull String toDisplayString(@NonNull AggregatedCallSite callsite) {
Collection<ISymbolProvider> symbolProviders = fSymbolProviders;
if (symbolProviders == null) {
ITmfTrace trace = getTrace();
if (trace == null) {
return String.valueOf(callsite.getObject());
}
symbolProviders = SymbolProviderManager.getInstance().getSymbolProviders(trace);
fSymbolProviders = symbolProviders;
}
return callsite.getObject().resolve(symbolProviders);
}
@Override
public AggregatedCallSite createCallSite(Object symbol) {
return new AggregatedStackTraces((ICallStackSymbol) symbol);
}
/**
* Get a callsite from a stack trace. The array should be non-zero length and
* the symbol at position 0 is the top of the stack, ie the last symbol called.
*
* @param dstGroup
* The element to which to add this stack trace
* @param stackTrace
* The stack trace to add to the group
* @param ts
* The timestamp at which this stack trace is added
* @return The callsite
* @throws ArrayIndexOutOfBoundsException
* Exception thrown if the stackTrace is empty
*/
public AggregatedCallSite getCallSite(ICallStackElement dstGroup, long[] stackTrace, long ts) {
if (stackTrace.length == 0) {
throw new ArrayIndexOutOfBoundsException("Get callsite, the received array should not be null"); //$NON-NLS-1$
}
// Create the callsite for this stack trace
AggregatedCallSite prevCallsite = createCallSite(CallStackSymbolFactory.createSymbol(stackTrace[stackTrace.length - 1], dstGroup, ts));
for (int i = stackTrace.length - 2; i >= 0; i--) {
AggregatedCallSite callsite = createCallSite(CallStackSymbolFactory.createSymbol(stackTrace[i], dstGroup, ts));
callsite.addChild(prevCallsite);
prevCallsite = callsite;
}
return prevCallsite;
}
/**
* Method to implement to get the call stack from an event. It returns a
* stack trace in the form or a Pair of {@link ICallStackElement} and
* {@link AggregatedCallSite}.
*
* @param event
* The trace event to process
* @return A pair of callstack element and aggregated callsite from this
* event
*/
protected abstract @Nullable Pair<ICallStackElement, AggregatedCallSite> getProfiledStackTrace(ITmfEvent event);
@Override
protected boolean executeAnalysis(@NonNull IProgressMonitor monitor) throws TmfAnalysisException {
CallGraph callgraph = executeForRange(TmfTimeRange.ETERNITY);
if (callgraph == null) {
return false;
}
fFullRangeCallGraph = callgraph;
return true;
}
private @Nullable CallGraph executeForRange(TmfTimeRange range) {
ITmfTrace trace = getTrace();
if (trace == null) {
throw new NullPointerException("Trace has not been set, yet the analysis is being run!"); //$NON-NLS-1$
}
/* Cancel any previous request */
ITmfEventRequest request = fRequest;
if ((request != null) && (!request.isCompleted())) {
request.cancel();
}
try {
CallGraph callGraph = new CallGraph();
request = new ProfilingEventRequest(trace, callGraph, range);
fRequest = request;
trace.sendRequest(request);
request.waitForCompletion();
if (!request.isCompleted()) {
return null;
}
return callGraph;
} catch (InterruptedException e) {
Activator.getInstance().logError("Request interrupted", e); //$NON-NLS-1$
}
return null;
}
@Override
protected void canceling() {
ITmfEventRequest req = fRequest;
if ((req != null) && (!req.isCompleted())) {
req.cancel();
}
}
private class ProfilingEventRequest extends TmfEventRequest {
private final ITmfTrace fTrace;
private final CallGraph fCallGraph;
/**
* Constructor
*
* @param trace
* The trace
* @param callgraph
* The callgraph to fill
* @param range
* The time range of this request
*/
public ProfilingEventRequest(ITmfTrace trace, CallGraph callgraph, TmfTimeRange range) {
super(TmfEvent.class,
range,
0,
ITmfEventRequest.ALL_DATA,
ITmfEventRequest.ExecutionType.BACKGROUND);
fTrace = trace;
fCallGraph = callgraph;
}
@Override
public void handleData(final ITmfEvent event) {
super.handleData(event);
if (event.getTrace() == fTrace) {
processEvent(event);
} else if (fTrace instanceof TmfExperiment) {
/*
* If the request is for an experiment, check if the event is
* from one of the child trace
*/
for (ITmfTrace childTrace : ((TmfExperiment) fTrace).getTraces()) {
if (childTrace == event.getTrace()) {
processEvent(event);
}
}
}
}
private void processEvent(ITmfEvent event) {
Pair<ICallStackElement, AggregatedCallSite> perfCallSite = getProfiledStackTrace(event);
if (perfCallSite == null) {
return;
}
fCallGraph.addAggregatedCallSite(perfCallSite.getFirst(), perfCallSite.getSecond());
}
}
}