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