gdbtrace: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.core.tests / src / org / eclipse / linuxtools / lttng2 / kernel / core / tests / stateprovider / StateSystemFullHistoryTest.java
index 1a266b88a59a045b25947231df28a393f52553b2..a8a0f62db7a5805bd7fa397460588c19c3683ffa 100644 (file)
 /*******************************************************************************
- * Copyright (c) 2012 Ericsson
- * Copyright (c) 2010, 2011 École Polytechnique de Montréal
- * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
+ * Copyright (c) 2012, 2014 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
  * accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
  *
- *******************************************************************************/
+ * Contributors:
+ *   Alexandre Montplaisir - Initial API and implementation
+ *   Bernd Hufmann - Use state system analysis module instead of factory
+ ******************************************************************************/
 
 package org.eclipse.linuxtools.lttng2.kernel.core.tests.stateprovider;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
-import java.util.List;
 
-import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
-import org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider.CtfKernelStateInput;
-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.exceptions.TmfTraceException;
-import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
-import org.eclipse.linuxtools.tmf.core.statesystem.IStateChangeInput;
-import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
-import org.eclipse.linuxtools.tmf.core.statesystem.StateSystemManager;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider.LttngKernelStateProvider;
+import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem;
+import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
+import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateProvider;
+import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
+import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
+import org.junit.After;
 import org.junit.Test;
 
 /**
- * Unit tests for the StateHistorySystem, which uses a full (non-partial)
- * history and the non-threaded CTF kernel handler.
- *
- * @author alexmont
+ * State system tests using a full history back-end and the LTTng kernel state
+ * input.
  *
+ * @author Alexandre Montplaisir
  */
