5db607983f0e34c69e5f13ef7d66eb88edb06480
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / internal / 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.internal.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.statesystem.core.ITmfStateSystem;
39 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
40 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
41 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
42 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
43 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
44 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
45 import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
46 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
47 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
48 import org.eclipse.tracecompass.tmf.core.util.Pair;
49 import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractStateSystemTimeGraphView;
50 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent;
51 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
52 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
53 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
54 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
55 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
56 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeLinkEvent;
57 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils;
58 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
59 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
60
61 import com.google.common.collect.ImmutableList;
62
63 /**
64 * The Control Flow view main object
65 *
66 */
67 public class ControlFlowView extends AbstractStateSystemTimeGraphView {
68
69 // ------------------------------------------------------------------------
70 // Constants
71 // ------------------------------------------------------------------------
72 /**
73 * View ID.
74 */
75 public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.views.controlflow"; //$NON-NLS-1$
76
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;
80 private static final String BIRTH_TIME_COLUMN = Messages.ControlFlowView_birthTimeColumn;
81 private static final String TRACE_COLUMN = Messages.ControlFlowView_traceColumn;
82
83 private static final String[] COLUMN_NAMES = new String[] {
84 PROCESS_COLUMN,
85 TID_COLUMN,
86 PTID_COLUMN,
87 BIRTH_TIME_COLUMN,
88 TRACE_COLUMN
89 };
90
91 private static final String[] FILTER_COLUMN_NAMES = new String[] {
92 PROCESS_COLUMN,
93 TID_COLUMN
94 };
95
96 // Timeout between updates in the build thread in ms
97 private static final long BUILD_UPDATE_TIMEOUT = 500;
98
99 private static final Comparator<ITimeGraphEntry>[] COLUMN_COMPARATORS;
100
101 private static final int INITIAL_SORT_COLUMN_INDEX = 3;
102
103 static {
104 ImmutableList.Builder<Comparator<ITimeGraphEntry>> builder = ImmutableList.builder();
105 builder.add(ControlFlowColumnComparators.PROCESS_NAME_COLUMN_COMPARATOR)
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);
110 List<Comparator<ITimeGraphEntry>> l = builder.build();
111 COLUMN_COMPARATORS = l.toArray(new Comparator[l.size()]);
112 }
113
114 // ------------------------------------------------------------------------
115 // Constructors
116 // ------------------------------------------------------------------------
117
118 /**
119 * Constructor
120 */
121 public ControlFlowView() {
122 super(ID, new ControlFlowPresentationProvider());
123 setTreeColumns(COLUMN_NAMES, COLUMN_COMPARATORS, INITIAL_SORT_COLUMN_INDEX);
124 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
125 setFilterColumns(FILTER_COLUMN_NAMES);
126 setFilterLabelProvider(new ControlFlowFilterLabelProvider());
127 setEntryComparator(ControlFlowColumnComparators.BIRTH_TIME_COLUMN_COMPARATOR);
128 }
129
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));
139 }
140
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()));
152 }
153 }
154 }
155
156 @Override
157 protected void fillLocalToolBar(IToolBarManager manager) {
158 super.fillLocalToolBar(manager);
159 IDialogSettings settings = Activator.getDefault().getDialogSettings();
160 IDialogSettings section = settings.getSection(getClass().getName());
161 if (section == null) {
162 section = settings.addNewSection(getClass().getName());
163 }
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);
177 }
178
179 @Override
180 protected String getNextText() {
181 return Messages.ControlFlowView_nextProcessActionNameText;
182 }
183
184 @Override
185 protected String getNextTooltip() {
186 return Messages.ControlFlowView_nextProcessActionToolTipText;
187 }
188
189 @Override
190 protected String getPrevText() {
191 return Messages.ControlFlowView_previousProcessActionNameText;
192 }
193
194 @Override
195 protected String getPrevTooltip() {
196 return Messages.ControlFlowView_previousProcessActionToolTipText;
197 }
198
199 /**
200 * @author gbastien
201 *
202 */
203 protected static class ControlFlowTreeLabelProvider extends TreeLabelProvider {
204
205 @Override
206 public String getColumnText(Object element, int columnIndex) {
207 ControlFlowEntry entry = (ControlFlowEntry) element;
208
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());
216 }
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();
221 }
222 return ""; //$NON-NLS-1$
223 }
224
225 }
226
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
243 // ------------------------------------------------------------------------
244 // Internal
245 // ------------------------------------------------------------------------
246
247 @Override
248 protected void buildEntryList(final ITmfTrace trace, final ITmfTrace parentTrace, final IProgressMonitor monitor) {
249 final ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
250 if (ssq == null) {
251 return;
252 }
253
254 final List<ControlFlowEntry> entryList = new ArrayList<>();
255 /** Map of view entries, key is a pair [threadId, cpuId] */
256 final Map<Pair<Integer, Integer>, ControlFlowEntry> entryMap = new HashMap<>();
257
258 long start = ssq.getStartTime();
259 setStartTime(Math.min(getStartTime(), start));
260
261 boolean complete = false;
262 while (!complete) {
263 if (monitor.isCanceled()) {
264 return;
265 }
266 complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
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 }
274 final long resolution = Math.max(1, (end - ssq.getStartTime()) / getDisplayWidth());
275 setEndTime(Math.max(getEndTime(), end + 1));
276 final List<Integer> threadQuarks = ssq.getQuarks(Attributes.THREADS, "*"); //$NON-NLS-1$
277 queryFullStates(ssq, start, end, resolution, monitor, new IQueryHandler() {
278 @Override
279 public void handle(List<List<ITmfStateInterval>> fullStates, List<ITmfStateInterval> prevFullState) {
280 for (int threadQuark : threadQuarks) {
281 String threadAttributeName = ssq.getAttributeName(threadQuark);
282
283 Pair<Integer, Integer> entryKey = Attributes.parseThreadAttributeName(threadAttributeName);
284 int threadId = entryKey.getFirst();
285
286 if (threadId < 0) { // ignore the 'unknown' (-1) thread
287 continue;
288 }
289
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;
306 }
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 }
334 }
335 if (!execNameInterval.getStateValue().isNull() &&
336 execNameInterval.getStateValue().getType() == ITmfStateValue.Type.STRING) {
337 String execName = execNameInterval.getStateValue().unboxStr();
338 int ppid = ppidInterval.getStateValue().unboxInt();
339 ControlFlowEntry entry = entryMap.get(entryKey);
340 if (entry == null) {
341 entry = new ControlFlowEntry(threadQuark, trace, execName, threadId, ppid, startTime, endTime);
342 entryList.add(entry);
343 entryMap.put(entryKey, entry);
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) {
356 entryMap.remove(entryKey);
357 }
358 lastExecNameStartTime = startTime;
359 lastExecNameEndTime = endTime;
360 lastPpidStartTime = ppidInterval.getStartTime();
361 }
362 }
363 updateTree(entryList, parentTrace, ssq);
364 }
365 });
366
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) {
370 for (final TimeGraphEntry entry : getEntryList(ssq)) {
371 if (monitor.isCanceled()) {
372 return;
373 }
374 buildStatusEvents(trace, parentTrace, ssq, fullStates, prevFullState, (ControlFlowEntry) entry, monitor, ssq.getStartTime(), end);
375 }
376 }
377 });
378
379 if (parentTrace.equals(getTrace())) {
380 refresh();
381 }
382
383 start = end;
384 }
385 }
386
387 private void updateTree(List<ControlFlowEntry> entryList, ITmfTrace parentTrace, ITmfStateSystem ss) {
388 List<TimeGraphEntry> rootListToAdd = new ArrayList<>();
389 List<TimeGraphEntry> rootListToRemove = new ArrayList<>();
390 List<TimeGraphEntry> rootList = getEntryList(ss);
391
392 for (ControlFlowEntry entry : entryList) {
393 boolean root = (entry.getParent() == null);
394 if (root && entry.getParentThreadId() > 0) {
395 for (ControlFlowEntry parent : entryList) {
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 */
404 if (parent.getThreadId() == entry.getParentThreadId() &&
405 !(entry.getStartTime() > parent.getEndTime() ||
406 entry.getEndTime() < parent.getStartTime())) {
407 parent.addChild(entry);
408 root = false;
409 if (rootList != null && rootList.contains(entry)) {
410 rootListToRemove.add(entry);
411 }
412 break;
413 }
414 }
415 }
416 if (root && (rootList == null || !rootList.contains(entry))) {
417 rootListToAdd.add(entry);
418 }
419 }
420
421 addToEntryList(parentTrace, ss, rootListToAdd);
422 removeFromEntryList(parentTrace, ss, rootListToRemove);
423 }
424
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) {
427 if (start < entry.getEndTime() && end > entry.getStartTime()) {
428 List<ITimeEvent> eventList = getEventList(entry, ss, fullStates, prevFullState, monitor);
429 if (eventList == null) {
430 return;
431 }
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 }
439 }
440 if (parentTrace.equals(getTrace())) {
441 redraw();
442 }
443 }
444 for (ITimeGraphEntry child : entry.getChildren()) {
445 if (monitor.isCanceled()) {
446 return;
447 }
448 buildStatusEvents(trace, parentTrace, ss, fullStates, prevFullState, (ControlFlowEntry) child, monitor, start, end);
449 }
450 }
451
452 @Override
453 protected @Nullable List<ITimeEvent> getEventList(@NonNull TimeGraphEntry tgentry, ITmfStateSystem ss,
454 @NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, @NonNull IProgressMonitor monitor) {
455 List<ITimeEvent> eventList = null;
456 if (!(tgentry instanceof ControlFlowEntry)) {
457 return eventList;
458 }
459 ControlFlowEntry entry = (ControlFlowEntry) tgentry;
460 try {
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) {
468 if (monitor.isCanceled()) {
469 return null;
470 }
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);
476 long time = statusInterval.getStartTime();
477 if (time == lastStartTime) {
478 continue;
479 }
480 long duration = statusInterval.getEndTime() - time + 1;
481 int status = -1;
482 try {
483 status = statusInterval.getStateValue().unboxInt();
484 } catch (StateValueTypeException e) {
485 Activator.getDefault().logError(e.getMessage());
486 }
487 if (lastEndTime != time && lastEndTime != -1) {
488 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
489 }
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;
496 lastEndTime = time + duration;
497 }
498 } catch (AttributeNotFoundException | TimeRangeException e) {
499 Activator.getDefault().logError(e.getMessage());
500 }
501 return eventList;
502 }
503
504 /**
505 * Returns a value corresponding to the selected entry.
506 *
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.
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;
516 for (ITmfTrace trace : TmfTraceManager.getTraceSet(getTrace())) {
517 if (thread > 0) {
518 break;
519 }
520 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
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;
536 }
537 }
538 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
539 Activator.getDefault().logError(e.getMessage());
540 } catch (StateSystemDisposedException e) {
541 /* Ignored */
542 }
543 }
544 }
545 }
546 return thread;
547 }
548
549 @Override
550 protected void synchingToTime(long time) {
551 int selected = getSelectionValue(time);
552 if (selected > 0) {
553 for (Object element : getTimeGraphViewer().getExpandedElements()) {
554 if (element instanceof ControlFlowEntry) {
555 ControlFlowEntry entry = (ControlFlowEntry) element;
556 if (entry.getThreadId() == selected) {
557 getTimeGraphCombo().setSelection(entry);
558 break;
559 }
560 }
561 }
562 }
563 }
564
565 @Override
566 protected @NonNull List<ILinkEvent> getLinkList(ITmfStateSystem ss,
567 @NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, @NonNull IProgressMonitor monitor) {
568 List<ILinkEvent> list = new ArrayList<>();
569 List<TimeGraphEntry> entryList = getEntryList(ss);
570 if (entryList == null) {
571 return list;
572 }
573 for (ITmfTrace trace : TmfTraceManager.getTraceSet(getTrace())) {
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 */
578 continue;
579 }
580 List<ITmfStateInterval> currentThreadIntervals = new ArrayList<>(fullStates.size() + 2);
581 try {
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 }
596 for (List<ITmfStateInterval> fullState : fullStates) {
597 currentThreadIntervals.add(fullState.get(currentThreadQuark));
598 }
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 }
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;
637 }
638 }
639 }
640 }
641 return list;
642 }
643
644 private ControlFlowEntry findEntry(List<? extends ITimeGraphEntry> entryList, ITmfTrace trace, int threadId) {
645 for (ITimeGraphEntry entry : entryList) {
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 }
660 }
This page took 0.051429 seconds and 5 git commands to generate.