Commit | Line | Data |
---|---|---|
6151d86c | 1 | /******************************************************************************* |
94cce698 | 2 | * Copyright (c) 2012, 2013 Ericsson |
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 | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources; | |
14 | ||
15 | import java.util.ArrayList; | |
16 | import java.util.Arrays; | |
17 | import java.util.Comparator; | |
faa38350 | 18 | import java.util.HashMap; |
6151d86c PT |
19 | import java.util.Iterator; |
20 | import java.util.List; | |
21 | ||
22 | import org.eclipse.core.runtime.IProgressMonitor; | |
23 | import org.eclipse.core.runtime.NullProgressMonitor; | |
24 | import org.eclipse.jface.action.Action; | |
25 | import org.eclipse.jface.action.IToolBarManager; | |
26 | import org.eclipse.jface.action.Separator; | |
27 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes; | |
28 | import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages; | |
29 | import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources.ResourcesEntry.Type; | |
30 | import org.eclipse.linuxtools.lttng2.kernel.core.trace.CtfKernelTrace; | |
31 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp; | |
6151d86c | 32 | import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException; |
96345c5a | 33 | import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException; |
6151d86c PT |
34 | import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException; |
35 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; | |
36 | import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval; | |
6151d86c PT |
37 | import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal; |
38 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; | |
6151d86c | 39 | import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal; |
faa38350 PT |
40 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceClosedSignal; |
41 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceSelectedSignal; | |
f1f86dfb | 42 | import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem; |
3bd46eef AM |
43 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
44 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; | |
45 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; | |
6151d86c PT |
46 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
47 | import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment; | |
48 | import org.eclipse.linuxtools.tmf.ui.views.TmfView; | |
49 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphRangeListener; | |
50 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphTimeListener; | |
51 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphRangeUpdateEvent; | |
52 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphTimeEvent; | |
53 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphViewer; | |
54 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent; | |
55 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; | |
56 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent; | |
026664b7 | 57 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat; |
6151d86c PT |
58 | import org.eclipse.swt.SWT; |
59 | import org.eclipse.swt.widgets.Composite; | |
60 | import org.eclipse.swt.widgets.Display; | |
61 | import org.eclipse.ui.IActionBars; | |
62 | ||
63 | /** | |
64 | * Main implementation for the LTTng 2.0 kernel Resource view | |
65 | * | |
66 | * @author Patrick Tasse | |
67 | */ | |
68 | public class ResourcesView extends TmfView { | |
69 | ||
70 | // ------------------------------------------------------------------------ | |
71 | // Constants | |
72 | // ------------------------------------------------------------------------ | |
73 | ||
74 | /** View ID. */ | |
75 | public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.ui.views.resources"; //$NON-NLS-1$ | |
76 | ||
6151d86c PT |
77 | /** |
78 | * Redraw state enum | |
79 | */ | |
80 | private enum State { IDLE, BUSY, PENDING } | |
81 | ||
82 | // ------------------------------------------------------------------------ | |
83 | // Fields | |
84 | // ------------------------------------------------------------------------ | |
85 | ||
86 | // The time graph viewer | |
87 | TimeGraphViewer fTimeGraphViewer; | |
88 | ||
faa38350 PT |
89 | // The selected trace |
90 | private ITmfTrace fTrace; | |
6151d86c PT |
91 | |
92 | // The time graph entry list | |
93 | private ArrayList<TraceEntry> fEntryList; | |
94 | ||
faa38350 PT |
95 | // The trace to entry list hash map |
96 | final private HashMap<ITmfTrace, ArrayList<TraceEntry>> fEntryListMap = new HashMap<ITmfTrace, ArrayList<TraceEntry>>(); | |
97 | ||
98 | // The trace to build thread hash map | |
99 | final private HashMap<ITmfTrace, BuildThread> fBuildThreadMap = new HashMap<ITmfTrace, BuildThread>(); | |
6151d86c PT |
100 | |
101 | // The start time | |
102 | private long fStartTime; | |
103 | ||
104 | // The end time | |
105 | private long fEndTime; | |
106 | ||
107 | // The display width | |
108 | private final int fDisplayWidth; | |
109 | ||
110 | // The next resource action | |
111 | private Action fNextResourceAction; | |
112 | ||
113 | // The previous resource action | |
114 | private Action fPreviousResourceAction; | |
115 | ||
116 | // The zoom thread | |
117 | private ZoomThread fZoomThread; | |
118 | ||
119 | // The redraw state used to prevent unnecessary queuing of display runnables | |
120 | private State fRedrawState = State.IDLE; | |
121 | ||
122 | // The redraw synchronization object | |
123 | final private Object fSyncObj = new Object(); | |
124 | ||
125 | // ------------------------------------------------------------------------ | |
126 | // Classes | |
127 | // ------------------------------------------------------------------------ | |
128 | ||
129 | private class TraceEntry implements ITimeGraphEntry { | |
130 | // The Trace | |
faa38350 | 131 | private final CtfKernelTrace fKernelTrace; |
6151d86c PT |
132 | // The start time |
133 | private final long fTraceStartTime; | |
134 | // The end time | |
135 | private final long fTraceEndTime; | |
136 | // The children of the entry | |
137 | private final ArrayList<ResourcesEntry> fChildren; | |
138 | // The name of entry | |
139 | private final String fName; | |
140 | ||
141 | public TraceEntry(CtfKernelTrace trace, String name, long startTime, long endTime) { | |
faa38350 | 142 | fKernelTrace = trace; |
6151d86c PT |
143 | fChildren = new ArrayList<ResourcesEntry>(); |
144 | fName = name; | |
145 | fTraceStartTime = startTime; | |
146 | fTraceEndTime = endTime; | |
147 | } | |
148 | ||
149 | @Override | |
150 | public ITimeGraphEntry getParent() { | |
151 | return null; | |
152 | } | |
153 | ||
154 | @Override | |
155 | public boolean hasChildren() { | |
156 | return fChildren != null && fChildren.size() > 0; | |
157 | } | |
158 | ||
159 | @Override | |
30652cc3 AM |
160 | public List<ResourcesEntry> getChildren() { |
161 | return fChildren; | |
6151d86c PT |
162 | } |
163 | ||
164 | @Override | |
165 | public String getName() { | |
166 | return fName; | |
167 | } | |
168 | ||
169 | @Override | |
170 | public long getStartTime() { | |
171 | return fTraceStartTime; | |
172 | } | |
173 | ||
174 | @Override | |
175 | public long getEndTime() { | |
176 | return fTraceEndTime; | |
177 | } | |
178 | ||
179 | @Override | |
180 | public boolean hasTimeEvents() { | |
181 | return false; | |
182 | } | |
183 | ||
184 | @Override | |
185 | public Iterator<ITimeEvent> getTimeEventsIterator() { | |
186 | return null; | |
187 | } | |
188 | ||
189 | @Override | |
190 | public <T extends ITimeEvent> Iterator<T> getTimeEventsIterator(long startTime, long stopTime, long visibleDuration) { | |
191 | return null; | |
192 | } | |
193 | ||
194 | public CtfKernelTrace getTrace() { | |
faa38350 | 195 | return fKernelTrace; |
6151d86c PT |
196 | } |
197 | ||
198 | public void addChild(ResourcesEntry entry) { | |
199 | int index; | |
200 | for (index = 0; index < fChildren.size(); index++) { | |
201 | ResourcesEntry other = fChildren.get(index); | |
202 | if (entry.getType().compareTo(other.getType()) < 0) { | |
203 | break; | |
204 | } else if (entry.getType().equals(other.getType())) { | |
205 | if (entry.getId() < other.getId()) { | |
206 | break; | |
207 | } | |
208 | } | |
209 | } | |
210 | entry.setParent(this); | |
211 | fChildren.add(index, entry); | |
212 | } | |
213 | } | |
214 | ||
215 | private static class TraceEntryComparator implements Comparator<ITimeGraphEntry> { | |
216 | @Override | |
217 | public int compare(ITimeGraphEntry o1, ITimeGraphEntry o2) { | |
218 | int result = o1.getStartTime() < o2.getStartTime() ? -1 : o1.getStartTime() > o2.getStartTime() ? 1 : 0; | |
219 | if (result == 0) { | |
220 | result = o1.getName().compareTo(o2.getName()); | |
221 | } | |
222 | return result; | |
223 | } | |
224 | } | |
225 | ||
faa38350 PT |
226 | private class BuildThread extends Thread { |
227 | private final ITmfTrace fBuildTrace; | |
228 | private final IProgressMonitor fMonitor; | |
229 | ||
230 | public BuildThread(ITmfTrace trace) { | |
231 | super("ResourcesView build"); //$NON-NLS-1$ | |
232 | fBuildTrace = trace; | |
233 | fMonitor = new NullProgressMonitor(); | |
234 | } | |
235 | ||
236 | @Override | |
237 | public void run() { | |
238 | buildEventList(fBuildTrace, fMonitor); | |
239 | synchronized (fBuildThreadMap) { | |
240 | fBuildThreadMap.remove(this); | |
241 | } | |
242 | } | |
243 | ||
244 | public void cancel() { | |
245 | fMonitor.setCanceled(true); | |
246 | } | |
247 | } | |
248 | ||
6151d86c | 249 | private class ZoomThread extends Thread { |
faa38350 | 250 | private final ArrayList<TraceEntry> fZoomEntryList; |
6151d86c PT |
251 | private final long fZoomStartTime; |
252 | private final long fZoomEndTime; | |
253 | private final IProgressMonitor fMonitor; | |
254 | ||
faa38350 | 255 | public ZoomThread(ArrayList<TraceEntry> entryList, long startTime, long endTime) { |
6151d86c | 256 | super("ResourcesView zoom"); //$NON-NLS-1$ |
faa38350 | 257 | fZoomEntryList = entryList; |
6151d86c PT |
258 | fZoomStartTime = startTime; |
259 | fZoomEndTime = endTime; | |
260 | fMonitor = new NullProgressMonitor(); | |
261 | } | |
262 | ||
263 | @Override | |
264 | public void run() { | |
faa38350 | 265 | if (fZoomEntryList == null) { |
6151d86c PT |
266 | return; |
267 | } | |
268 | long resolution = Math.max(1, (fZoomEndTime - fZoomStartTime) / fDisplayWidth); | |
faa38350 | 269 | for (TraceEntry traceEntry : fZoomEntryList) { |
35c160d9 | 270 | if (!traceEntry.fKernelTrace.getStateSystems().get(CtfKernelTrace.STATE_ID).waitUntilBuilt()) { |
faa38350 PT |
271 | return; |
272 | } | |
6151d86c PT |
273 | for (ITimeGraphEntry child : traceEntry.getChildren()) { |
274 | if (fMonitor.isCanceled()) { | |
275 | break; | |
276 | } | |
277 | ResourcesEntry entry = (ResourcesEntry) child; | |
278 | if (fZoomStartTime <= fStartTime && fZoomEndTime >= fEndTime) { | |
279 | entry.setZoomedEventList(null); | |
280 | } else { | |
281 | List<ITimeEvent> zoomedEventList = getEventList(entry, fZoomStartTime, fZoomEndTime, resolution, true, fMonitor); | |
282 | if (zoomedEventList != null) { | |
283 | entry.setZoomedEventList(zoomedEventList); | |
284 | } | |
285 | } | |
286 | redraw(); | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
291 | public void cancel() { | |
292 | fMonitor.setCanceled(true); | |
293 | } | |
294 | } | |
295 | ||
296 | // ------------------------------------------------------------------------ | |
297 | // Constructors | |
298 | // ------------------------------------------------------------------------ | |
299 | ||
300 | /** | |
301 | * Default constructor | |
302 | */ | |
303 | public ResourcesView() { | |
304 | super(ID); | |
305 | fDisplayWidth = Display.getDefault().getBounds().width; | |
306 | } | |
307 | ||
308 | // ------------------------------------------------------------------------ | |
309 | // ViewPart | |
310 | // ------------------------------------------------------------------------ | |
311 | ||
312 | /* (non-Javadoc) | |
313 | * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#createPartControl(org.eclipse.swt.widgets.Composite) | |
314 | */ | |
315 | @Override | |
316 | public void createPartControl(Composite parent) { | |
317 | fTimeGraphViewer = new TimeGraphViewer(parent, SWT.NONE); | |
318 | ||
713a70ae | 319 | fTimeGraphViewer.setTimeGraphProvider(new ResourcesPresentationProvider(fTimeGraphViewer)); |
6151d86c | 320 | |
026664b7 | 321 | fTimeGraphViewer.setTimeFormat(TimeFormat.CALENDAR); |
6151d86c PT |
322 | |
323 | fTimeGraphViewer.addRangeListener(new ITimeGraphRangeListener() { | |
324 | @Override | |
325 | public void timeRangeUpdated(TimeGraphRangeUpdateEvent event) { | |
326 | long startTime = event.getStartTime(); | |
327 | long endTime = event.getEndTime(); | |
328 | TmfTimeRange range = new TmfTimeRange(new CtfTmfTimestamp(startTime), new CtfTmfTimestamp(endTime)); | |
329 | TmfTimestamp time = new CtfTmfTimestamp(fTimeGraphViewer.getSelectedTime()); | |
330 | broadcast(new TmfRangeSynchSignal(ResourcesView.this, range, time)); | |
331 | startZoomThread(startTime, endTime); | |
332 | } | |
333 | }); | |
334 | ||
335 | fTimeGraphViewer.addTimeListener(new ITimeGraphTimeListener() { | |
336 | @Override | |
337 | public void timeSelected(TimeGraphTimeEvent event) { | |
338 | long time = event.getTime(); | |
339 | broadcast(new TmfTimeSynchSignal(ResourcesView.this, new CtfTmfTimestamp(time))); | |
340 | } | |
341 | }); | |
342 | ||
6151d86c PT |
343 | // View Action Handling |
344 | makeActions(); | |
345 | contributeToActionBars(); | |
faa38350 | 346 | |
3ac5721a AM |
347 | ITmfTrace trace = getActiveTrace(); |
348 | if (trace != null) { | |
349 | traceSelected(new TmfTraceSelectedSignal(this, trace)); | |
faa38350 | 350 | } |
6151d86c PT |
351 | } |
352 | ||
353 | /* (non-Javadoc) | |
354 | * @see org.eclipse.ui.part.WorkbenchPart#setFocus() | |
355 | */ | |
356 | @Override | |
357 | public void setFocus() { | |
6151d86c PT |
358 | fTimeGraphViewer.setFocus(); |
359 | } | |
360 | ||
361 | // ------------------------------------------------------------------------ | |
362 | // Signal handlers | |
363 | // ------------------------------------------------------------------------ | |
364 | ||
365 | /** | |
faa38350 | 366 | * Handler for the trace selected signal |
6151d86c PT |
367 | * |
368 | * @param signal | |
369 | * The incoming signal | |
370 | */ | |
371 | @TmfSignalHandler | |
faa38350 PT |
372 | public void traceSelected(final TmfTraceSelectedSignal signal) { |
373 | if (signal.getTrace() == fTrace) { | |
6151d86c PT |
374 | return; |
375 | } | |
faa38350 PT |
376 | fTrace = signal.getTrace(); |
377 | ||
378 | synchronized (fEntryListMap) { | |
379 | fEntryList = fEntryListMap.get(fTrace); | |
380 | if (fEntryList == null) { | |
381 | synchronized (fBuildThreadMap) { | |
382 | BuildThread buildThread = new BuildThread(fTrace); | |
383 | fBuildThreadMap.put(fTrace, buildThread); | |
384 | buildThread.start(); | |
385 | } | |
386 | } else { | |
387 | fStartTime = fTrace.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
388 | fEndTime = fTrace.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
d7ee91bb | 389 | refresh(); |
6151d86c | 390 | } |
faa38350 | 391 | } |
6151d86c PT |
392 | } |
393 | ||
394 | /** | |
faa38350 | 395 | * Trace is disposed: clear the data structures and the view |
6151d86c PT |
396 | * |
397 | * @param signal the signal received | |
398 | */ | |
399 | @TmfSignalHandler | |
faa38350 PT |
400 | public void traceClosed(final TmfTraceClosedSignal signal) { |
401 | synchronized (fBuildThreadMap) { | |
402 | BuildThread buildThread = fBuildThreadMap.remove(signal.getTrace()); | |
403 | if (buildThread != null) { | |
404 | buildThread.cancel(); | |
405 | } | |
406 | } | |
407 | synchronized (fEntryListMap) { | |
408 | fEntryListMap.remove(signal.getTrace()); | |
409 | } | |
410 | if (signal.getTrace() == fTrace) { | |
411 | fTrace = null; | |
6151d86c PT |
412 | fStartTime = 0; |
413 | fEndTime = 0; | |
faa38350 PT |
414 | if (fZoomThread != null) { |
415 | fZoomThread.cancel(); | |
6151d86c | 416 | } |
d7ee91bb | 417 | refresh(); |
6151d86c PT |
418 | } |
419 | } | |
420 | ||
421 | /** | |
422 | * Handler for the TimeSynch signal | |
423 | * | |
424 | * @param signal | |
425 | * The incoming signal | |
426 | */ | |
427 | @TmfSignalHandler | |
428 | public void synchToTime(final TmfTimeSynchSignal signal) { | |
faa38350 | 429 | if (signal.getSource() == this || fTrace == null) { |
6151d86c PT |
430 | return; |
431 | } | |
faa38350 | 432 | final long time = signal.getCurrentTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); |
6151d86c PT |
433 | Display.getDefault().asyncExec(new Runnable() { |
434 | @Override | |
435 | public void run() { | |
436 | if (fTimeGraphViewer.getControl().isDisposed()) { | |
437 | return; | |
438 | } | |
439 | fTimeGraphViewer.setSelectedTime(time, true); | |
440 | startZoomThread(fTimeGraphViewer.getTime0(), fTimeGraphViewer.getTime1()); | |
441 | } | |
442 | }); | |
443 | } | |
444 | ||
445 | /** | |
446 | * Handler for the RangeSynch signal | |
447 | * | |
448 | * @param signal | |
449 | * The incoming signal | |
450 | */ | |
451 | @TmfSignalHandler | |
452 | public void synchToRange(final TmfRangeSynchSignal signal) { | |
faa38350 | 453 | if (signal.getSource() == this || fTrace == null) { |
6151d86c PT |
454 | return; |
455 | } | |
1c6a842a PT |
456 | if (signal.getCurrentRange().getIntersection(fTrace.getTimeRange()) == null) { |
457 | return; | |
458 | } | |
faa38350 PT |
459 | final long startTime = signal.getCurrentRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); |
460 | final long endTime = signal.getCurrentRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
461 | final long time = signal.getCurrentTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
6151d86c PT |
462 | Display.getDefault().asyncExec(new Runnable() { |
463 | @Override | |
464 | public void run() { | |
465 | if (fTimeGraphViewer.getControl().isDisposed()) { | |
466 | return; | |
467 | } | |
468 | fTimeGraphViewer.setStartFinishTime(startTime, endTime); | |
469 | fTimeGraphViewer.setSelectedTime(time, false); | |
470 | startZoomThread(startTime, endTime); | |
471 | } | |
472 | }); | |
473 | } | |
474 | ||
6151d86c PT |
475 | // ------------------------------------------------------------------------ |
476 | // Internal | |
477 | // ------------------------------------------------------------------------ | |
478 | ||
faa38350 | 479 | private void buildEventList(final ITmfTrace trace, IProgressMonitor monitor) { |
6151d86c PT |
480 | fStartTime = Long.MAX_VALUE; |
481 | fEndTime = Long.MIN_VALUE; | |
faa38350 PT |
482 | ITmfTrace[] traces; |
483 | if (trace instanceof TmfExperiment) { | |
484 | TmfExperiment experiment = (TmfExperiment) trace; | |
485 | traces = experiment.getTraces(); | |
486 | } else { | |
487 | traces = new ITmfTrace[] { trace }; | |
488 | } | |
6151d86c | 489 | ArrayList<TraceEntry> entryList = new ArrayList<TraceEntry>(); |
faa38350 PT |
490 | for (ITmfTrace aTrace : traces) { |
491 | if (monitor.isCanceled()) { | |
492 | return; | |
493 | } | |
494 | if (aTrace instanceof CtfKernelTrace) { | |
495 | CtfKernelTrace ctfKernelTrace = (CtfKernelTrace) aTrace; | |
35c160d9 | 496 | ITmfStateSystem ssq = ctfKernelTrace.getStateSystems().get(CtfKernelTrace.STATE_ID); |
faa38350 PT |
497 | if (!ssq.waitUntilBuilt()) { |
498 | return; | |
499 | } | |
6151d86c PT |
500 | long startTime = ssq.getStartTime(); |
501 | long endTime = ssq.getCurrentEndTime() + 1; | |
faa38350 | 502 | TraceEntry groupEntry = new TraceEntry(ctfKernelTrace, aTrace.getName(), startTime, endTime); |
6151d86c PT |
503 | entryList.add(groupEntry); |
504 | fStartTime = Math.min(fStartTime, startTime); | |
505 | fEndTime = Math.max(fEndTime, endTime); | |
506 | List<Integer> cpuQuarks = ssq.getQuarks(Attributes.CPUS, "*"); //$NON-NLS-1$ | |
507 | ResourcesEntry[] cpuEntries = new ResourcesEntry[cpuQuarks.size()]; | |
508 | for (int i = 0; i < cpuQuarks.size(); i++) { | |
509 | int cpuQuark = cpuQuarks.get(i); | |
510 | int cpu = Integer.parseInt(ssq.getAttributeName(cpuQuark)); | |
511 | ResourcesEntry entry = new ResourcesEntry(cpuQuark, ctfKernelTrace, Type.CPU, cpu); | |
512 | groupEntry.addChild(entry); | |
513 | cpuEntries[i] = entry; | |
514 | } | |
515 | List<Integer> irqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.IRQS, "*"); //$NON-NLS-1$ | |
516 | ResourcesEntry[] irqEntries = new ResourcesEntry[irqQuarks.size()]; | |
517 | for (int i = 0; i < irqQuarks.size(); i++) { | |
518 | int irqQuark = irqQuarks.get(i); | |
519 | int irq = Integer.parseInt(ssq.getAttributeName(irqQuark)); | |
520 | ResourcesEntry entry = new ResourcesEntry(irqQuark, ctfKernelTrace, Type.IRQ, irq); | |
521 | groupEntry.addChild(entry); | |
522 | irqEntries[i] = entry; | |
523 | } | |
524 | List<Integer> softIrqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.SOFT_IRQS, "*"); //$NON-NLS-1$ | |
525 | ResourcesEntry[] softIrqEntries = new ResourcesEntry[softIrqQuarks.size()]; | |
526 | for (int i = 0; i < softIrqQuarks.size(); i++) { | |
527 | int softIrqQuark = softIrqQuarks.get(i); | |
528 | int softIrq = Integer.parseInt(ssq.getAttributeName(softIrqQuark)); | |
529 | ResourcesEntry entry = new ResourcesEntry(softIrqQuark, ctfKernelTrace, Type.SOFT_IRQ, softIrq); | |
530 | groupEntry.addChild(entry); | |
531 | softIrqEntries[i] = entry; | |
532 | } | |
533 | } | |
534 | } | |
faa38350 PT |
535 | synchronized (fEntryListMap) { |
536 | fEntryListMap.put(trace, (ArrayList<TraceEntry>) entryList.clone()); | |
537 | } | |
538 | if (trace == fTrace) { | |
d7ee91bb | 539 | refresh(); |
6151d86c | 540 | } |
6151d86c | 541 | for (TraceEntry traceEntry : entryList) { |
faa38350 PT |
542 | if (monitor.isCanceled()) { |
543 | return; | |
544 | } | |
6151d86c | 545 | CtfKernelTrace ctfKernelTrace = traceEntry.getTrace(); |
35c160d9 | 546 | ITmfStateSystem ssq = ctfKernelTrace.getStateSystems().get(CtfKernelTrace.STATE_ID); |
6151d86c PT |
547 | long startTime = ssq.getStartTime(); |
548 | long endTime = ssq.getCurrentEndTime() + 1; | |
549 | long resolution = (endTime - startTime) / fDisplayWidth; | |
550 | for (ResourcesEntry entry : traceEntry.getChildren()) { | |
faa38350 | 551 | List<ITimeEvent> eventList = getEventList(entry, startTime, endTime, resolution, false, monitor); |
6151d86c PT |
552 | entry.setEventList(eventList); |
553 | redraw(); | |
554 | } | |
555 | } | |
556 | } | |
557 | ||
558 | private static List<ITimeEvent> getEventList(ResourcesEntry entry, | |
559 | long startTime, long endTime, long resolution, boolean includeNull, | |
560 | IProgressMonitor monitor) { | |
35c160d9 | 561 | ITmfStateSystem ssq = entry.getTrace().getStateSystems().get(CtfKernelTrace.STATE_ID); |
41b5c37f AM |
562 | final long realStart = Math.max(startTime, ssq.getStartTime()); |
563 | final long realEnd = Math.min(endTime, ssq.getCurrentEndTime() + 1); | |
564 | if (realEnd <= realStart) { | |
6151d86c PT |
565 | return null; |
566 | } | |
567 | List<ITimeEvent> eventList = null; | |
568 | int quark = entry.getQuark(); | |
569 | try { | |
570 | if (entry.getType().equals(Type.CPU)) { | |
571 | int statusQuark = ssq.getQuarkRelative(quark, Attributes.STATUS); | |
41b5c37f | 572 | List<ITmfStateInterval> statusIntervals = ssq.queryHistoryRange(statusQuark, realStart, realEnd - 1, resolution, monitor); |
6151d86c PT |
573 | eventList = new ArrayList<ITimeEvent>(statusIntervals.size()); |
574 | long lastEndTime = -1; | |
575 | for (ITmfStateInterval statusInterval : statusIntervals) { | |
576 | if (monitor.isCanceled()) { | |
577 | return null; | |
578 | } | |
579 | int status = statusInterval.getStateValue().unboxInt(); | |
580 | long time = statusInterval.getStartTime(); | |
581 | long duration = statusInterval.getEndTime() - time + 1; | |
582 | if (!statusInterval.getStateValue().isNull()) { | |
583 | if (lastEndTime != time && lastEndTime != -1) { | |
584 | eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime)); | |
585 | } | |
586 | eventList.add(new ResourcesEvent(entry, time, duration, status)); | |
587 | lastEndTime = time + duration; | |
588 | } else { | |
589 | if (includeNull) { | |
590 | eventList.add(new ResourcesEvent(entry, time, duration)); | |
591 | } | |
592 | } | |
593 | } | |
594 | } else if (entry.getType().equals(Type.IRQ)) { | |
41b5c37f | 595 | List<ITmfStateInterval> irqIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor); |
6151d86c PT |
596 | eventList = new ArrayList<ITimeEvent>(irqIntervals.size()); |
597 | long lastEndTime = -1; | |
598 | boolean lastIsNull = true; | |
599 | for (ITmfStateInterval irqInterval : irqIntervals) { | |
600 | if (monitor.isCanceled()) { | |
601 | return null; | |
602 | } | |
603 | long time = irqInterval.getStartTime(); | |
604 | long duration = irqInterval.getEndTime() - time + 1; | |
605 | if (!irqInterval.getStateValue().isNull()) { | |
606 | int cpu = irqInterval.getStateValue().unboxInt(); | |
607 | eventList.add(new ResourcesEvent(entry, time, duration, cpu)); | |
608 | lastIsNull = false; | |
609 | } else { | |
610 | if (lastEndTime != time && lastEndTime != -1 && lastIsNull) { | |
611 | eventList.add(new ResourcesEvent(entry, lastEndTime, time - lastEndTime, -1)); | |
612 | } | |
613 | if (includeNull) { | |
614 | eventList.add(new ResourcesEvent(entry, time, duration)); | |
615 | } | |
616 | lastIsNull = true; | |
617 | } | |
618 | lastEndTime = time + duration; | |
619 | } | |
620 | } else if (entry.getType().equals(Type.SOFT_IRQ)) { | |
41b5c37f | 621 | List<ITmfStateInterval> softIrqIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor); |
6151d86c PT |
622 | eventList = new ArrayList<ITimeEvent>(softIrqIntervals.size()); |
623 | long lastEndTime = -1; | |
624 | boolean lastIsNull = true; | |
625 | for (ITmfStateInterval softIrqInterval : softIrqIntervals) { | |
626 | if (monitor.isCanceled()) { | |
627 | return null; | |
628 | } | |
629 | long time = softIrqInterval.getStartTime(); | |
630 | long duration = softIrqInterval.getEndTime() - time + 1; | |
631 | if (!softIrqInterval.getStateValue().isNull()) { | |
632 | int cpu = softIrqInterval.getStateValue().unboxInt(); | |
633 | eventList.add(new ResourcesEvent(entry, time, duration, cpu)); | |
634 | } else { | |
635 | if (lastEndTime != time && lastEndTime != -1 && lastIsNull) { | |
636 | eventList.add(new ResourcesEvent(entry, lastEndTime, time - lastEndTime, -1)); | |
637 | } | |
638 | if (includeNull) { | |
639 | eventList.add(new ResourcesEvent(entry, time, duration)); | |
640 | } | |
641 | lastIsNull = true; | |
642 | } | |
643 | lastEndTime = time + duration; | |
644 | } | |
645 | } | |
646 | } catch (AttributeNotFoundException e) { | |
647 | e.printStackTrace(); | |
648 | } catch (TimeRangeException e) { | |
649 | e.printStackTrace(); | |
650 | } catch (StateValueTypeException e) { | |
651 | e.printStackTrace(); | |
96345c5a AM |
652 | } catch (StateSystemDisposedException e) { |
653 | /* Ignored */ | |
6151d86c PT |
654 | } |
655 | return eventList; | |
656 | } | |
657 | ||
d7ee91bb | 658 | private void refresh() { |
6151d86c PT |
659 | Display.getDefault().asyncExec(new Runnable() { |
660 | @Override | |
661 | public void run() { | |
662 | if (fTimeGraphViewer.getControl().isDisposed()) { | |
663 | return; | |
664 | } | |
665 | ITimeGraphEntry[] entries = null; | |
faa38350 PT |
666 | synchronized (fEntryListMap) { |
667 | fEntryList = fEntryListMap.get(fTrace); | |
668 | if (fEntryList == null) { | |
669 | fEntryList = new ArrayList<TraceEntry>(); | |
6151d86c | 670 | } |
faa38350 | 671 | entries = fEntryList.toArray(new ITimeGraphEntry[0]); |
6151d86c PT |
672 | } |
673 | if (entries != null) { | |
674 | Arrays.sort(entries, new TraceEntryComparator()); | |
675 | fTimeGraphViewer.setInput(entries); | |
676 | fTimeGraphViewer.setTimeBounds(fStartTime, fEndTime); | |
677 | ||
d7ee91bb PT |
678 | long timestamp = fTrace == null ? 0 : fTrace.getCurrentTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); |
679 | long startTime = fTrace == null ? 0 : fTrace.getCurrentRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
680 | long endTime = fTrace == null ? 0 : fTrace.getCurrentRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
681 | startTime = Math.max(startTime, fStartTime); | |
682 | endTime = Math.min(endTime, fEndTime); | |
683 | fTimeGraphViewer.setSelectedTime(timestamp, false); | |
684 | fTimeGraphViewer.setStartFinishTime(startTime, endTime); | |
6151d86c | 685 | |
d7ee91bb | 686 | startZoomThread(startTime, endTime); |
6151d86c PT |
687 | } |
688 | } | |
689 | }); | |
690 | } | |
691 | ||
692 | private void redraw() { | |
693 | synchronized (fSyncObj) { | |
694 | if (fRedrawState == State.IDLE) { | |
695 | fRedrawState = State.BUSY; | |
696 | } else { | |
697 | fRedrawState = State.PENDING; | |
698 | return; | |
699 | } | |
700 | } | |
701 | Display.getDefault().asyncExec(new Runnable() { | |
702 | @Override | |
703 | public void run() { | |
704 | if (fTimeGraphViewer.getControl().isDisposed()) { | |
705 | return; | |
706 | } | |
707 | fTimeGraphViewer.getControl().redraw(); | |
708 | fTimeGraphViewer.getControl().update(); | |
709 | synchronized (fSyncObj) { | |
710 | if (fRedrawState == State.PENDING) { | |
711 | fRedrawState = State.IDLE; | |
712 | redraw(); | |
713 | } else { | |
714 | fRedrawState = State.IDLE; | |
715 | } | |
716 | } | |
717 | } | |
718 | }); | |
719 | } | |
720 | ||
721 | private void startZoomThread(long startTime, long endTime) { | |
722 | if (fZoomThread != null) { | |
723 | fZoomThread.cancel(); | |
724 | } | |
faa38350 | 725 | fZoomThread = new ZoomThread(fEntryList, startTime, endTime); |
6151d86c PT |
726 | fZoomThread.start(); |
727 | } | |
728 | ||
729 | private void makeActions() { | |
730 | fPreviousResourceAction = fTimeGraphViewer.getPreviousItemAction(); | |
731 | fPreviousResourceAction.setText(Messages.ResourcesView_previousResourceActionNameText); | |
732 | fPreviousResourceAction.setToolTipText(Messages.ResourcesView_previousResourceActionToolTipText); | |
733 | fNextResourceAction = fTimeGraphViewer.getNextItemAction(); | |
734 | fNextResourceAction.setText(Messages.ResourcesView_nextResourceActionNameText); | |
735 | fNextResourceAction.setToolTipText(Messages.ResourcesView_previousResourceActionToolTipText); | |
736 | } | |
737 | ||
738 | private void contributeToActionBars() { | |
739 | IActionBars bars = getViewSite().getActionBars(); | |
740 | fillLocalToolBar(bars.getToolBarManager()); | |
741 | } | |
742 | ||
743 | private void fillLocalToolBar(IToolBarManager manager) { | |
744 | manager.add(fTimeGraphViewer.getShowLegendAction()); | |
745 | manager.add(new Separator()); | |
746 | manager.add(fTimeGraphViewer.getResetScaleAction()); | |
747 | manager.add(fTimeGraphViewer.getPreviousEventAction()); | |
748 | manager.add(fTimeGraphViewer.getNextEventAction()); | |
749 | manager.add(fPreviousResourceAction); | |
750 | manager.add(fNextResourceAction); | |
751 | manager.add(fTimeGraphViewer.getZoomInAction()); | |
752 | manager.add(fTimeGraphViewer.getZoomOutAction()); | |
753 | manager.add(new Separator()); | |
754 | } | |
755 | } |