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