tmf: Add the Tmf- prefix to the state system interfaces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.core.tests / src / org / eclipse / linuxtools / lttng2 / kernel / core / tests / stateprovider / StateSystemFullHistoryTest.java
index 8830f809ebad94effac0d80e3fa994d9a69c92af..f3919ff60835e93ab07cd08fcf7e54842eaf8616 100644 (file)
@@ -2,53 +2,55 @@
  * Copyright (c) 2012 Ericsson
  * Copyright (c) 2010, 2011 École Polytechnique de Montréal
  * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
- * 
+ *
  * 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
- * 
+ *
  *******************************************************************************/
 
 package org.eclipse.linuxtools.lttng2.kernel.core.tests.stateprovider;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.PrintWriter;
 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.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.AttributeNotFoundException;
-import org.eclipse.linuxtools.tmf.core.statesystem.StateHistorySystem;
-import org.eclipse.linuxtools.tmf.core.statesystem.TimeRangeException;
-import org.eclipse.linuxtools.tmf.core.statesystem.backend.historytree.HistoryTreeBackend;
-import org.eclipse.linuxtools.tmf.core.statesystem.helpers.HistoryBuilder;
-import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateChangeInput;
-import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateHistoryBackend;
-import org.eclipse.linuxtools.tmf.core.statevalue.StateValueTypeException;
-import org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider.CTFKernelStateInput;
-import org.junit.*;
+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.junit.Test;
 
 /**
  * Unit tests for the StateHistorySystem, which uses a full (non-partial)
  * history and the non-threaded CTF kernel handler.
- * 
+ *
  * @author alexmont
- * 
+ *
  */
