Fix typo in method name: setNewProcessPio -> setNewProcessPrio
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / 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
e363eae1 14package org.eclipse.tracecompass.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
BH
31import org.eclipse.tracecompass.analysis.os.linux.core.kernel.Attributes;
32import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule;
8c684b48 33import org.eclipse.tracecompass.analysis.os.linux.core.signals.TmfCpuSelectedSignal;
e363eae1
AM
34import org.eclipse.tracecompass.analysis.os.linux.ui.views.resources.ResourcesEntry.Type;
35import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Messages;
8c684b48
MK
36import org.eclipse.tracecompass.internal.analysis.os.linux.ui.actions.FollowCpuAction;
37import org.eclipse.tracecompass.internal.analysis.os.linux.ui.actions.UnfollowCpuAction;
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;
8213a0c0 44import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractStateSystemTimeGraphView;
2bdf0193
AM
45import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
46import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
47import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
48import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
49import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
6151d86c
PT
50
51/**
52 * Main implementation for the LTTng 2.0 kernel Resource view
53 *
54 * @author Patrick Tasse
55 */
8213a0c0 56public class ResourcesView extends AbstractStateSystemTimeGraphView {
6151d86c
PT
57
58 /** View ID. */
e363eae1 59 public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.views.resources"; //$NON-NLS-1$
6151d86c 60
4999a196 61 private static final String[] FILTER_COLUMN_NAMES = new String[] {
747adf5c 62 Messages.ResourcesView_stateTypeName
4999a196 63 };
6151d86c 64
8c684b48
MK
65 private int fCurrentCpu = -1;
66
1cf25311
PT
67 // Timeout between updates in the build thread in ms
68 private static final long BUILD_UPDATE_TIMEOUT = 500;
69
6151d86c
PT
70 // ------------------------------------------------------------------------
71 // Constructors
72 // ------------------------------------------------------------------------
73
74 /**
75 * Default constructor
76 */
77 public ResourcesView() {
747adf5c
PT
78 super(ID, new ResourcesPresentationProvider());
79 setFilterColumns(FILTER_COLUMN_NAMES);
bb447fcb 80 setFilterLabelProvider(new ResourcesFilterLabelProvider());
263c3747 81 setEntryComparator(new ResourcesEntryComparator());
19ed6598 82 setAutoExpandLevel(1);
263c3747
PT
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 }
bb447fcb
PT
97 }
98
8c684b48 99
9bdf1671
BH
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()));
8c684b48
MK
115 }
116 }
117 }
9bdf1671 118 }
8c684b48
MK
119 }
120
bb447fcb
PT
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
6151d86c
PT
131 }
132
1cf25311
PT
133 // ------------------------------------------------------------------------
134 // Internal
135 // ------------------------------------------------------------------------
136
6151d86c 137 @Override
4999a196
GB
138 protected String getNextText() {
139 return Messages.ResourcesView_nextResourceActionNameText;
6151d86c
PT
140 }
141
6151d86c 142 @Override
4999a196
GB
143 protected String getNextTooltip() {
144 return Messages.ResourcesView_nextResourceActionToolTipText;
fec1ac0b
BH
145 }
146
4999a196
GB
147 @Override
148 protected String getPrevText() {
149 return Messages.ResourcesView_previousResourceActionNameText;
6151d86c
PT
150 }
151
4999a196
GB
152 @Override
153 protected String getPrevTooltip() {
154 return Messages.ResourcesView_previousResourceActionToolTipText;
6151d86c
PT
155 }
156
4999a196 157 @Override
8213a0c0
PT
158 protected void buildEventList(ITmfTrace trace, ITmfTrace parentTrace, final IProgressMonitor monitor) {
159 final ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
1cf25311
PT
160 if (ssq == null) {
161 return;
162 }
fec1ac0b 163
1cf25311
PT
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) {
faa38350
PT
172 if (monitor.isCanceled()) {
173 return;
174 }
1cf25311
PT
175 complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
176 if (ssq.isCancelled()) {
177 return;
178 }
179 long end = ssq.getCurrentEndTime();
19ed6598
MK
180 if (start == end && !complete) {
181 // when complete execute one last time regardless of end time
1cf25311
PT
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);
8213a0c0 190 addToEntryList(parentTrace, ssq, entryList);
1cf25311
PT
191 } else {
192 traceEntry.updateEndTime(endTime);
193 }
1cf25311 194 List<Integer> cpuQuarks = ssq.getQuarks(Attributes.CPUS, "*"); //$NON-NLS-1$
19ed6598 195 createCpuEntriesWithQuark(trace, ssq, entryMap, traceEntry, startTime, endTime, cpuQuarks);
1cf25311
PT
196 if (parentTrace.equals(getTrace())) {
197 refresh();
faa38350 198 }
8213a0c0
PT
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) {
19ed6598 207 if (!populateEventsRecursively(fullStates, prevFullState, child).isOK()) {
8213a0c0
PT
208 return;
209 }
19ed6598
MK
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);
8213a0c0 223 }
a3188982 224 }
1cf25311 225 }
19ed6598
MK
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;
1cf25311 233 }
8213a0c0 234 });
1cf25311
PT
235
236 start = end;
6151d86c 237 }
19ed6598
MK
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);
ff0b7e58
MK
306 final AggregateResourcesEntry irqCpuEntry = new AggregateResourcesEntry(trace, cpuEntry.getName(), startTime, endTime, type, cpuEntry.getId());
307 irqCpuEntry.addContributor(interruptEntry);
308 aggregateInterruptEntry.addChild(irqCpuEntry);
19ed6598
MK
309 break;
310 }
311 }
312 }
313 if (!found) {
314 AggregateResourcesEntry aggregateInterruptEntry = new AggregateResourcesEntry(trace, startTime, endTime, type, resourceId);
315 aggregateInterruptEntry.addContributor(interruptEntry);
ff0b7e58
MK
316 final AggregateResourcesEntry irqCpuEntry = new AggregateResourcesEntry(trace, cpuEntry.getName(), startTime, endTime, type, cpuEntry.getId());
317 irqCpuEntry.addContributor(interruptEntry);
318 aggregateInterruptEntry.addChild(irqCpuEntry);
19ed6598
MK
319 traceEntry.addChild(aggregateInterruptEntry);
320 }
321 } else {
322 interruptEntry.updateEndTime(endTime);
323 }
324 }
6151d86c
PT
325 }
326
4999a196 327 @Override
8213a0c0
PT
328 protected @Nullable List<ITimeEvent> getEventList(@NonNull TimeGraphEntry entry, ITmfStateSystem ssq,
329 @NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, @NonNull IProgressMonitor monitor) {
1d46dc38 330 ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
4999a196
GB
331 int quark = resourcesEntry.getQuark();
332
8213a0c0 333 if (resourcesEntry.getType().equals(Type.CPU)) {
19ed6598
MK
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()) {
8213a0c0
PT
360 return null;
361 }
19ed6598
MK
362 if (statusQuark >= fullState.size()) {
363 /* No information on this CPU (yet?), skip it for now */
364 continue;
8213a0c0 365 }
19ed6598
MK
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));
6151d86c 376 }
19ed6598
MK
377 eventList.add(new TimeEvent(entry, time, duration, status));
378 } else {
379 eventList.add(new NullTimeEvent(entry, time, duration));
6151d86c 380 }
19ed6598
MK
381 lastStartTime = time;
382 lastEndTime = time + duration;
6151d86c 383 }
19ed6598
MK
384 return eventList;
385 }
8213a0c0 386
19ed6598
MK
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 }
6151d86c
PT
427 return eventList;
428 }
429
8c684b48
MK
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
6151d86c 446}
This page took 0.097265 seconds and 5 git commands to generate.