TMF: Consolidate some view code into the AbstractTimeGraphView
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.ui / src / org / eclipse / linuxtools / internal / lttng2 / kernel / ui / views / resources / ResourcesView.java
CommitLineData
6151d86c 1/*******************************************************************************
4999a196 2 * Copyright (c) 2012, 2013 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
14package org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources;
15
16import java.util.ArrayList;
6151d86c
PT
17import java.util.List;
18
19import org.eclipse.core.runtime.IProgressMonitor;
6151d86c
PT
20import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
21import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages;
22import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources.ResourcesEntry.Type;
d3ba47d4 23import org.eclipse.linuxtools.lttng2.kernel.core.trace.LttngKernelTrace;
6151d86c 24import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
96345c5a 25import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException;
6151d86c
PT
26import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
27import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
28import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
f1f86dfb 29import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
6151d86c 30import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
4999a196 31import org.eclipse.linuxtools.tmf.ui.views.timegraph.AbstractTimeGraphView;
6151d86c 32import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
6151d86c 33import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent;
4999a196 34import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
6151d86c
PT
35
36/**
37 * Main implementation for the LTTng 2.0 kernel Resource view
38 *
39 * @author Patrick Tasse
40 */
4999a196 41public class ResourcesView extends AbstractTimeGraphView {
6151d86c
PT
42
43 /** View ID. */
44 public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.ui.views.resources"; //$NON-NLS-1$
45
6151d86c 46 /**
4999a196
GB
47 * Default value for events with no other value. Since value in this case is
48 * often a CPU number, this constant should be <0
6151d86c 49 */
4999a196 50 public static final int NO_VALUE_EVENT = -999;
6151d86c 51
4999a196 52 private static final String PROCESS_COLUMN = Messages.ControlFlowView_processColumn;
6151d86c 53
4999a196
GB
54 private static final String[] COLUMN_NAMES = new String[] {
55 PROCESS_COLUMN
56 };
6151d86c 57
4999a196
GB
58 private static final String[] FILTER_COLUMN_NAMES = new String[] {
59 PROCESS_COLUMN
60 };
6151d86c 61
4999a196 62 private static final int[] WEIGHTS = { 15, 85 };
6151d86c
PT
63
64 // ------------------------------------------------------------------------
65 // Constructors
66 // ------------------------------------------------------------------------
67
68 /**
69 * Default constructor
70 */
71 public ResourcesView() {
4999a196
GB
72 super(ID, COLUMN_NAMES, FILTER_COLUMN_NAMES, new ResourcesPresentationProvider());
73 setWeight(WEIGHTS);
6151d86c
PT
74 }
75
6151d86c 76 @Override
4999a196
GB
77 protected String getNextText() {
78 return Messages.ResourcesView_nextResourceActionNameText;
6151d86c
PT
79 }
80
6151d86c 81 @Override
4999a196
GB
82 protected String getNextTooltip() {
83 return Messages.ResourcesView_nextResourceActionToolTipText;
fec1ac0b
BH
84 }
85
4999a196
GB
86 @Override
87 protected String getPrevText() {
88 return Messages.ResourcesView_previousResourceActionNameText;
6151d86c
PT
89 }
90
4999a196
GB
91 @Override
92 protected String getPrevTooltip() {
93 return Messages.ResourcesView_previousResourceActionToolTipText;
6151d86c
PT
94 }
95
6151d86c
PT
96 // ------------------------------------------------------------------------
97 // Internal
98 // ------------------------------------------------------------------------
99
4999a196
GB
100 @Override
101 protected void buildEventList(ITmfTrace trace, IProgressMonitor monitor) {
102 setStartTime(Long.MAX_VALUE);
103 setEndTime(Long.MIN_VALUE);
fec1ac0b 104
4999a196
GB
105 ArrayList<ResourcesEntry> entryList = new ArrayList<ResourcesEntry>();
106 for (ITmfTrace aTrace : fTraceManager.getActiveTraceSet()) {
faa38350
PT
107 if (monitor.isCanceled()) {
108 return;
109 }
d3ba47d4 110 if (aTrace instanceof LttngKernelTrace) {
4999a196
GB
111 LttngKernelTrace lttngKernelTrace = (LttngKernelTrace) aTrace;
112 ITmfStateSystem ssq = lttngKernelTrace.getStateSystems().get(LttngKernelTrace.STATE_ID);
faa38350
PT
113 if (!ssq.waitUntilBuilt()) {
114 return;
115 }
6151d86c
PT
116 long startTime = ssq.getStartTime();
117 long endTime = ssq.getCurrentEndTime() + 1;
4999a196 118 ResourcesEntry groupEntry = new ResourcesEntry(lttngKernelTrace, aTrace.getName(), startTime, endTime, 0);
6151d86c 119 entryList.add(groupEntry);
4999a196
GB
120 setStartTime(Math.min(getStartTime(), startTime));
121 setEndTime(Math.max(getEndTime(), endTime));
6151d86c
PT
122 List<Integer> cpuQuarks = ssq.getQuarks(Attributes.CPUS, "*"); //$NON-NLS-1$
123 ResourcesEntry[] cpuEntries = new ResourcesEntry[cpuQuarks.size()];
124 for (int i = 0; i < cpuQuarks.size(); i++) {
125 int cpuQuark = cpuQuarks.get(i);
126 int cpu = Integer.parseInt(ssq.getAttributeName(cpuQuark));
4999a196 127 ResourcesEntry entry = new ResourcesEntry(cpuQuark, lttngKernelTrace, getStartTime(), getEndTime(), Type.CPU, cpu);
6151d86c
PT
128 groupEntry.addChild(entry);
129 cpuEntries[i] = entry;
130 }
131 List<Integer> irqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.IRQS, "*"); //$NON-NLS-1$
132 ResourcesEntry[] irqEntries = new ResourcesEntry[irqQuarks.size()];
133 for (int i = 0; i < irqQuarks.size(); i++) {
134 int irqQuark = irqQuarks.get(i);
135 int irq = Integer.parseInt(ssq.getAttributeName(irqQuark));
4999a196 136 ResourcesEntry entry = new ResourcesEntry(irqQuark, lttngKernelTrace, getStartTime(), getEndTime(), Type.IRQ, irq);
6151d86c
PT
137 groupEntry.addChild(entry);
138 irqEntries[i] = entry;
139 }
140 List<Integer> softIrqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.SOFT_IRQS, "*"); //$NON-NLS-1$
141 ResourcesEntry[] softIrqEntries = new ResourcesEntry[softIrqQuarks.size()];
142 for (int i = 0; i < softIrqQuarks.size(); i++) {
143 int softIrqQuark = softIrqQuarks.get(i);
144 int softIrq = Integer.parseInt(ssq.getAttributeName(softIrqQuark));
4999a196 145 ResourcesEntry entry = new ResourcesEntry(softIrqQuark, lttngKernelTrace, getStartTime(), getEndTime(), Type.SOFT_IRQ, softIrq);
6151d86c
PT
146 groupEntry.addChild(entry);
147 softIrqEntries[i] = entry;
148 }
149 }
150 }
4999a196
GB
151 putEntryList(trace, (ArrayList<TimeGraphEntry>) entryList.clone());
152
153 if (trace.equals(getTrace())) {
d7ee91bb 154 refresh();
6151d86c 155 }
4999a196 156 for (ResourcesEntry traceEntry : entryList) {
faa38350
PT
157 if (monitor.isCanceled()) {
158 return;
159 }
4999a196
GB
160 LttngKernelTrace lttngKernelTrace = traceEntry.getTrace();
161 ITmfStateSystem ssq = lttngKernelTrace.getStateSystems().get(LttngKernelTrace.STATE_ID);
6151d86c
PT
162 long startTime = ssq.getStartTime();
163 long endTime = ssq.getCurrentEndTime() + 1;
4999a196
GB
164 long resolution = (endTime - startTime) / getDisplayWidth();
165 for (TimeGraphEntry entry : traceEntry.getChildren()) {
166 List<ITimeEvent> eventList = getEventList(entry, startTime, endTime, resolution, monitor);
6151d86c
PT
167 entry.setEventList(eventList);
168 redraw();
169 }
170 }
171 }
172
4999a196
GB
173 @Override
174 protected List<ITimeEvent> getEventList(TimeGraphEntry entry,
175 long startTime, long endTime, long resolution,
6151d86c 176 IProgressMonitor monitor) {
d3ba47d4 177 ITmfStateSystem ssq = entry.getTrace().getStateSystems().get(LttngKernelTrace.STATE_ID);
41b5c37f
AM
178 final long realStart = Math.max(startTime, ssq.getStartTime());
179 final long realEnd = Math.min(endTime, ssq.getCurrentEndTime() + 1);
180 if (realEnd <= realStart) {
6151d86c
PT
181 return null;
182 }
183 List<ITimeEvent> eventList = null;
4999a196
GB
184
185 if (!(entry instanceof ResourcesEntry)) {
186 return eventList;
187 }
188 ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
189 int quark = resourcesEntry.getQuark();
190
6151d86c 191 try {
4999a196 192 if (resourcesEntry.getType().equals(Type.CPU)) {
6151d86c 193 int statusQuark = ssq.getQuarkRelative(quark, Attributes.STATUS);
41b5c37f 194 List<ITmfStateInterval> statusIntervals = ssq.queryHistoryRange(statusQuark, realStart, realEnd - 1, resolution, monitor);
6151d86c
PT
195 eventList = new ArrayList<ITimeEvent>(statusIntervals.size());
196 long lastEndTime = -1;
197 for (ITmfStateInterval statusInterval : statusIntervals) {
198 if (monitor.isCanceled()) {
199 return null;
200 }
201 int status = statusInterval.getStateValue().unboxInt();
202 long time = statusInterval.getStartTime();
203 long duration = statusInterval.getEndTime() - time + 1;
204 if (!statusInterval.getStateValue().isNull()) {
205 if (lastEndTime != time && lastEndTime != -1) {
206 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
207 }
4999a196 208 eventList.add(new TimeEvent(entry, time, duration, status));
6151d86c
PT
209 lastEndTime = time + duration;
210 } else {
4999a196
GB
211 if (true) {// includeNull) {
212 eventList.add(new TimeEvent(entry, time, duration, NO_VALUE_EVENT));
6151d86c
PT
213 }
214 }
215 }
4999a196 216 } else if (resourcesEntry.getType().equals(Type.IRQ)) {
41b5c37f 217 List<ITmfStateInterval> irqIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor);
6151d86c
PT
218 eventList = new ArrayList<ITimeEvent>(irqIntervals.size());
219 long lastEndTime = -1;
220 boolean lastIsNull = true;
221 for (ITmfStateInterval irqInterval : irqIntervals) {
222 if (monitor.isCanceled()) {
223 return null;
224 }
225 long time = irqInterval.getStartTime();
226 long duration = irqInterval.getEndTime() - time + 1;
227 if (!irqInterval.getStateValue().isNull()) {
228 int cpu = irqInterval.getStateValue().unboxInt();
4999a196 229 eventList.add(new TimeEvent(entry, time, duration, cpu));
6151d86c
PT
230 lastIsNull = false;
231 } else {
232 if (lastEndTime != time && lastEndTime != -1 && lastIsNull) {
4999a196
GB
233 /* 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) */
234 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
235 } else {
236 eventList.add(new TimeEvent(entry, time, duration, NO_VALUE_EVENT));
6151d86c
PT
237 }
238 lastIsNull = true;
239 }
240 lastEndTime = time + duration;
241 }
4999a196 242 } else if (resourcesEntry.getType().equals(Type.SOFT_IRQ)) {
41b5c37f 243 List<ITmfStateInterval> softIrqIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor);
6151d86c
PT
244 eventList = new ArrayList<ITimeEvent>(softIrqIntervals.size());
245 long lastEndTime = -1;
246 boolean lastIsNull = true;
247 for (ITmfStateInterval softIrqInterval : softIrqIntervals) {
248 if (monitor.isCanceled()) {
249 return null;
250 }
251 long time = softIrqInterval.getStartTime();
252 long duration = softIrqInterval.getEndTime() - time + 1;
253 if (!softIrqInterval.getStateValue().isNull()) {
254 int cpu = softIrqInterval.getStateValue().unboxInt();
4999a196 255 eventList.add(new TimeEvent(entry, time, duration, cpu));
6151d86c
PT
256 } else {
257 if (lastEndTime != time && lastEndTime != -1 && lastIsNull) {
4999a196
GB
258 /* 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) */
259 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
260 } else {
261 eventList.add(new TimeEvent(entry, time, duration, NO_VALUE_EVENT));
6151d86c
PT
262 }
263 lastIsNull = true;
264 }
265 lastEndTime = time + duration;
266 }
267 }
4999a196 268
6151d86c
PT
269 } catch (AttributeNotFoundException e) {
270 e.printStackTrace();
271 } catch (TimeRangeException e) {
272 e.printStackTrace();
273 } catch (StateValueTypeException e) {
274 e.printStackTrace();
96345c5a
AM
275 } catch (StateSystemDisposedException e) {
276 /* Ignored */
6151d86c
PT
277 }
278 return eventList;
279 }
280
6151d86c 281}
This page took 0.047037 seconds and 5 git commands to generate.