Commit | Line | Data |
---|---|---|
6151d86c | 1 | /******************************************************************************* |
b97d61f0 | 2 | * Copyright (c) 2012, 2015 Ericsson, École Polytechnique de Montréal and others. |
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 |
b97d61f0 | 12 | * Christian Mansky - Add check active / uncheck inactive buttons |
6151d86c PT |
13 | *******************************************************************************/ |
14 | ||
e363eae1 AM |
15 | package org.eclipse.tracecompass.analysis.os.linux.ui.views.controlflow; |
16 | ||
6151d86c | 17 | import java.util.ArrayList; |
6151d86c | 18 | import java.util.Comparator; |
1cf25311 | 19 | import java.util.HashMap; |
6151d86c | 20 | import java.util.List; |
1cf25311 | 21 | import java.util.Map; |
6151d86c PT |
22 | |
23 | import org.eclipse.core.runtime.IProgressMonitor; | |
dfa0ef96 | 24 | import org.eclipse.jdt.annotation.NonNull; |
d2120fb6 | 25 | import org.eclipse.jdt.annotation.Nullable; |
03ab8eeb | 26 | import org.eclipse.jface.action.IAction; |
79ec0b89 | 27 | import org.eclipse.jface.action.IToolBarManager; |
9620ac26 | 28 | import org.eclipse.jface.action.MenuManager; |
79ec0b89 | 29 | import org.eclipse.jface.dialogs.IDialogSettings; |
9620ac26 MK |
30 | import org.eclipse.jface.viewers.TreeViewer; |
31 | import org.eclipse.swt.events.MenuDetectEvent; | |
32 | import org.eclipse.swt.events.MenuDetectListener; | |
33 | import org.eclipse.swt.graphics.Point; | |
b97d61f0 | 34 | import org.eclipse.swt.widgets.Composite; |
9620ac26 MK |
35 | import org.eclipse.swt.widgets.Control; |
36 | import org.eclipse.swt.widgets.Menu; | |
37 | import org.eclipse.swt.widgets.TreeItem; | |
0f7a12d3 AM |
38 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.Attributes; |
39 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule; | |
e363eae1 AM |
40 | import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator; |
41 | import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Messages; | |
9620ac26 | 42 | import org.eclipse.tracecompass.internal.analysis.os.linux.ui.actions.FollowThreadAction; |
a4cddcbc | 43 | import org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow.ControlFlowColumnComparators; |
e894a508 AM |
44 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
45 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; | |
46 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException; | |
47 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; | |
48 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; | |
49 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; | |
50 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; | |
2bdf0193 AM |
51 | import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule; |
52 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
53 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; | |
8321a699 | 54 | import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractStateSystemTimeGraphView; |
9620ac26 | 55 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphCombo; |
2bdf0193 AM |
56 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent; |
57 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent; | |
58 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; | |
8321a699 | 59 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent; |
2bdf0193 AM |
60 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent; |
61 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry; | |
62 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeLinkEvent; | |
63 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils; | |
64 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution; | |
65 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat; | |
6151d86c | 66 | |
a4cddcbc BH |
67 | import com.google.common.collect.ImmutableList; |
68 | ||
6151d86c PT |
69 | /** |
70 | * The Control Flow view main object | |
71 | * | |
72 | */ | |
8321a699 | 73 | public class ControlFlowView extends AbstractStateSystemTimeGraphView { |
6151d86c PT |
74 | |
75 | // ------------------------------------------------------------------------ | |
76 | // Constants | |
77 | // ------------------------------------------------------------------------ | |
6151d86c PT |
78 | /** |
79 | * View ID. | |
80 | */ | |
e363eae1 | 81 | public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.views.controlflow"; //$NON-NLS-1$ |
6151d86c | 82 | |
4999a196 GB |
83 | private static final String PROCESS_COLUMN = Messages.ControlFlowView_processColumn; |
84 | private static final String TID_COLUMN = Messages.ControlFlowView_tidColumn; | |
85 | private static final String PTID_COLUMN = Messages.ControlFlowView_ptidColumn; | |
6151d86c | 86 | private static final String BIRTH_TIME_COLUMN = Messages.ControlFlowView_birthTimeColumn; |
4999a196 | 87 | private static final String TRACE_COLUMN = Messages.ControlFlowView_traceColumn; |
6151d86c | 88 | |
4999a196 | 89 | private static final String[] COLUMN_NAMES = new String[] { |
6151d86c PT |
90 | PROCESS_COLUMN, |
91 | TID_COLUMN, | |
92 | PTID_COLUMN, | |
93 | BIRTH_TIME_COLUMN, | |
94 | TRACE_COLUMN | |
95 | }; | |
96 | ||
4999a196 | 97 | private static final String[] FILTER_COLUMN_NAMES = new String[] { |
6ac5a950 AM |
98 | PROCESS_COLUMN, |
99 | TID_COLUMN | |
100 | }; | |
101 | ||
aae89862 PT |
102 | // Timeout between updates in the build thread in ms |
103 | private static final long BUILD_UPDATE_TIMEOUT = 500; | |
104 | ||
a4cddcbc BH |
105 | private static final Comparator<ITimeGraphEntry>[] COLUMN_COMPARATORS; |
106 | ||
a14d7bd5 BH |
107 | private static final int INITIAL_SORT_COLUMN_INDEX = 3; |
108 | ||
a4cddcbc BH |
109 | static { |
110 | ImmutableList.Builder<Comparator<ITimeGraphEntry>> builder = ImmutableList.builder(); | |
111 | builder.add(ControlFlowColumnComparators.PROCESS_NAME_COLUMN_COMPARATOR) | |
9620ac26 MK |
112 | .add(ControlFlowColumnComparators.TID_COLUMN_COMPARATOR) |
113 | .add(ControlFlowColumnComparators.PTID_COLUMN_COMPARATOR) | |
114 | .add(ControlFlowColumnComparators.BIRTH_TIME_COLUMN_COMPARATOR) | |
115 | .add(ControlFlowColumnComparators.TRACE_COLUMN_COMPARATOR); | |
a4cddcbc BH |
116 | List<Comparator<ITimeGraphEntry>> l = builder.build(); |
117 | COLUMN_COMPARATORS = l.toArray(new Comparator[l.size()]); | |
118 | } | |
119 | ||
9620ac26 MK |
120 | private MenuManager fMenuMgr; |
121 | ||
6151d86c | 122 | // ------------------------------------------------------------------------ |
4999a196 | 123 | // Constructors |
6151d86c PT |
124 | // ------------------------------------------------------------------------ |
125 | ||
4999a196 GB |
126 | /** |
127 | * Constructor | |
128 | */ | |
129 | public ControlFlowView() { | |
747adf5c | 130 | super(ID, new ControlFlowPresentationProvider()); |
a14d7bd5 | 131 | setTreeColumns(COLUMN_NAMES, COLUMN_COMPARATORS, INITIAL_SORT_COLUMN_INDEX); |
4999a196 | 132 | setTreeLabelProvider(new ControlFlowTreeLabelProvider()); |
747adf5c | 133 | setFilterColumns(FILTER_COLUMN_NAMES); |
a03b7ee4 | 134 | setFilterLabelProvider(new ControlFlowFilterLabelProvider()); |
a4cddcbc | 135 | setEntryComparator(ControlFlowColumnComparators.BIRTH_TIME_COLUMN_COMPARATOR); |
6151d86c PT |
136 | } |
137 | ||
b97d61f0 CM |
138 | @Override |
139 | public void createPartControl(Composite parent) { | |
140 | super.createPartControl(parent); | |
141 | // add "Check active" Button to TimeGraphFilterDialog | |
142 | super.getTimeGraphCombo().addTimeGraphFilterCheckActiveButton( | |
143 | new ControlFlowCheckActiveProvider(Messages.ControlFlowView_checkActiveLabel, Messages.ControlFlowView_checkActiveToolTip)); | |
144 | // add "Uncheck inactive" Button to TimeGraphFilterDialog | |
145 | super.getTimeGraphCombo().addTimeGraphFilterUncheckInactiveButton( | |
146 | new ControlFlowCheckActiveProvider(Messages.ControlFlowView_uncheckInactiveLabel, Messages.ControlFlowView_uncheckInactiveToolTip)); | |
9620ac26 MK |
147 | createContextMenu(); |
148 | } | |
149 | ||
150 | private void createContextMenu() { | |
151 | fMenuMgr = new MenuManager(); | |
152 | final TimeGraphCombo timeGraphCombo = getTimeGraphCombo(); | |
153 | TreeViewer treeViewer = timeGraphCombo.getTreeViewer(); | |
154 | Control control = treeViewer.getControl(); | |
155 | Menu menu = fMenuMgr.createContextMenu(control); | |
156 | control.setMenu(menu); | |
157 | control.addMenuDetectListener(new MenuDetectListener() { | |
158 | @Override | |
159 | public void menuDetected(MenuDetectEvent event) { | |
160 | fMenuMgr.removeAll(); | |
161 | Point point = control.toControl(event.x, event.y); | |
162 | // this is super important, it makes zoom still work. Do not try | |
163 | // to extend to the time graph area. | |
164 | TreeItem item = treeViewer.getTree().getItem(point); | |
165 | ||
166 | if (item.getData() instanceof ControlFlowEntry) { | |
167 | ControlFlowEntry entry = (ControlFlowEntry) item.getData(); | |
168 | fMenuMgr.add(new FollowThreadAction(ControlFlowView.this, entry.getName(), entry.getThreadId(), entry.getTrace())); | |
169 | } | |
170 | } | |
171 | }); | |
172 | ||
b97d61f0 CM |
173 | } |
174 | ||
79ec0b89 PT |
175 | @Override |
176 | protected void fillLocalToolBar(IToolBarManager manager) { | |
086f21ae | 177 | super.fillLocalToolBar(manager); |
79ec0b89 PT |
178 | IDialogSettings settings = Activator.getDefault().getDialogSettings(); |
179 | IDialogSettings section = settings.getSection(getClass().getName()); | |
180 | if (section == null) { | |
181 | section = settings.addNewSection(getClass().getName()); | |
182 | } | |
03ab8eeb PT |
183 | |
184 | IAction hideArrowsAction = getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section); | |
185 | manager.add(hideArrowsAction); | |
186 | ||
187 | IAction followArrowBwdAction = getTimeGraphCombo().getTimeGraphViewer().getFollowArrowBwdAction(); | |
188 | followArrowBwdAction.setText(Messages.ControlFlowView_followCPUBwdText); | |
189 | followArrowBwdAction.setToolTipText(Messages.ControlFlowView_followCPUBwdText); | |
190 | manager.add(followArrowBwdAction); | |
191 | ||
192 | IAction followArrowFwdAction = getTimeGraphCombo().getTimeGraphViewer().getFollowArrowFwdAction(); | |
193 | followArrowFwdAction.setText(Messages.ControlFlowView_followCPUFwdText); | |
194 | followArrowFwdAction.setToolTipText(Messages.ControlFlowView_followCPUFwdText); | |
195 | manager.add(followArrowFwdAction); | |
79ec0b89 PT |
196 | } |
197 | ||
4999a196 GB |
198 | @Override |
199 | protected String getNextText() { | |
200 | return Messages.ControlFlowView_nextProcessActionNameText; | |
201 | } | |
6151d86c | 202 | |
4999a196 GB |
203 | @Override |
204 | protected String getNextTooltip() { | |
205 | return Messages.ControlFlowView_nextProcessActionToolTipText; | |
206 | } | |
6151d86c | 207 | |
4999a196 GB |
208 | @Override |
209 | protected String getPrevText() { | |
210 | return Messages.ControlFlowView_previousProcessActionNameText; | |
211 | } | |
6151d86c | 212 | |
4999a196 GB |
213 | @Override |
214 | protected String getPrevTooltip() { | |
215 | return Messages.ControlFlowView_previousProcessActionToolTipText; | |
6151d86c PT |
216 | } |
217 | ||
6151d86c | 218 | /** |
4999a196 | 219 | * @author gbastien |
6151d86c | 220 | * |
6151d86c | 221 | */ |
4999a196 | 222 | protected static class ControlFlowTreeLabelProvider extends TreeLabelProvider { |
6151d86c | 223 | |
4999a196 GB |
224 | @Override |
225 | public String getColumnText(Object element, int columnIndex) { | |
226 | ControlFlowEntry entry = (ControlFlowEntry) element; | |
6151d86c | 227 | |
4999a196 GB |
228 | if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_processColumn)) { |
229 | return entry.getName(); | |
230 | } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_tidColumn)) { | |
231 | return Integer.toString(entry.getThreadId()); | |
232 | } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_ptidColumn)) { | |
233 | if (entry.getParentThreadId() > 0) { | |
234 | return Integer.toString(entry.getParentThreadId()); | |
6151d86c | 235 | } |
4999a196 GB |
236 | } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_birthTimeColumn)) { |
237 | return Utils.formatTime(entry.getStartTime(), TimeFormat.CALENDAR, Resolution.NANOSEC); | |
238 | } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_traceColumn)) { | |
239 | return entry.getTrace().getName(); | |
6151d86c | 240 | } |
4999a196 | 241 | return ""; //$NON-NLS-1$ |
6151d86c | 242 | } |
6151d86c | 243 | |
6151d86c PT |
244 | } |
245 | ||
a03b7ee4 PT |
246 | private static class ControlFlowFilterLabelProvider extends TreeLabelProvider { |
247 | ||
248 | @Override | |
249 | public String getColumnText(Object element, int columnIndex) { | |
250 | ControlFlowEntry entry = (ControlFlowEntry) element; | |
251 | ||
252 | if (columnIndex == 0) { | |
253 | return entry.getName(); | |
254 | } else if (columnIndex == 1) { | |
255 | return Integer.toString(entry.getThreadId()); | |
256 | } | |
257 | return ""; //$NON-NLS-1$ | |
258 | } | |
259 | ||
260 | } | |
261 | ||
6151d86c PT |
262 | // ------------------------------------------------------------------------ |
263 | // Internal | |
264 | // ------------------------------------------------------------------------ | |
265 | ||
4999a196 | 266 | @Override |
8321a699 PT |
267 | protected void buildEventList(final ITmfTrace trace, final ITmfTrace parentTrace, final IProgressMonitor monitor) { |
268 | final ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID); | |
1cf25311 PT |
269 | if (ssq == null) { |
270 | return; | |
271 | } | |
fec1ac0b | 272 | |
8321a699 PT |
273 | final List<ControlFlowEntry> entryList = new ArrayList<>(); |
274 | final Map<Integer, ControlFlowEntry> entryMap = new HashMap<>(); | |
1cf25311 PT |
275 | |
276 | long start = ssq.getStartTime(); | |
277 | setStartTime(Math.min(getStartTime(), start)); | |
278 | ||
279 | boolean complete = false; | |
280 | while (!complete) { | |
faa38350 PT |
281 | if (monitor.isCanceled()) { |
282 | return; | |
283 | } | |
aae89862 | 284 | complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT); |
1cf25311 PT |
285 | if (ssq.isCancelled()) { |
286 | return; | |
287 | } | |
288 | long end = ssq.getCurrentEndTime(); | |
289 | if (start == end && !complete) { // when complete execute one last time regardless of end time | |
290 | continue; | |
291 | } | |
8321a699 | 292 | final long resolution = Math.max(1, (end - ssq.getStartTime()) / getDisplayWidth()); |
1cf25311 | 293 | setEndTime(Math.max(getEndTime(), end + 1)); |
8321a699 PT |
294 | final List<Integer> threadQuarks = ssq.getQuarks(Attributes.THREADS, "*"); //$NON-NLS-1$ |
295 | final long qStart = start; | |
296 | final long qEnd = end; | |
297 | queryFullStates(ssq, qStart, qEnd, resolution, monitor, new IQueryHandler() { | |
298 | @Override | |
299 | public void handle(List<List<ITmfStateInterval>> fullStates, List<ITmfStateInterval> prevFullState) { | |
300 | for (int threadQuark : threadQuarks) { | |
301 | String threadName = ssq.getAttributeName(threadQuark); | |
302 | int threadId = -1; | |
303 | try { | |
304 | threadId = Integer.parseInt(threadName); | |
305 | } catch (NumberFormatException e1) { | |
306 | continue; | |
307 | } | |
308 | if (threadId <= 0) { // ignore the 'unknown' (-1) and swapper (0) threads | |
309 | continue; | |
310 | } | |
1c471b9e | 311 | |
8321a699 PT |
312 | int execNameQuark; |
313 | int ppidQuark; | |
314 | try { | |
315 | execNameQuark = ssq.getQuarkRelative(threadQuark, Attributes.EXEC_NAME); | |
316 | ppidQuark = ssq.getQuarkRelative(threadQuark, Attributes.PPID); | |
317 | } catch (AttributeNotFoundException e) { | |
318 | /* No information on this thread (yet?), skip it for now */ | |
319 | continue; | |
320 | } | |
321 | ITmfStateInterval lastExecNameInterval = prevFullState == null || execNameQuark >= prevFullState.size() ? null : prevFullState.get(execNameQuark); | |
322 | long lastExecNameStartTime = lastExecNameInterval == null ? -1 : lastExecNameInterval.getStartTime(); | |
323 | long lastExecNameEndTime = lastExecNameInterval == null ? -1 : lastExecNameInterval.getEndTime() + 1; | |
324 | long lastPpidStartTime = prevFullState == null || ppidQuark >= prevFullState.size() ? -1 : prevFullState.get(ppidQuark).getStartTime(); | |
325 | for (List<ITmfStateInterval> fullState : fullStates) { | |
326 | if (monitor.isCanceled()) { | |
327 | return; | |
6151d86c | 328 | } |
8321a699 PT |
329 | if (execNameQuark >= fullState.size() || ppidQuark >= fullState.size()) { |
330 | /* No information on this thread (yet?), skip it for now */ | |
331 | continue; | |
332 | } | |
333 | ITmfStateInterval execNameInterval = fullState.get(execNameQuark); | |
334 | ITmfStateInterval ppidInterval = fullState.get(ppidQuark); | |
335 | long startTime = execNameInterval.getStartTime(); | |
336 | long endTime = execNameInterval.getEndTime() + 1; | |
337 | if (startTime == lastExecNameStartTime && ppidInterval.getStartTime() == lastPpidStartTime) { | |
338 | continue; | |
339 | } | |
340 | boolean isNull = execNameInterval.getStateValue().isNull(); | |
341 | if (isNull && lastExecNameEndTime < startTime && lastExecNameEndTime != -1) { | |
342 | /* | |
343 | * There was a non-null interval in between the | |
344 | * full states, try to use it. | |
345 | */ | |
346 | try { | |
347 | execNameInterval = ssq.querySingleState(startTime - 1, execNameQuark); | |
348 | ppidInterval = ssq.querySingleState(startTime - 1, ppidQuark); | |
349 | startTime = execNameInterval.getStartTime(); | |
350 | endTime = execNameInterval.getEndTime() + 1; | |
351 | } catch (AttributeNotFoundException e) { | |
352 | Activator.getDefault().logError(e.getMessage()); | |
353 | } catch (StateSystemDisposedException e) { | |
354 | /* ignored */ | |
355 | } | |
1c471b9e | 356 | } |
8321a699 PT |
357 | if (!execNameInterval.getStateValue().isNull() && |
358 | execNameInterval.getStateValue().getType() == ITmfStateValue.Type.STRING) { | |
359 | String execName = execNameInterval.getStateValue().unboxStr(); | |
360 | int ppid = ppidInterval.getStateValue().unboxInt(); | |
361 | ControlFlowEntry entry = entryMap.get(threadId); | |
362 | if (entry == null) { | |
363 | entry = new ControlFlowEntry(threadQuark, trace, execName, threadId, ppid, startTime, endTime); | |
364 | entryList.add(entry); | |
365 | entryMap.put(threadId, entry); | |
366 | } else { | |
367 | /* | |
368 | * Update the name of the entry to the | |
369 | * latest execName and the parent thread id | |
370 | * to the latest ppid. | |
371 | */ | |
372 | entry.setName(execName); | |
373 | entry.setParentThreadId(ppid); | |
374 | entry.updateEndTime(endTime); | |
375 | } | |
376 | } | |
377 | if (isNull) { | |
378 | entryMap.remove(threadId); | |
379 | } | |
380 | lastExecNameStartTime = startTime; | |
381 | lastExecNameEndTime = endTime; | |
382 | lastPpidStartTime = ppidInterval.getStartTime(); | |
6151d86c | 383 | } |
6151d86c | 384 | } |
8321a699 | 385 | updateTree(entryList, parentTrace, ssq); |
4999a196 | 386 | |
8321a699 PT |
387 | for (final TimeGraphEntry entry : getEntryList(ssq)) { |
388 | if (monitor.isCanceled()) { | |
389 | return; | |
390 | } | |
391 | buildStatusEvents(trace, parentTrace, ssq, fullStates, prevFullState, (ControlFlowEntry) entry, monitor, qStart, qEnd); | |
392 | } | |
393 | } | |
394 | }); | |
1cf25311 PT |
395 | |
396 | if (parentTrace.equals(getTrace())) { | |
d7ee91bb | 397 | refresh(); |
6151d86c | 398 | } |
1cf25311 | 399 | |
1cf25311 | 400 | start = end; |
6151d86c PT |
401 | } |
402 | } | |
403 | ||
8321a699 | 404 | private void updateTree(List<ControlFlowEntry> entryList, ITmfTrace parentTrace, ITmfStateSystem ss) { |
1cf25311 PT |
405 | List<TimeGraphEntry> rootListToAdd = new ArrayList<>(); |
406 | List<TimeGraphEntry> rootListToRemove = new ArrayList<>(); | |
8321a699 | 407 | List<TimeGraphEntry> rootList = getEntryList(ss); |
1cf25311 | 408 | |
1d46dc38 | 409 | for (ControlFlowEntry entry : entryList) { |
1cf25311 PT |
410 | boolean root = (entry.getParent() == null); |
411 | if (root && entry.getParentThreadId() > 0) { | |
1d46dc38 | 412 | for (ControlFlowEntry parent : entryList) { |
0a35a36f GB |
413 | /* |
414 | * Associate the parent entry only if their time overlap. A | |
415 | * child entry may start before its parent, for example at | |
416 | * the beginning of the trace if a parent has not yet | |
417 | * appeared in the state system. We just want to make sure | |
418 | * that the entry didn't start after the parent ended or | |
419 | * ended before the parent started. | |
420 | */ | |
6151d86c | 421 | if (parent.getThreadId() == entry.getParentThreadId() && |
0a35a36f GB |
422 | !(entry.getStartTime() > parent.getEndTime() || |
423 | entry.getEndTime() < parent.getStartTime())) { | |
6151d86c PT |
424 | parent.addChild(entry); |
425 | root = false; | |
1cf25311 PT |
426 | if (rootList != null && rootList.contains(entry)) { |
427 | rootListToRemove.add(entry); | |
428 | } | |
6151d86c PT |
429 | break; |
430 | } | |
431 | } | |
432 | } | |
1cf25311 PT |
433 | if (root && (rootList == null || !rootList.contains(entry))) { |
434 | rootListToAdd.add(entry); | |
6151d86c PT |
435 | } |
436 | } | |
1cf25311 | 437 | |
8321a699 PT |
438 | addToEntryList(parentTrace, ss, rootListToAdd); |
439 | removeFromEntryList(parentTrace, ss, rootListToRemove); | |
6151d86c PT |
440 | } |
441 | ||
8321a699 PT |
442 | private void buildStatusEvents(ITmfTrace trace, ITmfTrace parentTrace, ITmfStateSystem ss, @NonNull List<List<ITmfStateInterval>> fullStates, |
443 | @Nullable List<ITmfStateInterval> prevFullState, ControlFlowEntry entry, @NonNull IProgressMonitor monitor, long start, long end) { | |
1cf25311 | 444 | if (start < entry.getEndTime() && end > entry.getStartTime()) { |
8321a699 | 445 | List<ITimeEvent> eventList = getEventList(entry, ss, fullStates, prevFullState, monitor); |
d2120fb6 | 446 | if (eventList == null) { |
1cf25311 PT |
447 | return; |
448 | } | |
449 | for (ITimeEvent event : eventList) { | |
450 | entry.addEvent(event); | |
451 | } | |
8321a699 | 452 | if (parentTrace.equals(getTrace())) { |
1cf25311 PT |
453 | redraw(); |
454 | } | |
faa38350 | 455 | } |
6151d86c | 456 | for (ITimeGraphEntry child : entry.getChildren()) { |
faa38350 PT |
457 | if (monitor.isCanceled()) { |
458 | return; | |
459 | } | |
8321a699 | 460 | buildStatusEvents(trace, parentTrace, ss, fullStates, prevFullState, (ControlFlowEntry) child, monitor, start, end); |
6151d86c PT |
461 | } |
462 | } | |
463 | ||
4999a196 | 464 | @Override |
8321a699 PT |
465 | protected @Nullable List<ITimeEvent> getEventList(@NonNull TimeGraphEntry tgentry, ITmfStateSystem ss, |
466 | @NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, @NonNull IProgressMonitor monitor) { | |
4999a196 GB |
467 | List<ITimeEvent> eventList = null; |
468 | if (!(tgentry instanceof ControlFlowEntry)) { | |
469 | return eventList; | |
470 | } | |
471 | ControlFlowEntry entry = (ControlFlowEntry) tgentry; | |
6151d86c | 472 | try { |
8321a699 PT |
473 | int threadQuark = entry.getThreadQuark(); |
474 | int statusQuark = ss.getQuarkRelative(threadQuark, Attributes.STATUS); | |
475 | eventList = new ArrayList<>(fullStates.size()); | |
476 | ITmfStateInterval lastInterval = prevFullState == null || statusQuark >= prevFullState.size() ? null : prevFullState.get(statusQuark); | |
477 | long lastStartTime = lastInterval == null ? -1 : lastInterval.getStartTime(); | |
478 | long lastEndTime = lastInterval == null ? -1 : lastInterval.getEndTime() + 1; | |
479 | for (List<ITmfStateInterval> fullState : fullStates) { | |
6151d86c PT |
480 | if (monitor.isCanceled()) { |
481 | return null; | |
482 | } | |
8321a699 PT |
483 | if (statusQuark >= fullState.size()) { |
484 | /* No information on this thread (yet?), skip it for now */ | |
485 | continue; | |
486 | } | |
487 | ITmfStateInterval statusInterval = fullState.get(statusQuark); | |
6151d86c | 488 | long time = statusInterval.getStartTime(); |
8321a699 PT |
489 | if (time == lastStartTime) { |
490 | continue; | |
491 | } | |
6151d86c PT |
492 | long duration = statusInterval.getEndTime() - time + 1; |
493 | int status = -1; | |
494 | try { | |
495 | status = statusInterval.getStateValue().unboxInt(); | |
496 | } catch (StateValueTypeException e) { | |
8321a699 | 497 | Activator.getDefault().logError(e.getMessage()); |
6151d86c PT |
498 | } |
499 | if (lastEndTime != time && lastEndTime != -1) { | |
af10fe06 | 500 | eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime)); |
6151d86c | 501 | } |
8321a699 PT |
502 | if (!statusInterval.getStateValue().isNull()) { |
503 | eventList.add(new TimeEvent(entry, time, duration, status)); | |
504 | } else { | |
505 | eventList.add(new NullTimeEvent(entry, time, duration)); | |
506 | } | |
507 | lastStartTime = time; | |
6151d86c PT |
508 | lastEndTime = time + duration; |
509 | } | |
8321a699 | 510 | } catch (AttributeNotFoundException | TimeRangeException e) { |
50a47aa6 | 511 | Activator.getDefault().logError(e.getMessage()); |
6151d86c PT |
512 | } |
513 | return eventList; | |
514 | } | |
515 | ||
4999a196 GB |
516 | /** |
517 | * Returns a value corresponding to the selected entry. | |
518 | * | |
1cf25311 PT |
519 | * Used in conjunction with synchingToTime to change the selected entry. If |
520 | * one of these methods is overridden in child class, then both should be. | |
4999a196 GB |
521 | * |
522 | * @param time | |
523 | * The currently selected time | |
524 | * @return a value identifying the entry | |
525 | */ | |
526 | private int getSelectionValue(long time) { | |
527 | int thread = -1; | |
c14c0757 | 528 | for (ITmfTrace trace : TmfTraceManager.getTraceSet(getTrace())) { |
4999a196 GB |
529 | if (thread > 0) { |
530 | break; | |
531 | } | |
6d16f5a9 | 532 | ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID); |
1cf25311 PT |
533 | if (ssq == null) { |
534 | continue; | |
535 | } | |
536 | if (time >= ssq.getStartTime() && time <= ssq.getCurrentEndTime()) { | |
537 | List<Integer> currentThreadQuarks = ssq.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$ | |
538 | for (int currentThreadQuark : currentThreadQuarks) { | |
539 | try { | |
540 | ITmfStateInterval currentThreadInterval = ssq.querySingleState(time, currentThreadQuark); | |
541 | int currentThread = currentThreadInterval.getStateValue().unboxInt(); | |
542 | if (currentThread > 0) { | |
543 | int statusQuark = ssq.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThread), Attributes.STATUS); | |
544 | ITmfStateInterval statusInterval = ssq.querySingleState(time, statusQuark); | |
545 | if (statusInterval.getStartTime() == time) { | |
546 | thread = currentThread; | |
547 | break; | |
4999a196 | 548 | } |
4999a196 | 549 | } |
1cf25311 | 550 | } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) { |
8321a699 | 551 | Activator.getDefault().logError(e.getMessage()); |
1cf25311 PT |
552 | } catch (StateSystemDisposedException e) { |
553 | /* Ignored */ | |
faa38350 | 554 | } |
6151d86c | 555 | } |
6151d86c | 556 | } |
4999a196 GB |
557 | } |
558 | return thread; | |
6151d86c PT |
559 | } |
560 | ||
4999a196 GB |
561 | @Override |
562 | protected void synchingToTime(long time) { | |
563 | int selected = getSelectionValue(time); | |
564 | if (selected > 0) { | |
747adf5c | 565 | for (Object element : getTimeGraphViewer().getExpandedElements()) { |
4999a196 GB |
566 | if (element instanceof ControlFlowEntry) { |
567 | ControlFlowEntry entry = (ControlFlowEntry) element; | |
568 | if (entry.getThreadId() == selected) { | |
569 | getTimeGraphCombo().setSelection(entry); | |
570 | break; | |
6151d86c PT |
571 | } |
572 | } | |
573 | } | |
6151d86c | 574 | } |
6151d86c | 575 | } |
79ec0b89 PT |
576 | |
577 | @Override | |
8321a699 | 578 | protected @NonNull List<ILinkEvent> getLinkList(ITmfStateSystem ss, |
143217ee | 579 | @NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, @NonNull IProgressMonitor monitor) { |
e0838ca1 | 580 | List<ILinkEvent> list = new ArrayList<>(); |
8321a699 | 581 | List<TimeGraphEntry> entryList = getEntryList(ss); |
c14c0757 | 582 | if (entryList == null) { |
79ec0b89 PT |
583 | return list; |
584 | } | |
c14c0757 | 585 | for (ITmfTrace trace : TmfTraceManager.getTraceSet(getTrace())) { |
8321a699 PT |
586 | List<Integer> currentThreadQuarks = ss.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$ |
587 | for (int currentThreadQuark : currentThreadQuarks) { | |
588 | if (currentThreadQuark >= fullStates.get(0).size()) { | |
589 | /* No information on this cpu (yet?), skip it for now */ | |
4bc53929 GB |
590 | continue; |
591 | } | |
8321a699 PT |
592 | List<ITmfStateInterval> currentThreadIntervals = new ArrayList<>(fullStates.size() + 2); |
593 | try { | |
143217ee PT |
594 | /* |
595 | * Add the previous interval if it is the first query | |
596 | * iteration and the first interval has currentThread=0. Add | |
597 | * the following interval if the last interval has | |
598 | * currentThread=0. These are diagonal arrows crossing the | |
599 | * query iteration range. | |
600 | */ | |
601 | if (prevFullState == null) { | |
602 | ITmfStateInterval currentThreadInterval = fullStates.get(0).get(currentThreadQuark); | |
603 | if (currentThreadInterval.getStateValue().unboxInt() == 0) { | |
604 | long start = Math.max(currentThreadInterval.getStartTime() - 1, ss.getStartTime()); | |
605 | currentThreadIntervals.add(ss.querySingleState(start, currentThreadQuark)); | |
606 | } | |
607 | } | |
8321a699 PT |
608 | for (List<ITmfStateInterval> fullState : fullStates) { |
609 | currentThreadIntervals.add(fullState.get(currentThreadQuark)); | |
610 | } | |
143217ee PT |
611 | ITmfStateInterval currentThreadInterval = fullStates.get(fullStates.size() - 1).get(currentThreadQuark); |
612 | if (currentThreadInterval.getStateValue().unboxInt() == 0) { | |
613 | long end = Math.min(currentThreadInterval.getEndTime() + 1, ss.getCurrentEndTime()); | |
614 | currentThreadIntervals.add(ss.querySingleState(end, currentThreadQuark)); | |
615 | } | |
8321a699 PT |
616 | } catch (AttributeNotFoundException e) { |
617 | Activator.getDefault().logError(e.getMessage()); | |
618 | return list; | |
619 | } catch (StateSystemDisposedException e) { | |
620 | /* Ignored */ | |
621 | return list; | |
622 | } | |
623 | int prevThread = 0; | |
624 | long prevEnd = 0; | |
625 | long lastEnd = 0; | |
626 | for (ITmfStateInterval currentThreadInterval : currentThreadIntervals) { | |
627 | if (monitor.isCanceled()) { | |
628 | return list; | |
629 | } | |
630 | if (currentThreadInterval.getEndTime() + 1 == lastEnd) { | |
631 | continue; | |
632 | } | |
633 | long time = currentThreadInterval.getStartTime(); | |
634 | if (time != lastEnd) { | |
635 | // don't create links where there are gaps in intervals due to the resolution | |
636 | prevThread = 0; | |
637 | prevEnd = 0; | |
638 | } | |
639 | int thread = currentThreadInterval.getStateValue().unboxInt(); | |
640 | if (thread > 0 && prevThread > 0) { | |
641 | ITimeGraphEntry prevEntry = findEntry(entryList, trace, prevThread); | |
642 | ITimeGraphEntry nextEntry = findEntry(entryList, trace, thread); | |
643 | list.add(new TimeLinkEvent(prevEntry, nextEntry, prevEnd, time - prevEnd, 0)); | |
644 | } | |
645 | lastEnd = currentThreadInterval.getEndTime() + 1; | |
646 | if (thread != 0) { | |
647 | prevThread = thread; | |
648 | prevEnd = lastEnd; | |
79ec0b89 | 649 | } |
79ec0b89 PT |
650 | } |
651 | } | |
652 | } | |
653 | return list; | |
654 | } | |
655 | ||
a3188982 PT |
656 | private ControlFlowEntry findEntry(List<? extends ITimeGraphEntry> entryList, ITmfTrace trace, int threadId) { |
657 | for (ITimeGraphEntry entry : entryList) { | |
79ec0b89 PT |
658 | if (entry instanceof ControlFlowEntry) { |
659 | ControlFlowEntry controlFlowEntry = (ControlFlowEntry) entry; | |
660 | if (controlFlowEntry.getThreadId() == threadId && controlFlowEntry.getTrace() == trace) { | |
661 | return controlFlowEntry; | |
662 | } else if (entry.hasChildren()) { | |
663 | controlFlowEntry = findEntry(entry.getChildren(), trace, threadId); | |
664 | if (controlFlowEntry != null) { | |
665 | return controlFlowEntry; | |
666 | } | |
667 | } | |
668 | } | |
669 | } | |
670 | return null; | |
671 | } | |
6151d86c | 672 | } |