1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson, École Polytechnique de Montréal
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
10 * Patrick Tasse - Initial API and implementation
11 * Geneviève Bastien - Move code to provide base classes for time graph view
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.ui
.views
.controlflow
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Collections
;
18 import java
.util
.Comparator
;
19 import java
.util
.List
;
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
;
49 * The Control Flow view main object
52 public class ControlFlowView
extends AbstractTimeGraphView
{
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
61 public static final String ID
= "org.eclipse.linuxtools.lttng2.kernel.ui.views.controlflow"; //$NON-NLS-1$
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
;
69 private static final String
[] COLUMN_NAMES
= new String
[] {
77 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
89 public ControlFlowView() {
90 super(ID
, COLUMN_NAMES
, FILTER_COLUMN_NAMES
, new ControlFlowPresentationProvider());
91 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
92 setEntryComparator(new ControlFlowEntryComparator());
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());
102 manager
.add(getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section
));
103 super.fillLocalToolBar(manager
);
107 protected String
getNextText() {
108 return Messages
.ControlFlowView_nextProcessActionNameText
;
112 protected String
getNextTooltip() {
113 return Messages
.ControlFlowView_nextProcessActionToolTipText
;
117 protected String
getPrevText() {
118 return Messages
.ControlFlowView_previousProcessActionNameText
;
122 protected String
getPrevTooltip() {
123 return Messages
.ControlFlowView_previousProcessActionToolTipText
;
126 private static class ControlFlowEntryComparator
implements Comparator
<ITimeGraphEntry
> {
129 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
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());
138 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
141 result
= entry1
.getThreadId() < entry2
.getThreadId() ?
-1 : entry1
.getThreadId() > entry2
.getThreadId() ?
1 : 0;
146 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
157 protected static class ControlFlowTreeLabelProvider
extends TreeLabelProvider
{
160 public String
getColumnText(Object element
, int columnIndex
) {
161 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
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());
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();
176 return ""; //$NON-NLS-1$
181 // ------------------------------------------------------------------------
183 // ------------------------------------------------------------------------
186 protected void buildEventList(final ITmfTrace trace
, IProgressMonitor monitor
) {
187 setStartTime(Long
.MAX_VALUE
);
188 setEndTime(Long
.MIN_VALUE
);
190 ArrayList
<TimeGraphEntry
> rootList
= new ArrayList
<TimeGraphEntry
>();
191 for (ITmfTrace aTrace
: fTraceManager
.getActiveTraceSet()) {
192 if (monitor
.isCanceled()) {
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()) {
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()) {
211 String threadName
= ssq
.getAttributeName(threadQuark
);
214 threadId
= Integer
.parseInt(threadName
);
215 } catch (NumberFormatException e1
) {
218 if (threadId
== 0) { // ignore the swapper thread
221 int execNameQuark
= -1;
224 execNameQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.EXEC_NAME
);
225 } catch (AttributeNotFoundException e
) {
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()) {
234 TimeGraphEntry entry
= null;
235 for (ITmfStateInterval execNameInterval
: execNameIntervals
) {
236 if (monitor
.isCanceled()) {
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;
245 if (ppidQuark
!= -1) {
246 ITmfStateInterval ppidInterval
= ssq
.querySingleState(startTime
, ppidQuark
);
247 ppid
= ppidInterval
.getStateValue().unboxInt();
250 entry
= new ControlFlowEntry(threadQuark
, ctfKernelTrace
, execName
, threadId
, ppid
, startTime
, endTime
);
251 entryList
.add(entry
);
253 // update the name of the entry to the
255 entry
.setName(execName
);
257 entry
.addEvent(new TimeEvent(entry
, startTime
, endTime
- startTime
));
262 } catch (AttributeNotFoundException e
) {
264 } catch (TimeRangeException e
) {
266 } catch (StateValueTypeException e
) {
268 } catch (StateSystemDisposedException e
) {
272 buildTree(entryList
, rootList
);
274 Collections
.sort(rootList
, getEntryComparator());
275 putEntryList(trace
, (ArrayList
<TimeGraphEntry
>) rootList
.clone());
277 if (trace
.equals(getTrace())) {
281 for (TimeGraphEntry entry
: rootList
) {
282 if (monitor
.isCanceled()) {
285 buildStatusEvents(trace
, entry
, monitor
);
289 private static void buildTree(ArrayList
<TimeGraphEntry
> entryList
,
290 ArrayList
<TimeGraphEntry
> rootList
) {
291 for (TimeGraphEntry listentry
: entryList
) {
292 ControlFlowEntry entry
= (ControlFlowEntry
) listentry
;
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
);
312 private void buildStatusEvents(ITmfTrace trace
, TimeGraphEntry entry
, IProgressMonitor monitor
) {
313 ITmfStateSystem ssq
= entry
.getTrace().getStateSystems().get(LttngKernelTrace
.STATE_ID
);
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()) {
322 entry
.setEventList(eventList
);
323 if (trace
.equals(getTrace())) {
326 for (ITimeGraphEntry child
: entry
.getChildren()) {
327 if (monitor
.isCanceled()) {
330 buildStatusEvents(trace
, (TimeGraphEntry
) child
, monitor
);
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
)) {
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
) {
346 ITmfStateSystem ssq
= entry
.getTrace().getStateSystems().get(LttngKernelTrace
.STATE_ID
);
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()) {
356 long time
= statusInterval
.getStartTime();
357 long duration
= statusInterval
.getEndTime() - time
+ 1;
360 status
= statusInterval
.getStateValue().unboxInt();
361 } catch (StateValueTypeException e
) {
364 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
365 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
367 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
368 lastEndTime
= time
+ duration
;
370 } catch (AttributeNotFoundException e
) {
372 } catch (TimeRangeException e
) {
374 } catch (StateSystemDisposedException e
) {
381 * Returns a value corresponding to the selected entry.
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.
387 * The currently selected time
388 * @return a value identifying the entry
390 private int getSelectionValue(long time
) {
392 for (ITmfTrace trace
: fTraceManager
.getActiveTraceSet()) {
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
) {
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
;
413 } catch (AttributeNotFoundException e
) {
415 } catch (TimeRangeException e
) {
417 } catch (StateValueTypeException e
) {
419 } catch (StateSystemDisposedException e
) {
430 protected void synchingToTime(long time
) {
431 int selected
= getSelectionValue(time
);
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
);
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) {
453 for (ITmfTrace trace
: traces
) {
454 if (trace
instanceof LttngKernelTrace
) {
455 ITmfStateSystem ssq
= trace
.getStateSystems().get(LttngKernelTrace
.STATE_ID
);
457 long start
= Math
.max(startTime
, ssq
.getStartTime());
458 long end
= Math
.min(endTime
, ssq
.getCurrentEndTime());
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
);
467 for (ITmfStateInterval currentThreadInterval
: currentThreadIntervals
) {
468 if (monitor
.isCanceled()) {
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));
479 prevEnd
= currentThreadInterval
.getEndTime() + 1;
482 } catch (TimeRangeException e
) {
484 } catch (AttributeNotFoundException e
) {
486 } catch (StateValueTypeException e
) {
488 } catch (StateSystemDisposedException e
) {
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
;