lttng: Rename CtfKernelTrace to LttngKernelTrace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.ui / src / org / eclipse / linuxtools / internal / lttng2 / kernel / ui / views / controlflow / ControlFlowView.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.controlflow;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.Comparator;
19 import java.util.HashMap;
20 import java.util.List;
21
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IToolBarManager;
26 import org.eclipse.jface.action.Separator;
27 import org.eclipse.jface.viewers.ILabelProviderListener;
28 import org.eclipse.jface.viewers.ITableLabelProvider;
29 import org.eclipse.jface.viewers.ITreeContentProvider;
30 import org.eclipse.jface.viewers.Viewer;
31 import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
32 import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages;
33 import org.eclipse.linuxtools.lttng2.kernel.core.trace.LttngKernelTrace;
34 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp;
35 import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
36 import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException;
37 import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
38 import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
39 import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
40 import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal;
41 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
42 import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
43 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceClosedSignal;
44 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceSelectedSignal;
45 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
46 import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue;
47 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
48 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
49 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
50 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
51 import org.eclipse.linuxtools.tmf.ui.views.TmfView;
52 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphRangeListener;
53 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphSelectionListener;
54 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphTimeListener;
55 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphCombo;
56 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphRangeUpdateEvent;
57 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphSelectionEvent;
58 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphTimeEvent;
59 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
60 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
61 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent;
62 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils;
63 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
64 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
65 import org.eclipse.swt.SWT;
66 import org.eclipse.swt.graphics.Image;
67 import org.eclipse.swt.widgets.Composite;
68 import org.eclipse.swt.widgets.Display;
69 import org.eclipse.swt.widgets.TreeColumn;
70 import org.eclipse.ui.IActionBars;
71
72 /**
73 * The Control Flow view main object
74 *
75 */
76 public class ControlFlowView extends TmfView {
77
78 // ------------------------------------------------------------------------
79 // Constants
80 // ------------------------------------------------------------------------
81
82 /**
83 * View ID.
84 */
85 public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.ui.views.controlflow"; //$NON-NLS-1$
86
87 private static final String PROCESS_COLUMN = Messages.ControlFlowView_processColumn;
88 private static final String TID_COLUMN = Messages.ControlFlowView_tidColumn;
89 private static final String PTID_COLUMN = Messages.ControlFlowView_ptidColumn;
90 private static final String BIRTH_TIME_COLUMN = Messages.ControlFlowView_birthTimeColumn;
91 private static final String TRACE_COLUMN = Messages.ControlFlowView_traceColumn;
92
93 private final String[] COLUMN_NAMES = new String[] {
94 PROCESS_COLUMN,
95 TID_COLUMN,
96 PTID_COLUMN,
97 BIRTH_TIME_COLUMN,
98 TRACE_COLUMN
99 };
100
101 private final String[] FILTER_COLUMN_NAMES = new String[] {
102 PROCESS_COLUMN,
103 TID_COLUMN
104 };
105
106 /**
107 * Redraw state enum
108 */
109 private enum State { IDLE, BUSY, PENDING }
110
111 // ------------------------------------------------------------------------
112 // Fields
113 // ------------------------------------------------------------------------
114
115 // The timegraph combo
116 private TimeGraphCombo fTimeGraphCombo;
117
118 // The selected trace
119 private ITmfTrace fTrace;
120
121 // The timegraph entry list
122 private ArrayList<ControlFlowEntry> fEntryList;
123
124 // The trace to entry list hash map
125 final private HashMap<ITmfTrace, ArrayList<ControlFlowEntry>> fEntryListMap = new HashMap<ITmfTrace, ArrayList<ControlFlowEntry>>();
126
127 // The trace to build thread hash map
128 final private HashMap<ITmfTrace, BuildThread> fBuildThreadMap = new HashMap<ITmfTrace, BuildThread>();
129
130 // The start time
131 private long fStartTime;
132
133 // The end time
134 private long fEndTime;
135
136 // The display width
137 private final int fDisplayWidth;
138
139 // The zoom thread
140 private ZoomThread fZoomThread;
141
142 // The next resource action
143 private Action fNextResourceAction;
144
145 // The previous resource action
146 private Action fPreviousResourceAction;
147
148 // A comparator class
149 private final ControlFlowEntryComparator fControlFlowEntryComparator = new ControlFlowEntryComparator();
150
151 // The redraw state used to prevent unnecessary queuing of display runnables
152 private State fRedrawState = State.IDLE;
153
154 // The redraw synchronization object
155 final private Object fSyncObj = new Object();
156
157 // ------------------------------------------------------------------------
158 // Classes
159 // ------------------------------------------------------------------------
160
161 private class TreeContentProvider implements ITreeContentProvider {
162
163 @Override
164 public void dispose() {
165 }
166
167 @Override
168 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
169 }
170
171 @Override
172 public Object[] getElements(Object inputElement) {
173 return (ITimeGraphEntry[]) inputElement;
174 }
175
176 @Override
177 public Object[] getChildren(Object parentElement) {
178 ITimeGraphEntry entry = (ITimeGraphEntry) parentElement;
179 List<? extends ITimeGraphEntry> children = entry.getChildren();
180 return children.toArray(new ITimeGraphEntry[children.size()]);
181 }
182
183 @Override
184 public Object getParent(Object element) {
185 ITimeGraphEntry entry = (ITimeGraphEntry) element;
186 return entry.getParent();
187 }
188
189 @Override
190 public boolean hasChildren(Object element) {
191 ITimeGraphEntry entry = (ITimeGraphEntry) element;
192 return entry.hasChildren();
193 }
194
195 }
196
197 private class TreeLabelProvider implements ITableLabelProvider {
198
199 @Override
200 public void addListener(ILabelProviderListener listener) {
201 }
202
203 @Override
204 public void dispose() {
205 }
206
207 @Override
208 public boolean isLabelProperty(Object element, String property) {
209 return false;
210 }
211
212 @Override
213 public void removeListener(ILabelProviderListener listener) {
214 }
215
216 @Override
217 public Image getColumnImage(Object element, int columnIndex) {
218 return null;
219 }
220
221 @Override
222 public String getColumnText(Object element, int columnIndex) {
223 ControlFlowEntry entry = (ControlFlowEntry) element;
224 if (columnIndex == 0) {
225 return entry.getName();
226 } else if (columnIndex == 1) {
227 return Integer.toString(entry.getThreadId());
228 } else if (columnIndex == 2) {
229 if (entry.getParentThreadId() > 0) {
230 return Integer.toString(entry.getParentThreadId());
231 }
232 } else if (columnIndex == 3) {
233 return Utils.formatTime(entry.getStartTime(), TimeFormat.CALENDAR, Resolution.NANOSEC);
234 } else if (columnIndex == 4) {
235 return entry.getTrace().getName();
236 }
237 return ""; //$NON-NLS-1$
238 }
239
240 }
241
242 private static class ControlFlowEntryComparator implements Comparator<ITimeGraphEntry> {
243
244 @Override
245 public int compare(ITimeGraphEntry o1, ITimeGraphEntry o2) {
246 int result = 0;
247
248 if ((o1 instanceof ControlFlowEntry) && (o2 instanceof ControlFlowEntry)) {
249 ControlFlowEntry entry1 = (ControlFlowEntry) o1;
250 ControlFlowEntry entry2 = (ControlFlowEntry) o2;
251 result = entry1.getTrace().getStartTime().compareTo(entry2.getTrace().getStartTime());
252 if (result == 0) {
253 result = entry1.getTrace().getName().compareTo(entry2.getTrace().getName());
254 }
255 if (result == 0) {
256 result = entry1.getThreadId() < entry2.getThreadId() ? -1 : entry1.getThreadId() > entry2.getThreadId() ? 1 : 0;
257 }
258 }
259
260 if (result == 0) {
261 result = o1.getStartTime() < o2.getStartTime() ? -1 : o1.getStartTime() > o2.getStartTime() ? 1 : 0;
262 }
263
264 return result;
265 }
266 }
267
268 private class BuildThread extends Thread {
269 private final ITmfTrace fBuildTrace;
270 private final IProgressMonitor fMonitor;
271
272 public BuildThread(ITmfTrace trace) {
273 super("ControlFlowView build"); //$NON-NLS-1$
274 fBuildTrace = trace;
275 fMonitor = new NullProgressMonitor();
276 }
277
278 @Override
279 public void run() {
280 buildEventList(fBuildTrace, fMonitor);
281 synchronized (fBuildThreadMap) {
282 fBuildThreadMap.remove(this);
283 }
284 }
285
286 public void cancel() {
287 fMonitor.setCanceled(true);
288 }
289 }
290
291 private class ZoomThread extends Thread {
292 private final ArrayList<ControlFlowEntry> fZoomEntryList;
293 private final long fZoomStartTime;
294 private final long fZoomEndTime;
295 private final long fResolution;
296 private final IProgressMonitor fMonitor;
297
298 public ZoomThread(ArrayList<ControlFlowEntry> entryList, long startTime, long endTime) {
299 super("ControlFlowView zoom"); //$NON-NLS-1$
300 fZoomEntryList = entryList;
301 fZoomStartTime = startTime;
302 fZoomEndTime = endTime;
303 fResolution = Math.max(1, (fZoomEndTime - fZoomStartTime) / fDisplayWidth);
304 fMonitor = new NullProgressMonitor();
305 }
306
307 @Override
308 public void run() {
309 if (fZoomEntryList == null) {
310 return;
311 }
312 for (ControlFlowEntry entry : fZoomEntryList) {
313 if (fMonitor.isCanceled()) {
314 break;
315 }
316 zoom(entry, fMonitor);
317 }
318 }
319
320 private void zoom(ControlFlowEntry entry, IProgressMonitor monitor) {
321 if (fZoomStartTime <= fStartTime && fZoomEndTime >= fEndTime) {
322 entry.setZoomedEventList(null);
323 } else {
324 List<ITimeEvent> zoomedEventList = getEventList(entry, fZoomStartTime, fZoomEndTime, fResolution, monitor);
325 if (zoomedEventList != null) {
326 entry.setZoomedEventList(zoomedEventList);
327 }
328 }
329 redraw();
330 for (ControlFlowEntry child : entry.getChildren()) {
331 if (fMonitor.isCanceled()) {
332 return;
333 }
334 zoom(child, monitor);
335 }
336 }
337
338 public void cancel() {
339 fMonitor.setCanceled(true);
340 }
341 }
342
343 // ------------------------------------------------------------------------
344 // Constructors
345 // ------------------------------------------------------------------------
346
347 /**
348 * Constructor
349 */
350 public ControlFlowView() {
351 super(ID);
352 fDisplayWidth = Display.getDefault().getBounds().width;
353 }
354
355 // ------------------------------------------------------------------------
356 // ViewPart
357 // ------------------------------------------------------------------------
358
359 @Override
360 public void createPartControl(Composite parent) {
361 fTimeGraphCombo = new TimeGraphCombo(parent, SWT.NONE);
362
363 fTimeGraphCombo.setTreeContentProvider(new TreeContentProvider());
364
365 fTimeGraphCombo.setTreeLabelProvider(new TreeLabelProvider());
366
367 fTimeGraphCombo.setTimeGraphProvider(new ControlFlowPresentationProvider());
368
369 fTimeGraphCombo.setTreeColumns(COLUMN_NAMES);
370
371 fTimeGraphCombo.setFilterContentProvider(new TreeContentProvider());
372
373 fTimeGraphCombo.setFilterLabelProvider(new TreeLabelProvider());
374
375 fTimeGraphCombo.setFilterColumns(FILTER_COLUMN_NAMES);
376
377 fTimeGraphCombo.getTimeGraphViewer().addRangeListener(new ITimeGraphRangeListener() {
378 @Override
379 public void timeRangeUpdated(TimeGraphRangeUpdateEvent event) {
380 final long startTime = event.getStartTime();
381 final long endTime = event.getEndTime();
382 TmfTimeRange range = new TmfTimeRange(new CtfTmfTimestamp(startTime), new CtfTmfTimestamp(endTime));
383 TmfTimestamp time = new CtfTmfTimestamp(fTimeGraphCombo.getTimeGraphViewer().getSelectedTime());
384 broadcast(new TmfRangeSynchSignal(ControlFlowView.this, range, time));
385 if (fZoomThread != null) {
386 fZoomThread.cancel();
387 }
388 startZoomThread(startTime, endTime);
389 }
390 });
391
392 fTimeGraphCombo.getTimeGraphViewer().addTimeListener(new ITimeGraphTimeListener() {
393 @Override
394 public void timeSelected(TimeGraphTimeEvent event) {
395 long time = event.getTime();
396 broadcast(new TmfTimeSynchSignal(ControlFlowView.this, new CtfTmfTimestamp(time)));
397 }
398 });
399
400 fTimeGraphCombo.addSelectionListener(new ITimeGraphSelectionListener() {
401 @Override
402 public void selectionChanged(TimeGraphSelectionEvent event) {
403 //ITimeGraphEntry selection = event.getSelection();
404 }
405 });
406
407 fTimeGraphCombo.getTimeGraphViewer().setTimeFormat(TimeFormat.CALENDAR);
408
409 // View Action Handling
410 makeActions();
411 contributeToActionBars();
412
413 ITmfTrace trace = getActiveTrace();
414 if (trace != null) {
415 traceSelected(new TmfTraceSelectedSignal(this, trace));
416 }
417
418 // make selection available to other views
419 getSite().setSelectionProvider(fTimeGraphCombo.getTreeViewer());
420 }
421
422 @Override
423 public void setFocus() {
424 fTimeGraphCombo.setFocus();
425 }
426
427 // ------------------------------------------------------------------------
428 // Signal handlers
429 // ------------------------------------------------------------------------
430
431 /**
432 * Handler for the trace selected signal
433 *
434 * @param signal
435 * The signal that's received
436 */
437 @TmfSignalHandler
438 public void traceSelected(final TmfTraceSelectedSignal signal) {
439 if (signal.getTrace() == fTrace) {
440 return;
441 }
442 fTrace = signal.getTrace();
443
444 synchronized (fEntryListMap) {
445 fEntryList = fEntryListMap.get(fTrace);
446 if (fEntryList == null) {
447 synchronized (fBuildThreadMap) {
448 BuildThread buildThread = new BuildThread(fTrace);
449 fBuildThreadMap.put(fTrace, buildThread);
450 buildThread.start();
451 }
452 } else {
453 fStartTime = fTrace.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
454 fEndTime = fTrace.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
455 refresh();
456 }
457 }
458 }
459
460 /**
461 * Trace is closed: clear the data structures and the view
462 *
463 * @param signal the signal received
464 */
465 @TmfSignalHandler
466 public void traceClosed(final TmfTraceClosedSignal signal) {
467 synchronized (fBuildThreadMap) {
468 BuildThread buildThread = fBuildThreadMap.remove(signal.getTrace());
469 if (buildThread != null) {
470 buildThread.cancel();
471 }
472 }
473 synchronized (fEntryListMap) {
474 fEntryListMap.remove(signal.getTrace());
475 }
476 if (signal.getTrace() == fTrace) {
477 fTrace = null;
478 fStartTime = 0;
479 fEndTime = 0;
480 if (fZoomThread != null) {
481 fZoomThread.cancel();
482 }
483 refresh();
484 }
485 }
486
487 /**
488 * Handler for the synch signal
489 *
490 * @param signal
491 * The signal that's received
492 */
493 @TmfSignalHandler
494 public void synchToTime(final TmfTimeSynchSignal signal) {
495 if (signal.getSource() == this || fTrace == null) {
496 return;
497 }
498 final long time = signal.getCurrentTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
499
500 int thread = -1;
501 for (ITmfTrace trace : fTrace.getTraces()) {
502 if (thread > 0) {
503 break;
504 }
505 if (trace instanceof LttngKernelTrace) {
506 LttngKernelTrace ctfKernelTrace = (LttngKernelTrace) trace;
507 ITmfStateSystem ssq = ctfKernelTrace.getStateSystems().get(LttngKernelTrace.STATE_ID);
508 if (time >= ssq.getStartTime() && time <= ssq.getCurrentEndTime()) {
509 List<Integer> currentThreadQuarks = ssq.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$
510 for (int currentThreadQuark : currentThreadQuarks) {
511 try {
512 ITmfStateInterval currentThreadInterval = ssq.querySingleState(time, currentThreadQuark);
513 int currentThread = currentThreadInterval.getStateValue().unboxInt();
514 if (currentThread > 0) {
515 int statusQuark = ssq.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThread), Attributes.STATUS);
516 ITmfStateInterval statusInterval = ssq.querySingleState(time, statusQuark);
517 if (statusInterval.getStartTime() == time) {
518 thread = currentThread;
519 break;
520 }
521 }
522 } catch (AttributeNotFoundException e) {
523 e.printStackTrace();
524 } catch (TimeRangeException e) {
525 e.printStackTrace();
526 } catch (StateValueTypeException e) {
527 e.printStackTrace();
528 } catch (StateSystemDisposedException e) {
529 /* Ignored */
530 }
531 }
532 }
533 }
534 }
535 final int selectedThread = thread;
536
537 Display.getDefault().asyncExec(new Runnable() {
538 @Override
539 public void run() {
540 if (fTimeGraphCombo.isDisposed()) {
541 return;
542 }
543 fTimeGraphCombo.getTimeGraphViewer().setSelectedTime(time, true);
544 startZoomThread(fTimeGraphCombo.getTimeGraphViewer().getTime0(), fTimeGraphCombo.getTimeGraphViewer().getTime1());
545
546 if (selectedThread > 0) {
547 for (Object element : fTimeGraphCombo.getTimeGraphViewer().getExpandedElements()) {
548 if (element instanceof ControlFlowEntry) {
549 ControlFlowEntry entry = (ControlFlowEntry) element;
550 if (entry.getThreadId() == selectedThread) {
551 fTimeGraphCombo.setSelection(entry);
552 break;
553 }
554 }
555 }
556 }
557 }
558 });
559 }
560
561 /**
562 * Handler for the range sync signal
563 *
564 * @param signal
565 * The signal that's received
566 */
567 @TmfSignalHandler
568 public void synchToRange(final TmfRangeSynchSignal signal) {
569 if (signal.getSource() == this || fTrace == null) {
570 return;
571 }
572 if (signal.getCurrentRange().getIntersection(fTrace.getTimeRange()) == null) {
573 return;
574 }
575 final long startTime = signal.getCurrentRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
576 final long endTime = signal.getCurrentRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
577 final long time = signal.getCurrentTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
578 Display.getDefault().asyncExec(new Runnable() {
579 @Override
580 public void run() {
581 if (fTimeGraphCombo.isDisposed()) {
582 return;
583 }
584 fTimeGraphCombo.getTimeGraphViewer().setStartFinishTime(startTime, endTime);
585 fTimeGraphCombo.getTimeGraphViewer().setSelectedTime(time, false);
586 startZoomThread(startTime, endTime);
587 }
588 });
589 }
590
591 // ------------------------------------------------------------------------
592 // Internal
593 // ------------------------------------------------------------------------
594
595 private void buildEventList(final ITmfTrace trace, IProgressMonitor monitor) {
596 fStartTime = Long.MAX_VALUE;
597 fEndTime = Long.MIN_VALUE;
598 ArrayList<ControlFlowEntry> rootList = new ArrayList<ControlFlowEntry>();
599 for (ITmfTrace aTrace : trace.getTraces()) {
600 if (monitor.isCanceled()) {
601 return;
602 }
603 if (aTrace instanceof LttngKernelTrace) {
604 ArrayList<ControlFlowEntry> entryList = new ArrayList<ControlFlowEntry>();
605 LttngKernelTrace ctfKernelTrace = (LttngKernelTrace) aTrace;
606 ITmfStateSystem ssq = ctfKernelTrace.getStateSystems().get(LttngKernelTrace.STATE_ID);
607 if (!ssq.waitUntilBuilt()) {
608 return;
609 }
610 long start = ssq.getStartTime();
611 long end = ssq.getCurrentEndTime() + 1;
612 fStartTime = Math.min(fStartTime, start);
613 fEndTime = Math.max(fEndTime, end);
614 List<Integer> threadQuarks = ssq.getQuarks(Attributes.THREADS, "*"); //$NON-NLS-1$
615 for (int threadQuark : threadQuarks) {
616 if (monitor.isCanceled()) {
617 return;
618 }
619 String threadName = ssq.getAttributeName(threadQuark);
620 int threadId = -1;
621 try {
622 threadId = Integer.parseInt(threadName);
623 } catch (NumberFormatException e1) {
624 continue;
625 }
626 if (threadId == 0) { // ignore the swapper thread
627 continue;
628 }
629 int execNameQuark = -1;
630 try {
631 try {
632 execNameQuark = ssq.getQuarkRelative(threadQuark, Attributes.EXEC_NAME);
633 } catch (AttributeNotFoundException e) {
634 continue;
635 }
636 int ppidQuark = ssq.getQuarkRelative(threadQuark, Attributes.PPID);
637 List<ITmfStateInterval> execNameIntervals = ssq.queryHistoryRange(execNameQuark, start, end - 1); // use monitor when available in api
638 if (monitor.isCanceled()) {
639 return;
640 }
641 ControlFlowEntry entry = null;
642 for (ITmfStateInterval execNameInterval : execNameIntervals) {
643 if (monitor.isCanceled()) {
644 return;
645 }
646 if (!execNameInterval.getStateValue().isNull() &&
647 execNameInterval.getStateValue().getType() == ITmfStateValue.Type.STRING) {
648 String execName = execNameInterval.getStateValue().unboxStr();
649 long startTime = execNameInterval.getStartTime();
650 long endTime = execNameInterval.getEndTime() + 1;
651 int ppid = -1;
652 if (ppidQuark != -1) {
653 ITmfStateInterval ppidInterval = ssq.querySingleState(startTime, ppidQuark);
654 ppid = ppidInterval.getStateValue().unboxInt();
655 }
656 if (entry == null) {
657 entry = new ControlFlowEntry(threadQuark, ctfKernelTrace, execName, threadId, ppid, startTime, endTime);
658 entryList.add(entry);
659 } else {
660 // update the name of the entry to the latest execName
661 entry.setName(execName);
662 }
663 entry.addEvent(new TimeEvent(entry, startTime, endTime - startTime));
664 } else {
665 entry = null;
666 }
667 }
668 } catch (AttributeNotFoundException e) {
669 e.printStackTrace();
670 } catch (TimeRangeException e) {
671 e.printStackTrace();
672 } catch (StateValueTypeException e) {
673 e.printStackTrace();
674 } catch (StateSystemDisposedException e) {
675 /* Ignored */
676 }
677 }
678 buildTree(entryList, rootList);
679 }
680 Collections.sort(rootList, fControlFlowEntryComparator);
681 synchronized (fEntryListMap) {
682 fEntryListMap.put(trace, (ArrayList<ControlFlowEntry>) rootList.clone());
683 }
684 if (trace == fTrace) {
685 refresh();
686 }
687 }
688 for (ControlFlowEntry entry : rootList) {
689 if (monitor.isCanceled()) {
690 return;
691 }
692 buildStatusEvents(trace, entry, monitor);
693 }
694 }
695
696 private static void buildTree(ArrayList<ControlFlowEntry> entryList,
697 ArrayList<ControlFlowEntry> rootList) {
698 for (ControlFlowEntry entry : entryList) {
699 boolean root = true;
700 if (entry.getParentThreadId() > 0) {
701 for (ControlFlowEntry parent : entryList) {
702 if (parent.getThreadId() == entry.getParentThreadId() &&
703 entry.getStartTime() >= parent.getStartTime() &&
704 entry.getStartTime() <= parent.getEndTime()) {
705 parent.addChild(entry);
706 root = false;
707 break;
708 }
709 }
710 }
711 if (root) {
712 rootList.add(entry);
713 }
714 }
715 }
716
717 private void buildStatusEvents(ITmfTrace trace, ControlFlowEntry entry, IProgressMonitor monitor) {
718 ITmfStateSystem ssq = entry.getTrace().getStateSystems().get(LttngKernelTrace.STATE_ID);
719 long start = ssq.getStartTime();
720 long end = ssq.getCurrentEndTime() + 1;
721 long resolution = Math.max(1, (end - start) / fDisplayWidth);
722 List<ITimeEvent> eventList = getEventList(entry, entry.getStartTime(), entry.getEndTime(), resolution, monitor);
723 if (monitor.isCanceled()) {
724 return;
725 }
726 entry.setEventList(eventList);
727 if (trace == fTrace) {
728 redraw();
729 }
730 for (ITimeGraphEntry child : entry.getChildren()) {
731 if (monitor.isCanceled()) {
732 return;
733 }
734 buildStatusEvents(trace, (ControlFlowEntry) child, monitor);
735 }
736 }
737
738 private static List<ITimeEvent> getEventList(ControlFlowEntry entry,
739 long startTime, long endTime, long resolution,
740 IProgressMonitor monitor) {
741 final long realStart = Math.max(startTime, entry.getStartTime());
742 final long realEnd = Math.min(endTime, entry.getEndTime());
743 if (realEnd <= realStart) {
744 return null;
745 }
746 ITmfStateSystem ssq = entry.getTrace().getStateSystems().get(LttngKernelTrace.STATE_ID);
747 List<ITimeEvent> eventList = null;
748 try {
749 int statusQuark = ssq.getQuarkRelative(entry.getThreadQuark(), Attributes.STATUS);
750 List<ITmfStateInterval> statusIntervals = ssq.queryHistoryRange(statusQuark, realStart, realEnd - 1, resolution, monitor);
751 eventList = new ArrayList<ITimeEvent>(statusIntervals.size());
752 long lastEndTime = -1;
753 for (ITmfStateInterval statusInterval : statusIntervals) {
754 if (monitor.isCanceled()) {
755 return null;
756 }
757 long time = statusInterval.getStartTime();
758 long duration = statusInterval.getEndTime() - time + 1;
759 int status = -1;
760 try {
761 status = statusInterval.getStateValue().unboxInt();
762 } catch (StateValueTypeException e) {
763 e.printStackTrace();
764 }
765 if (lastEndTime != time && lastEndTime != -1) {
766 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
767 }
768 eventList.add(new ControlFlowEvent(entry, time, duration, status));
769 lastEndTime = time + duration;
770 }
771 } catch (AttributeNotFoundException e) {
772 e.printStackTrace();
773 } catch (TimeRangeException e) {
774 e.printStackTrace();
775 } catch (StateSystemDisposedException e) {
776 /* Ignored */
777 }
778 return eventList;
779 }
780
781 private void refresh() {
782 Display.getDefault().asyncExec(new Runnable() {
783 @Override
784 public void run() {
785 if (fTimeGraphCombo.isDisposed()) {
786 return;
787 }
788 ITimeGraphEntry[] entries = null;
789 synchronized (fEntryListMap) {
790 fEntryList = fEntryListMap.get(fTrace);
791 if (fEntryList == null) {
792 fEntryList = new ArrayList<ControlFlowEntry>();
793 }
794 entries = fEntryList.toArray(new ITimeGraphEntry[0]);
795 }
796 Arrays.sort(entries, fControlFlowEntryComparator);
797 fTimeGraphCombo.setInput(entries);
798 fTimeGraphCombo.getTimeGraphViewer().setTimeBounds(fStartTime, fEndTime);
799
800 long timestamp = fTrace == null ? 0 : fTrace.getCurrentTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
801 long startTime = fTrace == null ? 0 : fTrace.getCurrentRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
802 long endTime = fTrace == null ? 0 : fTrace.getCurrentRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
803 startTime = Math.max(startTime, fStartTime);
804 endTime = Math.min(endTime, fEndTime);
805 fTimeGraphCombo.getTimeGraphViewer().setSelectedTime(timestamp, false);
806 fTimeGraphCombo.getTimeGraphViewer().setStartFinishTime(startTime, endTime);
807
808 for (TreeColumn column : fTimeGraphCombo.getTreeViewer().getTree().getColumns()) {
809 column.pack();
810 }
811
812 startZoomThread(startTime, endTime);
813 }
814 });
815 }
816
817 private void redraw() {
818 synchronized (fSyncObj) {
819 if (fRedrawState == State.IDLE) {
820 fRedrawState = State.BUSY;
821 } else {
822 fRedrawState = State.PENDING;
823 return;
824 }
825 }
826 Display.getDefault().asyncExec(new Runnable() {
827 @Override
828 public void run() {
829 if (fTimeGraphCombo.isDisposed()) {
830 return;
831 }
832 fTimeGraphCombo.redraw();
833 fTimeGraphCombo.update();
834 synchronized (fSyncObj) {
835 if (fRedrawState == State.PENDING) {
836 fRedrawState = State.IDLE;
837 redraw();
838 } else {
839 fRedrawState = State.IDLE;
840 }
841 }
842 }
843 });
844 }
845
846 private void startZoomThread(long startTime, long endTime) {
847 if (fZoomThread != null) {
848 fZoomThread.cancel();
849 }
850 fZoomThread = new ZoomThread(fEntryList, startTime, endTime);
851 fZoomThread.start();
852 }
853
854 private void makeActions() {
855 fPreviousResourceAction = fTimeGraphCombo.getTimeGraphViewer().getPreviousItemAction();
856 fPreviousResourceAction.setText(Messages.ControlFlowView_previousProcessActionNameText);
857 fPreviousResourceAction.setToolTipText(Messages.ControlFlowView_previousProcessActionToolTipText);
858 fNextResourceAction = fTimeGraphCombo.getTimeGraphViewer().getNextItemAction();
859 fNextResourceAction.setText(Messages.ControlFlowView_nextProcessActionNameText);
860 fNextResourceAction.setToolTipText(Messages.ControlFlowView_nextProcessActionToolTipText);
861 }
862
863 private void contributeToActionBars() {
864 IActionBars bars = getViewSite().getActionBars();
865 fillLocalToolBar(bars.getToolBarManager());
866 }
867
868 private void fillLocalToolBar(IToolBarManager manager) {
869 manager.add(fTimeGraphCombo.getShowFilterAction());
870 manager.add(fTimeGraphCombo.getTimeGraphViewer().getShowLegendAction());
871 manager.add(new Separator());
872 manager.add(fTimeGraphCombo.getTimeGraphViewer().getResetScaleAction());
873 manager.add(fTimeGraphCombo.getTimeGraphViewer().getPreviousEventAction());
874 manager.add(fTimeGraphCombo.getTimeGraphViewer().getNextEventAction());
875 manager.add(fPreviousResourceAction);
876 manager.add(fNextResourceAction);
877 manager.add(fTimeGraphCombo.getTimeGraphViewer().getZoomInAction());
878 manager.add(fTimeGraphCombo.getTimeGraphViewer().getZoomOutAction());
879 manager.add(new Separator());
880 }
881 }
This page took 0.052133 seconds and 6 git commands to generate.