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