-@SuppressWarnings({"nls", "javadoc"})
-public class StateSystemFullHistoryTest {
-
-    static File stateFile;
-    static File stateFileBenchmark;
+public class StateSystemFullHistoryTest extends StateSystemTest {
 
-    static IStateChangeInput input;
-    static ITmfStateSystem ssq;
+    private static final @NonNull String TEST_FILE_NAME = "test.ht";
+    private static final @NonNull String BENCHMARK_FILE_NAME = "test.benchmark.ht";
 
-    /* Offset in the trace + start time of the trace */
-    private final static long interestingTimestamp1 = 18670067372290L + 1331649577946812237L;
+    private File stateFile;
+    private File stateFileBenchmark;
+    private TestLttngKernelAnalysisModule module;
 
-    /* ID we give to the state system we build */
-    private static final String STATE_ID = "test-ss";
-
-    protected static String getTestFileName() {
-        return "/tmp/statefile.ht"; //$NON-NLS-1$
-    }
+    @Override
+    protected ITmfStateSystem initialize() {
+        stateFile = createStateFile(TEST_FILE_NAME);
+        stateFileBenchmark = createStateFile(BENCHMARK_FILE_NAME);
 
-    @BeforeClass
-    public static void initialize() {
-        stateFile = new File(getTestFileName());
-        stateFileBenchmark = new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
+        module = new TestLttngKernelAnalysisModule(TEST_FILE_NAME);
         try {
-            input = new CtfKernelStateInput(CtfTestFiles.getTestTrace());
-            ssq = StateSystemManager.loadStateHistory(stateFile, input, STATE_ID, true);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    @AfterClass
-    public static void cleanup() {
-        boolean ret1, ret2;
-        ret1 = stateFile.delete();
-        ret2 = stateFileBenchmark.delete();
-        if ( !(ret1 && ret2) ) {
-            System.err.println("Error cleaning up during unit testing, " + //$NON-NLS-1$
-                       "you might have leftovers state history files in /tmp"); //$NON-NLS-1$
+            module.setTrace(testTrace.getTrace());
+        } catch (TmfAnalysisException e) {
+            fail();
         }
+        module.schedule();
+        assertTrue(module.waitForCompletion());
+        return module.getStateSystem();
     }
 
     /**
-     * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
-     * us to @Test the @BeforeClass...
-     *
-     * @throws IOException
-     * @throws TmfTraceException
+     * Clean-up
      */
-    @Test
-    public void testBuild() throws TmfTraceException {
-        IStateChangeInput input2;
-        ITmfStateSystem ssb2;
-
-        input2 = new CtfKernelStateInput(CtfTestFiles.getTestTrace());
-        ssb2 = StateSystemManager.loadStateHistory(stateFileBenchmark, input2, STATE_ID, true);
-
-        assertEquals(CtfTestFiles.startTime, ssb2.getStartTime());
-        assertEquals(CtfTestFiles.endTime, ssb2.getCurrentEndTime());
-    }
-
-    @Test
-    public void testOpenExistingStateFile() throws TmfTraceException {
-        ITmfStateSystem ssb2;
-
-        /* 'newStateFile' should have already been created */
-        ssb2 = StateSystemManager.loadStateHistory(stateFile, null, STATE_ID, true);
-
-        assertNotNull(ssb2);
-        assertEquals(CtfTestFiles.startTime, ssb2.getStartTime());
-        assertEquals(CtfTestFiles.endTime, ssb2.getCurrentEndTime());
-    }
-
-    @Test
-    public void testFullQuery1() throws StateValueTypeException,
-            AttributeNotFoundException, TimeRangeException,
-            StateSystemDisposedException {
-
-        List<ITmfStateInterval> list;
-        ITmfStateInterval interval;
-        int quark, valueInt;
-        String valueStr;
-
-        list = ssq.queryFullState(interestingTimestamp1);
-
-        quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
-        interval = list.get(quark);
-        valueInt = interval.getStateValue().unboxInt();
-        assertEquals(1397, valueInt);
-
-        quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
-        interval = list.get(quark);
-        valueStr = interval.getStateValue().unboxStr();
-        assertEquals("gdbus", valueStr);
-
-        quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.SYSTEM_CALL);
-        interval = list.get(quark);
-        valueStr = interval.getStateValue().unboxStr();
-        assertTrue(valueStr.equals("sys_poll"));
-    }
-
-    @Test
-    public void testFullQuery2() {
-        //
-    }
-
-    @Test
-    public void testFullQuery3() {
-        //
-    }
-
-    @Test
-    public void testSingleQuery1() throws AttributeNotFoundException,
-            TimeRangeException, StateValueTypeException,
-            StateSystemDisposedException {
-
-        long timestamp = interestingTimestamp1;
-        int quark;
-        ITmfStateInterval interval;
-        String valueStr;
-
-        quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
-        interval = ssq.querySingleState(timestamp, quark);
-        valueStr = interval.getStateValue().unboxStr();
-        assertEquals("gdbus", valueStr);
-    }
-
-    @Test
-    public void testSingleQuery2() {
-        //
-    }
-
-    @Test
-    public void testSingleQuery3() {
-        //
+    @After
+    public void cleanup() {
+        if (module != null) {
+            module.close();
+        }
+        if (stateFile != null) {
+            stateFile.delete();
+        }
+        if (stateFileBenchmark != null) {
+            stateFileBenchmark.delete();
+        }
     }
 
-    /**
-     * Test a range query (with no resolution parameter, so all intervals)
-     */
-    @Test
-    public void testRangeQuery1() throws AttributeNotFoundException,
-            TimeRangeException, StateValueTypeException,
-            StateSystemDisposedException {
-
-        long time1 = interestingTimestamp1;
-        long time2 = time1 + 1L * CtfTestFiles.NANOSECS_PER_SEC;
-        int quark;
-        List<ITmfStateInterval> intervals;
-
-        quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
-        intervals = ssq.queryHistoryRange(quark, time1, time2);
-        assertEquals(487, intervals.size()); /* Number of context switches! */
-        assertEquals(1685, intervals.get(100).getStateValue().unboxInt());
-        assertEquals(1331668248427681372L, intervals.get(205).getEndTime());
-    }
+    // ------------------------------------------------------------------------
+    // Tests specific to a full-history
+    // ------------------------------------------------------------------------
 
     /**
-     * Range query, but with a t2 far off the end of the trace.
-     * The result should still be valid.
+     * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
+     * us to @Test the @BeforeClass...
      */
     @Test
-    public void testRangeQuery2() throws TimeRangeException,
-            AttributeNotFoundException, StateSystemDisposedException {
-
-        List<ITmfStateInterval> intervals;
-
-        int quark = ssq.getQuarkAbsolute(Attributes.RESOURCES, Attributes.IRQS, "1");
-        long ts1 = ssq.getStartTime(); /* start of the trace */
-        long ts2 = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid, but ignored */
-
-        intervals = ssq.queryHistoryRange(quark, ts1, ts2);
-
-        /* Activity of IRQ 1 over the whole trace */
-        assertEquals(65, intervals.size());
+    public void testBuild() {
+        try (TestLttngKernelAnalysisModule module2 =
+                new TestLttngKernelAnalysisModule(BENCHMARK_FILE_NAME);) {
+            try {
+                module2.setTrace(testTrace.getTrace());
+            } catch (TmfAnalysisException e) {
+                fail();
+            }
+            module2.schedule();
+            assertTrue(module2.waitForCompletion());
+            ITmfStateSystem ssb2 = module2.getStateSystem();
+
+            assertNotNull(ssb2);
+            assertEquals(startTime, ssb2.getStartTime());
+            assertEquals(endTime, ssb2.getCurrentEndTime());
+        }
     }
 
     /**
-     * Test a range query with a resolution
+     * Test re-opening the existing file.
      */
     @Test
-    public void testRangeQuery3() throws AttributeNotFoundException,
-            TimeRangeException, StateValueTypeException,
-            StateSystemDisposedException {
-
-        long time1 = interestingTimestamp1;
-        long time2 = time1 + 1L * CtfTestFiles.NANOSECS_PER_SEC;
-        long resolution = 1000000; /* One query every millisecond */
-        int quark;
-        List<ITmfStateInterval> intervals;
-
-        quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
-        intervals = ssq.queryHistoryRange(quark, time1, time2, resolution, null);
-        assertEquals(126, intervals.size()); /* Number of context switches! */
-        assertEquals(1452, intervals.get(50).getStateValue().unboxInt());
-        assertEquals(1331668248815698779L, intervals.get(100).getEndTime());
-    }
-
-    /**
-     * Ask for a time range outside of the trace's range
-     */
-    @Test(expected = TimeRangeException.class)
-    public void testFullQueryInvalidTime1() throws TimeRangeException,
-            StateSystemDisposedException {
-        long ts = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC;
-        ssq.queryFullState(ts);
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testFullQueryInvalidTime2() throws TimeRangeException,
-            StateSystemDisposedException {
-        long ts = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC;
-        ssq.queryFullState(ts);
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testSingleQueryInvalidTime1()
-            throws AttributeNotFoundException, TimeRangeException,
-            StateSystemDisposedException {
-
-        int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
-        long ts = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC;
-        ssq.querySingleState(ts, quark);
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testSingleQueryInvalidTime2()
-            throws AttributeNotFoundException, TimeRangeException,
-            StateSystemDisposedException {
-
-        int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
-        long ts = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC;
-        ssq.querySingleState(ts, quark);
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testRangeQueryInvalidTime1() throws AttributeNotFoundException,
-            TimeRangeException, StateSystemDisposedException {
-
-        int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
-        long ts1 = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */
-        long ts2 = CtfTestFiles.startTime + 1L * CtfTestFiles.NANOSECS_PER_SEC; /* valid */
-
-        ssq.queryHistoryRange(quark, ts1, ts2);
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testRangeQueryInvalidTime2() throws TimeRangeException,
-            AttributeNotFoundException, StateSystemDisposedException {
-
-        int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
-        long ts1 = CtfTestFiles.startTime - 1L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */
-        long ts2 = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */
-
-        ssq.queryHistoryRange(quark, ts1, ts2);
-    }
-
-    /**
-     * Ask for a non-existing attribute
-     *
-     * @throws AttributeNotFoundException
-     */
-    @Test(expected = AttributeNotFoundException.class)
-    public void testQueryInvalidAttribute() throws AttributeNotFoundException {
-
-        ssq.getQuarkAbsolute("There", "is", "no", "cow", "level");
+    public void testOpenExistingStateFile() {
+        /* 'newStateFile' should have already been created */
+        try (TestLttngKernelAnalysisModule module2 = new TestLttngKernelAnalysisModule(TEST_FILE_NAME);) {
+            try {
+                module2.setTrace(testTrace.getTrace());
+            } catch (TmfAnalysisException e) {
+                fail();
+            }
+            module2.schedule();
+            assertTrue(module2.waitForCompletion());
+            ITmfStateSystem ssb2 = module2.getStateSystem();
+
+            assertNotNull(ssb2);
+            assertEquals(startTime, ssb2.getStartTime());
+            assertEquals(endTime, ssb2.getCurrentEndTime());
+        }
     }
 
-    /**
-     * Query but with the wrong State Value type
-     */
-    @Test(expected = StateValueTypeException.class)
-    public void testQueryInvalidValuetype1() throws StateValueTypeException,
-            AttributeNotFoundException, TimeRangeException,
-            StateSystemDisposedException {
-        List<ITmfStateInterval> list;
-        ITmfStateInterval interval;
-        int quark;
+    @NonNullByDefault
+    private static class TestLttngKernelAnalysisModule extends TmfStateSystemAnalysisModule {
 
-        list = ssq.queryFullState(interestingTimestamp1);
-        quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
-        interval = list.get(quark);
-
-        /* This is supposed to be an int value */
-        interval.getStateValue().unboxStr();
-    }
+        private final String htFileName;
 
-    @Test(expected = StateValueTypeException.class)
-    public void testQueryInvalidValuetype2() throws StateValueTypeException,
-            AttributeNotFoundException, TimeRangeException,
-            StateSystemDisposedException {
-        List<ITmfStateInterval> list;
-        ITmfStateInterval interval;
-        int quark;
-
-        list = ssq.queryFullState(interestingTimestamp1);
-        quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
-        interval = list.get(quark);
+        /**
+         * Constructor adding the views to the analysis
+         * @param htFileName
+         *      The History File Name
+         */
+        public TestLttngKernelAnalysisModule(String htFileName) {
+            super();
+            this.htFileName = htFileName;
+        }
 
-        /* This is supposed to be a String value */
-        interval.getStateValue().unboxInt();
-    }
+        @Override
+        public void setTrace(@Nullable ITmfTrace trace) throws TmfAnalysisException {
+            if (!(trace instanceof CtfTmfTrace)) {
+                throw new IllegalStateException("TestLttngKernelAnalysisModule: trace should be of type CtfTmfTrace"); //$NON-NLS-1$
+            }
+            super.setTrace(trace);
+        }
 
-    @Test
-    public void testFullAttributeName() throws AttributeNotFoundException {
-        int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
-        String name = ssq.getFullAttributePath(quark);
-        assertEquals(name, "CPUs/0/Current_thread");
-    }
+        @Override
+        protected ITmfStateProvider createStateProvider() {
+            return new LttngKernelStateProvider(getTrace());
+        }
 
-    @Test
-    public void testGetQuarks_begin() {
-        List<Integer> list = ssq.getQuarks("*", "1577", Attributes.EXEC_NAME);
+        @Override
+        protected StateSystemBackendType getBackendType() {
+            return StateSystemBackendType.FULL;
+        }
 
-        assertEquals(1, list.size());
+        @Override
+        protected String getSsFileName() {
+            return htFileName;
+        }
     }
 
-    @Test
-    public void testGetQuarks_middle() {
-        List<Integer> list = ssq.getQuarks(Attributes.THREADS, "*", Attributes.EXEC_NAME);
-
-        /* Number of different kernel threads in the trace */
-        assertEquals(168, list.size());
+    private static File createStateFile(String name) {
+        File file = new File(TmfTraceManager.getSupplementaryFileDir(testTrace.getTrace()) + name);
+        if (file.exists()) {
+            file.delete();
+        }
+        return file;
     }
 
-    @Test
-    public void testGetQuarks_end() {
-        List<Integer> list = ssq.getQuarks(Attributes.THREADS, "1577", "*");
-
-        /* There should be 4 sub-attributes for each Thread node */
-        assertEquals(4, list.size());
-    }
 }
This page took 0.044888 seconds and 5 git commands to generate.