tmf: Split ITmfStateSystem.waitUntilBuilt() in separate methods
[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 == null) {
215 return;
216 }
217 ssq.waitUntilBuilt();
218 if (ssq.isCancelled()) {
219 return;
220 }
221 long start = ssq.getStartTime();
222 long end = ssq.getCurrentEndTime() + 1;
223 setStartTime(Math.min(getStartTime(), start));
224 setEndTime(Math.max(getEndTime(), end));
225 List<Integer> threadQuarks = ssq.getQuarks(Attributes.THREADS, "*"); //$NON-NLS-1$
226 for (int threadQuark : threadQuarks) {
227 if (monitor.isCanceled()) {
228 return;
229 }
230 String threadName = ssq.getAttributeName(threadQuark);
231 int threadId = -1;
232 try {
233 threadId = Integer.parseInt(threadName);
234 } catch (NumberFormatException e1) {
235 continue;
236 }
237 if (threadId == 0) { // ignore the swapper thread
238 continue;
239 }
240 int execNameQuark = -1;
241 try {
242 try {
243 execNameQuark = ssq.getQuarkRelative(threadQuark, Attributes.EXEC_NAME);
244 } catch (AttributeNotFoundException e) {
245 continue;
246 }
247 int ppidQuark = ssq.getQuarkRelative(threadQuark, Attributes.PPID);
248 List<ITmfStateInterval> execNameIntervals = ssq.queryHistoryRange(execNameQuark, start, end - 1);
249 // use monitor when available in api
250 if (monitor.isCanceled()) {
251 return;
252 }
253 ControlFlowEntry entry = null;
254 for (ITmfStateInterval execNameInterval : execNameIntervals) {
255 if (monitor.isCanceled()) {
256 return;
257 }
258 if (!execNameInterval.getStateValue().isNull() &&
259 execNameInterval.getStateValue().getType() == ITmfStateValue.Type.STRING) {
260 String execName = execNameInterval.getStateValue().unboxStr();
261 long startTime = execNameInterval.getStartTime();
262 long endTime = execNameInterval.getEndTime() + 1;
263 int ppid = -1;
264 if (ppidQuark != -1) {
265 ITmfStateInterval ppidInterval = ssq.querySingleState(startTime, ppidQuark);
266 ppid = ppidInterval.getStateValue().unboxInt();
267 }
268 if (entry == null) {
269 entry = new ControlFlowEntry(threadQuark, ctfKernelTrace, execName, threadId, ppid, startTime, endTime);
270 entryList.add(entry);
271 } else {
272 // update the name of the entry to the
273 // latest execName
274 entry.setName(execName);
275 }
276 entry.addEvent(new TimeEvent(entry, startTime, endTime - startTime));
277 } else {
278 entry = null;
279 }
280 }
281 } catch (AttributeNotFoundException e) {
282 e.printStackTrace();
283 } catch (TimeRangeException e) {
284 e.printStackTrace();
285 } catch (StateValueTypeException e) {
286 e.printStackTrace();
287 } catch (StateSystemDisposedException e) {
288 /* Ignored */
289 }
290 }
291 buildTree(entryList, rootList);
292 }
293 Collections.sort(rootList, getEntryComparator());
294 putEntryList(trace, new ArrayList<TimeGraphEntry>(rootList));
295
296 if (trace.equals(getTrace())) {
297 refresh();
298 }
299 }
300 for (ControlFlowEntry entry : rootList) {
301 if (monitor.isCanceled()) {
302 return;
303 }
304 buildStatusEvents(entry.getTrace(), entry, monitor);
305 }
306 }
307
308 private static void buildTree(ArrayList<ControlFlowEntry> entryList,
309 ArrayList<ControlFlowEntry> rootList) {
310 for (ControlFlowEntry entry : entryList) {
311 boolean root = true;
312 if (entry.getParentThreadId() > 0) {
313 for (ControlFlowEntry parent : entryList) {
314 if (parent.getThreadId() == entry.getParentThreadId() &&
315 entry.getStartTime() >= parent.getStartTime() &&
316 entry.getStartTime() <= parent.getEndTime()) {
317 parent.addChild(entry);
318 root = false;
319 break;
320 }
321 }
322 }
323 if (root) {
324 rootList.add(entry);
325 }
326 }
327 }
328
329 private void buildStatusEvents(ITmfTrace trace, ControlFlowEntry entry, IProgressMonitor monitor) {
330 ITmfStateSystem ssq = entry.getTrace().getStateSystems().get(LttngKernelTrace.STATE_ID);
331
332 long start = ssq.getStartTime();
333 long end = ssq.getCurrentEndTime() + 1;
334 long resolution = Math.max(1, (end - start) / getDisplayWidth());
335 List<ITimeEvent> eventList = getEventList(entry, entry.getStartTime(), entry.getEndTime(), resolution, monitor);
336 if (monitor.isCanceled()) {
337 return;
338 }
339 entry.setEventList(eventList);
340 if (trace.equals(getTrace())) {
341 redraw();
342 }
343 for (ITimeGraphEntry child : entry.getChildren()) {
344 if (monitor.isCanceled()) {
345 return;
346 }
347 buildStatusEvents(trace, (ControlFlowEntry) child, monitor);
348 }
349 }
350
351 @Override
352 protected List<ITimeEvent> getEventList(TimeGraphEntry tgentry, long startTime, long endTime, long resolution, IProgressMonitor monitor) {
353 List<ITimeEvent> eventList = null;
354 if (!(tgentry instanceof ControlFlowEntry)) {
355 return eventList;
356 }
357 ControlFlowEntry entry = (ControlFlowEntry) tgentry;
358 final long realStart = Math.max(startTime, entry.getStartTime());
359 final long realEnd = Math.min(endTime, entry.getEndTime());
360 if (realEnd <= realStart) {
361 return null;
362 }
363 ITmfStateSystem ssq = entry.getTrace().getStateSystems().get(LttngKernelTrace.STATE_ID);
364 try {
365 int statusQuark = ssq.getQuarkRelative(entry.getThreadQuark(), Attributes.STATUS);
366 List<ITmfStateInterval> statusIntervals = ssq.queryHistoryRange(statusQuark, realStart, realEnd - 1, resolution, monitor);
367 eventList = new ArrayList<>(statusIntervals.size());
368 long lastEndTime = -1;
369 for (ITmfStateInterval statusInterval : statusIntervals) {
370 if (monitor.isCanceled()) {
371 return null;
372 }
373 long time = statusInterval.getStartTime();
374 long duration = statusInterval.getEndTime() - time + 1;
375 int status = -1;
376 try {
377 status = statusInterval.getStateValue().unboxInt();
378 } catch (StateValueTypeException e) {
379 e.printStackTrace();
380 }
381 if (lastEndTime != time && lastEndTime != -1) {
382 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
383 }
384 eventList.add(new TimeEvent(entry, time, duration, status));
385 lastEndTime = time + duration;
386 }
387 } catch (AttributeNotFoundException e) {
388 e.printStackTrace();
389 } catch (TimeRangeException e) {
390 e.printStackTrace();
391 } catch (StateSystemDisposedException e) {
392 /* Ignored */
393 }
394 return eventList;
395 }
396
397 /**
398 * Returns a value corresponding to the selected entry.
399 *
400 * Used in conjunction with selectEntry to change the selected entry. If one
401 * of these methods is overridden in child class, then both should be.
402 *
403 * @param time
404 * The currently selected time
405 * @return a value identifying the entry
406 */
407 private int getSelectionValue(long time) {
408 int thread = -1;
409 for (ITmfTrace trace : fTraceManager.getActiveTraceSet()) {
410 if (thread > 0) {
411 break;
412 }
413 if (trace instanceof LttngKernelTrace) {
414 LttngKernelTrace ctfKernelTrace = (LttngKernelTrace) trace;
415 ITmfStateSystem ssq = ctfKernelTrace.getStateSystems().get(LttngKernelTrace.STATE_ID);
416 if (time >= ssq.getStartTime() && time <= ssq.getCurrentEndTime()) {
417 List<Integer> currentThreadQuarks = ssq.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$
418 for (int currentThreadQuark : currentThreadQuarks) {
419 try {
420 ITmfStateInterval currentThreadInterval = ssq.querySingleState(time, currentThreadQuark);
421 int currentThread = currentThreadInterval.getStateValue().unboxInt();
422 if (currentThread > 0) {
423 int statusQuark = ssq.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThread), Attributes.STATUS);
424 ITmfStateInterval statusInterval = ssq.querySingleState(time, statusQuark);
425 if (statusInterval.getStartTime() == time) {
426 thread = currentThread;
427 break;
428 }
429 }
430 } catch (AttributeNotFoundException e) {
431 e.printStackTrace();
432 } catch (TimeRangeException e) {
433 e.printStackTrace();
434 } catch (StateValueTypeException e) {
435 e.printStackTrace();
436 } catch (StateSystemDisposedException e) {
437 /* Ignored */
438 }
439 }
440 }
441 }
442 }
443 return thread;
444 }
445
446 @Override
447 protected void synchingToTime(long time) {
448 int selected = getSelectionValue(time);
449 if (selected > 0) {
450 for (Object element : getTimeGraphViewer().getExpandedElements()) {
451 if (element instanceof ControlFlowEntry) {
452 ControlFlowEntry entry = (ControlFlowEntry) element;
453 if (entry.getThreadId() == selected) {
454 getTimeGraphCombo().setSelection(entry);
455 break;
456 }
457 }
458 }
459 }
460 }
461
462 @Override
463 protected List<ILinkEvent> getLinkList(long startTime, long endTime, long resolution, IProgressMonitor monitor) {
464 List<ILinkEvent> list = new ArrayList<>();
465 ITmfTrace[] traces = TmfTraceManager.getTraceSet(getTrace());
466 List<TimeGraphEntry> entryList = getEntryListMap().get(getTrace());
467 if (traces == null || entryList == null) {
468 return list;
469 }
470 for (ITmfTrace trace : traces) {
471 if (trace instanceof LttngKernelTrace) {
472 ITmfStateSystem ssq = trace.getStateSystems().get(LttngKernelTrace.STATE_ID);
473 try {
474 long start = Math.max(startTime, ssq.getStartTime());
475 long end = Math.min(endTime, ssq.getCurrentEndTime());
476 if (end < start) {
477 continue;
478 }
479 List<Integer> currentThreadQuarks = ssq.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$
480 for (int currentThreadQuark : currentThreadQuarks) {
481 // adjust the query range to include the previous and following intervals
482 long qstart = Math.max(ssq.querySingleState(start, currentThreadQuark).getStartTime() - 1, ssq.getStartTime());
483 long qend = Math.min(ssq.querySingleState(end, currentThreadQuark).getEndTime() + 1, ssq.getCurrentEndTime());
484 List<ITmfStateInterval> currentThreadIntervals = ssq.queryHistoryRange(currentThreadQuark, qstart, qend, resolution, monitor);
485 int prevThread = 0;
486 long prevEnd = 0;
487 long lastEnd = 0;
488 for (ITmfStateInterval currentThreadInterval : currentThreadIntervals) {
489 if (monitor.isCanceled()) {
490 return null;
491 }
492 long time = currentThreadInterval.getStartTime();
493 if (time != lastEnd) {
494 // don't create links where there are gaps in intervals due to the resolution
495 prevThread = 0;
496 prevEnd = 0;
497 }
498 int thread = currentThreadInterval.getStateValue().unboxInt();
499 if (thread > 0 && prevThread > 0) {
500 ITimeGraphEntry prevEntry = findEntry(entryList, trace, prevThread);
501 ITimeGraphEntry nextEntry = findEntry(entryList, trace, thread);
502 list.add(new TimeLinkEvent(prevEntry, nextEntry, prevEnd, time - prevEnd, 0));
503 }
504 lastEnd = currentThreadInterval.getEndTime() + 1;
505 if (thread != 0) {
506 prevThread = thread;
507 prevEnd = lastEnd;
508 }
509 }
510 }
511 } catch (TimeRangeException e) {
512 e.printStackTrace();
513 } catch (AttributeNotFoundException e) {
514 e.printStackTrace();
515 } catch (StateValueTypeException e) {
516 e.printStackTrace();
517 } catch (StateSystemDisposedException e) {
518 /* Ignored */
519 }
520 }
521 }
522 return list;
523 }
524
525 private ControlFlowEntry findEntry(List<TimeGraphEntry> entryList, ITmfTrace trace, int threadId) {
526 for (TimeGraphEntry entry : entryList) {
527 if (entry instanceof ControlFlowEntry) {
528 ControlFlowEntry controlFlowEntry = (ControlFlowEntry) entry;
529 if (controlFlowEntry.getThreadId() == threadId && controlFlowEntry.getTrace() == trace) {
530 return controlFlowEntry;
531 } else if (entry.hasChildren()) {
532 controlFlowEntry = findEntry(entry.getChildren(), trace, threadId);
533 if (controlFlowEntry != null) {
534 return controlFlowEntry;
535 }
536 }
537 }
538 }
539 return null;
540 }
541 }
This page took 0.04311 seconds and 5 git commands to generate.