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