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