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