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
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.internal.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.KernelAnalysisModule;
32 import org.eclipse.tracecompass.analysis.os.linux.core.signals.TmfCpuSelectedSignal;
33 import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes;
34 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Messages;
35 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.actions.FollowCpuAction;
36 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.actions.UnfollowCpuAction;
37 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.resources.ResourcesEntry.Type;
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.core.trace.TmfTraceContext;
45 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
46 import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractStateSystemTimeGraphView;
47 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
48 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
49 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
50 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
51 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
52
53 /**
54 * Main implementation for the LTTng 2.0 kernel Resource view
55 *
56 * @author Patrick Tasse
57 */
58 public class ResourcesView extends AbstractStateSystemTimeGraphView {
59
60 /** View ID. */
61 public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.views.resources"; //$NON-NLS-1$
62
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
66 private static final String[] FILTER_COLUMN_NAMES = new String[] {
67 Messages.ResourcesView_stateTypeName
68 };
69
70 // Timeout between updates in the build thread in ms
71 private static final long BUILD_UPDATE_TIMEOUT = 500;
72
73 // ------------------------------------------------------------------------
74 // Constructors
75 // ------------------------------------------------------------------------
76
77 /**
78 * Default constructor
79 */
80 public ResourcesView() {
81 super(ID, new ResourcesPresentationProvider());
82 setFilterColumns(FILTER_COLUMN_NAMES);
83 setFilterLabelProvider(new ResourcesFilterLabelProvider());
84 setEntryComparator(new ResourcesEntryComparator());
85 setAutoExpandLevel(1);
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 }
100 }
101
102
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)) {
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) {
118 menuManager.add(new UnfollowCpuAction(ResourcesView.this, resourcesEntry.getId(), resourcesEntry.getTrace()));
119 } else {
120 menuManager.add(new FollowCpuAction(ResourcesView.this, resourcesEntry.getId(), resourcesEntry.getTrace()));
121 }
122 }
123 }
124 }
125 }
126
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
137 }
138
139 // ------------------------------------------------------------------------
140 // Internal
141 // ------------------------------------------------------------------------
142
143 @Override
144 protected String getNextText() {
145 return Messages.ResourcesView_nextResourceActionNameText;
146 }
147
148 @Override
149 protected String getNextTooltip() {
150 return Messages.ResourcesView_nextResourceActionToolTipText;
151 }
152
153 @Override
154 protected String getPrevText() {
155 return Messages.ResourcesView_previousResourceActionNameText;
156 }
157
158 @Override
159 protected String getPrevTooltip() {
160 return Messages.ResourcesView_previousResourceActionToolTipText;
161 }
162
163 @Override
164 protected void buildEntryList(ITmfTrace trace, ITmfTrace parentTrace, final IProgressMonitor monitor) {
165 final ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
166 if (ssq == null) {
167 return;
168 }
169
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) {
178 if (monitor.isCanceled()) {
179 return;
180 }
181 complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
182 if (ssq.isCancelled()) {
183 return;
184 }
185 long end = ssq.getCurrentEndTime();
186 if (start == end && !complete) {
187 // when complete execute one last time regardless of end time
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);
196 addToEntryList(parentTrace, ssq, entryList);
197 } else {
198 traceEntry.updateEndTime(endTime);
199 }
200 List<Integer> cpuQuarks = ssq.getQuarks(Attributes.CPUS, "*"); //$NON-NLS-1$
201 createCpuEntriesWithQuark(trace, ssq, entryMap, traceEntry, startTime, endTime, cpuQuarks);
202 if (parentTrace.equals(getTrace())) {
203 refresh();
204 }
205 final List<@NonNull TimeGraphEntry> traceEntryChildren = traceEntry.getChildren();
206 final long resolution = Math.max(1, (endTime - ssq.getStartTime()) / getDisplayWidth());
207 queryFullStates(ssq, ssq.getStartTime(), end, resolution, monitor, new IQueryHandler() {
208 @Override
209 public void handle(List<List<ITmfStateInterval>> fullStates, List<ITmfStateInterval> prevFullState) {
210 for (TimeGraphEntry child : traceEntryChildren) {
211 if (!populateEventsRecursively(fullStates, prevFullState, child).isOK()) {
212 return;
213 }
214 }
215 }
216
217 private IStatus populateEventsRecursively(@NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, @NonNull TimeGraphEntry entry) {
218 if (monitor.isCanceled()) {
219 return Status.CANCEL_STATUS;
220 }
221 List<ITimeEvent> eventList = getEventList(entry, ssq, fullStates, prevFullState, monitor);
222 if (eventList != null) {
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 }
230 }
231 }
232 for (TimeGraphEntry child : entry.getChildren()) {
233 IStatus status = populateEventsRecursively(fullStates, prevFullState, child);
234 if (!status.isOK()) {
235 return status;
236 }
237 }
238 return Status.OK_STATUS;
239 }
240 });
241
242 start = end;
243 }
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);
312 final AggregateResourcesEntry irqCpuEntry = new AggregateResourcesEntry(trace, cpuEntry.getName(), startTime, endTime, type, cpuEntry.getId());
313 irqCpuEntry.addContributor(interruptEntry);
314 aggregateInterruptEntry.addChild(irqCpuEntry);
315 break;
316 }
317 }
318 }
319 if (!found) {
320 AggregateResourcesEntry aggregateInterruptEntry = new AggregateResourcesEntry(trace, startTime, endTime, type, resourceId);
321 aggregateInterruptEntry.addContributor(interruptEntry);
322 final AggregateResourcesEntry irqCpuEntry = new AggregateResourcesEntry(trace, cpuEntry.getName(), startTime, endTime, type, cpuEntry.getId());
323 irqCpuEntry.addContributor(interruptEntry);
324 aggregateInterruptEntry.addChild(irqCpuEntry);
325 traceEntry.addChild(aggregateInterruptEntry);
326 }
327 } else {
328 interruptEntry.updateEndTime(endTime);
329 }
330 }
331 }
332
333 @Override
334 protected @Nullable List<ITimeEvent> getEventList(@NonNull TimeGraphEntry entry, ITmfStateSystem ssq,
335 @NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, @NonNull IProgressMonitor monitor) {
336 ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
337 int quark = resourcesEntry.getQuark();
338
339 if (resourcesEntry.getType().equals(Type.CPU)) {
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
348 private static List<ITimeEvent> createCpuEventsList(ITimeGraphEntry entry, ITmfStateSystem ssq, List<List<ITmfStateInterval>> fullStates, List<ITmfStateInterval> prevFullState, IProgressMonitor monitor, int quark) {
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 }
360 boolean isZoomThread = Thread.currentThread() instanceof ZoomThread;
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()) {
367 return null;
368 }
369 if (statusQuark >= fullState.size()) {
370 /* No information on this CPU (yet?), skip it for now */
371 continue;
372 }
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));
383 }
384 eventList.add(new TimeEvent(entry, time, duration, status));
385 } else if (isZoomThread) {
386 eventList.add(new NullTimeEvent(entry, time, duration));
387 }
388 lastStartTime = time;
389 lastEndTime = time + duration;
390 }
391 return eventList;
392 }
393
394 private static List<ITimeEvent> createIrqEventsList(ITimeGraphEntry entry, List<List<ITmfStateInterval>> fullStates, List<ITmfStateInterval> prevFullState, IProgressMonitor monitor, int quark) {
395 List<ITimeEvent> eventList;
396 boolean isZoomThread = Thread.currentThread() instanceof ZoomThread;
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 }
429 if (isZoomThread) {
430 eventList.add(new NullTimeEvent(entry, time, duration));
431 }
432 lastIsNull = true;
433 }
434 lastStartTime = time;
435 lastEndTime = time + duration;
436 }
437 return eventList;
438 }
439
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) {
449 int data = signal.getCore() >= 0 ? signal.getCore() : -1;
450 TmfTraceContext ctx = TmfTraceManager.getInstance().getCurrentTraceContext();
451 ctx.setData(RESOURCES_FOLLOW_CPU, data);
452 }
453
454 }
This page took 0.06498 seconds and 5 git commands to generate.