tmf: Fix legend image leak in Histogram view
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.kernel.ui / src / org / eclipse / tracecompass / internal / lttng2 / kernel / ui / views / controlflow / ControlFlowView.java
CommitLineData
6151d86c 1/*******************************************************************************
1cf25311 2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
6151d86c
PT
3 *
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 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
4999a196 11 * Geneviève Bastien - Move code to provide base classes for time graph view
6151d86c
PT
12 *******************************************************************************/
13
9bc60be7 14package org.eclipse.tracecompass.internal.lttng2.kernel.ui.views.controlflow;
6151d86c
PT
15
16import java.util.ArrayList;
6151d86c 17import java.util.Comparator;
1cf25311 18import java.util.HashMap;
6151d86c 19import java.util.List;
1cf25311 20import java.util.Map;
6151d86c
PT
21
22import org.eclipse.core.runtime.IProgressMonitor;
d2120fb6 23import org.eclipse.jdt.annotation.Nullable;
03ab8eeb 24import org.eclipse.jface.action.IAction;
79ec0b89
PT
25import org.eclipse.jface.action.IToolBarManager;
26import org.eclipse.jface.dialogs.IDialogSettings;
9bc60be7
AM
27import org.eclipse.tracecompass.internal.lttng2.kernel.core.Attributes;
28import org.eclipse.tracecompass.internal.lttng2.kernel.ui.Activator;
29import org.eclipse.tracecompass.internal.lttng2.kernel.ui.Messages;
30import org.eclipse.tracecompass.lttng2.kernel.core.analysis.LttngKernelAnalysisModule;
e894a508 31import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
1dd75589 32import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
e894a508
AM
33import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
34import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
35import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
36import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
37import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
38import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
2bdf0193
AM
39import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
40import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
41import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
42import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractTimeGraphView;
43import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent;
44import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
45import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
46import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
47import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
48import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeLinkEvent;
49import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils;
50import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
51import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
6151d86c
PT
52
53/**
54 * The Control Flow view main object
55 *
56 */
4999a196 57public class ControlFlowView extends AbstractTimeGraphView {
6151d86c
PT
58
59 // ------------------------------------------------------------------------
60 // Constants
61 // ------------------------------------------------------------------------
62
63 /**
64 * View ID.
65 */
66 public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.ui.views.controlflow"; //$NON-NLS-1$
67
4999a196
GB
68 private static final String PROCESS_COLUMN = Messages.ControlFlowView_processColumn;
69 private static final String TID_COLUMN = Messages.ControlFlowView_tidColumn;
70 private static final String PTID_COLUMN = Messages.ControlFlowView_ptidColumn;
6151d86c 71 private static final String BIRTH_TIME_COLUMN = Messages.ControlFlowView_birthTimeColumn;
4999a196 72 private static final String TRACE_COLUMN = Messages.ControlFlowView_traceColumn;
6151d86c 73
4999a196 74 private static final String[] COLUMN_NAMES = new String[] {
6151d86c
PT
75 PROCESS_COLUMN,
76 TID_COLUMN,
77 PTID_COLUMN,
78 BIRTH_TIME_COLUMN,
79 TRACE_COLUMN
80 };
81
4999a196 82 private static final String[] FILTER_COLUMN_NAMES = new String[] {
6ac5a950
AM
83 PROCESS_COLUMN,
84 TID_COLUMN
85 };
86
aae89862
PT
87 // Timeout between updates in the build thread in ms
88 private static final long BUILD_UPDATE_TIMEOUT = 500;
89
6151d86c 90 // ------------------------------------------------------------------------
4999a196 91 // Constructors
6151d86c
PT
92 // ------------------------------------------------------------------------
93
4999a196
GB
94 /**
95 * Constructor
96 */
97 public ControlFlowView() {
747adf5c
PT
98 super(ID, new ControlFlowPresentationProvider());
99 setTreeColumns(COLUMN_NAMES);
4999a196 100 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
747adf5c 101 setFilterColumns(FILTER_COLUMN_NAMES);
a03b7ee4 102 setFilterLabelProvider(new ControlFlowFilterLabelProvider());
4999a196 103 setEntryComparator(new ControlFlowEntryComparator());
6151d86c
PT
104 }
105
79ec0b89
PT
106 @Override
107 protected void fillLocalToolBar(IToolBarManager manager) {
086f21ae 108 super.fillLocalToolBar(manager);
79ec0b89
PT
109 IDialogSettings settings = Activator.getDefault().getDialogSettings();
110 IDialogSettings section = settings.getSection(getClass().getName());
111 if (section == null) {
112 section = settings.addNewSection(getClass().getName());
113 }
03ab8eeb
PT
114
115 IAction hideArrowsAction = getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section);
116 manager.add(hideArrowsAction);
117
118 IAction followArrowBwdAction = getTimeGraphCombo().getTimeGraphViewer().getFollowArrowBwdAction();
119 followArrowBwdAction.setText(Messages.ControlFlowView_followCPUBwdText);
120 followArrowBwdAction.setToolTipText(Messages.ControlFlowView_followCPUBwdText);
121 manager.add(followArrowBwdAction);
122
123 IAction followArrowFwdAction = getTimeGraphCombo().getTimeGraphViewer().getFollowArrowFwdAction();
124 followArrowFwdAction.setText(Messages.ControlFlowView_followCPUFwdText);
125 followArrowFwdAction.setToolTipText(Messages.ControlFlowView_followCPUFwdText);
126 manager.add(followArrowFwdAction);
79ec0b89
PT
127 }
128
4999a196
GB
129 @Override
130 protected String getNextText() {
131 return Messages.ControlFlowView_nextProcessActionNameText;
132 }
6151d86c 133
4999a196
GB
134 @Override
135 protected String getNextTooltip() {
136 return Messages.ControlFlowView_nextProcessActionToolTipText;
137 }
6151d86c 138
4999a196
GB
139 @Override
140 protected String getPrevText() {
141 return Messages.ControlFlowView_previousProcessActionNameText;
142 }
6151d86c 143
4999a196
GB
144 @Override
145 protected String getPrevTooltip() {
146 return Messages.ControlFlowView_previousProcessActionToolTipText;
6151d86c
PT
147 }
148
149 private static class ControlFlowEntryComparator implements Comparator<ITimeGraphEntry> {
150
151 @Override
152 public int compare(ITimeGraphEntry o1, ITimeGraphEntry o2) {
4999a196 153
6151d86c
PT
154 int result = 0;
155
156 if ((o1 instanceof ControlFlowEntry) && (o2 instanceof ControlFlowEntry)) {
157 ControlFlowEntry entry1 = (ControlFlowEntry) o1;
158 ControlFlowEntry entry2 = (ControlFlowEntry) o2;
159 result = entry1.getTrace().getStartTime().compareTo(entry2.getTrace().getStartTime());
160 if (result == 0) {
161 result = entry1.getTrace().getName().compareTo(entry2.getTrace().getName());
162 }
163 if (result == 0) {
164 result = entry1.getThreadId() < entry2.getThreadId() ? -1 : entry1.getThreadId() > entry2.getThreadId() ? 1 : 0;
165 }
166 }
167
168 if (result == 0) {
169 result = o1.getStartTime() < o2.getStartTime() ? -1 : o1.getStartTime() > o2.getStartTime() ? 1 : 0;
170 }
171
172 return result;
173 }
174 }
175
6151d86c 176 /**
4999a196 177 * @author gbastien
6151d86c 178 *
6151d86c 179 */
4999a196 180 protected static class ControlFlowTreeLabelProvider extends TreeLabelProvider {
6151d86c 181
4999a196
GB
182 @Override
183 public String getColumnText(Object element, int columnIndex) {
184 ControlFlowEntry entry = (ControlFlowEntry) element;
6151d86c 185
4999a196
GB
186 if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_processColumn)) {
187 return entry.getName();
188 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_tidColumn)) {
189 return Integer.toString(entry.getThreadId());
190 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_ptidColumn)) {
191 if (entry.getParentThreadId() > 0) {
192 return Integer.toString(entry.getParentThreadId());
6151d86c 193 }
4999a196
GB
194 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_birthTimeColumn)) {
195 return Utils.formatTime(entry.getStartTime(), TimeFormat.CALENDAR, Resolution.NANOSEC);
196 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_traceColumn)) {
197 return entry.getTrace().getName();
6151d86c 198 }
4999a196 199 return ""; //$NON-NLS-1$
6151d86c 200 }
6151d86c 201
6151d86c
PT
202 }
203
a03b7ee4
PT
204 private static class ControlFlowFilterLabelProvider extends TreeLabelProvider {
205
206 @Override
207 public String getColumnText(Object element, int columnIndex) {
208 ControlFlowEntry entry = (ControlFlowEntry) element;
209
210 if (columnIndex == 0) {
211 return entry.getName();
212 } else if (columnIndex == 1) {
213 return Integer.toString(entry.getThreadId());
214 }
215 return ""; //$NON-NLS-1$
216 }
217
218 }
219
6151d86c
PT
220 // ------------------------------------------------------------------------
221 // Internal
222 // ------------------------------------------------------------------------
223
4999a196 224 @Override
1cf25311 225 protected void buildEventList(final ITmfTrace trace, ITmfTrace parentTrace, IProgressMonitor monitor) {
72221aa4 226 if (trace == null) {
1cf25311
PT
227 return;
228 }
72221aa4 229 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, LttngKernelAnalysisModule.ID);
1cf25311
PT
230 if (ssq == null) {
231 return;
232 }
fec1ac0b 233
1cf25311
PT
234 List<ControlFlowEntry> entryList = new ArrayList<>();
235 Map<Integer, ControlFlowEntry> entryMap = new HashMap<>();
236
237 long start = ssq.getStartTime();
238 setStartTime(Math.min(getStartTime(), start));
239
240 boolean complete = false;
241 while (!complete) {
faa38350
PT
242 if (monitor.isCanceled()) {
243 return;
244 }
aae89862 245 complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
1cf25311
PT
246 if (ssq.isCancelled()) {
247 return;
248 }
249 long end = ssq.getCurrentEndTime();
250 if (start == end && !complete) { // when complete execute one last time regardless of end time
251 continue;
252 }
253 setEndTime(Math.max(getEndTime(), end + 1));
254 List<Integer> threadQuarks = ssq.getQuarks(Attributes.THREADS, "*"); //$NON-NLS-1$
255 for (int threadQuark : threadQuarks) {
256 if (monitor.isCanceled()) {
257 return;
4bc53929 258 }
1cf25311
PT
259 String threadName = ssq.getAttributeName(threadQuark);
260 int threadId = -1;
261 try {
262 threadId = Integer.parseInt(threadName);
263 } catch (NumberFormatException e1) {
4bc53929 264 continue;
2002c638 265 }
1cf25311 266 if (threadId <= 0) { // ignore the 'unknown' (-1) and swapper (0) threads
4bc53929 267 continue;
faa38350 268 }
1c471b9e
AM
269
270 int execNameQuark;
271 List<ITmfStateInterval> execNameIntervals;
1cf25311 272 try {
1c471b9e 273 execNameQuark = ssq.getQuarkRelative(threadQuark, Attributes.EXEC_NAME);
1dd75589 274 execNameIntervals = StateSystemUtils.queryHistoryRange(ssq, execNameQuark, start, end);
1c471b9e
AM
275 } catch (AttributeNotFoundException e) {
276 /* No information on this thread (yet?), skip it for now */
277 continue;
278 } catch (StateSystemDisposedException e) {
279 /* State system is closing down, no point continuing */
280 break;
281 }
282
283 for (ITmfStateInterval execNameInterval : execNameIntervals) {
284 if (monitor.isCanceled()) {
285 return;
286 }
287 ControlFlowEntry entry = entryMap.get(threadId);
288 if (!execNameInterval.getStateValue().isNull() &&
289 execNameInterval.getStateValue().getType() == ITmfStateValue.Type.STRING) {
290 String execName = execNameInterval.getStateValue().unboxStr();
291 long startTime = execNameInterval.getStartTime();
292 long endTime = execNameInterval.getEndTime() + 1;
293 if (entry == null) {
294 ITmfStateInterval ppidInterval = null;
295 try {
1cf25311 296 int ppidQuark = ssq.getQuarkRelative(threadQuark, Attributes.PPID);
1c471b9e
AM
297 ppidInterval = ssq.querySingleState(startTime, ppidQuark);
298 } catch (AttributeNotFoundException e) {
299 /* No info, keep PPID at -1 */
300 } catch (StateSystemDisposedException e) {
301 /* SS is closing down, time to bail */
302 break;
6151d86c 303 }
1c471b9e
AM
304 int ppid = -1;
305 if (!(ppidInterval == null) && !ppidInterval.getStateValue().isNull()) {
306 ppid = ppidInterval.getStateValue().unboxInt();
307 }
308 entry = new ControlFlowEntry(threadQuark, trace, execName, threadId, ppid, startTime, endTime);
309 entryList.add(entry);
310 entryMap.put(threadId, entry);
1cf25311 311 } else {
1c471b9e
AM
312 // update the name of the entry to the latest
313 // execName
314 entry.setName(execName);
315 entry.updateEndTime(endTime);
6151d86c 316 }
1c471b9e
AM
317 } else {
318 entryMap.remove(threadId);
6151d86c
PT
319 }
320 }
6151d86c 321 }
4999a196 322
1cf25311
PT
323 updateTree(entryList, parentTrace);
324
325 if (parentTrace.equals(getTrace())) {
d7ee91bb 326 refresh();
6151d86c 327 }
1cf25311
PT
328
329 for (ControlFlowEntry entry : entryList) {
330 if (monitor.isCanceled()) {
331 return;
332 }
333 buildStatusEvents(entry.getTrace(), entry, monitor, start, end);
faa38350 334 }
1cf25311
PT
335
336 start = end;
6151d86c
PT
337 }
338 }
339
1cf25311
PT
340 private void updateTree(List<ControlFlowEntry> entryList, ITmfTrace parentTrace) {
341 List<TimeGraphEntry> rootListToAdd = new ArrayList<>();
342 List<TimeGraphEntry> rootListToRemove = new ArrayList<>();
343 List<TimeGraphEntry> rootList = getEntryList(parentTrace);
344
1d46dc38 345 for (ControlFlowEntry entry : entryList) {
1cf25311
PT
346 boolean root = (entry.getParent() == null);
347 if (root && entry.getParentThreadId() > 0) {
1d46dc38 348 for (ControlFlowEntry parent : entryList) {
6151d86c
PT
349 if (parent.getThreadId() == entry.getParentThreadId() &&
350 entry.getStartTime() >= parent.getStartTime() &&
351 entry.getStartTime() <= parent.getEndTime()) {
352 parent.addChild(entry);
353 root = false;
1cf25311
PT
354 if (rootList != null && rootList.contains(entry)) {
355 rootListToRemove.add(entry);
356 }
6151d86c
PT
357 break;
358 }
359 }
360 }
1cf25311
PT
361 if (root && (rootList == null || !rootList.contains(entry))) {
362 rootListToAdd.add(entry);
6151d86c
PT
363 }
364 }
1cf25311
PT
365
366 addToEntryList(parentTrace, rootListToAdd);
367 removeFromEntryList(parentTrace, rootListToRemove);
6151d86c
PT
368 }
369
1cf25311
PT
370 private void buildStatusEvents(ITmfTrace trace, ControlFlowEntry entry, IProgressMonitor monitor, long start, long end) {
371 if (start < entry.getEndTime() && end > entry.getStartTime()) {
72221aa4 372 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(entry.getTrace(), LttngKernelAnalysisModule.ID);
1cf25311
PT
373 if (ssq == null) {
374 return;
375 }
376
377 long startTime = Math.max(start, entry.getStartTime());
378 long endTime = Math.min(end + 1, entry.getEndTime());
379 long resolution = Math.max(1, (end - ssq.getStartTime()) / getDisplayWidth());
380 List<ITimeEvent> eventList = getEventList(entry, startTime, endTime, resolution, monitor);
d2120fb6 381 if (eventList == null) {
1cf25311
PT
382 return;
383 }
384 for (ITimeEvent event : eventList) {
385 entry.addEvent(event);
386 }
387 if (trace.equals(getTrace())) {
388 redraw();
389 }
faa38350 390 }
6151d86c 391 for (ITimeGraphEntry child : entry.getChildren()) {
faa38350
PT
392 if (monitor.isCanceled()) {
393 return;
394 }
1cf25311 395 buildStatusEvents(trace, (ControlFlowEntry) child, monitor, start, end);
6151d86c
PT
396 }
397 }
398
4999a196 399 @Override
d2120fb6 400 protected @Nullable List<ITimeEvent> getEventList(TimeGraphEntry tgentry, long startTime, long endTime, long resolution, IProgressMonitor monitor) {
4999a196
GB
401 List<ITimeEvent> eventList = null;
402 if (!(tgentry instanceof ControlFlowEntry)) {
403 return eventList;
404 }
405 ControlFlowEntry entry = (ControlFlowEntry) tgentry;
41b5c37f
AM
406 final long realStart = Math.max(startTime, entry.getStartTime());
407 final long realEnd = Math.min(endTime, entry.getEndTime());
408 if (realEnd <= realStart) {
6151d86c
PT
409 return null;
410 }
72221aa4 411 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(entry.getTrace(), LttngKernelAnalysisModule.ID);
4bc53929
GB
412 if (ssq == null) {
413 return null;
414 }
6151d86c
PT
415 try {
416 int statusQuark = ssq.getQuarkRelative(entry.getThreadQuark(), Attributes.STATUS);
1dd75589 417 List<ITmfStateInterval> statusIntervals = StateSystemUtils.queryHistoryRange(ssq, statusQuark, realStart, realEnd - 1, resolution, monitor);
e0838ca1 418 eventList = new ArrayList<>(statusIntervals.size());
6151d86c
PT
419 long lastEndTime = -1;
420 for (ITmfStateInterval statusInterval : statusIntervals) {
421 if (monitor.isCanceled()) {
422 return null;
423 }
424 long time = statusInterval.getStartTime();
425 long duration = statusInterval.getEndTime() - time + 1;
426 int status = -1;
427 try {
428 status = statusInterval.getStateValue().unboxInt();
429 } catch (StateValueTypeException e) {
430 e.printStackTrace();
431 }
432 if (lastEndTime != time && lastEndTime != -1) {
af10fe06 433 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
6151d86c 434 }
4999a196 435 eventList.add(new TimeEvent(entry, time, duration, status));
6151d86c
PT
436 lastEndTime = time + duration;
437 }
1cf25311 438 } catch (AttributeNotFoundException | TimeRangeException e) {
6151d86c 439 e.printStackTrace();
96345c5a
AM
440 } catch (StateSystemDisposedException e) {
441 /* Ignored */
6151d86c
PT
442 }
443 return eventList;
444 }
445
4999a196
GB
446 /**
447 * Returns a value corresponding to the selected entry.
448 *
1cf25311
PT
449 * Used in conjunction with synchingToTime to change the selected entry. If
450 * one of these methods is overridden in child class, then both should be.
4999a196
GB
451 *
452 * @param time
453 * The currently selected time
454 * @return a value identifying the entry
455 */
456 private int getSelectionValue(long time) {
457 int thread = -1;
1cf25311
PT
458 ITmfTrace[] traces = TmfTraceManager.getTraceSet(getTrace());
459 if (traces == null) {
460 return thread;
461 }
462 for (ITmfTrace trace : traces) {
4999a196
GB
463 if (thread > 0) {
464 break;
465 }
72221aa4 466 if (trace == null) {
1cf25311
PT
467 continue;
468 }
72221aa4 469 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, LttngKernelAnalysisModule.ID);
1cf25311
PT
470 if (ssq == null) {
471 continue;
472 }
473 if (time >= ssq.getStartTime() && time <= ssq.getCurrentEndTime()) {
474 List<Integer> currentThreadQuarks = ssq.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$
475 for (int currentThreadQuark : currentThreadQuarks) {
476 try {
477 ITmfStateInterval currentThreadInterval = ssq.querySingleState(time, currentThreadQuark);
478 int currentThread = currentThreadInterval.getStateValue().unboxInt();
479 if (currentThread > 0) {
480 int statusQuark = ssq.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThread), Attributes.STATUS);
481 ITmfStateInterval statusInterval = ssq.querySingleState(time, statusQuark);
482 if (statusInterval.getStartTime() == time) {
483 thread = currentThread;
484 break;
4999a196 485 }
4999a196 486 }
1cf25311
PT
487 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
488 e.printStackTrace();
489 } catch (StateSystemDisposedException e) {
490 /* Ignored */
faa38350 491 }
6151d86c 492 }
6151d86c 493 }
4999a196
GB
494 }
495 return thread;
6151d86c
PT
496 }
497
4999a196
GB
498 @Override
499 protected void synchingToTime(long time) {
500 int selected = getSelectionValue(time);
501 if (selected > 0) {
747adf5c 502 for (Object element : getTimeGraphViewer().getExpandedElements()) {
4999a196
GB
503 if (element instanceof ControlFlowEntry) {
504 ControlFlowEntry entry = (ControlFlowEntry) element;
505 if (entry.getThreadId() == selected) {
506 getTimeGraphCombo().setSelection(entry);
507 break;
6151d86c
PT
508 }
509 }
510 }
6151d86c 511 }
6151d86c 512 }
79ec0b89
PT
513
514 @Override
515 protected List<ILinkEvent> getLinkList(long startTime, long endTime, long resolution, IProgressMonitor monitor) {
e0838ca1 516 List<ILinkEvent> list = new ArrayList<>();
79ec0b89 517 ITmfTrace[] traces = TmfTraceManager.getTraceSet(getTrace());
1cf25311 518 List<TimeGraphEntry> entryList = getEntryList(getTrace());
79ec0b89
PT
519 if (traces == null || entryList == null) {
520 return list;
521 }
522 for (ITmfTrace trace : traces) {
72221aa4 523 if (trace == null) {
1cf25311
PT
524 continue;
525 }
72221aa4 526 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, LttngKernelAnalysisModule.ID);
1cf25311
PT
527 if (ssq == null) {
528 continue;
529 }
530 try {
531 long start = Math.max(startTime, ssq.getStartTime());
532 long end = Math.min(endTime, ssq.getCurrentEndTime());
533 if (end < start) {
4bc53929
GB
534 continue;
535 }
1cf25311
PT
536 List<Integer> currentThreadQuarks = ssq.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$
537 for (int currentThreadQuark : currentThreadQuarks) {
538 // adjust the query range to include the previous and following intervals
539 long qstart = Math.max(ssq.querySingleState(start, currentThreadQuark).getStartTime() - 1, ssq.getStartTime());
540 long qend = Math.min(ssq.querySingleState(end, currentThreadQuark).getEndTime() + 1, ssq.getCurrentEndTime());
1dd75589 541 List<ITmfStateInterval> currentThreadIntervals = StateSystemUtils.queryHistoryRange(ssq, currentThreadQuark, qstart, qend, resolution, monitor);
1cf25311
PT
542 int prevThread = 0;
543 long prevEnd = 0;
544 long lastEnd = 0;
545 for (ITmfStateInterval currentThreadInterval : currentThreadIntervals) {
546 if (monitor.isCanceled()) {
547 return null;
548 }
549 long time = currentThreadInterval.getStartTime();
550 if (time != lastEnd) {
551 // don't create links where there are gaps in intervals due to the resolution
552 prevThread = 0;
553 prevEnd = 0;
554 }
555 int thread = currentThreadInterval.getStateValue().unboxInt();
556 if (thread > 0 && prevThread > 0) {
557 ITimeGraphEntry prevEntry = findEntry(entryList, trace, prevThread);
558 ITimeGraphEntry nextEntry = findEntry(entryList, trace, thread);
559 list.add(new TimeLinkEvent(prevEntry, nextEntry, prevEnd, time - prevEnd, 0));
560 }
561 lastEnd = currentThreadInterval.getEndTime() + 1;
562 if (thread != 0) {
563 prevThread = thread;
564 prevEnd = lastEnd;
79ec0b89
PT
565 }
566 }
79ec0b89 567 }
1cf25311
PT
568 } catch (TimeRangeException | AttributeNotFoundException | StateValueTypeException e) {
569 e.printStackTrace();
570 } catch (StateSystemDisposedException e) {
571 /* Ignored */
79ec0b89
PT
572 }
573 }
574 return list;
575 }
576
a3188982
PT
577 private ControlFlowEntry findEntry(List<? extends ITimeGraphEntry> entryList, ITmfTrace trace, int threadId) {
578 for (ITimeGraphEntry entry : entryList) {
79ec0b89
PT
579 if (entry instanceof ControlFlowEntry) {
580 ControlFlowEntry controlFlowEntry = (ControlFlowEntry) entry;
581 if (controlFlowEntry.getThreadId() == threadId && controlFlowEntry.getTrace() == trace) {
582 return controlFlowEntry;
583 } else if (entry.hasChildren()) {
584 controlFlowEntry = findEntry(entry.getChildren(), trace, threadId);
585 if (controlFlowEntry != null) {
586 return controlFlowEntry;
587 }
588 }
589 }
590 }
591 return null;
592 }
6151d86c 593}
This page took 0.146646 seconds and 5 git commands to generate.