import java.util.ArrayList;
import java.util.List;
+import java.util.Set;
import org.eclipse.linuxtools.internal.lttng2.kernel.core.TcpEventStrings;
import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
import org.eclipse.linuxtools.tmf.core.event.matching.TmfEventMatching.MatchingType;
import org.eclipse.linuxtools.tmf.core.event.matching.TmfNetworkEventMatching.Direction;
import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.TmfEventTypeCollectionHelper;
import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
+import com.google.common.collect.ImmutableSet;
+
/**
* Class to match tcp type events. This matching class applies to traces
* obtained with the 'addons' lttng module. This module can be obtained with
*/
public class TcpEventMatching implements ITmfNetworkMatchDefinition {
+ private static final ImmutableSet<String> REQUIRED_EVENTS = ImmutableSet.of(
+ TcpEventStrings.INET_SOCK_LOCAL_IN,
+ TcpEventStrings.INET_SOCK_LOCAL_OUT);
+
private static boolean canMatchPacket(final ITmfEvent event) {
/* Make sure all required fields are present to match with this event */
ITmfEventField content = event.getContent();
return false;
}
CtfTmfTrace ktrace = (CtfTmfTrace) trace;
- String[] events = { TcpEventStrings.INET_SOCK_LOCAL_IN, TcpEventStrings.INET_SOCK_LOCAL_OUT };
- return ktrace.hasAtLeastOneOfEvents(events);
+
+ Set<String> traceEvents = TmfEventTypeCollectionHelper.getEventNames(ktrace.getContainedEventTypes());
+ traceEvents.retainAll(REQUIRED_EVENTS);
+ return !traceEvents.isEmpty();
}
@Override
import java.util.ArrayList;
import java.util.List;
+import java.util.Set;
import org.eclipse.linuxtools.internal.lttng2.kernel.core.TcpEventStrings;
import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
+import org.eclipse.linuxtools.tmf.core.event.matching.ITmfNetworkMatchDefinition;
import org.eclipse.linuxtools.tmf.core.event.matching.TmfEventMatching.MatchingType;
import org.eclipse.linuxtools.tmf.core.event.matching.TmfNetworkEventMatching.Direction;
-import org.eclipse.linuxtools.tmf.core.event.matching.ITmfNetworkMatchDefinition;
import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.TmfEventTypeCollectionHelper;
import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
+import com.google.common.collect.ImmutableSet;
+
/**
* Class to match tcp type events. This class applies to traces obtained with
* the full network tracepoint data available from an experimental branch of
private static final String[] key_ackseq = { TcpEventStrings.TRANSPORT_FIELDS, TcpEventStrings.TYPE_TCP, TcpEventStrings.ACKSEQ };
private static final String[] key_flags = { TcpEventStrings.TRANSPORT_FIELDS, TcpEventStrings.TYPE_TCP, TcpEventStrings.FLAGS };
+ private static final ImmutableSet<String> REQUIRED_EVENTS = ImmutableSet.of(
+ TcpEventStrings.NET_DEV_QUEUE,
+ TcpEventStrings.NETIF_RECEIVE_SKB);
+
private static boolean canMatchPacket(final ITmfEvent event) {
TmfEventField field = (TmfEventField) event.getContent();
return false;
}
CtfTmfTrace ktrace = (CtfTmfTrace) trace;
- String[] events = { TcpEventStrings.NET_DEV_QUEUE, TcpEventStrings.NETIF_RECEIVE_SKB };
- return (ktrace.hasAtLeastOneOfEvents(events));
+
+ Set<String> traceEvents = TmfEventTypeCollectionHelper.getEventNames(ktrace.getContainedEventTypes());
+ traceEvents.retainAll(REQUIRED_EVENTS);
+ return !traceEvents.isEmpty();
}
@Override
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
import java.util.Arrays;
+import java.util.Set;
import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
fInstance.add(fContext2, fType2);
fInstance.add(fContext2, fType3);
- ITmfEventType[] types = fInstance.getTypes(fContext1);
- assertEquals("getTypes", 2, types.length);
- if (fType0 == types[0]) {
- assertSame("getTypes", fType1, types[1]);
- } else {
- assertSame("getTypes", fType0, types[1]);
- assertSame("getTypes", fType1, types[0]);
- }
+ Set<ITmfEventType> types = fInstance.getTypes(fContext1);
+ assertEquals("getTypes", 2, types.size());
+ assertTrue(types.contains(fType1));
+ assertTrue(types.contains(fType0));
types = fInstance.getTypes(fContext2);
- assertEquals("getTypes", 2, types.length);
- if (fType2 == types[0]) {
- assertSame("getTypes", fType3, types[1]);
- } else {
- assertSame("getTypes", fType2, types[1]);
- assertSame("getTypes", fType3, types[0]);
- }
+ assertEquals("getTypes", 2, types.size());
+ assertTrue(types.contains(fType2));
+ assertTrue(types.contains(fType3));
}
@Test
public void testClear() {
fInstance.clear();
assertEquals("clear", 0, fInstance.getContexts().length);
- assertEquals("clear", 0, fInstance.getTypes(null).length);
+ assertEquals("clear", 0, fInstance.getTypes(null).size());
assertNull("clear", fInstance.getType(null, null));
assertEquals("clear", "TmfEventTypeManager [fEventTypes={}]", fInstance.toString());
}
assertEquals("clear context", 1, contexts.length);
assertEquals("clear context", fContext2, contexts[0]);
- ITmfEventType[] types = fInstance.getTypes(fContext1);
- assertEquals("clear context", 0, types.length);
+ Set<ITmfEventType> types = fInstance.getTypes(fContext1);
+ assertEquals("clear context", 0, types.size());
ITmfEventType type = fInstance.getType(fContext1, fType0.getName());
assertNull("clear context", type);
assertNull("clear context", type);
types = fInstance.getTypes(fContext2);
- assertEquals("clear context", 2, types.length);
- if (fType2 == types[0]) {
- assertSame("clear context", fType3, types[1]);
- } else {
- assertSame("clear context", fType2, types[1]);
- assertSame("clear context", fType3, types[0]);
- }
+ assertEquals("clear context", 2, types.size());
+ assertTrue(types.contains(fType2));
+ assertTrue(types.contains(fType3));
}
@Test
assertEquals("add", 1, contexts.length);
assertEquals("add", fContext1, contexts[0]);
- final ITmfEventType[] types = fInstance.getTypes(contexts[0]);
- assertEquals("add", 1, types.length);
- assertSame("add", fType0, types[0]);
+ final Set<ITmfEventType> types = fInstance.getTypes(contexts[0]);
+ assertEquals("add", 1, types.size());
+ assertTrue(types.contains(fType0));
ITmfEventType type = fInstance.getType(contexts[0], fType0.getName());
assertSame("add", fType0, type);
assertEquals("add", fContext1, contexts[0]);
assertEquals("add", fContext2, contexts[1]);
- ITmfEventType[] types = fInstance.getTypes(fContext1);
- assertEquals("add", 2, types.length);
- if (fType0 == types[0]) {
- assertSame("add", fType1, types[1]);
- } else {
- assertSame("add", fType0, types[1]);
- assertSame("add", fType1, types[0]);
- }
+ Set<ITmfEventType> types = fInstance.getTypes(fContext1);
+ assertEquals("add", 2, types.size());
+ assertTrue(types.contains(fType0));
+ assertTrue(types.contains(fType1));
types = fInstance.getTypes(fContext2);
- assertEquals("add", 2, types.length);
- if (fType2 == types[0]) {
- assertSame("add", fType3, types[1]);
- } else {
- assertSame("add", fType2, types[1]);
- assertSame("add", fType3, types[0]);
- }
+ assertEquals("add", 2, types.size());
+ assertTrue(types.contains(fType2));
+ assertTrue(types.contains(fType3));
ITmfEventType type = fInstance.getType(fContext1, fType0.getName());
assertSame("add", fType0, type);
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
+
+import com.google.common.collect.ImmutableSet;
/**
* A central repository for the available event types. Types are managed by
*
* @param context the context to look into
* @return the list of types defined for that context
+ * @since 3.0
*/
- public synchronized ITmfEventType[] getTypes(final String context) {
+ public synchronized Set<ITmfEventType> getTypes(final String context) {
final HashMap<String, ITmfEventType> types = fEventTypes.get(context);
if (types != null) {
- return types.values().toArray(new ITmfEventType[types.size()]);
+ return ImmutableSet.copyOf(types.values());
}
- return new ITmfEventType[0];
+ return ImmutableSet.of();
}
/**
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2014 École Polytechnique de Montréal, 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:
+ * Geneviève Bastien - Initial API and implementation
+ * Matthew Khouzam - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.tmf.core.trace;
+
+import java.util.Set;
+
+import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
+
+/**
+ * This interface should be implemented by all trace classes who have a way to
+ * know in advance what events it may contain. It allows analyses and other
+ * external components to ask the list of events for the trace might contain.
+ *
+ * The methods from this interface will typically be called to determine whether
+ * or not it is worth reading a trace. If we can know in advance that a trace
+ * does not contain the events required by an analysis, then the analysis will
+ * not be run. So the response should not involve having to actually read the
+ * trace.
+ *
+ * @author Geneviève Bastien
+ * @author Matthew Khouzam
+ * @since 3.0
+ */
+public interface ITmfTraceWithPreDefinedEvents {
+
+ /**
+ * Return a set of event types declared in the trace, without actually
+ * reading the trace. This method can be called before reading a trace but
+ * after it is initialized, in order to compare this set with a set of
+ * events that a request handles, to determine whether or not it is worth
+ * reading the trace.
+ *
+ * Some trace types have ways to determine the events that were traced
+ * without having to read the whole trace and this is what this method will
+ * query. The presence of an event in the returned set does not guarantee
+ * that an event with this name actually happened during this trace, only
+ * that it can be there.
+ *
+ * The set should be immutable. Destructive set operations should be
+ * performed on a copy of this set.A helper class
+ * {@link TmfEventTypeCollectionHelper} will provide ways of working with
+ * this data structure.
+ *
+ * @return The set of events that might be present in the trace
+ */
+ Set<ITmfEventType> getContainedEventTypes();
+
+}
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 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:
+ * Matthew Khouzam - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.tmf.core.trace;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
+
+/**
+ * Set Helper for sets of ITmfTraceType
+ *
+ * TODO Remove once Java 8 is used (replace with Streams)
+ *
+ * @author Matthew Khouzam
+ * @since 3.0
+ */
+public final class TmfEventTypeCollectionHelper {
+
+ private TmfEventTypeCollectionHelper() {
+ }
+
+ /**
+ * Gets the event names from a collection of event types
+ *
+ * @param eventTypes
+ * an iterable collection of ITmfEventTypes
+ * @return a set of the names of these events, if some names are clashing
+ * they will only appear once
+ */
+ public static Set<String> getEventNames(Iterable<ITmfEventType> eventTypes) {
+ Set<String> retSet = new HashSet<>();
+ for (ITmfEventType eventType : eventTypes) {
+ retSet.add(eventType.getName());
+ }
+ return retSet;
+ }
+}
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
-import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
+import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal;
import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
+import org.eclipse.linuxtools.tmf.core.trace.TmfEventTypeCollectionHelper;
import org.eclipse.linuxtools.tmf.ctf.core.CtfLocation;
import org.eclipse.linuxtools.tmf.ctf.core.CtfLocationInfo;
import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEvent;
public void testCtfTmfTrace() {
try (CtfTmfTrace result = new CtfTmfTrace();) {
assertNotNull(result);
- assertNull(result.getEventType());
assertEquals(1000, result.getCacheSize());
assertEquals(0L, result.getNbEvents());
assertEquals(0L, result.getStreamingInterval());
fixture.broadcast(signal);
}
+ /**
+ * Run the void dispose() method test.
+ */
+ @Test
+ public void testClose() {
+ try (CtfTmfTrace emptyFixture = new CtfTmfTrace();) {
+ }
+ }
+
/**
* Run the int getCacheSize() method test.
*/
* Test the seekEvent() method with a location from a timestamp.
*/
@Test
- public void testSeekEventLoc_timetamp(){
+ public void testSeekEventLoc_timetamp() {
CtfLocation loc = new CtfLocation(new CtfTmfTimestamp(0L));
fixture.seekEvent(loc);
assertNotNull(fixture);
}
-
/**
* Run the ITmfTimestamp getEndTime() method test.
*/
public void testGetEnvValue() {
String key = "tracer_name";
String result = fixture.getTraceProperties().get(key);
- assertEquals("\"lttng-modules\"",result);
+ assertEquals("\"lttng-modules\"", result);
}
/**
- * Run the Class<CtfTmfEvent> getEventType() method test.
+ * Test the {@link CtfTmfTrace#getEventType()} method.
*/
@Test
public void testGetEventType() {
- Class<? extends ITmfEvent> result = fixture.getEventType();
+ Class<?> result = fixture.getEventType();
+ assertNotNull(result);
+ assertEquals(CtfTmfEvent.class, result);
+ }
+
+ /**
+ * Run the Class<CtfTmfEvent> getContainedEventTypes() method test.
+ */
+ @Test
+ public void testGetContainedEventTypes() {
+ Set<ITmfEventType> result = fixture.getContainedEventTypes();
assertNotNull(result);
+ assertFalse(result.isEmpty());
}
/**
*/
@Test
public void testEventLookup() {
- assertTrue(fixture.hasEvent("sched_switch"));
- assertFalse(fixture.hasEvent("Sched_switch"));
+ Set<ITmfEventType> eventTypes = fixture.getContainedEventTypes();
+ Set<String> eventNames = TmfEventTypeCollectionHelper.getEventNames(eventTypes);
+ assertTrue(eventNames.contains("sched_switch"));
+ assertFalse(eventNames.contains("Sched_switch"));
String[] events = { "sched_switch", "sched_wakeup", "timer_init" };
- assertTrue(fixture.hasAllEvents(events));
- assertTrue(fixture.hasAtLeastOneOfEvents(events));
+ assertTrue(eventNames.containsAll(Arrays.asList(events)));
+ Set<String> copy = new HashSet<>(eventNames);
+ copy.retainAll(Arrays.asList(events));
+ assertFalse(copy.isEmpty());
String[] names = { "inexistent", "sched_switch", "SomeThing" };
- assertTrue(fixture.hasAtLeastOneOfEvents(names));
- assertFalse(fixture.hasAllEvents(names));
+ copy = new HashSet<>(eventNames);
+ copy.retainAll(Arrays.asList(names));
+ assertTrue(!copy.isEmpty());
+ assertFalse(eventNames.containsAll(Arrays.asList(names)));
}
/**
org.eclipse.linuxtools.tmf.core;bundle-version="3.0.0"
Export-Package: org.eclipse.linuxtools.internal.tmf.ctf.core;x-internal:=true,
org.eclipse.linuxtools.tmf.ctf.core
+Import-Package: com.google.common.collect,
+ org.eclipse.emf.common.util
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
+import java.util.Set;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
import org.eclipse.linuxtools.internal.tmf.ctf.core.Activator;
import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
+import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
+import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
+import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
+import org.eclipse.linuxtools.tmf.core.event.TmfEventTypeManager;
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.ITmfEventParser;
import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceProperties;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceWithPreDefinedEvents;
import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
import org.eclipse.linuxtools.tmf.core.trace.TraceValidationStatus;
import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
+import com.google.common.collect.ImmutableSet;
+
/**
* The CTf trace handler
*
*/
public class CtfTmfTrace extends TmfTrace
implements ITmfEventParser, ITmfTraceProperties, ITmfPersistentlyIndexable,
- AutoCloseable {
+ ITmfTraceWithPreDefinedEvents, AutoCloseable {
// -------------------------------------------
// Constants
this.setStartTime(curTime);
this.setEndTime(curTime);
}
-
+ /*
+ * Register every event type. When you call getType, it will
+ * register a trace to that type in the TmfEventTypeManager
+ */
+ try (CtfIterator iter = CtfIteratorManager.getIterator(this, ctx)) {
+ for (IEventDeclaration ied : iter.getEventDeclarations()) {
+ CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(this, ied.getName());
+ if (ctfTmfEventType == null) {
+ List<ITmfEventField> content = new ArrayList<>();
+ /* Should only return null the first time */
+ for (String fieldName : ied.getFields().getFieldsList()) {
+ content.add(new TmfEventField(fieldName, null, null));
+ }
+ ITmfEventField contentTree = new TmfEventField(
+ ITmfEventField.ROOT_FIELD_ID,
+ null,
+ content.toArray(new ITmfEventField[content.size()])
+ );
+
+ ctfTmfEventType = new CtfTmfEventType(ied.getName(), this, contentTree);
+ }
+ }
+ }
} catch (final CTFReaderException e) {
/*
* If it failed at the init(), we can assume it's because the file
}
/**
- * 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<Long, IEventDeclaration> 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
+ * Gets the list of declared events
*
- * @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
+ * @since 3.0
*/
- public boolean hasAtLeastOneOfEvents(String[] names) {
- for (String name : names) {
- if (hasEvent(name)) {
- return true;
- }
- }
- return false;
+ @Override
+ public Set<ITmfEventType> getContainedEventTypes() {
+ TmfEventTypeManager instance = TmfEventTypeManager.getInstance();
+ Set<ITmfEventType> eventTypes = instance.getTypes(CtfTmfEventType.computeContextName(this));
+ return ImmutableSet.copyOf(eventTypes);
}
// -------------------------------------------