From 3480bf122f215ec6b6b9aadd59fe40cbe0394a2b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Genevi=C3=A8ve=20Bastien?= Date: Fri, 17 May 2013 16:11:33 -0400 Subject: [PATCH] TMF: Add functions to verify if events are present in a CTF Trace MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit These functions verify whether one, some or all events are present in the metadata of a CTF trace. It is useful to verify the presence of events before doing an analysis that requires them. Change-Id: Ieed7f9bcd905bf354aa5550b10c6304e534e5728 Signed-off-by: Geneviève Bastien Reviewed-on: https://git.eclipse.org/r/12952 Tested-by: Hudson CI Reviewed-by: Matthew Khouzam IP-Clean: Matthew Khouzam Tested-by: Matthew Khouzam Reviewed-by: Alexandre Montplaisir IP-Clean: Alexandre Montplaisir --- .../tests/ctfadaptor/CtfTmfTraceTest.java | 16 +++++ .../tmf/core/ctfadaptor/CtfTmfTrace.java | 60 ++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfTraceTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfTraceTest.java index 4eb16037e4..913e465d56 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfTraceTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfTraceTest.java @@ -14,6 +14,7 @@ package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -348,4 +349,19 @@ public class CtfTmfTraceTest { IStatus result = fixture.validate(project, path); assertTrue(result.isOK()); } + + /** + * Run the boolean hasEvent(final String) method test + */ + @Test + public void testEventLookup() { + assertTrue(fixture.hasEvent("sched_switch")); + assertFalse(fixture.hasEvent("Sched_switch")); + String[] events = { "sched_switch", "sched_wakeup", "timer_init" }; + assertTrue(fixture.hasAllEvents(events)); + assertTrue(fixture.hasAtLeastOneOfEvents(events)); + String[] names = { "inexistent", "sched_switch", "SomeThing" }; + assertTrue(fixture.hasAtLeastOneOfEvents(names)); + assertFalse(fixture.hasAllEvents(names)); + } } 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 1cfa63b3c3..700b0a9d7d 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 @@ -20,6 +20,7 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; +import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader; @@ -29,9 +30,9 @@ import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; -import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceProperties; import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; +import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceProperties; import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; /** @@ -307,6 +308,63 @@ public class CtfTmfTrace extends TmfTrace return 0; } + /** + * Returns whether or not an event is in the metadata of the trace, + * therefore if it can possibly be in the trace. It does not verify whether + * or not the event is actually in the trace + * + * @param eventName + * The name of the event to check + * @return Whether the event is in the metadata or not + * @since 2.1 + */ + public boolean hasEvent(final String eventName) { + Map events = fTrace.getEvents(0L); + if (events != null) { + for (IEventDeclaration decl : events.values()) { + if (decl.getName().equals(eventName)) { + return true; + } + } + } + return false; + } + + /** + * Return whether all requested events are in the metadata + * + * @param names + * The array of events to check for + * @return Whether all events are in the metadata + * @since 2.1 + */ + public boolean hasAllEvents(String[] names) { + for (String name : names) { + if (!hasEvent(name)) { + return false; + } + } + return true; + } + + /** + * Returns whether the metadata contains at least one of the requested + * events + * + * @param names + * The array of event names of check for + * @return Whether one of the event is present in trace metadata + * @since 2.1 + */ + public boolean hasAtLeastOneOfEvents(String[] names) { + for (String name : names) { + if (hasEvent(name)) { + return true; + } + } + return false; + } + // ------------------------------------------- // Parser // ------------------------------------------- -- 2.34.1