Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mt ctf #163

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

package org.eclipse.tracecompass.ctf.core.event.scope;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -28,7 +28,7 @@ public class LexicalScope implements ILexicalScope {
private int hash = 0;
private final @NonNull String fName;
private final @NonNull String fPath;
private final Map<String, ILexicalScope> fChildren = new ConcurrentHashMap<>();
private final Map<String, ILexicalScope> fChildren = new HashMap<>();

/**
* Hidden constructor for the root node only
Expand All @@ -40,6 +40,22 @@ protected LexicalScope() {
fName = ""; //$NON-NLS-1$
}

/**
* Create a scope
* @param parent
* The parent node, can be null, but shouldn't
* @param name
* the name of the field
* @return the scope
*/
public static @NonNull ILexicalScope create(ILexicalScope parent, @NonNull String name) {
ILexicalScope child = parent.getChild(name);
if( child == null) {
child = new LexicalScope(parent, name);
}
return child;
}

/**
* The scope constructor
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,10 @@ public ILexicalScope getPath(IDefinitionScope definitionScope, @NonNull String f
if (definitionScope != null) {
final ILexicalScope parentPath = definitionScope.getScopePath();
if (parentPath != null) {
ILexicalScope myScope = parentPath.getChild(fieldName);
if (myScope == null) {
myScope = new LexicalScope(parentPath, fieldName);
}
return myScope;
return LexicalScope.create(parentPath, fieldName);
}
}
ILexicalScope child = ILexicalScope.ROOT.getChild(fieldName);
if (child != null) {
return child;
}
return new LexicalScope(ILexicalScope.ROOT, fieldName);
return LexicalScope.create(ILexicalScope.ROOT, fieldName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -46,6 +47,9 @@
*/
public final class EnumDeclaration extends Declaration implements ISimpleDatatypeDeclaration {

private static final int CACHE_SIZE = 256;
private Map<Long, String> fCache = new HashMap<>();

/**
* A pair of longs class
*
Expand Down Expand Up @@ -141,7 +145,7 @@ public EnumDeclaration(IntegerDeclaration containerType) {
* Existing enum declaration table
* @since 2.3
*/
public EnumDeclaration(IntegerDeclaration containerType, Map<Pair, String> enumTree){
public EnumDeclaration(IntegerDeclaration containerType, Map<Pair, String> enumTree) {
fContainerType = containerType;
enumTree.entrySet().forEach(entry -> fEnumMap.put(entry.getKey(), entry.getValue()));
}
Expand Down Expand Up @@ -241,37 +245,50 @@ public boolean add(@Nullable String label) {
* @return the label of that value, can be null
*/
public @Nullable String query(long value) {
List<String> strValues = new ArrayList<>();
fEnumMap.forEach((k, v) -> {
if (value >= k.getFirst() && value <= k.getSecond()) {
strValues.add(v);
String retVal = fCache.get(value);
if (retVal == null) {
List<String> strValues = new ArrayList<>();
fEnumMap.forEach((k, v) -> {
if (value >= k.getFirst() && value <= k.getSecond()) {
strValues.add(v);
}
});
if (!strValues.isEmpty()) {
retVal = strValues.size() == 1 ? strValues.get(0) : strValues.toString();
fCache.put(value, retVal);
if (fCache.size() > CACHE_SIZE) {
fCache.remove(fCache.keySet().toArray()[0]);
}
return retVal;
}
});
if (!strValues.isEmpty()) {
return strValues.size() == 1 ? strValues.get(0) : strValues.toString();
}
/*
* Divide the positive value in bits and see if there is a value for all
* those bits
*/
List<String> flagsSet = new ArrayList<>();
for (int i = 0; i < Long.SIZE; i++) {
Long bitValue = 1L << i;
if ((bitValue & value) != 0) {
/*
* See if there is a value for this bit where lower == upper, no
* range accepted here
*/
Pair bitPair = new Pair(bitValue, bitValue);
Collection<String> flagValues = fEnumMap.get(bitPair);
if (flagValues.isEmpty()) {
// No value for this bit, not an enum flag
return null;
/*
* Divide the positive value in bits and see if there is a value for
* all those bits
*/
List<String> flagsSet = new ArrayList<>();
for (int i = 0; i < Long.SIZE; i++) {
Long bitValue = 1L << i;
if ((bitValue & value) != 0) {
/*
* See if there is a value for this bit where lower ==
* upper, no range accepted here
*/
Pair bitPair = new Pair(bitValue, bitValue);
Collection<String> flagValues = fEnumMap.get(bitPair);
if (flagValues.isEmpty()) {
// No value for this bit, not an enum flag
return null;
}
flagsSet.add(flagValues.size() == 1 ? flagValues.iterator().next() : flagValues.toString());
}
flagsSet.add(flagValues.size() == 1 ? flagValues.iterator().next() : flagValues.toString());
}
retVal = flagsSet.isEmpty() ? null : String.join(" | ", flagsSet); //$NON-NLS-1$
fCache.put(value, retVal);
if (fCache.size() > CACHE_SIZE) {
fCache.remove(fCache.keySet().toArray()[0]);
}
}
return flagsSet.isEmpty() ? null : String.join(" | ", flagsSet); //$NON-NLS-1$
return retVal;
}

/**
Expand Down Expand Up @@ -332,8 +349,8 @@ public boolean equals(@Nullable Object obj) {
return false;
}
/*
* Must iterate through the entry sets as the comparator used in the enum tree
* does not respect the contract
* Must iterate through the entry sets as the comparator used in the
* enum tree does not respect the contract
*/
return Iterables.elementsEqual(fEnumMap.entries(), other.fEnumMap.entries());
}
Expand All @@ -354,8 +371,8 @@ public boolean isBinaryEquivalent(@Nullable IDeclaration obj) {
return false;
}
/*
* Must iterate through the entry sets as the comparator used in the enum tree
* does not respect the contract
* Must iterate through the entry sets as the comparator used in the
* enum tree does not respect the contract
*/
return Iterables.elementsEqual(fEnumMap.entries(), other.fEnumMap.entries());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ public final class EnumDefinition extends SimpleDatatypeDefinition {

private static final String UNKNOWN_ENUM = "<unknown> (%s)"; //$NON-NLS-1$

private static final String UNINITIALIZED = "UNINITIALIZED"; //$NON-NLS-1$

private final IntegerDefinition fInteger;

private final @Nullable String fValue;
private @Nullable String fValue;

// ------------------------------------------------------------------------
// Constructors
Expand All @@ -59,9 +61,9 @@ public final class EnumDefinition extends SimpleDatatypeDefinition {
public EnumDefinition(@NonNull EnumDeclaration declaration,
IDefinitionScope definitionScope, @NonNull String fieldName, IntegerDefinition intValue) {
super(declaration, definitionScope, fieldName);

fInteger = intValue;
fValue = declaration.query(fInteger.getValue());
fValue = UNINITIALIZED;

}

// ------------------------------------------------------------------------
Expand All @@ -75,6 +77,9 @@ public EnumDefinition(@NonNull EnumDeclaration declaration,
* @return the value of the enum.
*/
public String getValue() {
if (fValue == UNINITIALIZED) {
fValue = getDeclaration().query(fInteger.getValue());
}
return fValue != null ? fValue : String.format(UNKNOWN_ENUM, getIntegerValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private BitBuffer createBitBufferForPacketHeader(FileChannel fc, long dataOffset
* create a packet bit buffer to read the packet header
*/
int maximumSize = fStreamPacketContextDecl.getMaximumSize() + fTracePacketHeaderDecl.getMaximumSize();
BitBuffer bitBuffer = new BitBuffer(createPacketBitBuffer(fc, dataOffsetbits/Byte.SIZE, maximumSize));
BitBuffer bitBuffer = new BitBuffer(createPacketBitBuffer(fc, dataOffsetbits / Byte.SIZE, maximumSize));
bitBuffer.setByteOrder(getStream().getTrace().getByteOrder());
return bitBuffer;
}
Expand Down Expand Up @@ -372,7 +372,7 @@ private StructDefinition parseTracePacketHeader(
}
if (!Objects.equals(getStream().getTrace().getUUID(), uuid) && !fUUIDMismatchWarning) {
fUUIDMismatchWarning = true;
CtfCoreLoggerUtil.logWarning("Reading CTF trace: UUID mismatch for trace " + getStream().getTrace()); //$NON-NLS-1$
CtfCoreLoggerUtil.logWarning("Reading CTF trace: UUID mismatch for trace " + getStream().getTrace() + " is not uuid from metadata (" + String.valueOf(uuid) + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
if (streamIDDef != null) {
long streamID = streamIDDef.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.ctf.core.CTFException;
Expand All @@ -32,7 +37,7 @@
import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
import org.eclipse.tracecompass.internal.ctf.core.CtfCoreLoggerUtil;
import org.eclipse.tracecompass.internal.ctf.core.SafeMappedByteBuffer;
import org.eclipse.tracecompass.internal.ctf.core.trace.CTFPacketReader;
import org.eclipse.tracecompass.internal.ctf.core.trace.CTFThreadedPacketReader;
import org.eclipse.tracecompass.internal.ctf.core.trace.NullPacketReader;

/**
Expand All @@ -51,6 +56,56 @@ public class CTFStreamInputReader implements AutoCloseable {
// Attributes
// ------------------------------------------------------------------------

private final class ExecutorClass implements Executor {
BlockingQueue<@NonNull Runnable> fQueue = new ArrayBlockingQueue<>(16);
Thread fThread = new Thread() {
@Override
public void run() {
try {
Runnable runnable = fQueue.take();
while (runnable != POISON_PILL) {
runnable.run();
runnable = fQueue.take();
}
} catch (InterruptedException e) {
// don't consume it
}
}
};

private ExecutorClass(String fileName) {
fThread.setName("StreamReader: " + fileName); //$NON-NLS-1$
fThread.start();
Thread.currentThread().setName("Stream enqueuer"); //$NON-NLS-1$
}

@Override
public void execute(@Nullable Runnable command) {
if (command != null) {
try {
fQueue.put(command);
} catch (InterruptedException e) {

}
}

}

Runnable POISON_PILL = new Runnable() {

@Override
public void run() {
}
};

public void terminate() throws InterruptedException {
fQueue.put(POISON_PILL);
fThread.join();
}
}

private Executor fStreamConsumer;

/**
* The StreamInput we are reading.
*/
Expand Down Expand Up @@ -97,6 +152,7 @@ public class CTFStreamInputReader implements AutoCloseable {
public CTFStreamInputReader(CTFStreamInput streamInput) throws CTFException {
fStreamInput = streamInput;
fFile = fStreamInput.getFile();
fStreamConsumer = new ExecutorClass(String.valueOf(fFile));
try {
fFileChannel = FileChannel.open(fFile.toPath(), StandardOpenOption.READ);
} catch (IOException e) {
Expand Down Expand Up @@ -141,7 +197,7 @@ private IPacketReader getCurrentPacketReader(@Nullable ICTFPacketDescriptor pack
bitBuffer.position(packet.getPayloadStartBits());
IDeclaration eventHeaderDeclaration = getStreamInput().getStream().getEventHeaderDeclaration();
CTFTrace trace = getStreamInput().getStream().getTrace();
ctfPacketReader = new CTFPacketReader(bitBuffer, packet, getEventDeclarations(), eventHeaderDeclaration, getStreamEventContextDecl(), trace.getPacketHeaderDef(), trace);
ctfPacketReader = new CTFThreadedPacketReader(fStreamConsumer, bitBuffer, packet, getEventDeclarations(), eventHeaderDeclaration, getStreamEventContextDecl(), Objects.requireNonNull(trace.getPacketHeaderDef()), trace);
}
return ctfPacketReader;
}
Expand Down Expand Up @@ -179,6 +235,10 @@ public void close() throws IOException {
if (fFileChannel != null) {
fFileChannel.close();
}
try {
((ExecutorClass) fStreamConsumer).terminate();
} catch (InterruptedException e) {
}
fPacketReader = NullPacketReader.INSTANCE;
}

Expand Down
Loading
Loading