Support for upgrading Linux Tools features to Trace Compass
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.kernel.ui / src / org / eclipse / tracecompass / internal / lttng2 / kernel / ui / views / resources / ResourcesView.java
CommitLineData
6151d86c 1/*******************************************************************************
1cf25311 2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
6151d86c
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 views
6151d86c
PT
12 *******************************************************************************/
13
9bc60be7 14package org.eclipse.tracecompass.internal.lttng2.kernel.ui.views.resources;
6151d86c
PT
15
16import java.util.ArrayList;
1cf25311 17import java.util.Collections;
9ba941e9 18import java.util.Comparator;
1cf25311 19import java.util.HashMap;
6151d86c 20import java.util.List;
1cf25311 21import java.util.Map;
6151d86c
PT
22
23import org.eclipse.core.runtime.IProgressMonitor;
d2120fb6 24import org.eclipse.jdt.annotation.Nullable;
9bc60be7
AM
25import org.eclipse.tracecompass.internal.lttng2.kernel.core.Attributes;
26import org.eclipse.tracecompass.internal.lttng2.kernel.ui.Messages;
27import org.eclipse.tracecompass.internal.lttng2.kernel.ui.views.resources.ResourcesEntry.Type;
42d5b5f2 28import org.eclipse.tracecompass.lttng2.kernel.core.analysis.kernel.LttngKernelAnalysis;
e894a508 29import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
1dd75589 30import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
e894a508
AM
31import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
32import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
33import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
34import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
35import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
2bdf0193
AM
36import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
37import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
38import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractTimeGraphView;
39import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
40import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
41import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
42import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
43import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
6151d86c
PT
44
45/**
46 * Main implementation for the LTTng 2.0 kernel Resource view
47 *
48 * @author Patrick Tasse
49 */
4999a196 50public class ResourcesView extends AbstractTimeGraphView {
6151d86c
PT
51
52 /** View ID. */
53 public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.ui.views.resources"; //$NON-NLS-1$
54
4999a196 55 private static final String[] FILTER_COLUMN_NAMES = new String[] {
747adf5c 56 Messages.ResourcesView_stateTypeName
4999a196 57 };
6151d86c 58
1cf25311
PT
59 // Timeout between updates in the build thread in ms
60 private static final long BUILD_UPDATE_TIMEOUT = 500;
61
6151d86c
PT
62 // ------------------------------------------------------------------------
63 // Constructors
64 // ------------------------------------------------------------------------
65
66 /**
67 * Default constructor
68 */
69 public ResourcesView() {
747adf5c
PT
70 super(ID, new ResourcesPresentationProvider());
71 setFilterColumns(FILTER_COLUMN_NAMES);
6151d86c
PT
72 }
73
1cf25311
PT
74 // ------------------------------------------------------------------------
75 // Internal
76 // ------------------------------------------------------------------------
77
6151d86c 78 @Override
4999a196
GB
79 protected String getNextText() {
80 return Messages.ResourcesView_nextResourceActionNameText;
6151d86c
PT
81 }
82
6151d86c 83 @Override
4999a196
GB
84 protected String getNextTooltip() {
85 return Messages.ResourcesView_nextResourceActionToolTipText;
fec1ac0b
BH
86 }
87
4999a196
GB
88 @Override
89 protected String getPrevText() {
90 return Messages.ResourcesView_previousResourceActionNameText;
6151d86c
PT
91 }
92
4999a196
GB
93 @Override
94 protected String getPrevTooltip() {
95 return Messages.ResourcesView_previousResourceActionToolTipText;
6151d86c
PT
96 }
97
4999a196 98 @Override
1cf25311 99 protected void buildEventList(ITmfTrace trace, ITmfTrace parentTrace, IProgressMonitor monitor) {
42d5b5f2 100 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, LttngKernelAnalysis.ID);
1cf25311
PT
101 if (ssq == null) {
102 return;
103 }
9ba941e9
PT
104 Comparator<ITimeGraphEntry> comparator = new Comparator<ITimeGraphEntry>() {
105 @Override
106 public int compare(ITimeGraphEntry o1, ITimeGraphEntry o2) {
107 return ((ResourcesEntry) o1).compareTo(o2);
108 }
109 };
fec1ac0b 110
1cf25311
PT
111 Map<Integer, ResourcesEntry> entryMap = new HashMap<>();
112 TimeGraphEntry traceEntry = null;
113
114 long startTime = ssq.getStartTime();
115 long start = startTime;
116 setStartTime(Math.min(getStartTime(), startTime));
117 boolean complete = false;
118 while (!complete) {
faa38350
PT
119 if (monitor.isCanceled()) {
120 return;
121 }
1cf25311
PT
122 complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
123 if (ssq.isCancelled()) {
124 return;
125 }
126 long end = ssq.getCurrentEndTime();
127 if (start == end && !complete) { // when complete execute one last time regardless of end time
128 continue;
129 }
130 long endTime = end + 1;
131 setEndTime(Math.max(getEndTime(), endTime));
132
133 if (traceEntry == null) {
134 traceEntry = new ResourcesEntry(trace, trace.getName(), startTime, endTime, 0);
9ba941e9 135 traceEntry.sortChildren(comparator);
1cf25311
PT
136 List<TimeGraphEntry> entryList = Collections.singletonList(traceEntry);
137 addToEntryList(parentTrace, entryList);
138 } else {
139 traceEntry.updateEndTime(endTime);
140 }
141
142 List<Integer> cpuQuarks = ssq.getQuarks(Attributes.CPUS, "*"); //$NON-NLS-1$
143 for (Integer cpuQuark : cpuQuarks) {
144 int cpu = Integer.parseInt(ssq.getAttributeName(cpuQuark));
145 ResourcesEntry entry = entryMap.get(cpuQuark);
146 if (entry == null) {
147 entry = new ResourcesEntry(cpuQuark, trace, startTime, endTime, Type.CPU, cpu);
148 entryMap.put(cpuQuark, entry);
149 traceEntry.addChild(entry);
150 } else {
151 entry.updateEndTime(endTime);
6151d86c 152 }
1cf25311
PT
153 }
154 List<Integer> irqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.IRQS, "*"); //$NON-NLS-1$
155 for (Integer irqQuark : irqQuarks) {
156 int irq = Integer.parseInt(ssq.getAttributeName(irqQuark));
157 ResourcesEntry entry = entryMap.get(irqQuark);
158 if (entry == null) {
159 entry = new ResourcesEntry(irqQuark, trace, startTime, endTime, Type.IRQ, irq);
160 entryMap.put(irqQuark, entry);
161 traceEntry.addChild(entry);
162 } else {
163 entry.updateEndTime(endTime);
6151d86c 164 }
1cf25311
PT
165 }
166 List<Integer> softIrqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.SOFT_IRQS, "*"); //$NON-NLS-1$
167 for (Integer softIrqQuark : softIrqQuarks) {
168 int softIrq = Integer.parseInt(ssq.getAttributeName(softIrqQuark));
169 ResourcesEntry entry = entryMap.get(softIrqQuark);
170 if (entry == null) {
171 entry = new ResourcesEntry(softIrqQuark, trace, startTime, endTime, Type.SOFT_IRQ, softIrq);
172 entryMap.put(softIrqQuark, entry);
173 traceEntry.addChild(entry);
174 } else {
175 entry.updateEndTime(endTime);
6151d86c
PT
176 }
177 }
4999a196 178
1cf25311
PT
179 if (parentTrace.equals(getTrace())) {
180 refresh();
faa38350 181 }
1cf25311 182 long resolution = Math.max(1, (endTime - ssq.getStartTime()) / getDisplayWidth());
a3188982 183 for (ITimeGraphEntry child : traceEntry.getChildren()) {
1cf25311
PT
184 if (monitor.isCanceled()) {
185 return;
186 }
a3188982
PT
187 if (child instanceof TimeGraphEntry) {
188 TimeGraphEntry entry = (TimeGraphEntry) child;
189 List<ITimeEvent> eventList = getEventList(entry, start, endTime, resolution, monitor);
190 if (eventList != null) {
191 for (ITimeEvent event : eventList) {
192 entry.addEvent(event);
193 }
1cf25311 194 }
a3188982 195 redraw();
1cf25311 196 }
6151d86c 197 }
1cf25311
PT
198
199 start = end;
6151d86c
PT
200 }
201 }
202
4999a196 203 @Override
d2120fb6 204 protected @Nullable List<ITimeEvent> getEventList(TimeGraphEntry entry,
4999a196 205 long startTime, long endTime, long resolution,
6151d86c 206 IProgressMonitor monitor) {
1d46dc38 207 ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
42d5b5f2 208 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(resourcesEntry.getTrace(), LttngKernelAnalysis.ID);
4bc53929
GB
209 if (ssq == null) {
210 return null;
211 }
41b5c37f
AM
212 final long realStart = Math.max(startTime, ssq.getStartTime());
213 final long realEnd = Math.min(endTime, ssq.getCurrentEndTime() + 1);
214 if (realEnd <= realStart) {
6151d86c
PT
215 return null;
216 }
217 List<ITimeEvent> eventList = null;
4999a196
GB
218 int quark = resourcesEntry.getQuark();
219
6151d86c 220 try {
4999a196 221 if (resourcesEntry.getType().equals(Type.CPU)) {
8f9ad413
AM
222 int statusQuark;
223 try {
224 statusQuark = ssq.getQuarkRelative(quark, Attributes.STATUS);
225 } catch (AttributeNotFoundException e) {
226 /*
227 * The sub-attribute "status" is not available. May happen
228 * if the trace does not have sched_switch events enabled.
229 */
230 return null;
231 }
1dd75589 232 List<ITmfStateInterval> statusIntervals = StateSystemUtils.queryHistoryRange(ssq, statusQuark, realStart, realEnd - 1, resolution, monitor);
e0838ca1 233 eventList = new ArrayList<>(statusIntervals.size());
6151d86c
PT
234 long lastEndTime = -1;
235 for (ITmfStateInterval statusInterval : statusIntervals) {
236 if (monitor.isCanceled()) {
237 return null;
238 }
239 int status = statusInterval.getStateValue().unboxInt();
240 long time = statusInterval.getStartTime();
241 long duration = statusInterval.getEndTime() - time + 1;
242 if (!statusInterval.getStateValue().isNull()) {
243 if (lastEndTime != time && lastEndTime != -1) {
244 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
245 }
4999a196 246 eventList.add(new TimeEvent(entry, time, duration, status));
1d46dc38
PT
247 } else if (lastEndTime == -1 || time + duration >= endTime) {
248 // add null event if it intersects the start or end time
249 eventList.add(new NullTimeEvent(entry, time, duration));
6151d86c 250 }
1d46dc38 251 lastEndTime = time + duration;
6151d86c 252 }
4999a196 253 } else if (resourcesEntry.getType().equals(Type.IRQ)) {
1dd75589 254 List<ITmfStateInterval> irqIntervals = StateSystemUtils.queryHistoryRange(ssq, quark, realStart, realEnd - 1, resolution, monitor);
e0838ca1 255 eventList = new ArrayList<>(irqIntervals.size());
6151d86c
PT
256 long lastEndTime = -1;
257 boolean lastIsNull = true;
258 for (ITmfStateInterval irqInterval : irqIntervals) {
259 if (monitor.isCanceled()) {
260 return null;
261 }
262 long time = irqInterval.getStartTime();
263 long duration = irqInterval.getEndTime() - time + 1;
264 if (!irqInterval.getStateValue().isNull()) {
265 int cpu = irqInterval.getStateValue().unboxInt();
4999a196 266 eventList.add(new TimeEvent(entry, time, duration, cpu));
6151d86c
PT
267 lastIsNull = false;
268 } else {
1d46dc38
PT
269 if (lastEndTime == -1) {
270 // add null event if it intersects the start time
271 eventList.add(new NullTimeEvent(entry, time, duration));
4999a196 272 } else {
1d46dc38
PT
273 if (lastEndTime != time && lastIsNull) {
274 /* This is a special case where we want to show IRQ_ACTIVE state but we don't know the CPU (it is between two null samples) */
275 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
276 }
277 if (time + duration >= endTime) {
278 // add null event if it intersects the end time
279 eventList.add(new NullTimeEvent(entry, time, duration));
280 }
6151d86c
PT
281 }
282 lastIsNull = true;
283 }
284 lastEndTime = time + duration;
285 }
4999a196 286 } else if (resourcesEntry.getType().equals(Type.SOFT_IRQ)) {
1dd75589 287 List<ITmfStateInterval> softIrqIntervals = StateSystemUtils.queryHistoryRange(ssq, quark, realStart, realEnd - 1, resolution, monitor);
e0838ca1 288 eventList = new ArrayList<>(softIrqIntervals.size());
6151d86c
PT
289 long lastEndTime = -1;
290 boolean lastIsNull = true;
291 for (ITmfStateInterval softIrqInterval : softIrqIntervals) {
292 if (monitor.isCanceled()) {
293 return null;
294 }
295 long time = softIrqInterval.getStartTime();
296 long duration = softIrqInterval.getEndTime() - time + 1;
297 if (!softIrqInterval.getStateValue().isNull()) {
298 int cpu = softIrqInterval.getStateValue().unboxInt();
4999a196 299 eventList.add(new TimeEvent(entry, time, duration, cpu));
6151d86c 300 } else {
1d46dc38
PT
301 if (lastEndTime == -1) {
302 // add null event if it intersects the start time
303 eventList.add(new NullTimeEvent(entry, time, duration));
4999a196 304 } else {
1d46dc38
PT
305 if (lastEndTime != time && lastIsNull) {
306 /* This is a special case where we want to show IRQ_ACTIVE state but we don't know the CPU (it is between two null samples) */
307 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
308 }
309 if (time + duration >= endTime) {
310 // add null event if it intersects the end time
311 eventList.add(new NullTimeEvent(entry, time, duration));
312 }
6151d86c
PT
313 }
314 lastIsNull = true;
315 }
316 lastEndTime = time + duration;
317 }
318 }
4999a196 319
1cf25311 320 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
6151d86c 321 e.printStackTrace();
96345c5a
AM
322 } catch (StateSystemDisposedException e) {
323 /* Ignored */
6151d86c
PT
324 }
325 return eventList;
326 }
327
6151d86c 328}
This page took 0.081254 seconds and 5 git commands to generate.