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