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