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