Commit 5168b26d authored by Lubomir Bulej's avatar Lubomir Bulej

Use ByteBuffer to access invocation data

This should avoid copying the ByteString into an extra array
and wrapping it with ByteArrayInputStream and DataInputStream.
parent 3daba446
......@@ -45,7 +45,7 @@ final class AnalysisRequestDispatcher {
shadowVm.HandleAnalyze (
analyze.getOrderingID (),
analyze.getInvocationCount (),
analyze.getRawData ().toByteArray ()
analyze.getRawData ().asReadOnlyByteBuffer ()
);
return;
}
......
package ch.usi.dag.shvm;
import java.io.DataInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.List;
......@@ -43,12 +43,12 @@ final class AnalysisHandler {
*
* @param orderingId Total ordering ID
* @param invocationCount Number of method invocations
* @param is Input stream with the rest of the data.
* @param rawData Input stream with the rest of the data.
* @throws DiSLREServerException
*/
void handle (
final long orderingId, final int invocationCount,
final DataInputStream is
final ByteBuffer rawData
) throws DiSLREServerException {
if (invocationCount < 0) {
throw new DiSLREServerException (
......@@ -58,7 +58,7 @@ final class AnalysisHandler {
}
final List <AnalysisInvocation> invocations = __unmarshalInvocations (
invocationCount, is
invocationCount, rawData
);
__dispatcher.addTask (orderingId, invocations);
......@@ -66,12 +66,12 @@ final class AnalysisHandler {
private List <AnalysisInvocation> __unmarshalInvocations (
final int invocationCount, final DataInputStream is
final int invocationCount, final ByteBuffer rawData
) throws DiSLREServerException {
final List <AnalysisInvocation> result = new LinkedList <> ();
for (int i = 0; i < invocationCount; ++i) {
result.add (__unmarshalInvocation (is));
result.add (__unmarshalInvocation (rawData));
}
return result;
......@@ -79,13 +79,13 @@ final class AnalysisHandler {
private AnalysisInvocation __unmarshalInvocation (
final DataInputStream is
final ByteBuffer rawData
) throws DiSLREServerException {
try {
// *** retrieve method ***
// read method id from network and retrieve method
final short methodId = is.readShort ();
final short methodId = rawData.getShort ();
final AnalysisMethodHolder amh = __shvmContext
.getAnalysisResolver ().getMethod (methodId);
......@@ -94,7 +94,7 @@ final class AnalysisHandler {
final Method method = amh.getAnalysisMethod ();
// read the length of argument data in the request
final short argsLength = is.readShort ();
final short argsLength = rawData.getShort ();
if (argsLength < 0) {
throw new DiSLREServerException (
"invalid length of argument data for analysis method %d (%s.%s): %d",
......@@ -116,7 +116,7 @@ final class AnalysisHandler {
int argIndex = 0;
for (final Class <?> argClass : paramTypes) {
argsRead += unmarshalArgument (
is, argClass, method, argIndex, args
rawData, argClass, method, argIndex, args
);
argIndex += 1;
......@@ -151,62 +151,62 @@ final class AnalysisHandler {
private int unmarshalArgument (
final DataInputStream is, final Class <?> argClass,
final ByteBuffer rawData, final Class <?> argClass,
final Method analysisMethod, final int index, final Object [] args
) throws IOException, DiSLREServerException {
if (boolean.class.equals (argClass)) {
args [index] = is.readBoolean ();
args [index] = rawData.get () != 0;
return Byte.SIZE / Byte.SIZE;
}
if (char.class.equals (argClass)) {
args [index] = is.readChar ();
args [index] = rawData.getChar ();
return Character.SIZE / Byte.SIZE;
}
if (byte.class.equals (argClass)) {
args [index] = is.readByte ();
args [index] = rawData.get ();
return Byte.SIZE / Byte.SIZE;
}
if (short.class.equals (argClass)) {
args [index] = is.readShort ();
args [index] = rawData.getShort ();
return Short.SIZE / Byte.SIZE;
}
if (int.class.equals (argClass)) {
args [index] = is.readInt ();
args [index] = rawData.getInt ();
return Integer.SIZE / Byte.SIZE;
}
if (long.class.equals (argClass)) {
args [index] = is.readLong ();
args [index] = rawData.getLong ();
return Long.SIZE / Byte.SIZE;
}
if (float.class.equals (argClass)) {
args [index] = is.readFloat ();
args [index] = rawData.getFloat ();
return Float.SIZE / Byte.SIZE;
}
if (double.class.equals (argClass)) {
args [index] = is.readDouble ();
args [index] = rawData.getDouble ();
return Double.SIZE / Byte.SIZE;
}
if (ShadowObject.class.isAssignableFrom (argClass)) {
// zero tag means null
final long tag = is.readLong();
final long tag = rawData.getLong ();
args [index] = (tag == 0) ? null : __shvmContext.getShadowObjectTable().get (tag);
return Long.SIZE / Byte.SIZE;
}
throw new DiSLREServerException (String.format (
throw new DiSLREServerException (
"Unsupported data type %s in analysis method %s.%s",
argClass.getName (), analysisMethod.getDeclaringClass ().getName (),
analysisMethod.getName ()
));
);
}
void threadEnded(final long threadId) {
......
......@@ -6,6 +6,8 @@
*/
package ch.usi.dag.shvm;
import java.nio.ByteBuffer;
/**
* Public interface specifying the public API of ShadowVM object model.
*
......@@ -34,7 +36,7 @@ public interface SHVM {
* @param invocationCount Number of method invocations
* @param rawData Packed raw data
*/
void HandleAnalyze(long orderingId, int invocationCount, byte[] rawData) throws DiSLREServerException;
void HandleAnalyze(long orderingId, int invocationCount, ByteBuffer rawData) throws DiSLREServerException;
/**
* Handle the message about closing the connection and end of the analysis.
......
package ch.usi.dag.shvm;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.nio.ByteBuffer;
import ch.usi.dag.dislreserver.remoteanalysis.RemoteAnalysis;
import ch.usi.dag.util.logging.Logger;
......@@ -63,11 +62,10 @@ public final class SHVMImpl implements SHVM {
*/
@Override
public void HandleAnalyze (
final long orderingId, final int invocationCount, final byte [] rawData
final long orderingId, final int invocationCount, final ByteBuffer rawData
) throws DiSLREServerException {
__analysisHandler.handle (
orderingId, invocationCount,
new DataInputStream (new ByteArrayInputStream (rawData))
orderingId, invocationCount, rawData
);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment