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