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