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