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