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