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