tmf: Make TimeGraphEntry implementation less restrictive
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.ui / src / org / eclipse / linuxtools / internal / lttng2 / kernel / ui / views / resources / ResourcesView.java
index 135b45259303df808b8adb3b9669938b0a24199b..37011c90bbe9eedfdc2d530bd8391b278cc12907 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2013 Ericsson, École Polytechnique de Montréal
+ * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
 package org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
 import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages;
 import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources.ResourcesEntry.Type;
-import org.eclipse.linuxtools.lttng2.kernel.core.trace.LttngKernelTrace;
-import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
-import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException;
-import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
-import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
-import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
-import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
+import org.eclipse.linuxtools.lttng2.kernel.core.analysis.LttngKernelAnalysisModule;
+import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem;
+import org.eclipse.linuxtools.statesystem.core.exceptions.AttributeNotFoundException;
+import org.eclipse.linuxtools.statesystem.core.exceptions.StateSystemDisposedException;
+import org.eclipse.linuxtools.statesystem.core.exceptions.StateValueTypeException;
+import org.eclipse.linuxtools.statesystem.core.exceptions.TimeRangeException;
+import org.eclipse.linuxtools.statesystem.core.interval.ITmfStateInterval;
+import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
 import org.eclipse.linuxtools.tmf.ui.views.timegraph.AbstractTimeGraphView;
 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
+import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
+import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.NullTimeEvent;
 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent;
 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
 
