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