1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.ui
.views
.resources
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Arrays
;
17 import java
.util
.Comparator
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
23 import org
.eclipse
.jface
.action
.Action
;
24 import org
.eclipse
.jface
.action
.IToolBarManager
;
25 import org
.eclipse
.jface
.action
.Separator
;
26 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.Attributes
;
27 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.ui
.Messages
;
28 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.ui
.views
.resources
.ResourcesEntry
.Type
;
29 import org
.eclipse
.linuxtools
.lttng2
.kernel
.core
.trace
.CtfKernelTrace
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfTimestamp
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentSelectedSignal
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfRangeSynchSignal
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfStateSystemBuildCompleted
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemQuerier
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
47 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
48 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphRangeListener
;
49 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
50 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphRangeUpdateEvent
;
51 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
52 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphViewer
;
53 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
54 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
55 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
56 import org
.eclipse
.swt
.SWT
;
57 import org
.eclipse
.swt
.widgets
.Composite
;
58 import org
.eclipse
.swt
.widgets
.Display
;
59 import org
.eclipse
.ui
.IActionBars
;
61 public class ResourcesView
extends TmfView
{
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
70 public static final String ID
= "org.eclipse.linuxtools.lttng2.kernel.ui.views.resources"; //$NON-NLS-1$
75 private static final long INITIAL_WINDOW_OFFSET
= (1L * 100 * 1000 * 1000); // .1sec
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
81 // The time graph viewer
82 TimeGraphViewer fTimeGraphViewer
;
84 // The selected experiment
85 private TmfExperiment
<ITmfEvent
> fSelectedExperiment
;
87 // The time graph entry list
88 private ArrayList
<TraceEntry
> fEntryList
;
91 private long fStartTime
;
94 private long fEndTime
;
97 private int fDisplayWidth
;
99 // The next resource action
100 private Action fNextResourceAction
;
102 // The previous resource action
103 private Action fPreviousResourceAction
;
106 private ZoomThread fZoomThread
;
108 // ------------------------------------------------------------------------
110 // ------------------------------------------------------------------------
112 private class TraceEntry
implements ITimeGraphEntry
{
114 private CtfKernelTrace fTrace
;
116 private long fTraceStartTime
;
118 private long fTraceEndTime
;
119 // The children of the entry
120 private ArrayList
<ResourcesEntry
> fChildren
;
122 private String fName
;
124 public TraceEntry(CtfKernelTrace trace
, String name
, long startTime
, long endTime
) {
126 fChildren
= new ArrayList
<ResourcesEntry
>();
128 fTraceStartTime
= startTime
;
129 fTraceEndTime
= endTime
;
133 public ITimeGraphEntry
getParent() {
138 public boolean hasChildren() {
139 return fChildren
!= null && fChildren
.size() > 0;
143 public ResourcesEntry
[] getChildren() {
144 return fChildren
.toArray(new ResourcesEntry
[0]);
148 public String
getName() {
153 public long getStartTime() {
154 return fTraceStartTime
;
158 public long getEndTime() {
159 return fTraceEndTime
;
163 public boolean hasTimeEvents() {
168 public Iterator
<ITimeEvent
> getTimeEventsIterator() {
173 public <T
extends ITimeEvent
> Iterator
<T
> getTimeEventsIterator(long startTime
, long stopTime
, long visibleDuration
) {
177 public CtfKernelTrace
getTrace() {
181 public void addChild(ResourcesEntry entry
) {
183 for (index
= 0; index
< fChildren
.size(); index
++) {
184 ResourcesEntry other
= fChildren
.get(index
);
185 if (entry
.getType().compareTo(other
.getType()) < 0) {
187 } else if (entry
.getType().equals(other
.getType())) {
188 if (entry
.getId() < other
.getId()) {
193 entry
.setParent(this);
194 fChildren
.add(index
, entry
);
198 private static class TraceEntryComparator
implements Comparator
<ITimeGraphEntry
> {
200 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
201 int result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
203 result
= o1
.getName().compareTo(o2
.getName());
209 private class ZoomThread
extends Thread
{
210 private long fZoomStartTime
;
211 private long fZoomEndTime
;
212 private IProgressMonitor fMonitor
;
214 public ZoomThread(long startTime
, long endTime
) {
215 super("ResourcesView zoom"); //$NON-NLS-1$
216 fZoomStartTime
= startTime
;
217 fZoomEndTime
= endTime
;
218 fMonitor
= new NullProgressMonitor();
223 ArrayList
<TraceEntry
> entryList
= fEntryList
;
224 if (entryList
== null) {
227 long resolution
= Math
.max(1, (fZoomEndTime
- fZoomStartTime
) / fDisplayWidth
);
228 for (TraceEntry traceEntry
: entryList
) {
229 for (ITimeGraphEntry child
: traceEntry
.getChildren()) {
230 ResourcesEntry entry
= (ResourcesEntry
) child
;
231 if (fZoomStartTime
<= entry
.getStartTime() && fZoomEndTime
>= entry
.getEndTime()) {
232 entry
.setZoomedEventList(null);
235 if (fMonitor
.isCanceled()) {
238 List
<ITimeEvent
> zoomedEventList
= getEventList(entry
, fZoomStartTime
, fZoomEndTime
, resolution
, true, fMonitor
);
239 if (fMonitor
.isCanceled()) {
242 entry
.setZoomedEventList(zoomedEventList
);
248 public void cancel() {
249 fMonitor
.setCanceled(true);
253 // ------------------------------------------------------------------------
255 // ------------------------------------------------------------------------
257 public ResourcesView() {
259 fDisplayWidth
= Display
.getDefault().getBounds().width
;
262 // ------------------------------------------------------------------------
264 // ------------------------------------------------------------------------
267 * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#createPartControl(org.eclipse.swt.widgets.Composite)
270 public void createPartControl(Composite parent
) {
271 fTimeGraphViewer
= new TimeGraphViewer(parent
, SWT
.NONE
);
273 fTimeGraphViewer
.setTimeGraphProvider(new ResourcesPresentationProvider());
275 fTimeGraphViewer
.setTimeCalendarFormat(true);
277 fTimeGraphViewer
.addRangeListener(new ITimeGraphRangeListener() {
279 public void timeRangeUpdated(TimeGraphRangeUpdateEvent event
) {
280 long startTime
= event
.getStartTime();
281 long endTime
= event
.getEndTime();
282 TmfTimeRange range
= new TmfTimeRange(new CtfTmfTimestamp(startTime
), new CtfTmfTimestamp(endTime
));
283 TmfTimestamp time
= new CtfTmfTimestamp(fTimeGraphViewer
.getSelectedTime());
284 broadcast(new TmfRangeSynchSignal(ResourcesView
.this, range
, time
));
285 startZoomThread(startTime
, endTime
);
289 fTimeGraphViewer
.addTimeListener(new ITimeGraphTimeListener() {
291 public void timeSelected(TimeGraphTimeEvent event
) {
292 long time
= event
.getTime();
293 broadcast(new TmfTimeSynchSignal(ResourcesView
.this, new CtfTmfTimestamp(time
)));
297 final Thread thread
= new Thread("ResourcesView build") { //$NON-NLS-1$
300 if (TmfExperiment
.getCurrentExperiment() != null) {
301 selectExperiment(TmfExperiment
.getCurrentExperiment());
307 // View Action Handling
309 contributeToActionBars();
313 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
316 public void setFocus() {
317 fTimeGraphViewer
.setFocus();
320 // ------------------------------------------------------------------------
322 // ------------------------------------------------------------------------
325 public void experimentSelected(final TmfExperimentSelectedSignal
<?
extends TmfEvent
> signal
) {
326 if (signal
.getExperiment().equals(fSelectedExperiment
)) {
330 final Thread thread
= new Thread("ResourcesView build") { //$NON-NLS-1$
333 selectExperiment(signal
.getExperiment());
340 public void synchToTime(final TmfTimeSynchSignal signal
) {
341 if (signal
.getSource() == this) {
344 final long time
= signal
.getCurrentTime().normalize(0, -9).getValue();
345 Display
.getDefault().asyncExec(new Runnable() {
348 if (fTimeGraphViewer
.getControl().isDisposed()) {
351 fTimeGraphViewer
.setSelectedTime(time
, true);
352 startZoomThread(fTimeGraphViewer
.getTime0(), fTimeGraphViewer
.getTime1());
358 public void synchToRange(final TmfRangeSynchSignal signal
) {
359 if (signal
.getSource() == this) {
362 final long startTime
= signal
.getCurrentRange().getStartTime().normalize(0, -9).getValue();
363 final long endTime
= signal
.getCurrentRange().getEndTime().normalize(0, -9).getValue();
364 final long time
= signal
.getCurrentTime().normalize(0, -9).getValue();
365 Display
.getDefault().asyncExec(new Runnable() {
368 if (fTimeGraphViewer
.getControl().isDisposed()) {
371 fTimeGraphViewer
.setStartFinishTime(startTime
, endTime
);
372 fTimeGraphViewer
.setSelectedTime(time
, false);
373 startZoomThread(startTime
, endTime
);
379 public void stateSystemBuildCompleted (final TmfStateSystemBuildCompleted signal
) {
380 final TmfExperiment
<?
> selectedExperiment
= fSelectedExperiment
;
381 if (selectedExperiment
== null) {
384 for (ITmfTrace
<?
> trace
: selectedExperiment
.getTraces()) {
385 if (trace
== signal
.getTrace() && trace
instanceof CtfKernelTrace
) {
386 final Thread thread
= new Thread("ResourcesView build") { //$NON-NLS-1$
390 selectExperiment(selectedExperiment
);
398 // ------------------------------------------------------------------------
400 // ------------------------------------------------------------------------
402 @SuppressWarnings("unchecked")
403 private void selectExperiment(TmfExperiment
<?
> experiment
) {
404 fStartTime
= Long
.MAX_VALUE
;
405 fEndTime
= Long
.MIN_VALUE
;
406 fSelectedExperiment
= (TmfExperiment
<ITmfEvent
>) experiment
;
407 ArrayList
<TraceEntry
> entryList
= new ArrayList
<TraceEntry
>();
408 for (ITmfTrace
<?
> trace
: experiment
.getTraces()) {
409 if (trace
instanceof CtfKernelTrace
) {
410 CtfKernelTrace ctfKernelTrace
= (CtfKernelTrace
) trace
;
411 IStateSystemQuerier ssq
= ctfKernelTrace
.getStateSystem();
412 long startTime
= ssq
.getStartTime();
413 long endTime
= ssq
.getCurrentEndTime() + 1;
414 TraceEntry groupEntry
= new TraceEntry(ctfKernelTrace
, trace
.getName(), startTime
, endTime
);
415 entryList
.add(groupEntry
);
416 fStartTime
= Math
.min(fStartTime
, startTime
);
417 fEndTime
= Math
.max(fEndTime
, endTime
);
418 List
<Integer
> cpuQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*"); //$NON-NLS-1$
419 ResourcesEntry
[] cpuEntries
= new ResourcesEntry
[cpuQuarks
.size()];
420 for (int i
= 0; i
< cpuQuarks
.size(); i
++) {
421 int cpuQuark
= cpuQuarks
.get(i
);
422 int cpu
= Integer
.parseInt(ssq
.getAttributeName(cpuQuark
));
423 ResourcesEntry entry
= new ResourcesEntry(cpuQuark
, ctfKernelTrace
, Type
.CPU
, cpu
);
424 groupEntry
.addChild(entry
);
425 cpuEntries
[i
] = entry
;
427 List
<Integer
> irqQuarks
= ssq
.getQuarks(Attributes
.RESOURCES
, Attributes
.IRQS
, "*"); //$NON-NLS-1$
428 ResourcesEntry
[] irqEntries
= new ResourcesEntry
[irqQuarks
.size()];
429 for (int i
= 0; i
< irqQuarks
.size(); i
++) {
430 int irqQuark
= irqQuarks
.get(i
);
431 int irq
= Integer
.parseInt(ssq
.getAttributeName(irqQuark
));
432 ResourcesEntry entry
= new ResourcesEntry(irqQuark
, ctfKernelTrace
, Type
.IRQ
, irq
);
433 groupEntry
.addChild(entry
);
434 irqEntries
[i
] = entry
;
436 List
<Integer
> softIrqQuarks
= ssq
.getQuarks(Attributes
.RESOURCES
, Attributes
.SOFT_IRQS
, "*"); //$NON-NLS-1$
437 ResourcesEntry
[] softIrqEntries
= new ResourcesEntry
[softIrqQuarks
.size()];
438 for (int i
= 0; i
< softIrqQuarks
.size(); i
++) {
439 int softIrqQuark
= softIrqQuarks
.get(i
);
440 int softIrq
= Integer
.parseInt(ssq
.getAttributeName(softIrqQuark
));
441 ResourcesEntry entry
= new ResourcesEntry(softIrqQuark
, ctfKernelTrace
, Type
.SOFT_IRQ
, softIrq
);
442 groupEntry
.addChild(entry
);
443 softIrqEntries
[i
] = entry
;
447 fEntryList
= entryList
;
448 refresh(INITIAL_WINDOW_OFFSET
);
449 for (TraceEntry traceEntry
: fEntryList
) {
450 CtfKernelTrace ctfKernelTrace
= ((TraceEntry
) traceEntry
).getTrace();
451 IStateSystemQuerier ssq
= ctfKernelTrace
.getStateSystem();
452 long startTime
= ssq
.getStartTime();
453 long endTime
= ssq
.getCurrentEndTime() + 1;
454 long resolution
= (endTime
- startTime
) / fDisplayWidth
;
455 for (ResourcesEntry entry
: traceEntry
.getChildren()) {
456 List
<ITimeEvent
> eventList
= getEventList(entry
, startTime
, endTime
, resolution
, false, new NullProgressMonitor());
457 entry
.setEventList(eventList
);
463 private List
<ITimeEvent
> getEventList(ResourcesEntry entry
, long startTime
, long endTime
, long resolution
, boolean includeNull
, IProgressMonitor monitor
) {
464 IStateSystemQuerier ssq
= entry
.getTrace().getStateSystem();
465 startTime
= Math
.max(startTime
, ssq
.getStartTime());
466 endTime
= Math
.min(endTime
, ssq
.getCurrentEndTime() + 1);
467 if (endTime
<= startTime
) {
470 List
<ITimeEvent
> eventList
= null;
471 int quark
= entry
.getQuark();
473 if (entry
.getType().equals(Type
.CPU
)) {
474 int statusQuark
= ssq
.getQuarkRelative(quark
, Attributes
.STATUS
);
475 List
<ITmfStateInterval
> statusIntervals
= ssq
.queryHistoryRange(statusQuark
, startTime
, endTime
- 1, resolution
);
476 eventList
= new ArrayList
<ITimeEvent
>(statusIntervals
.size());
477 long lastEndTime
= -1;
478 for (ITmfStateInterval statusInterval
: statusIntervals
) {
479 if (monitor
.isCanceled()) {
482 int status
= statusInterval
.getStateValue().unboxInt();
483 long time
= statusInterval
.getStartTime();
484 long duration
= statusInterval
.getEndTime() - time
+ 1;
485 if (!statusInterval
.getStateValue().isNull()) {
486 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
487 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
489 eventList
.add(new ResourcesEvent(entry
, time
, duration
, status
));
490 lastEndTime
= time
+ duration
;
493 eventList
.add(new ResourcesEvent(entry
, time
, duration
));
497 } else if (entry
.getType().equals(Type
.IRQ
)) {
498 List
<ITmfStateInterval
> irqIntervals
= ssq
.queryHistoryRange(quark
, startTime
, endTime
- 1, resolution
);
499 eventList
= new ArrayList
<ITimeEvent
>(irqIntervals
.size());
500 long lastEndTime
= -1;
501 boolean lastIsNull
= true;
502 for (ITmfStateInterval irqInterval
: irqIntervals
) {
503 if (monitor
.isCanceled()) {
506 long time
= irqInterval
.getStartTime();
507 long duration
= irqInterval
.getEndTime() - time
+ 1;
508 if (!irqInterval
.getStateValue().isNull()) {
509 int cpu
= irqInterval
.getStateValue().unboxInt();
510 eventList
.add(new ResourcesEvent(entry
, time
, duration
, cpu
));
513 if (lastEndTime
!= time
&& lastEndTime
!= -1 && lastIsNull
) {
514 eventList
.add(new ResourcesEvent(entry
, lastEndTime
, time
- lastEndTime
, -1));
517 eventList
.add(new ResourcesEvent(entry
, time
, duration
));
521 lastEndTime
= time
+ duration
;
523 } else if (entry
.getType().equals(Type
.SOFT_IRQ
)) {
524 List
<ITmfStateInterval
> softIrqIntervals
= ssq
.queryHistoryRange(quark
, startTime
, endTime
- 1, resolution
);
525 eventList
= new ArrayList
<ITimeEvent
>(softIrqIntervals
.size());
526 long lastEndTime
= -1;
527 boolean lastIsNull
= true;
528 for (ITmfStateInterval softIrqInterval
: softIrqIntervals
) {
529 if (monitor
.isCanceled()) {
532 long time
= softIrqInterval
.getStartTime();
533 long duration
= softIrqInterval
.getEndTime() - time
+ 1;
534 if (!softIrqInterval
.getStateValue().isNull()) {
535 int cpu
= softIrqInterval
.getStateValue().unboxInt();
536 eventList
.add(new ResourcesEvent(entry
, time
, duration
, cpu
));
538 if (lastEndTime
!= time
&& lastEndTime
!= -1 && lastIsNull
) {
539 eventList
.add(new ResourcesEvent(entry
, lastEndTime
, time
- lastEndTime
, -1));
542 eventList
.add(new ResourcesEvent(entry
, time
, duration
));
546 lastEndTime
= time
+ duration
;
549 } catch (AttributeNotFoundException e
) {
551 } catch (TimeRangeException e
) {
553 } catch (StateValueTypeException e
) {
559 private void refresh(final long windowRange
) {
560 Display
.getDefault().asyncExec(new Runnable() {
563 if (fTimeGraphViewer
.getControl().isDisposed()) {
566 ITimeGraphEntry
[] entries
= fEntryList
.toArray(new ITimeGraphEntry
[0]);
567 Arrays
.sort(entries
, new TraceEntryComparator());
568 fTimeGraphViewer
.setInput(entries
);
569 fTimeGraphViewer
.setTimeBounds(fStartTime
, fEndTime
);
571 long endTime
= fStartTime
+ windowRange
;
573 if (fEndTime
< endTime
) {
576 fTimeGraphViewer
.setStartFinishTime(fStartTime
, endTime
);
578 startZoomThread(fStartTime
, endTime
);
584 private void redraw() {
585 Display
.getDefault().asyncExec(new Runnable() {
588 if (fTimeGraphViewer
.getControl().isDisposed()) {
591 fTimeGraphViewer
.getControl().redraw();
592 fTimeGraphViewer
.getControl().update();
597 private void startZoomThread(long startTime
, long endTime
) {
598 if (fZoomThread
!= null) {
599 fZoomThread
.cancel();
601 fZoomThread
= new ZoomThread(startTime
, endTime
);
605 private void makeActions() {
606 fPreviousResourceAction
= fTimeGraphViewer
.getPreviousItemAction();
607 fPreviousResourceAction
.setText(Messages
.ResourcesView_previousResourceActionNameText
);
608 fPreviousResourceAction
.setToolTipText(Messages
.ResourcesView_previousResourceActionToolTipText
);
609 fNextResourceAction
= fTimeGraphViewer
.getNextItemAction();
610 fNextResourceAction
.setText(Messages
.ResourcesView_nextResourceActionNameText
);
611 fNextResourceAction
.setToolTipText(Messages
.ResourcesView_previousResourceActionToolTipText
);
614 private void contributeToActionBars() {
615 IActionBars bars
= getViewSite().getActionBars();
616 fillLocalToolBar(bars
.getToolBarManager());
619 private void fillLocalToolBar(IToolBarManager manager
) {
620 manager
.add(fTimeGraphViewer
.getShowLegendAction());
621 manager
.add(new Separator());
622 manager
.add(fTimeGraphViewer
.getResetScaleAction());
623 manager
.add(fTimeGraphViewer
.getPreviousEventAction());
624 manager
.add(fTimeGraphViewer
.getNextEventAction());
625 manager
.add(fPreviousResourceAction
);
626 manager
.add(fNextResourceAction
);
627 manager
.add(fTimeGraphViewer
.getZoomInAction());
628 manager
.add(fTimeGraphViewer
.getZoomOutAction());
629 manager
.add(new Separator());