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