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