e1b2ec4694d02f20f3ff50a0bb51643bbaa1692c
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / analysis / os / linux / ui / views / resources / ResourcesView.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2016 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 views
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.analysis.os.linux.ui.views.resources;
15
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.Comparator;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.eclipse.jface.action.IMenuManager;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.tracecompass.analysis.os.linux.core.kernel.Attributes;
32 import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule;
33 import org.eclipse.tracecompass.analysis.os.linux.core.signals.TmfCpuSelectedSignal;
34 import org.eclipse.tracecompass.analysis.os.linux.ui.views.resources.ResourcesEntry.Type;
35 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Messages;
36 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.actions.FollowCpuAction;
37 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.actions.UnfollowCpuAction;
38 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
39 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
40 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
41 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
42 import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
43 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
44 import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractStateSystemTimeGraphView;
45 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
46 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
47 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
48 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
49 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
50
51 /**
52 * Main implementation for the LTTng 2.0 kernel Resource view
53 *
54 * @author Patrick Tasse
55 */
56 public class ResourcesView extends AbstractStateSystemTimeGraphView {
57
58 /** View ID. */
59 public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.views.resources"; //$NON-NLS-1$
60
61 private static final String[] FILTER_COLUMN_NAMES = new String[] {
62 Messages.ResourcesView_stateTypeName
63 };
64
65 private int fCurrentCpu = -1;
66
67 // Timeout between updates in the build thread in ms
68 private static final long BUILD_UPDATE_TIMEOUT = 500;
69
70 // ------------------------------------------------------------------------
71 // Constructors
72 // ------------------------------------------------------------------------
73
74 /**
75 * Default constructor
76 */
77 public ResourcesView() {
78 super(ID, new ResourcesPresentationProvider());
79 setFilterColumns(FILTER_COLUMN_NAMES);
80 setFilterLabelProvider(new ResourcesFilterLabelProvider());
81 setEntryComparator(new ResourcesEntryComparator());
82 setAutoExpandLevel(1);
83 }
84
85 private static class ResourcesEntryComparator implements Comparator<ITimeGraphEntry> {
86 @Override
87 public int compare(ITimeGraphEntry o1, ITimeGraphEntry o2) {
88 ResourcesEntry entry1 = (ResourcesEntry) o1;
89 ResourcesEntry entry2 = (ResourcesEntry) o2;
90 if (entry1.getType() == Type.NULL && entry2.getType() == Type.NULL) {
91 /* sort trace entries alphabetically */
92 return entry1.getName().compareTo(entry2.getName());
93 }
94 /* sort resource entries by their defined order */
95 return entry1.compareTo(entry2);
96 }
97 }
98
99
100 /**
101 * @since 2.0
102 */
103 @Override
104 protected void fillTimeGraphEntryContextMenu(@NonNull IMenuManager menuManager) {
105 ISelection selection = getSite().getSelectionProvider().getSelection();
106 if (selection instanceof IStructuredSelection) {
107 IStructuredSelection sSel = (IStructuredSelection) selection;
108 if (sSel.getFirstElement() instanceof ResourcesEntry) {
109 ResourcesEntry resourcesEntry = (ResourcesEntry) sSel.getFirstElement();
110 if (resourcesEntry.getType().equals(ResourcesEntry.Type.CPU)) {
111 if (fCurrentCpu >= 0) {
112 menuManager.add(new UnfollowCpuAction(ResourcesView.this, resourcesEntry.getId(), resourcesEntry.getTrace()));
113 } else {
114 menuManager.add(new FollowCpuAction(ResourcesView.this, resourcesEntry.getId(), resourcesEntry.getTrace()));
115 }
116 }
117 }
118 }
119 }
120
121 private static class ResourcesFilterLabelProvider extends TreeLabelProvider {
122 @Override
123 public String getColumnText(Object element, int columnIndex) {
124 ResourcesEntry entry = (ResourcesEntry) element;
125 if (columnIndex == 0) {
126 return entry.getName();
127 }
128 return ""; //$NON-NLS-1$
129 }
130
131 }
132
133 // ------------------------------------------------------------------------
134 // Internal
135 // ------------------------------------------------------------------------
136
137 @Override
138 protected String getNextText() {
139 return Messages.ResourcesView_nextResourceActionNameText;
140 }
141
142 @Override
143 protected String getNextTooltip() {
144 return Messages.ResourcesView_nextResourceActionToolTipText;
145 }
146
147 @Override
148 protected String getPrevText() {
149 return Messages.ResourcesView_previousResourceActionNameText;
150 }
151
152 @Override
153 protected String getPrevTooltip() {
154 return Messages.ResourcesView_previousResourceActionToolTipText;
155 }
156
157 @Override
158 protected void buildEventList(ITmfTrace trace, ITmfTrace parentTrace, final IProgressMonitor monitor) {
159 final ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
160 if (ssq == null) {
161 return;
162 }
163
164 Map<Integer, ResourcesEntry> entryMap = new HashMap<>();
165 TimeGraphEntry traceEntry = null;
166
167 long startTime = ssq.getStartTime();
168 long start = startTime;
169 setStartTime(Math.min(getStartTime(), startTime));
170 boolean complete = false;
171 while (!complete) {
172 if (monitor.isCanceled()) {
173 return;
174 }
175 complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
176 if (ssq.isCancelled()) {
177 return;
178 }
179 long end = ssq.getCurrentEndTime();
180 if (start == end && !complete) {
181 // when complete execute one last time regardless of end time
182 continue;
183 }
184 long endTime = end + 1;
185 setEndTime(Math.max(getEndTime(), endTime));
186
187 if (traceEntry == null) {
188 traceEntry = new ResourcesEntry(trace, trace.getName(), startTime, endTime, 0);
189 List<TimeGraphEntry> entryList = Collections.singletonList(traceEntry);
190 addToEntryList(parentTrace, ssq, entryList);
191 } else {
192 traceEntry.updateEndTime(endTime);
193 }
194 List<Integer> cpuQuarks = ssq.getQuarks(Attributes.CPUS, "*"); //$NON-NLS-1$
195 createCpuEntriesWithQuark(trace, ssq, entryMap, traceEntry, startTime, endTime, cpuQuarks);
196 if (parentTrace.equals(getTrace())) {
197 refresh();
198 }
199 final List<? extends ITimeGraphEntry> traceEntryChildren = traceEntry.getChildren();
200 final long resolution = Math.max(1, (endTime - ssq.getStartTime()) / getDisplayWidth());
201 final long qStart = start;
202 final long qEnd = end;
203 queryFullStates(ssq, qStart, qEnd, resolution, monitor, new IQueryHandler() {
204 @Override
205 public void handle(List<List<ITmfStateInterval>> fullStates, List<ITmfStateInterval> prevFullState) {
206 for (ITimeGraphEntry child : traceEntryChildren) {
207 if (!populateEventsRecursively(fullStates, prevFullState, child).isOK()) {
208 return;
209 }
210 }
211 }
212
213 private IStatus populateEventsRecursively(@NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, ITimeGraphEntry entry) {
214 if (monitor.isCanceled()) {
215 return Status.CANCEL_STATUS;
216 }
217 if (entry instanceof TimeGraphEntry) {
218 TimeGraphEntry timeGraphEntry = (TimeGraphEntry) entry;
219 List<ITimeEvent> eventList = getEventList(timeGraphEntry, ssq, fullStates, prevFullState, monitor);
220 if (eventList != null) {
221 for (ITimeEvent event : eventList) {
222 timeGraphEntry.addEvent(event);
223 }
224 }
225 }
226 for (ITimeGraphEntry child : entry.getChildren()) {
227 IStatus status = populateEventsRecursively(fullStates, prevFullState, child);
228 if (!status.isOK()) {
229 return status;
230 }
231 }
232 return Status.OK_STATUS;
233 }
234 });
235
236 start = end;
237 }
238
239 }
240
241 private static void createCpuEntriesWithQuark(@NonNull ITmfTrace trace, final ITmfStateSystem ssq, Map<Integer, ResourcesEntry> entryMap, TimeGraphEntry traceEntry, long startTime, long endTime, List<Integer> cpuQuarks) {
242 for (Integer cpuQuark : cpuQuarks) {
243 final @NonNull String cpuName = ssq.getAttributeName(cpuQuark);
244 int cpu = Integer.parseInt(cpuName);
245 ResourcesEntry cpuEntry = entryMap.get(cpuQuark);
246 if (cpuEntry == null) {
247 cpuEntry = new ResourcesEntry(cpuQuark, trace, startTime, endTime, Type.CPU, cpu);
248 entryMap.put(cpuQuark, cpuEntry);
249 traceEntry.addChild(cpuEntry);
250 } else {
251 cpuEntry.updateEndTime(endTime);
252 }
253 List<Integer> irqQuarks = ssq.getQuarks(Attributes.CPUS, cpuName, Attributes.IRQS, "*"); //$NON-NLS-1$
254 createCpuInterruptEntryWithQuark(trace, ssq, entryMap, startTime, endTime, traceEntry, cpuEntry, irqQuarks, Type.IRQ);
255 List<Integer> softIrqQuarks = ssq.getQuarks(Attributes.CPUS, cpuName, Attributes.SOFT_IRQS, "*"); //$NON-NLS-1$
256 createCpuInterruptEntryWithQuark(trace, ssq, entryMap, startTime, endTime, traceEntry, cpuEntry, softIrqQuarks, Type.SOFT_IRQ);
257 }
258 }
259
260 /**
261 * Create and add execution contexts to a cpu entry. Also creates an
262 * aggregate entry in the root trace entry. The execution context is
263 * basically what the cpu is doing in its execution stack. It can be in an
264 * IRQ, Soft IRQ. MCEs, NMIs, Userland and Kernel execution is not yet
265 * supported.
266 *
267 * @param trace
268 * the trace
269 * @param ssq
270 * the state system
271 * @param entryMap
272 * the entry map
273 * @param startTime
274 * the start time in nanoseconds
275 * @param endTime
276 * the end time in nanoseconds
277 * @param traceEntry
278 * the trace timegraph entry
279 * @param cpuEntry
280 * the cpu timegraph entry (the entry under the trace entry
281 * @param childrenQuarks
282 * the quarks to add to cpu entry
283 * @param type
284 * the type of entry being added
285 */
286 private static void createCpuInterruptEntryWithQuark(@NonNull ITmfTrace trace,
287 final ITmfStateSystem ssq, Map<Integer, ResourcesEntry> entryMap,
288 long startTime, long endTime,
289 TimeGraphEntry traceEntry, ResourcesEntry cpuEntry,
290 List<Integer> childrenQuarks, Type type) {
291 for (Integer quark : childrenQuarks) {
292 final @NonNull String resourceName = ssq.getAttributeName(quark);
293 int resourceId = Integer.parseInt(resourceName);
294 ResourcesEntry interruptEntry = entryMap.get(quark);
295 if (interruptEntry == null) {
296 interruptEntry = new ResourcesEntry(quark, trace, startTime, endTime, type, resourceId);
297 entryMap.put(quark, interruptEntry);
298 cpuEntry.addChild(interruptEntry);
299 boolean found = false;
300 for (ITimeGraphEntry rootElem : traceEntry.getChildren()) {
301 if (rootElem instanceof AggregateResourcesEntry) {
302 AggregateResourcesEntry aggregateInterruptEntry = (AggregateResourcesEntry) rootElem;
303 if (aggregateInterruptEntry.getId() == resourceId && aggregateInterruptEntry.getType().equals(type)) {
304 found = true;
305 aggregateInterruptEntry.addContributor(interruptEntry);
306 final AggregateResourcesEntry irqCpuEntry = new AggregateResourcesEntry(trace, cpuEntry.getName(), startTime, endTime, type, cpuEntry.getId());
307 irqCpuEntry.addContributor(interruptEntry);
308 aggregateInterruptEntry.addChild(irqCpuEntry);
309 break;
310 }
311 }
312 }
313 if (!found) {
314 AggregateResourcesEntry aggregateInterruptEntry = new AggregateResourcesEntry(trace, startTime, endTime, type, resourceId);
315 aggregateInterruptEntry.addContributor(interruptEntry);
316 final AggregateResourcesEntry irqCpuEntry = new AggregateResourcesEntry(trace, cpuEntry.getName(), startTime, endTime, type, cpuEntry.getId());
317 irqCpuEntry.addContributor(interruptEntry);
318 aggregateInterruptEntry.addChild(irqCpuEntry);
319 traceEntry.addChild(aggregateInterruptEntry);
320 }
321 } else {
322 interruptEntry.updateEndTime(endTime);
323 }
324 }
325 }
326
327 @Override
328 protected @Nullable List<ITimeEvent> getEventList(@NonNull TimeGraphEntry entry, ITmfStateSystem ssq,
329 @NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, @NonNull IProgressMonitor monitor) {
330 ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
331 int quark = resourcesEntry.getQuark();
332
333 if (resourcesEntry.getType().equals(Type.CPU)) {
334 return createCpuEventsList(entry, ssq, fullStates, prevFullState, monitor, quark);
335 } else if ((resourcesEntry.getType().equals(Type.IRQ) || resourcesEntry.getType().equals(Type.SOFT_IRQ)) && (quark >= 0)) {
336 return createIrqEventsList(entry, fullStates, prevFullState, monitor, quark);
337 }
338
339 return null;
340 }
341
342 private static List<ITimeEvent> createCpuEventsList(TimeGraphEntry entry, ITmfStateSystem ssq, List<List<ITmfStateInterval>> fullStates, List<ITmfStateInterval> prevFullState, IProgressMonitor monitor, int quark) {
343 List<ITimeEvent> eventList;
344 int statusQuark;
345 try {
346 statusQuark = ssq.getQuarkRelative(quark, Attributes.STATUS);
347 } catch (AttributeNotFoundException e) {
348 /*
349 * The sub-attribute "status" is not available. May happen if the
350 * trace does not have sched_switch events enabled.
351 */
352 return null;
353 }
354 eventList = new ArrayList<>(fullStates.size());
355 ITmfStateInterval lastInterval = prevFullState == null || statusQuark >= prevFullState.size() ? null : prevFullState.get(statusQuark);
356 long lastStartTime = lastInterval == null ? -1 : lastInterval.getStartTime();
357 long lastEndTime = lastInterval == null ? -1 : lastInterval.getEndTime() + 1;
358 for (List<ITmfStateInterval> fullState : fullStates) {
359 if (monitor.isCanceled()) {
360 return null;
361 }
362 if (statusQuark >= fullState.size()) {
363 /* No information on this CPU (yet?), skip it for now */
364 continue;
365 }
366 ITmfStateInterval statusInterval = fullState.get(statusQuark);
367 int status = statusInterval.getStateValue().unboxInt();
368 long time = statusInterval.getStartTime();
369 long duration = statusInterval.getEndTime() - time + 1;
370 if (time == lastStartTime) {
371 continue;
372 }
373 if (!statusInterval.getStateValue().isNull()) {
374 if (lastEndTime != time && lastEndTime != -1) {
375 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
376 }
377 eventList.add(new TimeEvent(entry, time, duration, status));
378 } else {
379 eventList.add(new NullTimeEvent(entry, time, duration));
380 }
381 lastStartTime = time;
382 lastEndTime = time + duration;
383 }
384 return eventList;
385 }
386
387 private static List<ITimeEvent> createIrqEventsList(TimeGraphEntry entry, List<List<ITmfStateInterval>> fullStates, List<ITmfStateInterval> prevFullState, IProgressMonitor monitor, int quark) {
388 List<ITimeEvent> eventList;
389 eventList = new ArrayList<>(fullStates.size());
390 ITmfStateInterval lastInterval = prevFullState == null || quark >= prevFullState.size() ? null : prevFullState.get(quark);
391 long lastStartTime = lastInterval == null ? -1 : lastInterval.getStartTime();
392 long lastEndTime = lastInterval == null ? -1 : lastInterval.getEndTime() + 1;
393 boolean lastIsNull = lastInterval == null ? false : lastInterval.getStateValue().isNull();
394 for (List<ITmfStateInterval> fullState : fullStates) {
395 if (monitor.isCanceled()) {
396 return null;
397 }
398 if (quark >= fullState.size()) {
399 /* No information on this IRQ (yet?), skip it for now */
400 continue;
401 }
402 ITmfStateInterval irqInterval = fullState.get(quark);
403 long time = irqInterval.getStartTime();
404 long duration = irqInterval.getEndTime() - time + 1;
405 if (time == lastStartTime) {
406 continue;
407 }
408 if (!irqInterval.getStateValue().isNull()) {
409 int cpu = irqInterval.getStateValue().unboxInt();
410 eventList.add(new TimeEvent(entry, time, duration, cpu));
411 lastIsNull = false;
412 } else {
413 if (lastEndTime != time && lastIsNull) {
414 /*
415 * This is a special case where we want to show IRQ_ACTIVE
416 * state but we don't know the CPU (it is between two null
417 * samples)
418 */
419 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
420 }
421 eventList.add(new NullTimeEvent(entry, time, duration));
422 lastIsNull = true;
423 }
424 lastStartTime = time;
425 lastEndTime = time + duration;
426 }
427 return eventList;
428 }
429
430 /**
431 * Signal handler for a cpu selected signal.
432 *
433 * @param signal
434 * the cpu selected signal
435 * @since 2.0
436 */
437 @TmfSignalHandler
438 public void listenToCpu(TmfCpuSelectedSignal signal) {
439 if (signal.getCore() >= 0) {
440 fCurrentCpu = signal.getCore();
441 } else {
442 fCurrentCpu = -1;
443 }
444 }
445
446 }
This page took 0.042564 seconds and 4 git commands to generate.