@@ -43,16 +50,13 @@ public class ResourcesView extends AbstractTimeGraphView {
     /** View ID. */
     public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.ui.views.resources"; //$NON-NLS-1$
 
-    /**
-     * Default value for events with no other value. Since value in this case is
-     * often a CPU number, this constant should be <0
-     */
-    public static final int NO_VALUE_EVENT = -999;
-
     private static final String[] FILTER_COLUMN_NAMES = new String[] {
             Messages.ResourcesView_stateTypeName
     };
 
+    // Timeout between updates in the build thread in ms
+    private static final long BUILD_UPDATE_TIMEOUT = 500;
+
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -65,6 +69,10 @@ public class ResourcesView extends AbstractTimeGraphView {
         setFilterColumns(FILTER_COLUMN_NAMES);
     }
 
+    // ------------------------------------------------------------------------
+    // Internal
+    // ------------------------------------------------------------------------
+
     @Override
     protected String getNextText() {
         return Messages.ResourcesView_nextResourceActionNameText;
@@ -85,106 +93,129 @@ public class ResourcesView extends AbstractTimeGraphView {
         return Messages.ResourcesView_previousResourceActionToolTipText;
     }
 
-    // ------------------------------------------------------------------------
-    // Internal
-    // ------------------------------------------------------------------------
-
     @Override
-    protected void buildEventList(ITmfTrace trace, IProgressMonitor monitor) {
-        setStartTime(Long.MAX_VALUE);
-        setEndTime(Long.MIN_VALUE);
+    protected void buildEventList(ITmfTrace trace, ITmfTrace parentTrace, IProgressMonitor monitor) {
+        if (trace == null) {
+            return;
+        }
+        ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, LttngKernelAnalysisModule.ID);
+        if (ssq == null) {
+            return;
+        }
+
+        Map<Integer, ResourcesEntry> entryMap = new HashMap<>();
+        TimeGraphEntry traceEntry = null;
 
-        ArrayList<ResourcesEntry> entryList = new ArrayList<ResourcesEntry>();
-        for (ITmfTrace aTrace : fTraceManager.getActiveTraceSet()) {
+        long startTime = ssq.getStartTime();
+        long start = startTime;
+        setStartTime(Math.min(getStartTime(), startTime));
+        boolean complete = false;
+        while (!complete) {
             if (monitor.isCanceled()) {
                 return;
             }
-            if (aTrace instanceof LttngKernelTrace) {
-                LttngKernelTrace lttngKernelTrace = (LttngKernelTrace) aTrace;
-                ITmfStateSystem ssq = lttngKernelTrace.getStateSystems().get(LttngKernelTrace.STATE_ID);
-                if (!ssq.waitUntilBuilt()) {
-                    return;
-                }
-                long startTime = ssq.getStartTime();
-                long endTime = ssq.getCurrentEndTime() + 1;
-                ResourcesEntry groupEntry = new ResourcesEntry(lttngKernelTrace, aTrace.getName(), startTime, endTime, 0);
-                entryList.add(groupEntry);
-                setStartTime(Math.min(getStartTime(), startTime));
-                setEndTime(Math.max(getEndTime(), endTime));
-                List<Integer> cpuQuarks = ssq.getQuarks(Attributes.CPUS, "*"); //$NON-NLS-1$
-                ResourcesEntry[] cpuEntries = new ResourcesEntry[cpuQuarks.size()];
-                for (int i = 0; i < cpuQuarks.size(); i++) {
-                    int cpuQuark = cpuQuarks.get(i);
-                    int cpu = Integer.parseInt(ssq.getAttributeName(cpuQuark));
-                    ResourcesEntry entry = new ResourcesEntry(cpuQuark, lttngKernelTrace, getStartTime(), getEndTime(), Type.CPU, cpu);
-                    groupEntry.addChild(entry);
-                    cpuEntries[i] = entry;
+            complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
+            if (ssq.isCancelled()) {
+                return;
+            }
+            long end = ssq.getCurrentEndTime();
+            if (start == end && !complete) { // when complete execute one last time regardless of end time
+                continue;
+            }
+            long endTime = end + 1;
+            setEndTime(Math.max(getEndTime(), endTime));
+
+            if (traceEntry == null) {
+                traceEntry = new ResourcesEntry(trace, trace.getName(), startTime, endTime, 0);
+                List<TimeGraphEntry> entryList = Collections.singletonList(traceEntry);
+                addToEntryList(parentTrace, entryList);
+            } else {
+                traceEntry.updateEndTime(endTime);
+            }
+
+            List<Integer> cpuQuarks = ssq.getQuarks(Attributes.CPUS, "*"); //$NON-NLS-1$
+            for (Integer cpuQuark : cpuQuarks) {
+                int cpu = Integer.parseInt(ssq.getAttributeName(cpuQuark));
+                ResourcesEntry entry = entryMap.get(cpuQuark);
+                if (entry == null) {
+                    entry = new ResourcesEntry(cpuQuark, trace, startTime, endTime, Type.CPU, cpu);
+                    entryMap.put(cpuQuark, entry);
+                    traceEntry.addChild(entry);
+                } else {
+                    entry.updateEndTime(endTime);
                 }
-                List<Integer> irqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.IRQS, "*"); //$NON-NLS-1$
-                ResourcesEntry[] irqEntries = new ResourcesEntry[irqQuarks.size()];
-                for (int i = 0; i < irqQuarks.size(); i++) {
-                    int irqQuark = irqQuarks.get(i);
-                    int irq = Integer.parseInt(ssq.getAttributeName(irqQuark));
-                    ResourcesEntry entry = new ResourcesEntry(irqQuark, lttngKernelTrace, getStartTime(), getEndTime(), Type.IRQ, irq);
-                    groupEntry.addChild(entry);
-                    irqEntries[i] = entry;
+            }
+            List<Integer> irqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.IRQS, "*"); //$NON-NLS-1$
+            for (Integer irqQuark : irqQuarks) {
+                int irq = Integer.parseInt(ssq.getAttributeName(irqQuark));
+                ResourcesEntry entry = entryMap.get(irqQuark);
+                if (entry == null) {
+                    entry = new ResourcesEntry(irqQuark, trace, startTime, endTime, Type.IRQ, irq);
+                    entryMap.put(irqQuark, entry);
+                    traceEntry.addChild(entry);
+                } else {
+                    entry.updateEndTime(endTime);
                 }
-                List<Integer> softIrqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.SOFT_IRQS, "*"); //$NON-NLS-1$
-                ResourcesEntry[] softIrqEntries = new ResourcesEntry[softIrqQuarks.size()];
-                for (int i = 0; i < softIrqQuarks.size(); i++) {
-                    int softIrqQuark = softIrqQuarks.get(i);
-                    int softIrq = Integer.parseInt(ssq.getAttributeName(softIrqQuark));
-                    ResourcesEntry entry = new ResourcesEntry(softIrqQuark, lttngKernelTrace, getStartTime(), getEndTime(), Type.SOFT_IRQ, softIrq);
-                    groupEntry.addChild(entry);
-                    softIrqEntries[i] = entry;
+            }
+            List<Integer> softIrqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.SOFT_IRQS, "*"); //$NON-NLS-1$
+            for (Integer softIrqQuark : softIrqQuarks) {
+                int softIrq = Integer.parseInt(ssq.getAttributeName(softIrqQuark));
+                ResourcesEntry entry = entryMap.get(softIrqQuark);
+                if (entry == null) {
+                    entry = new ResourcesEntry(softIrqQuark, trace, startTime, endTime, Type.SOFT_IRQ, softIrq);
+                    entryMap.put(softIrqQuark, entry);
+                    traceEntry.addChild(entry);
+                } else {
+                    entry.updateEndTime(endTime);
                 }
             }
-        }
-        putEntryList(trace, new ArrayList<TimeGraphEntry>(entryList));
 
-        if (trace.equals(getTrace())) {
-            refresh();
-        }
-        for (ResourcesEntry traceEntry : entryList) {
-            if (monitor.isCanceled()) {
-                return;
+            if (parentTrace.equals(getTrace())) {
+                refresh();
             }
-            LttngKernelTrace lttngKernelTrace = traceEntry.getTrace();
-            ITmfStateSystem ssq = lttngKernelTrace.getStateSystems().get(LttngKernelTrace.STATE_ID);
-            long startTime = ssq.getStartTime();
-            long endTime = ssq.getCurrentEndTime() + 1;
-            long resolution = (endTime - startTime) / getDisplayWidth();
-            for (TimeGraphEntry entry : traceEntry.getChildren()) {
-                List<ITimeEvent> eventList = getEventList(entry, startTime, endTime, resolution, monitor);
-                entry.setEventList(eventList);
-                redraw();
+            long resolution = Math.max(1, (endTime - ssq.getStartTime()) / getDisplayWidth());
+            for (ITimeGraphEntry child : traceEntry.getChildren()) {
+                if (monitor.isCanceled()) {
+                    return;
+                }
+                if (child instanceof TimeGraphEntry) {
+                    TimeGraphEntry entry = (TimeGraphEntry) child;
+                    List<ITimeEvent> eventList = getEventList(entry, start, endTime, resolution, monitor);
+                    if (eventList != null) {
+                        for (ITimeEvent event : eventList) {
+                            entry.addEvent(event);
+                        }
+                    }
+                    redraw();
+                }
             }
+
+            start = end;
         }
     }
 
     @Override
-    protected List<ITimeEvent> getEventList(TimeGraphEntry entry,
+    protected @Nullable List<ITimeEvent> getEventList(TimeGraphEntry entry,
             long startTime, long endTime, long resolution,
             IProgressMonitor monitor) {
-        ITmfStateSystem ssq = entry.getTrace().getStateSystems().get(LttngKernelTrace.STATE_ID);
+        ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
+        ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(resourcesEntry.getTrace(), LttngKernelAnalysisModule.ID);
+        if (ssq == null) {
+            return null;
+        }
         final long realStart = Math.max(startTime, ssq.getStartTime());
         final long realEnd = Math.min(endTime, ssq.getCurrentEndTime() + 1);
         if (realEnd <= realStart) {
             return null;
         }
         List<ITimeEvent> eventList = null;
-
-        if (!(entry instanceof ResourcesEntry)) {
-            return eventList;
-        }
-        ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
         int quark = resourcesEntry.getQuark();
 
         try {
             if (resourcesEntry.getType().equals(Type.CPU)) {
                 int statusQuark = ssq.getQuarkRelative(quark, Attributes.STATUS);
                 List<ITmfStateInterval> statusIntervals = ssq.queryHistoryRange(statusQuark, realStart, realEnd - 1, resolution, monitor);
-                eventList = new ArrayList<ITimeEvent>(statusIntervals.size());
+                eventList = new ArrayList<>(statusIntervals.size());
                 long lastEndTime = -1;
                 for (ITmfStateInterval statusInterval : statusIntervals) {
                     if (monitor.isCanceled()) {
@@ -198,16 +229,15 @@ public class ResourcesView extends AbstractTimeGraphView {
                             eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
                         }
                         eventList.add(new TimeEvent(entry, time, duration, status));
-                        lastEndTime = time + duration;
-                    } else {
-                        if (true) {// includeNull) {
-                            eventList.add(new TimeEvent(entry, time, duration, NO_VALUE_EVENT));
-                        }
+                    } else if (lastEndTime == -1 || time + duration >= endTime) {
+                        // add null event if it intersects the start or end time
+                        eventList.add(new NullTimeEvent(entry, time, duration));
                     }
+                    lastEndTime = time + duration;
                 }
             } else if (resourcesEntry.getType().equals(Type.IRQ)) {
                 List<ITmfStateInterval> irqIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor);
-                eventList = new ArrayList<ITimeEvent>(irqIntervals.size());
+                eventList = new ArrayList<>(irqIntervals.size());
                 long lastEndTime = -1;
                 boolean lastIsNull = true;
                 for (ITmfStateInterval irqInterval : irqIntervals) {
@@ -221,11 +251,18 @@ public class ResourcesView extends AbstractTimeGraphView {
                         eventList.add(new TimeEvent(entry, time, duration, cpu));
                         lastIsNull = false;
                     } else {
-                        if (lastEndTime != time && lastEndTime != -1 && lastIsNull) {
-                            /* This is a special case where we want to show IRQ_ACTIVE state but we don't know the CPU (it is between two null samples) */
-                            eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
+                        if (lastEndTime == -1) {
+                            // add null event if it intersects the start time
+                            eventList.add(new NullTimeEvent(entry, time, duration));
                         } else {
-                            eventList.add(new TimeEvent(entry, time, duration, NO_VALUE_EVENT));
+                            if (lastEndTime != time && lastIsNull) {
+                                /* This is a special case where we want to show IRQ_ACTIVE state but we don't know the CPU (it is between two null samples) */
+                                eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
+                            }
+                            if (time + duration >= endTime) {
+                                // add null event if it intersects the end time
+                                eventList.add(new NullTimeEvent(entry, time, duration));
+                            }
                         }
                         lastIsNull = true;
                     }
@@ -233,7 +270,7 @@ public class ResourcesView extends AbstractTimeGraphView {
                 }
             } else if (resourcesEntry.getType().equals(Type.SOFT_IRQ)) {
                 List<ITmfStateInterval> softIrqIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor);
-                eventList = new ArrayList<ITimeEvent>(softIrqIntervals.size());
+                eventList = new ArrayList<>(softIrqIntervals.size());
                 long lastEndTime = -1;
                 boolean lastIsNull = true;
                 for (ITmfStateInterval softIrqInterval : softIrqIntervals) {
@@ -246,11 +283,18 @@ public class ResourcesView extends AbstractTimeGraphView {
                         int cpu = softIrqInterval.getStateValue().unboxInt();
                         eventList.add(new TimeEvent(entry, time, duration, cpu));
                     } else {
-                        if (lastEndTime != time && lastEndTime != -1 && lastIsNull) {
-                            /* This is a special case where we want to show IRQ_ACTIVE state but we don't know the CPU (it is between two null samples) */
-                            eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
+                        if (lastEndTime == -1) {
+                            // add null event if it intersects the start time
+                            eventList.add(new NullTimeEvent(entry, time, duration));
                         } else {
-                            eventList.add(new TimeEvent(entry, time, duration, NO_VALUE_EVENT));
+                            if (lastEndTime != time && lastIsNull) {
+                                /* This is a special case where we want to show IRQ_ACTIVE state but we don't know the CPU (it is between two null samples) */
+                                eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
+                            }
+                            if (time + duration >= endTime) {
+                                // add null event if it intersects the end time
+                                eventList.add(new NullTimeEvent(entry, time, duration));
+                            }
                         }
                         lastIsNull = true;
                     }
@@ -258,11 +302,7 @@ public class ResourcesView extends AbstractTimeGraphView {
                 }
             }
 
-        } catch (AttributeNotFoundException e) {
-            e.printStackTrace();
-        } catch (TimeRangeException e) {
-            e.printStackTrace();
-        } catch (StateValueTypeException e) {
+        } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
             e.printStackTrace();
         } catch (StateSystemDisposedException e) {
             /* Ignored */
This page took 0.029789 seconds and 5 git commands to generate.