TMF: Validate if an analysis can be executed using the requirements
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Fri, 9 May 2014 02:04:53 +0000 (22:04 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Sat, 24 May 2014 05:53:13 +0000 (01:53 -0400)
Change-Id: If4b51af84c4fc3dc7541629b69e950b0cc54cb00
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/26294
Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Tested-by: Hudson CI
org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/analysis/LttngKernelAnalysisTest.java
org.eclipse.linuxtools.lttng2.kernel.core/src/org/eclipse/linuxtools/lttng2/kernel/core/analysis/LttngKernelAnalysisModule.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/analysis/IAnalysisModule.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/analysis/Messages.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/analysis/TmfAbstractAnalysisModule.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/analysis/TmfAnalysisRequirement.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/analysis/messages.properties
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTrace.java

index 4e6e5919cd9765071a2fac19749718244aefd7f2..d49c8e8da17a54b591d07aae31725f48dbac59b5 100644 (file)
@@ -31,6 +31,7 @@ import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisRequirement;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
 import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestHelper;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
 import org.eclipse.linuxtools.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
 import org.junit.After;
 import org.junit.Before;
@@ -91,6 +92,21 @@ public class LttngKernelAnalysisTest {
         assertFalse(quarks.isEmpty());
     }
 
+    /**
+     * Test the canExecute method on valid and invalid traces
+     */
+    @Test
+    public void testCanExecute() {
+        /* Test with a valid kernel trace */
+        assertTrue(fKernelAnalysisModule.canExecute(fTrace));
+
+        /* Test with a CTF trace that does not have required events */
+        assumeTrue(CtfTmfTestTrace.CYG_PROFILE.exists());
+        try (CtfTmfTrace trace = CtfTmfTestTrace.CYG_PROFILE.getTrace();) {
+            assertFalse(fKernelAnalysisModule.canExecute(trace));
+        }
+    }
+
     /**
      * Test for {@link LttngKernelAnalysisModule#getAnalysisRequirements()}
      */
index 0e2beaf01f9323a75bf93cd66e679951791f207f..e3e0bfb6fbe2d771531d02061980da9279fd5baa 100644 (file)
@@ -41,6 +41,7 @@ public class LttngKernelAnalysisModule extends TmfStateSystemAnalysisModule {
     /** The ID of this analysis module */
     public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.analysis"; //$NON-NLS-1$
 
+    /* TODO: Are all those mandatory events really mandatory or should some of them be optional? */
     private static final ImmutableSet<String> REQUIRED_EVENTS = ImmutableSet.of(
             LttngStrings.EXIT_SYSCALL,
             LttngStrings.IRQ_HANDLER_ENTRY,
@@ -54,11 +55,15 @@ public class LttngKernelAnalysisModule extends TmfStateSystemAnalysisModule {
             LttngStrings.SCHED_PROCESS_FREE,
             LttngStrings.STATEDUMP_PROCESS_STATE,
             LttngStrings.SCHED_WAKEUP,
-            LttngStrings.SCHED_WAKEUP_NEW,
+            LttngStrings.SCHED_WAKEUP_NEW
+            );
+
+    private static final ImmutableSet<String> OPTIONAL_EVENTS = ImmutableSet.of(
             /* Add the prefix for syscalls */
             LttngStrings.SYSCALL_PREFIX
             );
 
+
     /** The requirements as an immutable set */
     private static final ImmutableSet<TmfAnalysisRequirement> REQUIREMENTS;
 
@@ -67,7 +72,10 @@ public class LttngKernelAnalysisModule extends TmfStateSystemAnalysisModule {
         TmfAnalysisRequirement domainReq = new TmfAnalysisRequirement(SessionConfigStrings.CONFIG_ELEMENT_DOMAIN);
         domainReq.addValue(SessionConfigStrings.CONFIG_DOMAIN_TYPE_KERNEL, ValuePriorityLevel.MANDATORY);
 
-        REQUIREMENTS = ImmutableSet.of(domainReq, new TmfAnalysisRequirement(SessionConfigStrings.CONFIG_ELEMENT_EVENT, REQUIRED_EVENTS, ValuePriorityLevel.MANDATORY));
+        TmfAnalysisRequirement eventReq = new TmfAnalysisRequirement(SessionConfigStrings.CONFIG_ELEMENT_EVENT, REQUIRED_EVENTS, ValuePriorityLevel.MANDATORY);
+        eventReq.addValues(OPTIONAL_EVENTS, ValuePriorityLevel.OPTIONAL);
+
+        REQUIREMENTS = ImmutableSet.of(domainReq, eventReq);
     }
 
     @Override
index a2c1377b165b5c036722157f8368da0122746490..3e30dc9f71b85124859065cb9577ddf932adbbb7 100644 (file)
@@ -142,7 +142,7 @@ public interface IAnalysisModule extends ITmfComponent, IAnalysisRequirementProv
      *            The trace to analyze
      * @return Whether the analysis can be executed
      */
-    boolean canExecute(ITmfTrace trace);
+    boolean canExecute(@NonNull ITmfTrace trace);
 
     /**
      * Schedule the execution of the analysis. If the trace has been set and is
index 5b57833aec9b18c8ce07de5c537c65b431cd2ad0..fb8e1eebb299b39ea1367c59ef9878ec3b484bd9 100644 (file)
@@ -40,6 +40,9 @@ public class Messages extends NLS {
     /** Parameter is invalid */
     public static String TmfAbstractAnalysisModule_InvalidParameter;
 
+    /** The trace to set was null */
+    public static String TmfAbstractAnalysisModule_NullTrace;
+
     /** Running analysis */
     public static String TmfAbstractAnalysisModule_RunningAnalysis;
 
index f0403d7c9ae5742a3ee24fbc21b5d2dc43022c96..6ac0f8153f332759f32ff24774d01f3ab93f8acc 100644 (file)
@@ -100,6 +100,9 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
 
     @Override
     public void setTrace(ITmfTrace trace) throws TmfAnalysisException {
+        if (trace == null) {
+            throw new TmfAnalysisException(Messages.TmfAbstractAnalysisModule_NullTrace);
+        }
         if (fTrace != null) {
             throw new TmfAnalysisException(NLS.bind(Messages.TmfAbstractAnalysisModule_TraceSetMoreThanOnce, getName()));
         }
@@ -108,6 +111,7 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
         if (!canExecute(trace)) {
             throw new TmfAnalysisException(NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisCannotExecute, getName()));
         }
+
         fTrace = trace;
         /* Get the parameter providers for this trace */
         fParameterProviders = TmfAnalysisManager.getParameterProviders(this, fTrace);
@@ -182,7 +186,12 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
     }
 
     @Override
-    public boolean canExecute(ITmfTrace trace) {
+    public boolean canExecute(@NonNull ITmfTrace trace) {
+        for (TmfAnalysisRequirement requirement : getAnalysisRequirements()) {
+            if (!requirement.isFulfilled(trace)) {
+                return false;
+            }
+        }
         return true;
     }
 
@@ -251,7 +260,7 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
         cancel();
     }
 
-    private void execute() {
+    private void execute(@NonNull final ITmfTrace trace) {
 
         /*
          * TODO: The analysis in a job should be done at the analysis manager
@@ -272,7 +281,6 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
             fStarted = true;
         }
 
-        final ITmfTrace trace = fTrace;
         /*
          * Actual analysis will be run on a separate thread
          */
@@ -311,10 +319,11 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
 
     @Override
     public IStatus schedule() {
-        if (fTrace == null) {
+        final ITmfTrace trace = fTrace;
+        if (trace == null) {
             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, String.format("No trace specified for analysis %s", getName())); //$NON-NLS-1$
         }
-        execute();
+        execute(trace);
 
         return Status.OK_STATUS;
     }
index 0809b519229ca6f321aa2c2004691aadf844afbd..9397b2469d6a15933b507bc77ad5422fed753711 100644 (file)
 
 package org.eclipse.linuxtools.tmf.core.analysis;
 
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
-import java.util.HashMap;
+import java.util.Map.Entry;
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceWithPreDefinedEvents;
+import org.eclipse.linuxtools.tmf.core.trace.TmfEventTypeCollectionHelper;
+
 /**
  * Class that contains all the values associated with a type needed by an
  * analysis in order to execute. Each value is peered with a level that
@@ -41,6 +47,11 @@ import java.util.Set;
  */
 public class TmfAnalysisRequirement {
 
+    /**
+     * String for requirement type 'event', that can be used by analysis
+     */
+    public static final String TYPE_EVENT = "event"; //$NON-NLS-1$
+
     private final String fType;
     private final Map<String, ValuePriorityLevel> fValues = new HashMap<>();
     private final Set<String> fInformation = new HashSet<>();
@@ -209,6 +220,26 @@ public class TmfAnalysisRequirement {
         }
     }
 
+    /**
+     * Gets all the values associated with the requirement with a given priority
+     * level.
+     *
+     * @param level
+     *            The desired level
+     * @return Set containing the values with the given priority level
+     */
+    public Set<String> getValues(ValuePriorityLevel level) {
+        synchronized (fValues) {
+            Set<String> values = new HashSet<>();
+            for (Entry<String, ValuePriorityLevel> entry : fValues.entrySet()) {
+                if (entry.getValue() == level) {
+                    values.add(entry.getKey());
+                }
+            }
+            return values;
+        }
+    }
+
     /**
      * Gets information about the requirement.
      *
@@ -230,4 +261,31 @@ public class TmfAnalysisRequirement {
             return fValues.get(value);
         }
     }
+
+    /**
+     * Verifies whether a trace fulfills this requirement
+     *
+     * @param trace
+     *            The trace on which to check for this requirement
+     * @return True if the trace has all mandatory values of this requirement
+     */
+    public boolean isFulfilled(@NonNull ITmfTrace trace) {
+        switch (fType) {
+        case TYPE_EVENT:
+            if (trace instanceof ITmfTraceWithPreDefinedEvents) {
+                Set<String> traceEvents = TmfEventTypeCollectionHelper.getEventNames(((ITmfTraceWithPreDefinedEvents) trace).getContainedEventTypes());
+                Set<String> mandatoryValues = getValues(ValuePriorityLevel.MANDATORY);
+                return traceEvents.containsAll(mandatoryValues);
+            }
+            break;
+        default:
+            return true;
+        }
+        return true;
+    }
+
+    @Override
+    public String toString() {
+        return fType + ": " + fValues; //$NON-NLS-1$
+    }
 }
index 46e25973993e824a8e376c589de9f6470b71b930..9744dea2547d50cb59e397d404ba7846f54a0c80 100644 (file)
@@ -15,6 +15,7 @@ TmfAnalysisModuleHelper_AnalysisDoesNotApply=Cannot perform analysis "{0}" on th
 TmfAbstractAnalysisModule_AnalysisForTrace=Analysis module: {0} for trace {1}
 TmfAbstractAnalysisModule_AnalysisModule=Analysis module: {0}
 TmfAbstractAnalysisModule_InvalidParameter=Parameter {0} is not valid for analysis module {1}
+TmfAbstractAnalysisModule_NullTrace=Setting a null trace to analysis module
 TmfAbstractAnalysisModule_RunningAnalysis=Running analysis {0}
 TmfAnalysisManager_ErrorParameterProvider=Error instantiating parameter provider
 TmfAnalysisModuleHelper_ImpossibleToCreateModule=Could not instantiate module "{0}"
index 1190cb038230d344665531dd31f02942964d4de5..e220ccf66fed78dd3c0f02bd63cc6241a060f621 100644 (file)
@@ -290,7 +290,7 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
                     status.add(module.schedule());
                 }
             } catch (TmfAnalysisException e) {
-                status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
+                status.add(new Status(IStatus.WARNING, Activator.PLUGIN_ID, e.getMessage()));
             }
         }
         return status;
This page took 0.03197 seconds and 5 git commands to generate.