/
CallGraphAnalysis.java
409 lines (369 loc) · 17.4 KB
/
CallGraphAnalysis.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*******************************************************************************
* Copyright (c) 2016 Ericsson
*
* 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.internal.analysis.callstack.core.callgraph;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tracecompass.internal.analysis.callstack.core.Activator;
import org.eclipse.tracecompass.internal.analysis.callstack.core.CallStack;
import org.eclipse.tracecompass.internal.analysis.callstack.core.CallStackSeries;
import org.eclipse.tracecompass.internal.analysis.callstack.core.CallStackSymbolFactory;
import org.eclipse.tracecompass.internal.analysis.callstack.core.base.FlameWithKernelPalette;
import org.eclipse.tracecompass.internal.analysis.callstack.core.base.ICallStackElement;
import org.eclipse.tracecompass.internal.analysis.callstack.core.base.ICallStackSymbol;
import org.eclipse.tracecompass.internal.analysis.callstack.core.base.IDataPalette;
import org.eclipse.tracecompass.internal.analysis.callstack.core.instrumented.IFlameChartProvider;
import org.eclipse.tracecompass.internal.analysis.callstack.core.instrumented.InstrumentedCallStackElement;
import org.eclipse.tracecompass.internal.analysis.callstack.core.model.IHostModel;
import org.eclipse.tracecompass.internal.analysis.callstack.core.model.ModelManager;
import org.eclipse.tracecompass.internal.analysis.callstack.core.model.ProcessStatusInterval;
import org.eclipse.tracecompass.internal.analysis.callstack.core.tree.IWeightedTreeGroupDescriptor;
import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
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 com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
/**
* Call stack analysis used to create a segment for each call function from an
* entry/exit event. It builds a segment tree from the state system. An example
* taken from the Fibonacci trace's callStack shows the structure of the segment
* tree given by this analysis:
*
* <pre>
* (Caller) main
* ↓↑
* (Callee) Fibonacci
* ↓↑ ↓↑
* Fibonacci Fibonacci
* ↓↑ ↓↑
* ... ...
* </pre>
*
* @author Sonia Farrah
*/
public class CallGraphAnalysis extends TmfAbstractAnalysisModule implements ICallGraphProvider {
/**
* Public ID for this analysis
*/
public static final String ID = "org.eclipse.tracecompass.analysis.callstack.callgraph"; //$NON-NLS-1$
/**
* Index of some metrics, to get its statistics. package-private so
* aggregated called function can access it
*/
static final int SELF_TIME_METRIC_INDEX = 0;
static final int CPU_TIME_METRIC_INDEX = 1;
private static final String SELF_TIME_TITLE = Objects.requireNonNull(Messages.CallGraphStats_SelfTime);
private static final String CPU_TIME_TITLE = Objects.requireNonNull(Messages.CallGraphStats_CpuTime);
private static final String NB_CALLS_TITLE = Objects.requireNonNull(Messages.CallGraphStats_NbCalls);
private static final MetricType DURATION_METRIC = new MetricType(Objects.requireNonNull(Messages.CallGraphStats_Duration), DataType.NANOSECONDS, null, true);
private static final List<MetricType> METRICS = ImmutableList.of(
new MetricType(SELF_TIME_TITLE, DataType.NANOSECONDS, null, true),
new MetricType(CPU_TIME_TITLE, DataType.NANOSECONDS, null, true),
new MetricType(NB_CALLS_TITLE, DataType.NUMBER, null, false));
// ------------------------------------------------------------------------
// Attributes
// ------------------------------------------------------------------------
private final IFlameChartProvider fCsProvider;
private final CallGraph fCallGraph = new CallGraph();
private @Nullable Collection<ISymbolProvider> fSymbolProviders = null;
private boolean fHasKernelStatuses = false;
// Keep a very small cache of selection callgraphs, to avoid having to
// compute again
private final LoadingCache<TmfTimeRange, CallGraph> fRangeCallgraphs = Objects.requireNonNull(CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<TmfTimeRange, CallGraph>() {
@Override
public CallGraph load(TmfTimeRange range) {
CallGraph cg = new CallGraph();
executeForRange(cg, range, new NullProgressMonitor());
return cg;
}
}));
/**
* Constructor
*
* @param csProvider
* The call stack provider to use with this analysis
*/
@SuppressWarnings("null")
public CallGraphAnalysis(IFlameChartProvider csProvider) {
super();
fCsProvider = csProvider;
setName(NLS.bind(Messages.CallGraphAnalysis_NamePrefix, csProvider.getName()));
}
@Override
public @NonNull String getHelpText() {
String msg = Messages.CallGraphAnalysis_Description;
return (msg != null) ? msg : super.getHelpText();
}
@SuppressWarnings("null")
@Override
public void setName(String name) {
super.setName(NLS.bind(Messages.CallGraphAnalysis_NamePrefix, name));
}
@Override
public @NonNull String getHelpText(ITmfTrace trace) {
return getHelpText();
}
@Override
public boolean canExecute(ITmfTrace trace) {
return true;
}
@Override
protected Iterable<IAnalysisModule> getDependentAnalyses() {
return Collections.singleton(fCsProvider);
}
@Override
protected boolean executeAnalysis(@Nullable IProgressMonitor monitor) {
return executeForRange(fCallGraph, TmfTimeRange.ETERNITY, monitor);
}
private boolean executeForRange(CallGraph callgraph, TmfTimeRange range, @Nullable IProgressMonitor monitor) {
ITmfTrace trace = getTrace();
if (monitor == null || trace == null) {
return false;
}
Iterable<IAnalysisModule> dependentAnalyses = getDependentAnalyses();
for (IAnalysisModule module : dependentAnalyses) {
if (!(module instanceof IFlameChartProvider)) {
return false;
}
module.schedule();
}
// TODO: Look at updates while the state system's being built
dependentAnalyses.forEach(t -> t.waitForCompletion(monitor));
for (IAnalysisModule module : dependentAnalyses) {
IFlameChartProvider callstackModule = (IFlameChartProvider) module;
IHostModel model = ModelManager.getModelFor(callstackModule.getHostId());
CallStackSeries callstack = callstackModule.getCallStackSeries();
if (callstack != null) {
long time0 = range.getStartTime().toNanos();
long time1 = range.getEndTime().toNanos();
long start = Math.min(time0, time1);
long end = Math.max(time0, time1);
if (!iterateOverCallstackSerie(callstack, model, callgraph, start, end, monitor)) {
return false;
}
}
}
monitor.worked(1);
monitor.done();
return true;
}
/**
* Iterate over a callstack series. It will do a depth-first search to
* create the callgraph
*
* @param callstackSerie
* The series to iterate over
* @param model
* The model of the host on which this callstack was running
* @param callgraph
* The callgraph to fill
* @param start
* the start time of the request
* @param end
* The end time of the request
* @param monitor
* A progress monitor
* @return Whether the series was successfully iterated over
*/
@VisibleForTesting
protected boolean iterateOverCallstackSerie(CallStackSeries callstackSerie, IHostModel model, CallGraph callgraph, long start, long end, IProgressMonitor monitor) {
// The root elements are the same as the one from the callstack series
Collection<ICallStackElement> rootElements = callstackSerie.getRootElements();
for (ICallStackElement element : rootElements) {
if (monitor.isCanceled()) {
return false;
}
iterateOverElement(element, model, callgraph, start, end, monitor);
}
return true;
}
private void iterateOverElement(ICallStackElement element, IHostModel model, CallGraph callgraph, long start, long end, IProgressMonitor monitor) {
// Iterator over the children of the element until we reach the leaves
if (element.isLeaf()) {
iterateOverLeafElement(element, model, callgraph, start, end, monitor);
return;
}
for (ICallStackElement child : element.getChildrenElements()) {
iterateOverElement(child, model, callgraph, start, end, monitor);
}
}
private void iterateOverLeafElement(ICallStackElement element, IHostModel model, CallGraph callgraph, long start, long end, IProgressMonitor monitor) {
if (!(element instanceof InstrumentedCallStackElement)) {
throw new IllegalStateException("Call Graph Analysis: The element does not have the right type"); //$NON-NLS-1$
}
InstrumentedCallStackElement insElement = (InstrumentedCallStackElement) element;
CallStack callStack = insElement.getCallStack();
// If there is no children for this callstack, just return
if (callStack.getMaxDepth() == 0) {
return;
}
fHasKernelStatuses |= callStack.hasKernelStatuses();
// Start with the first function
AbstractCalledFunction nextFunction = (AbstractCalledFunction) callStack.getNextFunction(callStack.getStartTime(), 1, null, model, start, end);
while (nextFunction != null) {
AggregatedCalledFunction aggregatedChild = createCallSite(CallStackSymbolFactory.createSymbol(nextFunction.getSymbol(), element, nextFunction.getStart()));
iterateOverCallstack(element, callStack, nextFunction, 2, aggregatedChild, model, start, end, monitor);
aggregatedChild.addFunctionCall(nextFunction);
// Add the kernel statuses if available
Iterable<ProcessStatusInterval> kernelStatuses = callStack.getKernelStatuses(nextFunction, Collections.emptyList());
for (ProcessStatusInterval status : kernelStatuses) {
aggregatedChild.addKernelStatus(status);
}
callgraph.addAggregatedCallSite(element, aggregatedChild);
nextFunction = (AbstractCalledFunction) callStack.getNextFunction(nextFunction.getEnd(), 1, null, model, start, end);
}
}
private void iterateOverCallstack(ICallStackElement element, CallStack callstack, ICalledFunction function, int nextLevel, AggregatedCalledFunction aggregatedCall, IHostModel model, long start, long end, IProgressMonitor monitor) {
if (nextLevel > callstack.getMaxDepth()) {
return;
}
int threadId = function.getThreadId();
long lastSampleEnd = start;
AbstractCalledFunction nextFunction = (AbstractCalledFunction) callstack.getNextFunction(function.getStart(), nextLevel, function, model, Math.max(function.getStart(), start), Math.min(function.getEnd(), end));
while (nextFunction != null) {
// Add sampling data of the time between next function and beginning
// of next level
if (threadId > 0) {
Collection<AggregatedCallSite> samplingData = model.getSamplingData(threadId, lastSampleEnd, nextFunction.getStart());
samplingData.forEach(aggregatedCall::addChild);
lastSampleEnd = nextFunction.getEnd();
}
AggregatedCalledFunction aggregatedChild = createCallSite(CallStackSymbolFactory.createSymbol(nextFunction.getSymbol(), element, nextFunction.getStart()));
iterateOverCallstack(element, callstack, nextFunction, nextLevel + 1, aggregatedChild, model, start, end, monitor);
aggregatedCall.addChild(nextFunction, aggregatedChild);
nextFunction = (AbstractCalledFunction) callstack.getNextFunction(nextFunction.getEnd(), nextLevel, function, model, Math.max(function.getStart(), start), Math.min(function.getEnd(), end));
}
// Get the sampling to the end of the function
if (threadId > 0) {
Collection<AggregatedCallSite> samplingData = model.getSamplingData(threadId, lastSampleEnd, function.getEnd() - lastSampleEnd);
samplingData.forEach(aggregatedCall::addChild);
}
}
/**
* Get the callstack series of the providers of this analysis
*
* @return The collection of callstack series
*/
public @Nullable CallStackSeries getSeries() {
CallStackSeries series = null;
for (IAnalysisModule dependent : getDependentAnalyses()) {
if (!(dependent instanceof IFlameChartProvider)) {
continue;
}
IFlameChartProvider csProvider = (IFlameChartProvider) dependent;
series = csProvider.getCallStackSeries();
}
return series;
}
@Override
protected void canceling() {
// Do nothing
}
@Override
public CallGraph getCallGraph(ITmfTimestamp start, ITmfTimestamp end) {
return fRangeCallgraphs.getUnchecked(new TmfTimeRange(start, end));
}
@Override
public CallGraph getCallGraph() {
return fCallGraph;
}
@Override
public Collection<IWeightedTreeGroupDescriptor> getGroupDescriptors() {
List<IWeightedTreeGroupDescriptor> descriptors = new ArrayList<>();
for (IAnalysisModule module : getDependentAnalyses()) {
if (module instanceof IFlameChartProvider) {
CallStackSeries serie = ((IFlameChartProvider) module).getCallStackSeries();
if (serie != null) {
descriptors.add(serie.getRootGroup());
}
}
}
return descriptors;
}
@Override
public AggregatedCalledFunction createCallSite(Object symbol) {
return new AggregatedCalledFunction((ICallStackSymbol) symbol);
}
@Override
public List<String> getExtraDataSets() {
if (fHasKernelStatuses) {
return Collections.singletonList(String.valueOf(Messages.FlameChartDataProvider_KernelStatusTitle));
}
return ICallGraphProvider.super.getExtraDataSets();
}
@Override
public MetricType getWeightType() {
return DURATION_METRIC;
}
@Override
public List<MetricType> getAdditionalMetrics() {
return METRICS;
}
@Override
public String toDisplayString(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 Object getAdditionalMetric(AggregatedCallSite object, int metricIndex) {
if (object instanceof AggregatedCalledFunction) {
switch (metricIndex) {
case 0:
return ((AggregatedCalledFunction) object).getSelfTime();
case 1:
long cpuTime = ((AggregatedCalledFunction) object).getCpuTime();
return cpuTime >= 0 ? cpuTime : StringUtils.EMPTY;
case 2:
return ((AggregatedCalledFunction) object).getNbCalls();
default:
Activator.getInstance().logError("Unknown metric at position " + metricIndex); //$NON-NLS-1$
// Unknown metric, should not happen
break;
}
}
return StringUtils.EMPTY;
}
@Override
public String getTitle() {
return Objects.requireNonNull(Messages.CallGraphAnalysis_Title);
}
@Override
public @NonNull IDataPalette getPalette() {
// Use the palette with kernel styles, at worst, kernel styles won't be
// used
return FlameWithKernelPalette.getInstance();
}
}