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