tmf: Move timestamps to their own package
[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.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.timestamp.ITmfTimestamp;
47 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
48 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
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 // make selection available to other views
427 getSite().setSelectionProvider(fTimeGraphCombo.getTreeViewer());
428 }
429
430 /* (non-Javadoc)
431 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
432 */
433 @Override
434 public void setFocus() {
435 fTimeGraphCombo.setFocus();
436 }
437
438 // ------------------------------------------------------------------------
439 // Signal handlers
440 // ------------------------------------------------------------------------
441
442 /**
443 * Handler for the trace selected signal
444 *
445 * @param signal
446 * The signal that's received
447 */
448 @TmfSignalHandler
449 public void traceSelected(final TmfTraceSelectedSignal signal) {
450 if (signal.getTrace() == fTrace) {
451 return;
452 }
453 fTrace = signal.getTrace();
454
455 synchronized (fEntryListMap) {
456 fEntryList = fEntryListMap.get(fTrace);
457 if (fEntryList == null) {
458 synchronized (fBuildThreadMap) {
459 BuildThread buildThread = new BuildThread(fTrace);
460 fBuildThreadMap.put(fTrace, buildThread);
461 buildThread.start();
462 }
463 } else {
464 fStartTime = fTrace.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
465 fEndTime = fTrace.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
466 refresh();
467 }
468 }
469 }
470
471 /**
472 * Trace is closed: clear the data structures and the view
473 *
474 * @param signal the signal received
475 */
476 @TmfSignalHandler
477 public void traceClosed(final TmfTraceClosedSignal signal) {
478 synchronized (fBuildThreadMap) {
479 BuildThread buildThread = fBuildThreadMap.remove(signal.getTrace());
480 if (buildThread != null) {
481 buildThread.cancel();
482 }
483 }
484 synchronized (fEntryListMap) {
485 fEntryListMap.remove(signal.getTrace());
486 }
487 if (signal.getTrace() == fTrace) {
488 fTrace = null;
489 fStartTime = 0;
490 fEndTime = 0;
491 if (fZoomThread != null) {
492 fZoomThread.cancel();
493 }
494 refresh();
495 }
496 }
497
498 /**
499 * Handler for the synch signal
500 *
501 * @param signal
502 * The signal that's received
503 */
504 @TmfSignalHandler
505 public void synchToTime(final TmfTimeSynchSignal signal) {
506 if (signal.getSource() == this || fTrace == null) {
507 return;
508 }
509 final long time = signal.getCurrentTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
510
511 int thread = -1;
512 ITmfTrace[] traces;
513 if (fTrace instanceof TmfExperiment) {
514 TmfExperiment experiment = (TmfExperiment) fTrace;
515 traces = experiment.getTraces();
516 } else {
517 traces = new ITmfTrace[] { fTrace };
518 }
519 for (ITmfTrace trace : traces) {
520 if (thread > 0) {
521 break;
522 }
523 if (trace instanceof CtfKernelTrace) {
524 CtfKernelTrace ctfKernelTrace = (CtfKernelTrace) trace;
525 ITmfStateSystem ssq = ctfKernelTrace.getStateSystem(CtfKernelTrace.STATE_ID);
526 if (time >= ssq.getStartTime() && time <= ssq.getCurrentEndTime()) {
527 List<Integer> currentThreadQuarks = ssq.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$
528 for (int currentThreadQuark : currentThreadQuarks) {
529 try {
530 ITmfStateInterval currentThreadInterval = ssq.querySingleState(time, currentThreadQuark);
531 int currentThread = currentThreadInterval.getStateValue().unboxInt();
532 if (currentThread > 0) {
533 int statusQuark = ssq.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThread), Attributes.STATUS);
534 ITmfStateInterval statusInterval = ssq.querySingleState(time, statusQuark);
535 if (statusInterval.getStartTime() == time) {
536 thread = currentThread;
537 break;
538 }
539 }
540 } catch (AttributeNotFoundException e) {
541 e.printStackTrace();
542 } catch (TimeRangeException e) {
543 e.printStackTrace();
544 } catch (StateValueTypeException e) {
545 e.printStackTrace();
546 } catch (StateSystemDisposedException e) {
547 /* Ignored */
548 }
549 }
550 }
551 }
552 }
553 final int selectedThread = thread;
554
555 Display.getDefault().asyncExec(new Runnable() {
556 @Override
557 public void run() {
558 if (fTimeGraphCombo.isDisposed()) {
559 return;
560 }
561 fTimeGraphCombo.getTimeGraphViewer().setSelectedTime(time, true);
562 startZoomThread(fTimeGraphCombo.getTimeGraphViewer().getTime0(), fTimeGraphCombo.getTimeGraphViewer().getTime1());
563
564 if (selectedThread > 0) {
565 for (Object element : fTimeGraphCombo.getTimeGraphViewer().getExpandedElements()) {
566 if (element instanceof ControlFlowEntry) {
567 ControlFlowEntry entry = (ControlFlowEntry) element;
568 if (entry.getThreadId() == selectedThread) {
569 fTimeGraphCombo.setSelection(entry);
570 break;
571 }
572 }
573 }
574 }
575 }
576 });
577 }
578
579 /**
580 * Handler for the range sync signal
581 *
582 * @param signal
583 * The signal that's received
584 */
585 @TmfSignalHandler
586 public void synchToRange(final TmfRangeSynchSignal signal) {
587 if (signal.getSource() == this || fTrace == null) {
588 return;
589 }
590 if (signal.getCurrentRange().getIntersection(fTrace.getTimeRange()) == null) {
591 return;
592 }
593 final long startTime = signal.getCurrentRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
594 final long endTime = signal.getCurrentRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
595 final long time = signal.getCurrentTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
596 Display.getDefault().asyncExec(new Runnable() {
597 @Override
598 public void run() {
599 if (fTimeGraphCombo.isDisposed()) {
600 return;
601 }
602 fTimeGraphCombo.getTimeGraphViewer().setStartFinishTime(startTime, endTime);
603 fTimeGraphCombo.getTimeGraphViewer().setSelectedTime(time, false);
604 startZoomThread(startTime, endTime);
605 }
606 });
607 }
608
609 // ------------------------------------------------------------------------
610 // Internal
611 // ------------------------------------------------------------------------
612
613 private void buildEventList(final ITmfTrace trace, IProgressMonitor monitor) {
614 fStartTime = Long.MAX_VALUE;
615 fEndTime = Long.MIN_VALUE;
616 ITmfTrace[] traces;
617 if (trace instanceof TmfExperiment) {
618 TmfExperiment experiment = (TmfExperiment) trace;
619 traces = experiment.getTraces();
620 } else {
621 traces = new ITmfTrace[] { trace };
622 }
623 ArrayList<ControlFlowEntry> rootList = new ArrayList<ControlFlowEntry>();
624 for (ITmfTrace aTrace : traces) {
625 if (monitor.isCanceled()) {
626 return;
627 }
628 if (aTrace instanceof CtfKernelTrace) {
629 ArrayList<ControlFlowEntry> entryList = new ArrayList<ControlFlowEntry>();
630 CtfKernelTrace ctfKernelTrace = (CtfKernelTrace) aTrace;
631 ITmfStateSystem ssq = ctfKernelTrace.getStateSystem(CtfKernelTrace.STATE_ID);
632 if (!ssq.waitUntilBuilt()) {
633 return;
634 }
635 long start = ssq.getStartTime();
636 long end = ssq.getCurrentEndTime() + 1;
637 fStartTime = Math.min(fStartTime, start);
638 fEndTime = Math.max(fEndTime, end);
639 List<Integer> threadQuarks = ssq.getQuarks(Attributes.THREADS, "*"); //$NON-NLS-1$
640 for (int threadQuark : threadQuarks) {
641 if (monitor.isCanceled()) {
642 return;
643 }
644 String threadName = ssq.getAttributeName(threadQuark);
645 int threadId = -1;
646 try {
647 threadId = Integer.parseInt(threadName);
648 } catch (NumberFormatException e1) {
649 continue;
650 }
651 if (threadId == 0) { // ignore the swapper thread
652 continue;
653 }
654 int execNameQuark = -1;
655 try {
656 try {
657 execNameQuark = ssq.getQuarkRelative(threadQuark, Attributes.EXEC_NAME);
658 } catch (AttributeNotFoundException e) {
659 continue;
660 }
661 int ppidQuark = ssq.getQuarkRelative(threadQuark, Attributes.PPID);
662 List<ITmfStateInterval> execNameIntervals = ssq.queryHistoryRange(execNameQuark, start, end - 1); // use monitor when available in api
663 if (monitor.isCanceled()) {
664 return;
665 }
666 ControlFlowEntry entry = null;
667 for (ITmfStateInterval execNameInterval : execNameIntervals) {
668 if (monitor.isCanceled()) {
669 return;
670 }
671 if (!execNameInterval.getStateValue().isNull() && execNameInterval.getStateValue().getType() == 1) {
672 String execName = execNameInterval.getStateValue().unboxStr();
673 long startTime = execNameInterval.getStartTime();
674 long endTime = execNameInterval.getEndTime() + 1;
675 int ppid = -1;
676 if (ppidQuark != -1) {
677 ITmfStateInterval ppidInterval = ssq.querySingleState(startTime, ppidQuark);
678 ppid = ppidInterval.getStateValue().unboxInt();
679 }
680 if (entry == null) {
681 entry = new ControlFlowEntry(threadQuark, ctfKernelTrace, execName, threadId, ppid, startTime, endTime);
682 entryList.add(entry);
683 } else {
684 // update the name of the entry to the latest execName
685 entry.setName(execName);
686 }
687 entry.addEvent(new TimeEvent(entry, startTime, endTime - startTime));
688 } else {
689 entry = null;
690 }
691 }
692 } catch (AttributeNotFoundException e) {
693 e.printStackTrace();
694 } catch (TimeRangeException e) {
695 e.printStackTrace();
696 } catch (StateValueTypeException e) {
697 e.printStackTrace();
698 } catch (StateSystemDisposedException e) {
699 /* Ignored */
700 }
701 }
702 buildTree(entryList, rootList);
703 }
704 Collections.sort(rootList, fControlFlowEntryComparator);
705 synchronized (fEntryListMap) {
706 fEntryListMap.put(trace, (ArrayList<ControlFlowEntry>) rootList.clone());
707 }
708 if (trace == fTrace) {
709 refresh();
710 }
711 }
712 for (ControlFlowEntry entry : rootList) {
713 if (monitor.isCanceled()) {
714 return;
715 }
716 buildStatusEvents(trace, entry, monitor);
717 }
718 }
719
720 private static void buildTree(ArrayList<ControlFlowEntry> entryList,
721 ArrayList<ControlFlowEntry> rootList) {
722 for (ControlFlowEntry entry : entryList) {
723 boolean root = true;
724 if (entry.getParentThreadId() > 0) {
725 for (ControlFlowEntry parent : entryList) {
726 if (parent.getThreadId() == entry.getParentThreadId() &&
727 entry.getStartTime() >= parent.getStartTime() &&
728 entry.getStartTime() <= parent.getEndTime()) {
729 parent.addChild(entry);
730 root = false;
731 break;
732 }
733 }
734 }
735 if (root) {
736 rootList.add(entry);
737 }
738 }
739 }
740
741 private void buildStatusEvents(ITmfTrace trace, ControlFlowEntry entry, IProgressMonitor monitor) {
742 ITmfStateSystem ssq = entry.getTrace().getStateSystem(CtfKernelTrace.STATE_ID);
743 long start = ssq.getStartTime();
744 long end = ssq.getCurrentEndTime() + 1;
745 long resolution = Math.max(1, (end - start) / fDisplayWidth);
746 List<ITimeEvent> eventList = getEventList(entry, entry.getStartTime(), entry.getEndTime(), resolution, monitor);
747 if (monitor.isCanceled()) {
748 return;
749 }
750 entry.setEventList(eventList);
751 if (trace == fTrace) {
752 redraw();
753 }
754 for (ITimeGraphEntry child : entry.getChildren()) {
755 if (monitor.isCanceled()) {
756 return;
757 }
758 buildStatusEvents(trace, (ControlFlowEntry) child, monitor);
759 }
760 }
761
762 private static List<ITimeEvent> getEventList(ControlFlowEntry entry,
763 long startTime, long endTime, long resolution,
764 IProgressMonitor monitor) {
765 final long realStart = Math.max(startTime, entry.getStartTime());
766 final long realEnd = Math.min(endTime, entry.getEndTime());
767 if (realEnd <= realStart) {
768 return null;
769 }
770 ITmfStateSystem ssq = entry.getTrace().getStateSystem(CtfKernelTrace.STATE_ID);
771 List<ITimeEvent> eventList = null;
772 try {
773 int statusQuark = ssq.getQuarkRelative(entry.getThreadQuark(), Attributes.STATUS);
774 List<ITmfStateInterval> statusIntervals = ssq.queryHistoryRange(statusQuark, realStart, realEnd - 1, resolution, monitor);
775 eventList = new ArrayList<ITimeEvent>(statusIntervals.size());
776 long lastEndTime = -1;
777 for (ITmfStateInterval statusInterval : statusIntervals) {
778 if (monitor.isCanceled()) {
779 return null;
780 }
781 long time = statusInterval.getStartTime();
782 long duration = statusInterval.getEndTime() - time + 1;
783 int status = -1;
784 try {
785 status = statusInterval.getStateValue().unboxInt();
786 } catch (StateValueTypeException e) {
787 e.printStackTrace();
788 }
789 if (lastEndTime != time && lastEndTime != -1) {
790 eventList.add(new ControlFlowEvent(entry, lastEndTime, time - lastEndTime, 0));
791 }
792 eventList.add(new ControlFlowEvent(entry, time, duration, status));
793 lastEndTime = time + duration;
794 }
795 } catch (AttributeNotFoundException e) {
796 e.printStackTrace();
797 } catch (TimeRangeException e) {
798 e.printStackTrace();
799 } catch (StateSystemDisposedException e) {
800 /* Ignored */
801 }
802 return eventList;
803 }
804
805 private void refresh() {
806 Display.getDefault().asyncExec(new Runnable() {
807 @Override
808 public void run() {
809 if (fTimeGraphCombo.isDisposed()) {
810 return;
811 }
812 ITimeGraphEntry[] entries = null;
813 synchronized (fEntryListMap) {
814 fEntryList = fEntryListMap.get(fTrace);
815 if (fEntryList == null) {
816 fEntryList = new ArrayList<ControlFlowEntry>();
817 }
818 entries = fEntryList.toArray(new ITimeGraphEntry[0]);
819 }
820 Arrays.sort(entries, fControlFlowEntryComparator);
821 fTimeGraphCombo.setInput(entries);
822 fTimeGraphCombo.getTimeGraphViewer().setTimeBounds(fStartTime, fEndTime);
823
824 long timestamp = fTrace == null ? 0 : fTrace.getCurrentTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
825 long startTime = fTrace == null ? 0 : fTrace.getCurrentRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
826 long endTime = fTrace == null ? 0 : fTrace.getCurrentRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
827 startTime = Math.max(startTime, fStartTime);
828 endTime = Math.min(endTime, fEndTime);
829 fTimeGraphCombo.getTimeGraphViewer().setSelectedTime(timestamp, false);
830 fTimeGraphCombo.getTimeGraphViewer().setStartFinishTime(startTime, endTime);
831
832 for (TreeColumn column : fTimeGraphCombo.getTreeViewer().getTree().getColumns()) {
833 column.pack();
834 }
835
836 startZoomThread(startTime, endTime);
837 }
838 });
839 }
840
841 private void redraw() {
842 synchronized (fSyncObj) {
843 if (fRedrawState == State.IDLE) {
844 fRedrawState = State.BUSY;
845 } else {
846 fRedrawState = State.PENDING;
847 return;
848 }
849 }
850 Display.getDefault().asyncExec(new Runnable() {
851 @Override
852 public void run() {
853 if (fTimeGraphCombo.isDisposed()) {
854 return;
855 }
856 fTimeGraphCombo.redraw();
857 fTimeGraphCombo.update();
858 synchronized (fSyncObj) {
859 if (fRedrawState == State.PENDING) {
860 fRedrawState = State.IDLE;
861 redraw();
862 } else {
863 fRedrawState = State.IDLE;
864 }
865 }
866 }
867 });
868 }
869
870 private void startZoomThread(long startTime, long endTime) {
871 if (fZoomThread != null) {
872 fZoomThread.cancel();
873 }
874 fZoomThread = new ZoomThread(fEntryList, startTime, endTime);
875 fZoomThread.start();
876 }
877
878 private void makeActions() {
879 fPreviousResourceAction = fTimeGraphCombo.getTimeGraphViewer().getPreviousItemAction();
880 fPreviousResourceAction.setText(Messages.ControlFlowView_previousProcessActionNameText);
881 fPreviousResourceAction.setToolTipText(Messages.ControlFlowView_previousProcessActionToolTipText);
882 fNextResourceAction = fTimeGraphCombo.getTimeGraphViewer().getNextItemAction();
883 fNextResourceAction.setText(Messages.ControlFlowView_nextProcessActionNameText);
884 fNextResourceAction.setToolTipText(Messages.ControlFlowView_nextProcessActionToolTipText);
885 }
886
887 private void contributeToActionBars() {
888 IActionBars bars = getViewSite().getActionBars();
889 fillLocalToolBar(bars.getToolBarManager());
890 }
891
892 private void fillLocalToolBar(IToolBarManager manager) {
893 manager.add(fTimeGraphCombo.getShowFilterAction());
894 manager.add(fTimeGraphCombo.getTimeGraphViewer().getShowLegendAction());
895 manager.add(new Separator());
896 manager.add(fTimeGraphCombo.getTimeGraphViewer().getResetScaleAction());
897 manager.add(fTimeGraphCombo.getTimeGraphViewer().getPreviousEventAction());
898 manager.add(fTimeGraphCombo.getTimeGraphViewer().getNextEventAction());
899 manager.add(fPreviousResourceAction);
900 manager.add(fNextResourceAction);
901 manager.add(fTimeGraphCombo.getTimeGraphViewer().getZoomInAction());
902 manager.add(fTimeGraphCombo.getTimeGraphViewer().getZoomOutAction());
903 manager.add(new Separator());
904 }
905 }
This page took 0.068873 seconds and 6 git commands to generate.