From 13cb5f432e825b48641b61cf7d8ba765aa09d19e Mon Sep 17 00:00:00 2001 From: Francois Chouinard Date: Tue, 24 Apr 2012 14:07:13 -0400 Subject: [PATCH] Refactor TmfTrace and dependencies - minor changes --- .../tmf/core/tests/trace/AllTests.java | 2 +- .../tests/trace/TmfCheckpointIndexTest.java | 144 ++++++++++++++++++ .../tmf/core/ctfadaptor/CtfTmfTrace.java | 2 +- .../tmf/core/experiment/TmfExperiment.java | 2 +- .../linuxtools/tmf/core/trace/ITmfTrace.java | 16 +- .../linuxtools/tmf/core/trace/TmfTrace.java | 51 ++++++- 6 files changed, 196 insertions(+), 21 deletions(-) create mode 100644 org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointIndexTest.java diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/AllTests.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/AllTests.java index 21669eac3d..e5d304222d 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/AllTests.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/AllTests.java @@ -31,7 +31,7 @@ public class AllTests { suite.addTestSuite(TmfLocationTest.class); suite.addTestSuite(TmfContextTest.class); suite.addTestSuite(TmfCheckpointTest.class); -// suite.addTestSuite(TmfCheckpointIndexTest.class); + suite.addTestSuite(TmfCheckpointIndexTest.class); suite.addTestSuite(TmfTraceTest.class); //$JUnit-END$ return suite; diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointIndexTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointIndexTest.java new file mode 100644 index 0000000000..9511b7b555 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointIndexTest.java @@ -0,0 +1,144 @@ +/******************************************************************************* + * Copyright (c) 2009, 2010, 20112 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: + * Francois Chouinard - Initial API and implementation + * Francois Chouinard - Adapted for TMF Trace Model 1.0 + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.core.tests.trace; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Vector; + +import junit.framework.TestCase; + +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.Path; +import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; +import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; +import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; +import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpoint; +import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer; +import org.eclipse.linuxtools.tmf.core.trace.TmfContext; +import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; + +/** + * TmfTraceTest + *

+ * Test suite for the TmfTrace class. + */ +@SuppressWarnings("nls") +public class TmfCheckpointIndexTest extends TestCase { + + // ------------------------------------------------------------------------ + // Variables + // ------------------------------------------------------------------------ + + private static final String DIRECTORY = "testfiles"; + private static final String TEST_STREAM = "A-Test-10K"; + private static final int BLOCK_SIZE = 500; + private static final int NB_EVENTS = 10000; + private static TestTrace fTrace = null; + + // ------------------------------------------------------------------------ + // Housekeeping + // ------------------------------------------------------------------------ + + public TmfCheckpointIndexTest(final String name) throws Exception { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + fTrace = setupTrace(DIRECTORY + File.separator + TEST_STREAM); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + fTrace.dispose(); + fTrace = null; + } + + // ------------------------------------------------------------------------ + // Helper classes + // ------------------------------------------------------------------------ + + private class TestIndexer extends TmfCheckpointIndexer> { + @SuppressWarnings({ "unchecked", "rawtypes" }) + public TestIndexer(TestTrace testTrace) { + super((ITmfTrace) testTrace); + } + public Vector getCheckpoints() { + return fTraceIndex; + } + } + + private class TestTrace extends TmfTraceStub { + public TestTrace(String path, int blockSize) throws FileNotFoundException { + super(path, blockSize); + fIndexer = new TestIndexer(this); + } + public TestIndexer getIndexer() { + return (TestIndexer) fIndexer; + } + } + + // ------------------------------------------------------------------------ + // Helper functions + // ------------------------------------------------------------------------ + + private TestTrace setupTrace(final String path) { + if (fTrace == null) { + try { + final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null); + final File test = new File(FileLocator.toFileURL(location).toURI()); + fTrace = new TestTrace(test.toURI().getPath(), BLOCK_SIZE); + fTrace.indexTrace(); + } catch (final URISyntaxException e) { + e.printStackTrace(); + } catch (final IOException e) { + e.printStackTrace(); + } + } + return fTrace; + } + + // ------------------------------------------------------------------------ + // Verify checkpoints + // ------------------------------------------------------------------------ + + public void testTmfTraceIndexing() throws Exception { + assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize()); + assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents()); + assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue()); + assertEquals("getRange-end", NB_EVENTS, fTrace.getTimeRange().getEndTime().getValue()); + assertEquals("getStartTime", 1, fTrace.getStartTime().getValue()); + assertEquals("getEndTime", NB_EVENTS, fTrace.getEndTime().getValue()); + + Vector checkpoints = fTrace.getIndexer().getCheckpoints(); + int pageSize = fTrace.getCacheSize(); + assertTrue("Checkpoints exist", checkpoints != null); + + // Validate that each checkpoint points to the right event + for (int i = 0; i < checkpoints.size(); i++) { + TmfCheckpoint checkpoint = checkpoints.get(i); + TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize); + ITmfEvent event = fTrace.parseEvent(context); + assertTrue(context.getRank() == i * pageSize); + assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0)); + } + } + +} \ No newline at end of file diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java index 1a57e6e5fb..faff4ad4a3 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java @@ -121,7 +121,7 @@ public class CtfTmfTrace extends TmfEventProvider implements ITmfTr * @return the trace path */ @Override - public Class getType() { + public Class getEventType() { return fType; } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/experiment/TmfExperiment.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/experiment/TmfExperiment.java index 4b4cb22639..5c179244c8 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/experiment/TmfExperiment.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/experiment/TmfExperiment.java @@ -183,7 +183,7 @@ public class TmfExperiment extends TmfEventProvider impl // ------------------------------------------------------------------------ @Override - public Class getType() { + public Class getEventType() { return fType; } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/ITmfTrace.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/ITmfTrace.java index af9e741576..190306d7f9 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/ITmfTrace.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/ITmfTrace.java @@ -144,7 +144,7 @@ public interface ITmfTrace extends ITmfDataProvider { /** * @return the trace event type */ - public Class getType(); + public Class getEventType(); /** * @return the associated trace resource @@ -185,6 +185,11 @@ public interface ITmfTrace extends ITmfDataProvider { */ public ITmfTimestamp getEndTime(); + /** + * @return the streaming interval in ms (0 if not a streaming trace) + */ + public long getStreamingInterval(); + // ------------------------------------------------------------------------ // Trace positioning getters // ------------------------------------------------------------------------ @@ -276,13 +281,4 @@ public interface ITmfTrace extends ITmfDataProvider { */ public ITmfEvent readEvent(ITmfContext context); - // ------------------------------------------------------------------------ - // - // ------------------------------------------------------------------------ - - /** - * @return the streaming interval in ms (0 if not a streaming trace) - */ - public long getStreamingInterval(); - } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTrace.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTrace.java index 75891a7a0a..5ea35f60a7 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTrace.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTrace.java @@ -32,7 +32,21 @@ import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; *

* Abstract implementation of ITmfTrace. *

- * Document me... + * Since the concept of 'location' is trace specific, the concrete classes have + * to provide the related methods, namely: + *

+ * A concrete trace must provide its corresponding parser. A common way to + * accomplish this is by making the concrete class extend TmfTrace and + * implement ITmfEventParser. + *

+ * The concrete class can either specify its own indexer or use the provided + * TmfCheckpointIndexer (default). In this case, the trace cache size will be + * used as checkpoint interval. */ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace { @@ -98,7 +112,8 @@ public abstract class TmfTrace extends TmfEventProvider } /** - * The standard constructor (non-live trace) + * The standard constructor (non-live trace). Applicable when the trace + * implements its own parser and if at checkpoint-based index is OK. * * @param resource the resource associated to the trace * @param type the trace event type @@ -111,7 +126,8 @@ public abstract class TmfTrace extends TmfEventProvider } /** - * The standard constructor (live trace) + * The standard constructor (live trace). Applicable when the trace + * implements its own parser and if at checkpoint-based index is OK. * * @param resource the resource associated to the trace * @param type the trace event type @@ -125,7 +141,8 @@ public abstract class TmfTrace extends TmfEventProvider } /** - * The full constructor + * The 'non-default indexer' constructor. Allows to provide a trace + * specific indexer. * * @param resource the resource associated to the trace * @param type the trace event type @@ -134,13 +151,30 @@ public abstract class TmfTrace extends TmfEventProvider * @param indexer the trace indexer * @throws FileNotFoundException */ - @SuppressWarnings({ "unchecked", "rawtypes" }) protected TmfTrace(final IResource resource, final Class type, final String path, final int cacheSize, final long interval, final ITmfTraceIndexer indexer) throws FileNotFoundException { + this(resource, type, path, cacheSize, interval, null, null); + } + + /** + * The full constructor where trace specific indexer/parser are provided. + * + * @param resource the resource associated to the trace + * @param type the trace event type + * @param path the trace path + * @param cacheSize the trace cache size + * @param indexer the trace indexer + * @param parser the trace event parser + * @throws FileNotFoundException + */ + @SuppressWarnings({ "unchecked", "rawtypes" }) + protected TmfTrace(final IResource resource, final Class type, final String path, final int cacheSize, + final long interval, final ITmfTraceIndexer indexer, final ITmfEventParser parser) throws FileNotFoundException { super(); fCacheSize = (cacheSize > 0) ? cacheSize : DEFAULT_TRACE_CACHE_SIZE; fStreamingInterval = interval; fIndexer = (indexer != null) ? indexer : new TmfCheckpointIndexer(this, fCacheSize); + fParser = parser; initialize(resource, path, type); } @@ -157,7 +191,8 @@ public abstract class TmfTrace extends TmfEventProvider fCacheSize = trace.getCacheSize(); fStreamingInterval = trace.getStreamingInterval(); fIndexer = new TmfCheckpointIndexer(this); - initialize(trace.getResource(), trace.getPath(), trace.getType()); + fParser = trace.fParser; + initialize(trace.getResource(), trace.getPath(), trace.getEventType()); } // ------------------------------------------------------------------------ @@ -212,11 +247,11 @@ public abstract class TmfTrace extends TmfEventProvider // ------------------------------------------------------------------------ /* (non-Javadoc) - * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#getType() + * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType() */ @Override @SuppressWarnings("unchecked") - public Class getType() { + public Class getEventType() { return (Class) super.getType(); } -- 2.34.1