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