Commit | Line | Data |
---|---|---|
b1a7aebb MG |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 É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 | ||
10 | package org.eclipse.tracecompass.internal.lttng2.kernel.ui.views.vm.vcpuview; | |
11 | ||
12 | import org.eclipse.jdt.annotation.Nullable; | |
13 | import org.eclipse.swt.SWT; | |
14 | import org.eclipse.swt.graphics.Color; | |
15 | import org.eclipse.swt.graphics.GC; | |
16 | import org.eclipse.swt.graphics.RGB; | |
17 | import org.eclipse.swt.graphics.Rectangle; | |
18 | import org.eclipse.swt.widgets.Display; | |
0f7a12d3 | 19 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.StateValues; |
b1a7aebb MG |
20 | import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.vm.VcpuStateValues; |
21 | import org.eclipse.tracecompass.internal.lttng2.kernel.ui.views.vm.vcpuview.VirtualMachineCommon.Type; | |
22 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem; | |
23 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider; | |
24 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent; | |
25 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent; | |
26 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent; | |
27 | ||
28 | /** | |
29 | * Presentation provider for the Virtual Machine view, based on the generic TMF | |
30 | * presentation provider. | |
31 | * | |
32 | * @author Mohamad Gebai | |
33 | */ | |
34 | public class VirtualMachinePresentationProvider extends TimeGraphPresentationProvider { | |
35 | ||
673cf5c3 GB |
36 | private static final int ALPHA = 70; |
37 | ||
b1a7aebb MG |
38 | /* |
39 | * TODO: Some of it is copy-pasted from the control flow presentation | |
40 | * provider because it actually is the same data as from the control flow | |
41 | * view. Ideally, we should reuse what is there instead of rewriting it here | |
42 | */ | |
43 | private enum State { | |
44 | UNKNOWN(new RGB(100, 100, 100)), | |
45 | IDLE(new RGB(200, 200, 200)), | |
46 | USERMODE(new RGB(0, 200, 0)), | |
47 | WAIT_VMM(new RGB(200, 0, 0)), | |
48 | VCPU_PREEMPTED(new RGB(120, 40, 90)), | |
49 | THREAD_UNKNOWN(new RGB(100, 100, 100)), | |
50 | THREAD_WAIT_BLOCKED(new RGB(200, 200, 0)), | |
51 | THREAD_WAIT_FOR_CPU(new RGB(200, 100, 0)), | |
52 | THREAD_USERMODE(new RGB(0, 200, 0)), | |
53 | THREAD_SYSCALL(new RGB(0, 0, 200)), | |
54 | THREAD_INTERRUPTED(new RGB(200, 0, 100)); | |
55 | ||
673cf5c3 | 56 | private final RGB fRgb; |
b1a7aebb MG |
57 | |
58 | private State(RGB rgb) { | |
673cf5c3 | 59 | fRgb = rgb; |
b1a7aebb MG |
60 | } |
61 | } | |
62 | ||
63 | /** | |
64 | * Default constructor | |
65 | */ | |
66 | public VirtualMachinePresentationProvider() { | |
67 | super(); | |
68 | } | |
69 | ||
70 | private static State[] getStateValues() { | |
71 | return State.values(); | |
72 | } | |
73 | ||
74 | private static State getStateForVcpu(int value) { | |
75 | if ((value & VcpuStateValues.VCPU_PREEMPT) > 0) { | |
76 | return State.VCPU_PREEMPTED; | |
77 | } else if ((value & VcpuStateValues.VCPU_VMM) > 0) { | |
78 | return State.WAIT_VMM; | |
79 | } else if (value == 2) { | |
80 | return State.USERMODE; | |
81 | } else if (value == 1) { | |
82 | return State.IDLE; | |
83 | } else { | |
84 | return State.UNKNOWN; | |
85 | } | |
86 | } | |
87 | ||
88 | private static @Nullable State getStateForThread(int value) { | |
89 | if (value == VcpuStateValues.VCPU_PREEMPT) { | |
90 | return null; | |
91 | } | |
92 | switch (value) { | |
93 | case StateValues.PROCESS_STATUS_RUN_USERMODE: | |
94 | return State.THREAD_USERMODE; | |
95 | case StateValues.PROCESS_STATUS_RUN_SYSCALL: | |
96 | return State.THREAD_SYSCALL; | |
97 | case StateValues.PROCESS_STATUS_WAIT_FOR_CPU: | |
98 | return State.THREAD_WAIT_FOR_CPU; | |
99 | case StateValues.PROCESS_STATUS_WAIT_BLOCKED: | |
100 | return State.THREAD_WAIT_BLOCKED; | |
101 | case StateValues.PROCESS_STATUS_INTERRUPTED: | |
102 | return State.THREAD_INTERRUPTED; | |
103 | case StateValues.PROCESS_STATUS_UNKNOWN: | |
104 | case StateValues.PROCESS_STATUS_WAIT_UNKNOWN: | |
105 | return State.THREAD_UNKNOWN; | |
106 | default: | |
107 | return null; | |
108 | } | |
109 | } | |
110 | ||
111 | private static @Nullable State getEventState(TimeEvent event) { | |
112 | if (event.hasValue()) { | |
113 | VirtualMachineViewEntry entry = (VirtualMachineViewEntry) event.getEntry(); | |
114 | int value = event.getValue(); | |
115 | ||
116 | if (entry.getType() == Type.VCPU) { | |
117 | return getStateForVcpu(value); | |
118 | } else if (entry.getType() == Type.THREAD) { | |
119 | return getStateForThread(value); | |
120 | } | |
121 | } | |
122 | return null; | |
123 | } | |
124 | ||
125 | @Override | |
126 | public int getStateTableIndex(@Nullable ITimeEvent event) { | |
127 | if (event == null) { | |
128 | return TRANSPARENT; | |
129 | } | |
130 | State state = getEventState((TimeEvent) event); | |
131 | if (state != null) { | |
132 | return state.ordinal(); | |
133 | } | |
134 | if (event instanceof NullTimeEvent) { | |
135 | return INVISIBLE; | |
136 | } | |
137 | return TRANSPARENT; | |
138 | } | |
139 | ||
140 | @Override | |
141 | public StateItem[] getStateTable() { | |
142 | State[] states = getStateValues(); | |
143 | StateItem[] stateTable = new StateItem[states.length]; | |
144 | for (int i = 0; i < stateTable.length; i++) { | |
145 | State state = states[i]; | |
673cf5c3 | 146 | stateTable[i] = new StateItem(state.fRgb, state.toString()); |
b1a7aebb MG |
147 | } |
148 | return stateTable; | |
149 | } | |
150 | ||
151 | @Override | |
152 | public @Nullable String getEventName(@Nullable ITimeEvent event) { | |
153 | if (event == null) { | |
154 | return null; | |
155 | } | |
156 | State state = getEventState((TimeEvent) event); | |
157 | if (state != null) { | |
158 | return state.toString(); | |
159 | } | |
160 | if (event instanceof NullTimeEvent) { | |
161 | return null; | |
162 | } | |
163 | return Messages.VmView_multipleStates; | |
164 | } | |
165 | ||
166 | @Override | |
167 | public void postDrawEvent(@Nullable ITimeEvent event, @Nullable Rectangle bounds, @Nullable GC gc) { | |
673cf5c3 | 168 | if (bounds == null || gc == null || !(event instanceof TimeEvent)) { |
b1a7aebb MG |
169 | return; |
170 | } | |
171 | boolean visible = bounds.width == 0 ? false : true; | |
172 | if (!visible) { | |
173 | return; | |
174 | } | |
b1a7aebb MG |
175 | TimeEvent ev = (TimeEvent) event; |
176 | /* | |
177 | * FIXME: There seems to be a bug when multiple events should be drawn | |
178 | * under a alpha event. See FIXME comment in | |
179 | * VirtualMachineView#getEventList | |
180 | */ | |
181 | if (ev.hasValue()) { | |
182 | VirtualMachineViewEntry entry = (VirtualMachineViewEntry) event.getEntry(); | |
183 | ||
184 | if (entry.getType() == Type.THREAD) { | |
185 | int value = ev.getValue(); | |
186 | if ((value & VcpuStateValues.VCPU_PREEMPT) != 0) { | |
187 | /* | |
188 | * If the status was preempted at this time, draw an alpha | |
189 | * over this state | |
190 | */ | |
191 | Color alphaColor = Display.getDefault().getSystemColor(SWT.COLOR_RED); | |
192 | ||
193 | int alpha = gc.getAlpha(); | |
194 | Color background = gc.getBackground(); | |
195 | // fill all rect area | |
196 | gc.setBackground(alphaColor); | |
673cf5c3 | 197 | gc.setAlpha(ALPHA); |
b1a7aebb MG |
198 | gc.fillRectangle(bounds); |
199 | ||
200 | gc.setBackground(background); | |
201 | gc.setAlpha(alpha); | |
202 | } | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | } |