lttng: Add arrows in Control Flow view
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.ui / src / org / eclipse / linuxtools / internal / lttng2 / kernel / ui / views / controlflow / ControlFlowView.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson, École Polytechnique de Montréal
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 * Geneviève Bastien - Move code to provide base classes for time graph view
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.controlflow;
15
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.Comparator;
19 import java.util.List;
20
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jface.action.IToolBarManager;
23 import org.eclipse.jface.dialogs.IDialogSettings;
24 import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
25 import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Activator;
26 import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages;
27 import org.eclipse.linuxtools.lttng2.kernel.core.trace.LttngKernelTrace;
28 import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
29 import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException;
30 import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
31 import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
32 import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
33 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
34 import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue;
35 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
36 import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
37 import org.eclipse.linuxtools.tmf.ui.views.timegraph.AbstractTimeGraphView;
38 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ILinkEvent;
39 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
40 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
41 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent;
42 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
43 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeLinkEvent;
44 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils;
45 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
46 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
47
48 /**
49 * The Control Flow view main object
50 *
51 */
52 public class ControlFlowView extends AbstractTimeGraphView {
53
54 // ------------------------------------------------------------------------
55 // Constants
56 // ------------------------------------------------------------------------
57
58 /**
59 * View ID.
60 */
61 public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.ui.views.controlflow"; //$NON-NLS-1$
62
63 private static final String PROCESS_COLUMN = Messages.ControlFlowView_processColumn;
64 private static final String TID_COLUMN = Messages.ControlFlowView_tidColumn;
65 private static final String PTID_COLUMN = Messages.ControlFlowView_ptidColumn;
66 private static final String BIRTH_TIME_COLUMN = Messages.ControlFlowView_birthTimeColumn;
67 private static final String TRACE_COLUMN = Messages.ControlFlowView_traceColumn;
68
69 private static final String[] COLUMN_NAMES = new String[] {
70 PROCESS_COLUMN,
71 TID_COLUMN,
72 PTID_COLUMN,
73 BIRTH_TIME_COLUMN,
74 TRACE_COLUMN
75 };
76
77 private static final String[] FILTER_COLUMN_NAMES = new String[] {
78 PROCESS_COLUMN,
79 TID_COLUMN
80 };
81
82 // ------------------------------------------------------------------------
83 // Constructors
84 // ------------------------------------------------------------------------
85
86 /**
87 * Constructor
88 */
89 public ControlFlowView() {
90 super(ID, COLUMN_NAMES, FILTER_COLUMN_NAMES, new ControlFlowPresentationProvider());
91 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
92 setEntryComparator(new ControlFlowEntryComparator());
93 }
94
95 @Override
96 protected void fillLocalToolBar(IToolBarManager manager) {
97 IDialogSettings settings = Activator.getDefault().getDialogSettings();
98 IDialogSettings section = settings.getSection(getClass().getName());
99 if (section == null) {
100 section = settings.addNewSection(getClass().getName());
101 }
102 manager.add(getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section));
103 super.fillLocalToolBar(manager);
104 }
105
106 @Override
107 protected String getNextText() {
108 return Messages.ControlFlowView_nextProcessActionNameText;
109 }
110
111 @Override
112 protected String getNextTooltip() {
113 return Messages.ControlFlowView_nextProcessActionToolTipText;
114 }
115
116 @Override
117 protected String getPrevText() {
118 return Messages.ControlFlowView_previousProcessActionNameText;
119 }
120
121 @Override
122 protected String getPrevTooltip() {
123 return Messages.ControlFlowView_previousProcessActionToolTipText;
124 }
125
126 private static class ControlFlowEntryComparator implements Comparator<ITimeGraphEntry> {
127
128 @Override
129 public int compare(ITimeGraphEntry o1, ITimeGraphEntry o2) {
130
131 int result = 0;
132
133 if ((o1 instanceof ControlFlowEntry) && (o2 instanceof ControlFlowEntry)) {
134 ControlFlowEntry entry1 = (ControlFlowEntry) o1;
135 ControlFlowEntry entry2 = (ControlFlowEntry) o2;
136 result = entry1.getTrace().getStartTime().compareTo(entry2.getTrace().getStartTime());
137 if (result == 0) {
138 result = entry1.getTrace().getName().compareTo(entry2.getTrace().getName());
139 }
140 if (result == 0) {
141 result = entry1.getThreadId() < entry2.getThreadId() ? -1 : entry1.getThreadId() > entry2.getThreadId() ? 1 : 0;
142 }
143 }
144
145 if (result == 0) {
146 result = o1.getStartTime() < o2.getStartTime() ? -1 : o1.getStartTime() > o2.getStartTime() ? 1 : 0;
147 }
148
149 return result;
150 }
151 }
152
153 /**
154 * @author gbastien
155 *
156 */
157 protected static class ControlFlowTreeLabelProvider extends TreeLabelProvider {
158
159 @Override
160 public String getColumnText(Object element, int columnIndex) {
161 ControlFlowEntry entry = (ControlFlowEntry) element;
162
163 if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_processColumn)) {
164 return entry.getName();
165 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_tidColumn)) {
166 return Integer.toString(entry.getThreadId());
167 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_ptidColumn)) {
168 if (entry.getParentThreadId() > 0) {
169 return Integer.toString(entry.getParentThreadId());
170 }
171 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_birthTimeColumn)) {
172 return Utils.formatTime(entry.getStartTime(), TimeFormat.CALENDAR, Resolution.NANOSEC);
173 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_traceColumn)) {
174 return entry.getTrace().getName();
175 }
176 return ""; //$NON-NLS-1$
177 }
178
179 }
180
181 // ------------------------------------------------------------------------
182 // Internal
183 // ------------------------------------------------------------------------
184
185 @Override
186 protected void buildEventList(final ITmfTrace trace, IProgressMonitor monitor) {
187 setStartTime(Long.MAX_VALUE);
188 setEndTime(Long.MIN_VALUE);
189
190 ArrayList<TimeGraphEntry> rootList = new ArrayList<TimeGraphEntry>();
191 for (ITmfTrace aTrace : fTraceManager.getActiveTraceSet()) {
192 if (monitor.isCanceled()) {
193 return;
194 }
195 if (aTrace instanceof LttngKernelTrace) {
196 ArrayList<TimeGraphEntry> entryList = new ArrayList<TimeGraphEntry>();
197 LttngKernelTrace ctfKernelTrace = (LttngKernelTrace) aTrace;
198 ITmfStateSystem ssq = ctfKernelTrace.getStateSystems().get(LttngKernelTrace.STATE_ID);
199 if (!ssq.waitUntilBuilt()) {
200 return;
201 }
202 long start = ssq.getStartTime();
203 long end = ssq.getCurrentEndTime() + 1;
204 setStartTime(Math.min(getStartTime(), start));
205 setEndTime(Math.max(getEndTime(), end));
206 List<Integer> threadQuarks = ssq.getQuarks(Attributes.THREADS, "*"); //$NON-NLS-1$
207 for (int threadQuark : threadQuarks) {
208 if (monitor.isCanceled()) {
209 return;
210 }
211 String threadName = ssq.getAttributeName(threadQuark);
212 int threadId = -1;
213 try {
214 threadId = Integer.parseInt(threadName);
215 } catch (NumberFormatException e1) {
216 continue;
217 }
218 if (threadId == 0) { // ignore the swapper thread
219 continue;
220 }
221 int execNameQuark = -1;
222 try {
223 try {
224 execNameQuark = ssq.getQuarkRelative(threadQuark, Attributes.EXEC_NAME);
225 } catch (AttributeNotFoundException e) {
226 continue;
227 }
228 int ppidQuark = ssq.getQuarkRelative(threadQuark, Attributes.PPID);
229 List<ITmfStateInterval> execNameIntervals = ssq.queryHistoryRange(execNameQuark, start, end - 1);
230 // use monitor when available in api
231 if (monitor.isCanceled()) {
232 return;
233 }
234 TimeGraphEntry entry = null;
235 for (ITmfStateInterval execNameInterval : execNameIntervals) {
236 if (monitor.isCanceled()) {
237 return;
238 }
239 if (!execNameInterval.getStateValue().isNull() &&
240 execNameInterval.getStateValue().getType() == ITmfStateValue.Type.STRING) {
241 String execName = execNameInterval.getStateValue().unboxStr();
242 long startTime = execNameInterval.getStartTime();
243 long endTime = execNameInterval.getEndTime() + 1;
244 int ppid = -1;
245 if (ppidQuark != -1) {
246 ITmfStateInterval ppidInterval = ssq.querySingleState(startTime, ppidQuark);
247 ppid = ppidInterval.getStateValue().unboxInt();
248 }
249 if (entry == null) {
250 entry = new ControlFlowEntry(threadQuark, ctfKernelTrace, execName, threadId, ppid, startTime, endTime);
251 entryList.add(entry);
252 } else {
253 // update the name of the entry to the
254 // latest execName
255 entry.setName(execName);
256 }
257 entry.addEvent(new TimeEvent(entry, startTime, endTime - startTime));
258 } else {
259 entry = null;
260 }
261 }
262 } catch (AttributeNotFoundException e) {
263 e.printStackTrace();
264 } catch (TimeRangeException e) {
265 e.printStackTrace();
266 } catch (StateValueTypeException e) {
267 e.printStackTrace();
268 } catch (StateSystemDisposedException e) {
269 /* Ignored */
270 }
271 }
272 buildTree(entryList, rootList);
273 }
274 Collections.sort(rootList, getEntryComparator());
275 putEntryList(trace, (ArrayList<TimeGraphEntry>) rootList.clone());
276
277 if (trace.equals(getTrace())) {
278 refresh();
279 }
280 }
281 for (TimeGraphEntry entry : rootList) {
282 if (monitor.isCanceled()) {
283 return;
284 }
285 buildStatusEvents(trace, entry, monitor);
286 }
287 }
288
289 private static void buildTree(ArrayList<TimeGraphEntry> entryList,
290 ArrayList<TimeGraphEntry> rootList) {
291 for (TimeGraphEntry listentry : entryList) {
292 ControlFlowEntry entry = (ControlFlowEntry) listentry;
293 boolean root = true;
294 if (entry.getParentThreadId() > 0) {
295 for (TimeGraphEntry parententry : entryList) {
296 ControlFlowEntry parent = (ControlFlowEntry) parententry;
297 if (parent.getThreadId() == entry.getParentThreadId() &&
298 entry.getStartTime() >= parent.getStartTime() &&
299 entry.getStartTime() <= parent.getEndTime()) {
300 parent.addChild(entry);
301 root = false;
302 break;
303 }
304 }
305 }
306 if (root) {
307 rootList.add(entry);
308 }
309 }
310 }
311
312 private void buildStatusEvents(ITmfTrace trace, TimeGraphEntry entry, IProgressMonitor monitor) {
313 ITmfStateSystem ssq = entry.getTrace().getStateSystems().get(LttngKernelTrace.STATE_ID);
314
315 long start = ssq.getStartTime();
316 long end = ssq.getCurrentEndTime() + 1;
317 long resolution = Math.max(1, (end - start) / getDisplayWidth());
318 List<ITimeEvent> eventList = getEventList(entry, entry.getStartTime(), entry.getEndTime(), resolution, monitor);
319 if (monitor.isCanceled()) {
320 return;
321 }
322 entry.setEventList(eventList);
323 if (trace.equals(getTrace())) {
324 redraw();
325 }
326 for (ITimeGraphEntry child : entry.getChildren()) {
327 if (monitor.isCanceled()) {
328 return;
329 }
330 buildStatusEvents(trace, (TimeGraphEntry) child, monitor);
331 }
332 }
333
334 @Override
335 protected List<ITimeEvent> getEventList(TimeGraphEntry tgentry, long startTime, long endTime, long resolution, IProgressMonitor monitor) {
336 List<ITimeEvent> eventList = null;
337 if (!(tgentry instanceof ControlFlowEntry)) {
338 return eventList;
339 }
340 ControlFlowEntry entry = (ControlFlowEntry) tgentry;
341 final long realStart = Math.max(startTime, entry.getStartTime());
342 final long realEnd = Math.min(endTime, entry.getEndTime());
343 if (realEnd <= realStart) {
344 return null;
345 }
346 ITmfStateSystem ssq = entry.getTrace().getStateSystems().get(LttngKernelTrace.STATE_ID);
347 try {
348 int statusQuark = ssq.getQuarkRelative(entry.getThreadQuark(), Attributes.STATUS);
349 List<ITmfStateInterval> statusIntervals = ssq.queryHistoryRange(statusQuark, realStart, realEnd - 1, resolution, monitor);
350 eventList = new ArrayList<ITimeEvent>(statusIntervals.size());
351 long lastEndTime = -1;
352 for (ITmfStateInterval statusInterval : statusIntervals) {
353 if (monitor.isCanceled()) {
354 return null;
355 }
356 long time = statusInterval.getStartTime();
357 long duration = statusInterval.getEndTime() - time + 1;
358 int status = -1;
359 try {
360 status = statusInterval.getStateValue().unboxInt();
361 } catch (StateValueTypeException e) {
362 e.printStackTrace();
363 }
364 if (lastEndTime != time && lastEndTime != -1) {
365 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
366 }
367 eventList.add(new TimeEvent(entry, time, duration, status));
368 lastEndTime = time + duration;
369 }
370 } catch (AttributeNotFoundException e) {
371 e.printStackTrace();
372 } catch (TimeRangeException e) {
373 e.printStackTrace();
374 } catch (StateSystemDisposedException e) {
375 /* Ignored */
376 }
377 return eventList;
378 }
379
380 /**
381 * Returns a value corresponding to the selected entry.
382 *
383 * Used in conjunction with selectEntry to change the selected entry. If one
384 * of these methods is overridden in child class, then both should be.
385 *
386 * @param time
387 * The currently selected time
388 * @return a value identifying the entry
389 */
390 private int getSelectionValue(long time) {
391 int thread = -1;
392 for (ITmfTrace trace : fTraceManager.getActiveTraceSet()) {
393 if (thread > 0) {
394 break;
395 }
396 if (trace instanceof LttngKernelTrace) {
397 LttngKernelTrace ctfKernelTrace = (LttngKernelTrace) trace;
398 ITmfStateSystem ssq = ctfKernelTrace.getStateSystems().get(LttngKernelTrace.STATE_ID);
399 if (time >= ssq.getStartTime() && time <= ssq.getCurrentEndTime()) {
400 List<Integer> currentThreadQuarks = ssq.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$
401 for (int currentThreadQuark : currentThreadQuarks) {
402 try {
403 ITmfStateInterval currentThreadInterval = ssq.querySingleState(time, currentThreadQuark);
404 int currentThread = currentThreadInterval.getStateValue().unboxInt();
405 if (currentThread > 0) {
406 int statusQuark = ssq.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThread), Attributes.STATUS);
407 ITmfStateInterval statusInterval = ssq.querySingleState(time, statusQuark);
408 if (statusInterval.getStartTime() == time) {
409 thread = currentThread;
410 break;
411 }
412 }
413 } catch (AttributeNotFoundException e) {
414 e.printStackTrace();
415 } catch (TimeRangeException e) {
416 e.printStackTrace();
417 } catch (StateValueTypeException e) {
418 e.printStackTrace();
419 } catch (StateSystemDisposedException e) {
420 /* Ignored */
421 }
422 }
423 }
424 }
425 }
426 return thread;
427 }
428
429 @Override
430 protected void synchingToTime(long time) {
431 int selected = getSelectionValue(time);
432 if (selected > 0) {
433 for (Object element : getTimeGraphCombo().getTimeGraphViewer().getExpandedElements()) {
434 if (element instanceof ControlFlowEntry) {
435 ControlFlowEntry entry = (ControlFlowEntry) element;
436 if (entry.getThreadId() == selected) {
437 getTimeGraphCombo().setSelection(entry);
438 break;
439 }
440 }
441 }
442 }
443 }
444
445 @Override
446 protected List<ILinkEvent> getLinkList(long startTime, long endTime, long resolution, IProgressMonitor monitor) {
447 List<ILinkEvent> list = new ArrayList<ILinkEvent>();
448 ITmfTrace[] traces = TmfTraceManager.getTraceSet(getTrace());
449 List<TimeGraphEntry> entryList = getEntryListMap().get(getTrace());
450 if (traces == null || entryList == null) {
451 return list;
452 }
453 for (ITmfTrace trace : traces) {
454 if (trace instanceof LttngKernelTrace) {
455 ITmfStateSystem ssq = trace.getStateSystems().get(LttngKernelTrace.STATE_ID);
456 try {
457 long start = Math.max(startTime, ssq.getStartTime());
458 long end = Math.min(endTime, ssq.getCurrentEndTime());
459 if (end < start) {
460 continue;
461 }
462 List<Integer> currentThreadQuarks = ssq.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$
463 for (int currentThreadQuark : currentThreadQuarks) {
464 List<ITmfStateInterval> currentThreadIntervals = ssq.queryHistoryRange(currentThreadQuark, start, end, resolution, monitor);
465 int prevThread = 0;
466 long prevEnd = 0;
467 for (ITmfStateInterval currentThreadInterval : currentThreadIntervals) {
468 if (monitor.isCanceled()) {
469 return null;
470 }
471 long time = currentThreadInterval.getStartTime();
472 int thread = currentThreadInterval.getStateValue().unboxInt();
473 if (thread > 0 && prevThread > 0 && thread != prevThread && time == prevEnd) {
474 ITimeGraphEntry prevEntry = findEntry(entryList, trace, prevThread);
475 ITimeGraphEntry nextEntry = findEntry(entryList, trace, thread);
476 list.add(new TimeLinkEvent(prevEntry, nextEntry, time, 0, 0));
477 }
478 prevThread = thread;
479 prevEnd = currentThreadInterval.getEndTime() + 1;
480 }
481 }
482 } catch (TimeRangeException e) {
483 e.printStackTrace();
484 } catch (AttributeNotFoundException e) {
485 e.printStackTrace();
486 } catch (StateValueTypeException e) {
487 e.printStackTrace();
488 } catch (StateSystemDisposedException e) {
489 /* Ignored */
490 }
491 }
492 }
493 return list;
494 }
495
496 private ControlFlowEntry findEntry(List<TimeGraphEntry> entryList, ITmfTrace trace, int threadId) {
497 for (TimeGraphEntry entry : entryList) {
498 if (entry instanceof ControlFlowEntry) {
499 ControlFlowEntry controlFlowEntry = (ControlFlowEntry) entry;
500 if (controlFlowEntry.getThreadId() == threadId && controlFlowEntry.getTrace() == trace) {
501 return controlFlowEntry;
502 } else if (entry.hasChildren()) {
503 controlFlowEntry = findEntry(entry.getChildren(), trace, threadId);
504 if (controlFlowEntry != null) {
505 return controlFlowEntry;
506 }
507 }
508 }
509 }
510 return null;
511 }
512 }
This page took 0.043154 seconds and 5 git commands to generate.