-@SuppressWarnings("nls")
+@SuppressWarnings({"nls", "javadoc"})
 public class StateSystemFullHistoryTest {
 
-    protected static File stateFile;
-    protected static File stateFileBenchmark;
+    static File stateFile;
+    static File stateFileBenchmark;
 
-    protected static HistoryBuilder builder;
-    protected static IStateChangeInput input;
-    protected static IStateHistoryBackend hp;
-    protected static StateHistorySystem shs;
+    static IStateChangeInput input;
+    static ITmfStateSystem ssq;
+
+    /* Offset in the trace + start time of the trace */
+    private final static long interestingTimestamp1 = 18670067372290L + 1331649577946812237L;
 
     protected static String getTestFileName() {
         return "/tmp/statefile.ht"; //$NON-NLS-1$
@@ -59,83 +61,80 @@ public class StateSystemFullHistoryTest {
         stateFile = new File(getTestFileName());
         stateFileBenchmark = new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
         try {
-            input = new CTFKernelStateInput(CTFTestFiles.getTestTrace());
-            hp = new HistoryTreeBackend(stateFile, input.getStartTime());
-            builder = new HistoryBuilder(input, hp);
+            input = new CtfKernelStateInput(CtfTestFiles.getTestTrace());
+            ssq = StateSystemManager.loadStateHistory(stateFile, input, true);
         } catch (Exception e) {
             e.printStackTrace();
         }
-        builder.run();
-        shs = (StateHistorySystem) builder.getSS();
     }
 
     @AfterClass
     public static void cleanup() {
-        stateFile.delete();
-        stateFileBenchmark.delete();
+        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$
+        }
     }
 
     /**
      * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
      * us to @Test the @BeforeClass...
+     *
+     * @throws IOException
+     * @throws TmfTraceException
      */
     @Test
-    public void testBuild() {
-        HistoryBuilder zebuilder;
-        IStateChangeInput zeinput;
-        IStateHistoryBackend zehp;
+    public void testBuild() throws TmfTraceException {
+        IStateChangeInput input2;
+        ITmfStateSystem ssb2;
 
-        try {
-            zeinput = new CTFKernelStateInput(CTFTestFiles.getTestTrace());
-            zehp = new HistoryTreeBackend(stateFileBenchmark,
-                    zeinput.getStartTime());
-            zebuilder = new HistoryBuilder(zeinput, zehp);
-            zebuilder.run();
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+        input2 = new CtfKernelStateInput(CtfTestFiles.getTestTrace());
+        ssb2 = StateSystemManager.loadStateHistory(stateFileBenchmark, input2, true);
+
+        assertEquals(CtfTestFiles.startTime, ssb2.getStartTime());
+        assertEquals(CtfTestFiles.endTime, ssb2.getCurrentEndTime());
     }
 
     @Test
-    public void testOpenExistingStateFile() {
-        IStateHistoryBackend hp2 = null;
-        StateHistorySystem shs2 = null;
-        try {
-            /* 'newStateFile' should have already been created */
-            hp2 = new HistoryTreeBackend(stateFile);
-            shs2 = new StateHistorySystem(hp2, false);
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        assertTrue(shs2 != null);
+    public void testOpenExistingStateFile() throws TmfTraceException {
+        ITmfStateSystem ssb2;
+
+        /* 'newStateFile' should have already been created */
+        ssb2 = StateSystemManager.loadStateHistory(stateFile, null, true);
+
+        assertNotNull(ssb2);
+        assertEquals(CtfTestFiles.startTime, ssb2.getStartTime());
+        assertEquals(CtfTestFiles.endTime, ssb2.getCurrentEndTime());
     }
 
     @Test
     public void testFullQuery1() throws StateValueTypeException,
             AttributeNotFoundException, TimeRangeException {
 
+        List<ITmfStateInterval> list;
         ITmfStateInterval interval;
         int quark, valueInt;
         String valueStr;
 
-        shs.loadStateAtTime(17622841472359L);
+        list = ssq.queryFullState(interestingTimestamp1);
 
-        quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        interval = shs.queryState(quark);
+        quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
+        interval = list.get(quark);
         valueInt = interval.getStateValue().unboxInt();
-        assertTrue(valueInt == 1929);
+        assertEquals(1397, valueInt);
+
+        quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
+        interval = list.get(quark);
+        valueStr = interval.getStateValue().unboxStr();
+        assertEquals("gdbus", valueStr);
 
-        quark = shs.getQuarkAbsolute("Threads", "1197", "Exec_name");
-        interval = shs.queryState(quark);
+        quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.SYSTEM_CALL);
+        interval = list.get(quark);
         valueStr = interval.getStateValue().unboxStr();
-        assertTrue(valueStr.equals("apache2"));
-
-        // FIXME fails at the moment (attribute type is int, and = 3129??), I'll
-        // figure it out later
-        // quark = shs.getQuarkAbsolute("Threads", "3109", "Exec_mode_stack");
-        // interval = shs.getState(quark);
-        // valueStr = interval.getStateValue().unboxStr();
-        // assertTrue( valueStr.equals("bloup") );
+        assertTrue(valueStr.equals("sys_poll"));
     }
 
     @Test
@@ -152,15 +151,15 @@ public class StateSystemFullHistoryTest {
     public void testSingleQuery1() throws AttributeNotFoundException,
             TimeRangeException, StateValueTypeException {
 
-        long timestamp = 17622841472359L;
+        long timestamp = interestingTimestamp1;
         int quark;
         ITmfStateInterval interval;
         String valueStr;
 
-        quark = shs.getQuarkAbsolute("Threads", "1197", "Exec_name");
-        interval = shs.querySingleState(timestamp, quark);
+        quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
+        interval = ssq.querySingleState(timestamp, quark);
         valueStr = interval.getStateValue().unboxStr();
-        assertTrue(valueStr.equals("apache2"));
+        assertEquals("gdbus", valueStr);
     }
 
     @Test
@@ -173,38 +172,81 @@ public class StateSystemFullHistoryTest {
         //
     }
 
+    /**
+     * Test a range query (with no resolution parameter, so all intervals)
+     */
     @Test
     public void testRangeQuery1() throws AttributeNotFoundException,
             TimeRangeException, StateValueTypeException {
 
-        long time1 = 17622841472359L;
-        long time2 = time1 + 1L * CTFTestFiles.NANOSECS_PER_SEC;
+        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());
+    }
+
+    /**
+     * Range query, but with a t2 far off the end of the trace.
+     * The result should still be valid.
+     */
+    @Test
+    public void testRangeQuery2() throws TimeRangeException,
+            AttributeNotFoundException {
+
+        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());
+    }
+
+    /**
+     * Test a range query with a resolution
+     */
+    @Test
+    public void testRangeQuery3() throws AttributeNotFoundException,
+            TimeRangeException, StateValueTypeException {
+
+        long time1 = interestingTimestamp1;
+        long time2 = time1 + 1L * CtfTestFiles.NANOSECS_PER_SEC;
+        long resolution = 1000000; /* One query every millisecond */
         int quark;
         List<ITmfStateInterval> intervals;
 
-        quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        intervals = shs.queryHistoryRange(quark, time1, time2);
-        assertTrue(intervals.size() == 1018); /* Number of context switches! */
-        assertTrue(intervals.get(100).getStateValue().unboxInt() == 2974);
-        assertTrue(intervals.get(205).getEndTime() == 17622977386059L);
+        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
-     * 
+     *
      * @throws TimeRangeException
      */
     @Test(expected = TimeRangeException.class)
     public void testFullQueryInvalidTime1() throws TimeRangeException {
-        shs.loadStateAtTime(CTFTestFiles.startTime1 + 20L
-                * CTFTestFiles.NANOSECS_PER_SEC);
+        long ts = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC;
+        ssq.queryFullState(ts);
 
     }
 
     @Test(expected = TimeRangeException.class)
     public void testFullQueryInvalidTime2() throws TimeRangeException {
-        shs.loadStateAtTime(CTFTestFiles.startTime1 - 20L
-                * CTFTestFiles.NANOSECS_PER_SEC);
+        long ts = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC;
+        ssq.queryFullState(ts);
 
     }
 
@@ -212,75 +254,56 @@ public class StateSystemFullHistoryTest {
     public void testSingleQueryInvalidTime1()
             throws AttributeNotFoundException, TimeRangeException {
 
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        long time = CTFTestFiles.startTime1 + 20L
-                * CTFTestFiles.NANOSECS_PER_SEC;
-        shs.querySingleState(time, quark);
+        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 {
 
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        long time = CTFTestFiles.startTime1 - 20L
-                * CTFTestFiles.NANOSECS_PER_SEC;
-        shs.querySingleState(time, quark);
+        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 {
 
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        long time1 = CTFTestFiles.startTime1 - 20L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
-        long time2 = CTFTestFiles.startTime1 + 1L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
+        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 */
 
-        shs.queryHistoryRange(quark, time1, time2);
+        ssq.queryHistoryRange(quark, ts1, ts2);
     }
 
     @Test(expected = TimeRangeException.class)
     public void testRangeQueryInvalidTime2() throws TimeRangeException,
             AttributeNotFoundException {
 
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        long time1 = CTFTestFiles.startTime1 + 1L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
-        long time2 = CTFTestFiles.startTime1 + 20L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
+        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 */
 
-        shs.queryHistoryRange(quark, time1, time2);
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testRangeQueryInvalidTime3() throws TimeRangeException,
-            AttributeNotFoundException {
-
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        long time1 = CTFTestFiles.startTime1 - 1L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
-        long time2 = CTFTestFiles.startTime1 + 20L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
-
-        shs.queryHistoryRange(quark, time1, time2);
+        ssq.queryHistoryRange(quark, ts1, ts2);
     }
 
     /**
      * Ask for a non-existing attribute
-     * 
+     *
      * @throws AttributeNotFoundException
      */
     @Test(expected = AttributeNotFoundException.class)
     public void testQueryInvalidAttribute() throws AttributeNotFoundException {
 
-        shs.getQuarkAbsolute("There", "is", "no", "cow", "level");
+        ssq.getQuarkAbsolute("There", "is", "no", "cow", "level");
     }
 
     /**
      * Query but with the wrong State Value type
-     * 
+     *
      * @throws StateValueTypeException
      * @throws AttributeNotFoundException
      * @throws TimeRangeException
@@ -288,46 +311,60 @@ public class StateSystemFullHistoryTest {
     @Test(expected = StateValueTypeException.class)
     public void testQueryInvalidValuetype1() throws StateValueTypeException,
             AttributeNotFoundException, TimeRangeException {
-
+        List<ITmfStateInterval> list;
         ITmfStateInterval interval;
         int quark;
 
-        shs.loadStateAtTime(17622841472359L);
+        list = ssq.queryFullState(interestingTimestamp1);
+        quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
+        interval = list.get(quark);
 
-        quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        interval = shs.queryState(quark);
-        interval.getStateValue().unboxStr(); /*
-                                              * This is supposed to be a int
-                                              * value
-                                              */
+        /* This is supposed to be an int value */
+        interval.getStateValue().unboxStr();
     }
 
     @Test(expected = StateValueTypeException.class)
     public void testQueryInvalidValuetype2() throws StateValueTypeException,
             AttributeNotFoundException, TimeRangeException {
-
+        List<ITmfStateInterval> list;
         ITmfStateInterval interval;
         int quark;
 
-        shs.loadStateAtTime(17622841472359L);
+        list = ssq.queryFullState(interestingTimestamp1);
+        quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
+        interval = list.get(quark);
 
-        quark = shs.getQuarkAbsolute("Threads", "1197", "Exec_name");
-        interval = shs.queryState(quark);
-        interval.getStateValue().unboxInt(); /*
-                                              * This is supposed to be a String
-                                              * value
-                                              */
+        /* This is supposed to be a String value */
+        interval.getStateValue().unboxInt();
     }
 
     @Test
     public void testFullAttributeName() throws AttributeNotFoundException {
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        String name = shs.getFullAttributePath(quark);
-        assertTrue(name.equals("CPUs/0/Current_thread"));
+        int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
+        String name = ssq.getFullAttributePath(quark);
+        assertEquals(name, "CPUs/0/Current_thread");
     }
 
     @Test
-    public void testDebugPrinting() throws FileNotFoundException {
-        shs.debugPrint(new PrintWriter(new File("/dev/null")));
+    public void testGetQuarks_begin() {
+        List<Integer> list = ssq.getQuarks("*", "1577", Attributes.EXEC_NAME);
+
+        assertEquals(1, list.size());
+    }
+
+    @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());
+    }
+
+    @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.032221 seconds and 5 git commands to generate.