tmf: Split the state system in a separate plugin
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.ui / src / org / eclipse / linuxtools / internal / lttng2 / kernel / ui / views / resources / ResourcesPresentationProvider.java
CommitLineData
dedc7dec 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
dedc7dec
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
dedc7dec
PT
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources;
15
16import java.util.LinkedHashMap;
17import java.util.List;
18import java.util.Map;
19
20import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
21import org.eclipse.linuxtools.internal.lttng2.kernel.core.StateValues;
4bc53929 22import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Activator;
dedc7dec
PT
23import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages;
24import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources.ResourcesEntry.Type;
e3366401 25import org.eclipse.linuxtools.lttng2.kernel.core.analysis.LttngKernelAnalysisModule;
bcec0116
AM
26import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem;
27import org.eclipse.linuxtools.statesystem.core.exceptions.AttributeNotFoundException;
28import org.eclipse.linuxtools.statesystem.core.exceptions.StateSystemDisposedException;
29import org.eclipse.linuxtools.statesystem.core.exceptions.StateValueTypeException;
30import org.eclipse.linuxtools.statesystem.core.exceptions.TimeRangeException;
31import org.eclipse.linuxtools.statesystem.core.interval.ITmfStateInterval;
32import org.eclipse.linuxtools.statesystem.core.statevalue.ITmfStateValue;
dedc7dec
PT
33import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.StateItem;
34import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider;
35import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
713a70ae 36import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
1d46dc38 37import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.NullTimeEvent;
4999a196
GB
38import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent;
39import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.ITmfTimeGraphDrawingHelper;
dedc7dec
PT
40import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils;
41import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
42import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
713a70ae
PT
43import org.eclipse.swt.SWT;
44import org.eclipse.swt.graphics.GC;
dedc7dec 45import org.eclipse.swt.graphics.RGB;
713a70ae 46import org.eclipse.swt.graphics.Rectangle;
dedc7dec
PT
47
48/**
49 * Presentation provider for the Resource view, based on the generic TMF
50 * presentation provider.
51 *
52 * @author Patrick Tasse
53 */
54public class ResourcesPresentationProvider extends TimeGraphPresentationProvider {
55
4999a196 56 private long fLastThreadId = -1;
713a70ae 57
dedc7dec 58 private enum State {
4999a196 59 IDLE (new RGB(200, 200, 200)),
1d46dc38
PT
60 USERMODE (new RGB( 0, 200, 0)),
61 SYSCALL (new RGB( 0, 0, 200)),
4999a196
GB
62 IRQ (new RGB(200, 0, 100)),
63 SOFT_IRQ (new RGB(200, 150, 100)),
64 IRQ_ACTIVE (new RGB(200, 0, 100)),
1d46dc38 65 SOFT_IRQ_RAISED (new RGB(200, 200, 0)),
4999a196 66 SOFT_IRQ_ACTIVE (new RGB(200, 150, 100));
dedc7dec
PT
67
68 public final RGB rgb;
69
4999a196 70 private State(RGB rgb) {
dedc7dec
PT
71 this.rgb = rgb;
72 }
73 }
74
713a70ae
PT
75 /**
76 * Default constructor
713a70ae 77 */
4999a196 78 public ResourcesPresentationProvider() {
713a70ae 79 super();
713a70ae
PT
80 }
81
4999a196
GB
82 private static State[] getStateValues() {
83 return State.values();
dedc7dec
PT
84 }
85
1d46dc38
PT
86 private static State getEventState(TimeEvent event) {
87 if (event.hasValue()) {
4999a196 88 ResourcesEntry entry = (ResourcesEntry) event.getEntry();
1d46dc38 89 int value = event.getValue();
4999a196
GB
90
91 if (entry.getType() == Type.CPU) {
92 if (value == StateValues.CPU_STATUS_IDLE) {
93 return State.IDLE;
94 } else if (value == StateValues.CPU_STATUS_RUN_USERMODE) {
95 return State.USERMODE;
96 } else if (value == StateValues.CPU_STATUS_RUN_SYSCALL) {
97 return State.SYSCALL;
98 } else if (value == StateValues.CPU_STATUS_IRQ) {
99 return State.IRQ;
100 } else if (value == StateValues.CPU_STATUS_SOFTIRQ) {
101 return State.SOFT_IRQ;
102 }
1d46dc38 103 } else if (entry.getType() == Type.IRQ) {
4999a196 104 return State.IRQ_ACTIVE;
1d46dc38 105 } else if (entry.getType() == Type.SOFT_IRQ) {
4999a196
GB
106 if (value == StateValues.SOFT_IRQ_RAISED) {
107 return State.SOFT_IRQ_RAISED;
108 }
109 return State.SOFT_IRQ_ACTIVE;
110 }
dedc7dec 111 }
4999a196 112 return null;
dedc7dec
PT
113 }
114
115 @Override
116 public int getStateTableIndex(ITimeEvent event) {
1d46dc38 117 State state = getEventState((TimeEvent) event);
4999a196
GB
118 if (state != null) {
119 return state.ordinal();
120 }
1d46dc38
PT
121 if (event instanceof NullTimeEvent) {
122 return INVISIBLE;
dedc7dec 123 }
af10fe06 124 return TRANSPARENT;
dedc7dec
PT
125 }
126
4999a196
GB
127 @Override
128 public StateItem[] getStateTable() {
129 State[] states = getStateValues();
130 StateItem[] stateTable = new StateItem[states.length];
131 for (int i = 0; i < stateTable.length; i++) {
132 State state = states[i];
133 stateTable[i] = new StateItem(state.rgb, state.toString());
134 }
135 return stateTable;
136 }
137
dedc7dec
PT
138 @Override
139 public String getEventName(ITimeEvent event) {
1d46dc38 140 State state = getEventState((TimeEvent) event);
4999a196
GB
141 if (state != null) {
142 return state.toString();
143 }
1d46dc38
PT
144 if (event instanceof NullTimeEvent) {
145 return null;
dedc7dec 146 }
af10fe06 147 return Messages.ResourcesView_multipleStates;
dedc7dec
PT
148 }
149
150 @Override
151 public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event, long hoverTime) {
152
e0838ca1 153 Map<String, String> retMap = new LinkedHashMap<>();
4999a196 154 if (event instanceof TimeEvent && ((TimeEvent) event).hasValue()) {
dedc7dec 155
4999a196
GB
156 TimeEvent tcEvent = (TimeEvent) event;
157 ResourcesEntry entry = (ResourcesEntry) event.getEntry();
dedc7dec 158
4999a196 159 if (tcEvent.hasValue()) {
ff3f02c8 160 LttngKernelAnalysisModule module = entry.getTrace().getAnalysisModuleOfClass(LttngKernelAnalysisModule.class, LttngKernelAnalysisModule.ID);
1cf25311
PT
161 if (module == null) {
162 return retMap;
163 }
4bc53929
GB
164 ITmfStateSystem ss = module.getStateSystem();
165 if (ss == null) {
166 return retMap;
167 }
4999a196
GB
168 // Check for IRQ or Soft_IRQ type
169 if (entry.getType().equals(Type.IRQ) || entry.getType().equals(Type.SOFT_IRQ)) {
dedc7dec 170
4999a196
GB
171 // Get CPU of IRQ or SoftIRQ and provide it for the tooltip display
172 int cpu = tcEvent.getValue();
173 if (cpu >= 0) {
174 retMap.put(Messages.ResourcesView_attributeCpuName, String.valueOf(cpu));
175 }
dedc7dec 176 }
dedc7dec 177
4999a196
GB
178 // Check for type CPU
179 else if (entry.getType().equals(Type.CPU)) {
180 int status = tcEvent.getValue();
181
182 if (status == StateValues.CPU_STATUS_IRQ) {
183 // In IRQ state get the IRQ that caused the interruption
4999a196
GB
184 int cpu = entry.getId();
185
186 try {
187 List<ITmfStateInterval> fullState = ss.queryFullState(event.getTime());
188 List<Integer> irqQuarks = ss.getQuarks(Attributes.RESOURCES, Attributes.IRQS, "*"); //$NON-NLS-1$
189
190 for (int irqQuark : irqQuarks) {
191 if (fullState.get(irqQuark).getStateValue().unboxInt() == cpu) {
192 ITmfStateInterval value = ss.querySingleState(event.getTime(), irqQuark);
193 if (!value.getStateValue().isNull()) {
194 int irq = Integer.parseInt(ss.getAttributeName(irqQuark));
195 retMap.put(Messages.ResourcesView_attributeIrqName, String.valueOf(irq));
196 }
197 break;
dedc7dec 198 }
dedc7dec 199 }
4bc53929
GB
200 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
201 Activator.getDefault().logError("Error in ResourcesPresentationProvider", e); //$NON-NLS-1$
4999a196
GB
202 } catch (StateSystemDisposedException e) {
203 /* Ignored */
dedc7dec 204 }
4999a196
GB
205 } else if (status == StateValues.CPU_STATUS_SOFTIRQ) {
206 // In SOFT_IRQ state get the SOFT_IRQ that caused the interruption
4999a196
GB
207 int cpu = entry.getId();
208
209 try {
210 List<ITmfStateInterval> fullState = ss.queryFullState(event.getTime());
211 List<Integer> softIrqQuarks = ss.getQuarks(Attributes.RESOURCES, Attributes.SOFT_IRQS, "*"); //$NON-NLS-1$
212
213 for (int softIrqQuark : softIrqQuarks) {
214 if (fullState.get(softIrqQuark).getStateValue().unboxInt() == cpu) {
215 ITmfStateInterval value = ss.querySingleState(event.getTime(), softIrqQuark);
216 if (!value.getStateValue().isNull()) {
217 int softIrq = Integer.parseInt(ss.getAttributeName(softIrqQuark));
218 retMap.put(Messages.ResourcesView_attributeSoftIrqName, String.valueOf(softIrq));
219 }
220 break;
dedc7dec 221 }
dedc7dec 222 }
4bc53929
GB
223 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
224 Activator.getDefault().logError("Error in ResourcesPresentationProvider", e); //$NON-NLS-1$
4999a196
GB
225 } catch (StateSystemDisposedException e) {
226 /* Ignored */
dedc7dec 227 }
4999a196
GB
228 } else if (status == StateValues.CPU_STATUS_RUN_USERMODE || status == StateValues.CPU_STATUS_RUN_SYSCALL) {
229 // In running state get the current tid
4999a196
GB
230
231 try {
232 retMap.put(Messages.ResourcesView_attributeHoverTime, Utils.formatTime(hoverTime, TimeFormat.CALENDAR, Resolution.NANOSEC));
233 int cpuQuark = entry.getQuark();
4bc53929
GB
234 int currentThreadQuark = ss.getQuarkRelative(cpuQuark, Attributes.CURRENT_THREAD);
235 ITmfStateInterval interval = ss.querySingleState(hoverTime, currentThreadQuark);
dedc7dec 236 if (!interval.getStateValue().isNull()) {
4999a196
GB
237 ITmfStateValue value = interval.getStateValue();
238 int currentThreadId = value.unboxInt();
239 retMap.put(Messages.ResourcesView_attributeTidName, Integer.toString(currentThreadId));
4bc53929
GB
240 int execNameQuark = ss.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThreadId), Attributes.EXEC_NAME);
241 interval = ss.querySingleState(hoverTime, execNameQuark);
dedc7dec
PT
242 if (!interval.getStateValue().isNull()) {
243 value = interval.getStateValue();
4999a196
GB
244 retMap.put(Messages.ResourcesView_attributeProcessName, value.unboxStr());
245 }
246 if (status == StateValues.CPU_STATUS_RUN_SYSCALL) {
4bc53929
GB
247 int syscallQuark = ss.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThreadId), Attributes.SYSTEM_CALL);
248 interval = ss.querySingleState(hoverTime, syscallQuark);
4999a196
GB
249 if (!interval.getStateValue().isNull()) {
250 value = interval.getStateValue();
251 retMap.put(Messages.ResourcesView_attributeSyscallName, value.unboxStr());
252 }
dedc7dec
PT
253 }
254 }
4bc53929
GB
255 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
256 Activator.getDefault().logError("Error in ResourcesPresentationProvider", e); //$NON-NLS-1$
4999a196
GB
257 } catch (StateSystemDisposedException e) {
258 /* Ignored */
dedc7dec 259 }
dedc7dec 260 }
96345c5a 261 }
dedc7dec
PT
262 }
263 }
264
265 return retMap;
266 }
267
713a70ae
PT
268 @Override
269 public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) {
4999a196 270 ITmfTimeGraphDrawingHelper drawingHelper = getDrawingHelper();
713a70ae
PT
271 if (bounds.width <= gc.getFontMetrics().getAverageCharWidth()) {
272 return;
273 }
4999a196
GB
274
275 if (!(event instanceof TimeEvent)) {
713a70ae
PT
276 return;
277 }
4999a196
GB
278 TimeEvent tcEvent = (TimeEvent) event;
279 if (!tcEvent.hasValue()) {
713a70ae
PT
280 return;
281 }
4999a196
GB
282
283 ResourcesEntry entry = (ResourcesEntry) event.getEntry();
284 if (!entry.getType().equals(Type.CPU)) {
285 return;
286 }
287
288 int status = tcEvent.getValue();
713a70ae
PT
289 if (status != StateValues.CPU_STATUS_RUN_USERMODE && status != StateValues.CPU_STATUS_RUN_SYSCALL) {
290 return;
291 }
4999a196 292
ff3f02c8 293 LttngKernelAnalysisModule module = entry.getTrace().getAnalysisModuleOfClass(LttngKernelAnalysisModule.class, LttngKernelAnalysisModule.ID);
1cf25311
PT
294 if (module == null) {
295 return;
296 }
4bc53929
GB
297 ITmfStateSystem ss = module.getStateSystem();
298 if (ss == null) {
299 return;
300 }
713a70ae 301 long time = event.getTime();
a3fbd3f4
PT
302 try {
303 while (time < event.getTime() + event.getDuration()) {
713a70ae
PT
304 int cpuQuark = entry.getQuark();
305 int currentThreadQuark = ss.getQuarkRelative(cpuQuark, Attributes.CURRENT_THREAD);
306 ITmfStateInterval tidInterval = ss.querySingleState(time, currentThreadQuark);
307 if (!tidInterval.getStateValue().isNull()) {
308 ITmfStateValue value = tidInterval.getStateValue();
309 int currentThreadId = value.unboxInt();
310 if (status == StateValues.CPU_STATUS_RUN_USERMODE && currentThreadId != fLastThreadId) {
311 int execNameQuark = ss.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThreadId), Attributes.EXEC_NAME);
312 ITmfStateInterval interval = ss.querySingleState(time, execNameQuark);
313 if (!interval.getStateValue().isNull()) {
314 value = interval.getStateValue();
315 gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
316 long startTime = Math.max(tidInterval.getStartTime(), event.getTime());
317 long endTime = Math.min(tidInterval.getEndTime() + 1, event.getTime() + event.getDuration());
4999a196
GB
318 if (drawingHelper.getXForTime(endTime) > bounds.x) {
319 int x = Math.max(drawingHelper.getXForTime(startTime), bounds.x);
320 int width = Math.min(drawingHelper.getXForTime(endTime), bounds.x + bounds.width) - x;
713a70ae
PT
321 int drawn = Utils.drawText(gc, value.unboxStr(), x + 1, bounds.y - 2, width - 1, true, true);
322 if (drawn > 0) {
323 fLastThreadId = currentThreadId;
324 }
325 }
326 }
327 } else if (status == StateValues.CPU_STATUS_RUN_SYSCALL) {
328 int syscallQuark = ss.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThreadId), Attributes.SYSTEM_CALL);
329 ITmfStateInterval interval = ss.querySingleState(time, syscallQuark);
330 if (!interval.getStateValue().isNull()) {
331 value = interval.getStateValue();
332 gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
333 long startTime = Math.max(tidInterval.getStartTime(), event.getTime());
334 long endTime = Math.min(tidInterval.getEndTime() + 1, event.getTime() + event.getDuration());
4999a196
GB
335 if (drawingHelper.getXForTime(endTime) > bounds.x) {
336 int x = Math.max(drawingHelper.getXForTime(startTime), bounds.x);
337 int width = Math.min(drawingHelper.getXForTime(endTime), bounds.x + bounds.width) - x;
713a70ae
PT
338 Utils.drawText(gc, value.unboxStr().substring(4), x + 1, bounds.y - 2, width - 1, true, true);
339 }
340 }
341 }
342 }
343 time = tidInterval.getEndTime() + 1;
344 if (time < event.getTime() + event.getDuration()) {
4999a196 345 int x = drawingHelper.getXForTime(time);
713a70ae
PT
346 if (x >= bounds.x) {
347 gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_GRAY));
348 gc.drawLine(x, bounds.y + 1, x, bounds.y + bounds.height - 2);
349 }
350 }
713a70ae 351 }
4bc53929
GB
352 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
353 Activator.getDefault().logError("Error in ResourcesPresentationProvider", e); //$NON-NLS-1$
a3fbd3f4
PT
354 } catch (StateSystemDisposedException e) {
355 /* Ignored */
713a70ae
PT
356 }
357 }
358
359 @Override
360 public void postDrawEntry(ITimeGraphEntry entry, Rectangle bounds, GC gc) {
361 fLastThreadId = -1;
362 }
dedc7dec 363}
This page took 0.11776 seconds and 5 git commands to generate.