| 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.LinkedHashMap; |
| 17 | import java.util.List; |
| 18 | import java.util.Map; |
| 19 | |
| 20 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes; |
| 21 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.StateValues; |
| 22 | import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Activator; |
| 23 | import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages; |
| 24 | import org.eclipse.linuxtools.lttng2.kernel.core.analysis.LttngKernelAnalysisModule; |
| 25 | import org.eclipse.swt.SWT; |
| 26 | import org.eclipse.swt.graphics.GC; |
| 27 | import org.eclipse.swt.graphics.RGB; |
| 28 | import org.eclipse.swt.graphics.Rectangle; |
| 29 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
| 30 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
| 31 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException; |
| 32 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; |
| 33 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; |
| 34 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; |
| 35 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; |
| 36 | import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule; |
| 37 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem; |
| 38 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider; |
| 39 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent; |
| 40 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent; |
| 41 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils; |
| 42 | |
| 43 | /** |
| 44 | * Presentation provider for the control flow view |
| 45 | */ |
| 46 | public class ControlFlowPresentationProvider extends TimeGraphPresentationProvider { |
| 47 | |
| 48 | private enum State { |
| 49 | UNKNOWN (new RGB(100, 100, 100)), |
| 50 | WAIT_BLOCKED (new RGB(200, 200, 0)), |
| 51 | WAIT_FOR_CPU (new RGB(200, 100, 0)), |
| 52 | USERMODE (new RGB(0, 200, 0)), |
| 53 | SYSCALL (new RGB(0, 0, 200)), |
| 54 | INTERRUPTED (new RGB(200, 0, 100)); |
| 55 | |
| 56 | public final RGB rgb; |
| 57 | |
| 58 | private State(RGB rgb) { |
| 59 | this.rgb = rgb; |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Default constructor |
| 66 | */ |
| 67 | public ControlFlowPresentationProvider() { |
| 68 | super(Messages.ControlFlowView_stateTypeName); |
| 69 | } |
| 70 | |
| 71 | private static State[] getStateValues() { |
| 72 | return State.values(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public StateItem[] getStateTable() { |
| 77 | State[] states = getStateValues(); |
| 78 | StateItem[] stateTable = new StateItem[states.length]; |
| 79 | for (int i = 0; i < stateTable.length; i++) { |
| 80 | State state = states[i]; |
| 81 | stateTable[i] = new StateItem(state.rgb, state.toString()); |
| 82 | } |
| 83 | return stateTable; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int getStateTableIndex(ITimeEvent event) { |
| 88 | if (event instanceof TimeEvent && ((TimeEvent) event).hasValue()) { |
| 89 | int status = ((TimeEvent) event).getValue(); |
| 90 | return getMatchingState(status).ordinal(); |
| 91 | } |
| 92 | return TRANSPARENT; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public String getEventName(ITimeEvent event) { |
| 97 | if (event instanceof TimeEvent) { |
| 98 | TimeEvent ev = (TimeEvent) event; |
| 99 | if (ev.hasValue()) { |
| 100 | return getMatchingState(ev.getValue()).toString(); |
| 101 | } |
| 102 | } |
| 103 | return Messages.ControlFlowView_multipleStates; |
| 104 | } |
| 105 | |
| 106 | private static State getMatchingState(int status) { |
| 107 | switch (status) { |
| 108 | case StateValues.PROCESS_STATUS_WAIT_BLOCKED: |
| 109 | return State.WAIT_BLOCKED; |
| 110 | case StateValues.PROCESS_STATUS_WAIT_FOR_CPU: |
| 111 | return State.WAIT_FOR_CPU; |
| 112 | case StateValues.PROCESS_STATUS_RUN_USERMODE: |
| 113 | return State.USERMODE; |
| 114 | case StateValues.PROCESS_STATUS_RUN_SYSCALL: |
| 115 | return State.SYSCALL; |
| 116 | case StateValues.PROCESS_STATUS_INTERRUPTED: |
| 117 | return State.INTERRUPTED; |
| 118 | default: |
| 119 | return State.UNKNOWN; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event) { |
| 125 | Map<String, String> retMap = new LinkedHashMap<>(); |
| 126 | if (!(event instanceof TimeEvent) || !((TimeEvent) event).hasValue() || |
| 127 | !(event.getEntry() instanceof ControlFlowEntry)) { |
| 128 | return retMap; |
| 129 | } |
| 130 | ControlFlowEntry entry = (ControlFlowEntry) event.getEntry(); |
| 131 | ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(entry.getTrace(), LttngKernelAnalysisModule.ID); |
| 132 | if (ssq == null) { |
| 133 | return retMap; |
| 134 | } |
| 135 | int tid = entry.getThreadId(); |
| 136 | |
| 137 | try { |
| 138 | // Find every CPU first, then get the current thread |
| 139 | int cpusQuark = ssq.getQuarkAbsolute(Attributes.CPUS); |
| 140 | List<Integer> cpuQuarks = ssq.getSubAttributes(cpusQuark, false); |
| 141 | for (Integer cpuQuark : cpuQuarks) { |
| 142 | int currentThreadQuark = ssq.getQuarkRelative(cpuQuark, Attributes.CURRENT_THREAD); |
| 143 | ITmfStateInterval interval = ssq.querySingleState(event.getTime(), currentThreadQuark); |
| 144 | if (!interval.getStateValue().isNull()) { |
| 145 | ITmfStateValue state = interval.getStateValue(); |
| 146 | int currentThreadId = state.unboxInt(); |
| 147 | if (tid == currentThreadId) { |
| 148 | retMap.put(Messages.ControlFlowView_attributeCpuName, ssq.getAttributeName(cpuQuark)); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) { |
| 155 | Activator.getDefault().logError("Error in ControlFlowPresentationProvider", e); //$NON-NLS-1$ |
| 156 | } catch (StateSystemDisposedException e) { |
| 157 | /* Ignored */ |
| 158 | } |
| 159 | int status = ((TimeEvent) event).getValue(); |
| 160 | if (status == StateValues.PROCESS_STATUS_RUN_SYSCALL) { |
| 161 | try { |
| 162 | int syscallQuark = ssq.getQuarkRelative(entry.getThreadQuark(), Attributes.SYSTEM_CALL); |
| 163 | ITmfStateInterval value = ssq.querySingleState(event.getTime(), syscallQuark); |
| 164 | if (!value.getStateValue().isNull()) { |
| 165 | ITmfStateValue state = value.getStateValue(); |
| 166 | retMap.put(Messages.ControlFlowView_attributeSyscallName, state.toString()); |
| 167 | } |
| 168 | |
| 169 | } catch (AttributeNotFoundException | TimeRangeException e) { |
| 170 | Activator.getDefault().logError("Error in ControlFlowPresentationProvider", e); //$NON-NLS-1$ |
| 171 | } catch (StateSystemDisposedException e) { |
| 172 | /* Ignored */ |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return retMap; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) { |
| 181 | if (bounds.width <= gc.getFontMetrics().getAverageCharWidth()) { |
| 182 | return; |
| 183 | } |
| 184 | if (!(event instanceof TimeEvent)) { |
| 185 | return; |
| 186 | } |
| 187 | ControlFlowEntry entry = (ControlFlowEntry) event.getEntry(); |
| 188 | ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(entry.getTrace(), LttngKernelAnalysisModule.ID); |
| 189 | if (ss == null) { |
| 190 | return; |
| 191 | } |
| 192 | int status = ((TimeEvent) event).getValue(); |
| 193 | |
| 194 | if (status != StateValues.PROCESS_STATUS_RUN_SYSCALL) { |
| 195 | return; |
| 196 | } |
| 197 | try { |
| 198 | int syscallQuark = ss.getQuarkRelative(entry.getThreadQuark(), Attributes.SYSTEM_CALL); |
| 199 | ITmfStateInterval value = ss.querySingleState(event.getTime(), syscallQuark); |
| 200 | if (!value.getStateValue().isNull()) { |
| 201 | ITmfStateValue state = value.getStateValue(); |
| 202 | gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE)); |
| 203 | Utils.drawText(gc, state.toString().substring(4), bounds.x, bounds.y - 2, bounds.width, true, true); |
| 204 | } |
| 205 | } catch (AttributeNotFoundException | TimeRangeException e) { |
| 206 | Activator.getDefault().logError("Error in ControlFlowPresentationProvider", e); //$NON-NLS-1$ |
| 207 | } catch (StateSystemDisposedException e) { |
| 208 | /* Ignored */ |
| 209 | } |
| 210 | } |
| 211 | } |