Commit e2e5a833 authored by Nicola Amadio's avatar Nicola Amadio
Browse files

add feature (just one). Seems to serialize corrently but some issues in freud-statistics

parent 941c0528
public class FreudFeature {
private long nameOffset; // must be same as
private long typeOffset;
private long value;
public FreudFeature(long value) {
this.typeOffset = 0; // system feature
this.value = value;
}
public void setNameOffset(long nameOffset) {
this.nameOffset = nameOffset;
}
public long getTypeOffset() {
return typeOffset;
}
public long getValue() {
return value;
}
}
public class FreudFeatureName {
private short VarNameLen;
private String VarName;
public FreudFeatureName(short varNameLen, String varName) {
VarNameLen = varNameLen;
VarName = varName;
}
public short getVarNameLen() {
return VarNameLen;
}
public String getVarName() {
return VarName;
}
}
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;
import com.google.common.primitives.Shorts;
import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ArrayUtils;
import java.io.*; import java.io.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.List; import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
public class Main { public class Main {
...@@ -27,32 +33,54 @@ public class Main { ...@@ -27,32 +33,54 @@ public class Main {
private static void writeObjectToFile(FileOutputStream fos, TestClassToSerialize myObject) throws IOException { private static void writeObjectToFile(FileOutputStream fos, TestClassToSerialize myObject) throws IOException {
fos.write(intToByteArray(myObject.getSymbolNameLen())); fos.write(intToByteArray(myObject.getSymbolNameLen()));
fos.write(myObject.getSymbolName().getBytes()); fos.write(myObject.getSymbolName().getBytes());
Path path = Paths.get(PATHNAME);
LinkedList<Long> featureOffsets = new LinkedList<>();
fos.write(intToByteArray(myObject.getFeatureNamesCount())); fos.write(intToByteArray(myObject.getFeatureNamesCount()));
for (FreudFeatureName featureName: myObject.getFeatureNamesList()) {
SeekableByteChannel ch = Files.newByteChannel(path); // Defaults to read-only
featureOffsets.add(ch.position());
fos.write(shortToByteArray(featureName.getVarNameLen()));
fos.write(featureName.getVarName().getBytes());
}
fos.write(intToByteArray(myObject.getTypeNamesCount())); fos.write(intToByteArray(myObject.getTypeNamesCount()));
fos.write(intToByteArray(myObject.getSamplesCount())); fos.write(intToByteArray(myObject.getSamplesCount()));
List<Sample> samples = myObject.getSampleList(); for (Sample sample: myObject.getSampleList()) {
for (Sample sample: samples) {
fos.write(intToByteArray(sample.getUidR())); fos.write(intToByteArray(sample.getUidR()));
fos.write(longToBytes(sample.getTime())); fos.write(longToByteArray(sample.getTime()));
fos.write(longToBytes(sample.getMem())); fos.write(longToByteArray(sample.getMem()));
fos.write(longToBytes(sample.getLockHoldingTime())); fos.write(longToByteArray(sample.getLockHoldingTime()));
fos.write(longToBytes(sample.getWaitingTime())); fos.write(longToByteArray(sample.getWaitingTime()));
fos.write(longToBytes(sample.getMinorPageFaults())); fos.write(longToByteArray(sample.getMinorPageFaults()));
fos.write(longToBytes(sample.getMajorPageFaults())); fos.write(longToByteArray(sample.getMajorPageFaults()));
fos.write(intToByteArray(sample.getNumOfFeatures())); fos.write(intToByteArray(sample.getNumOfFeatures()));
int i = 0;
for (FreudFeature feature: sample.getFeatures()) {
fos.write(longToByteArray(20));
fos.write(longToByteArray(feature.getTypeOffset()));
fos.write(longToByteArray(feature.getValue()));
i++;
}
fos.write(intToByteArray(sample.getNumOfBranches())); fos.write(intToByteArray(sample.getNumOfBranches()));
fos.write(intToByteArray(sample.getNumOfChildren())); fos.write(intToByteArray(sample.getNumOfChildren()));
} }
fos.close(); fos.close();
} }
public static byte[] intToByteArray(int data) { public static byte[] shortToByteArray(short x) {
byte[] bytes = Ints.toByteArray(data); byte[] bytes = Shorts.toByteArray(x);
ArrayUtils.reverse(bytes);
return bytes;
}
public static byte[] intToByteArray(int x) {
byte[] bytes = Ints.toByteArray(x);
ArrayUtils.reverse(bytes); ArrayUtils.reverse(bytes);
return bytes; return bytes;
} }
public static byte[] longToBytes(long x) { public static byte[] longToByteArray(long x) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(x); buffer.putLong(x);
byte[] array = buffer.array(); byte[] array = buffer.array();
......
import java.util.LinkedList;
import java.util.List;
public class Sample { public class Sample {
int uidR; private int uidR;
long time; // so far, this is the only sample variable to be set to a value, the others get set to 0 private long time; // so far, this is the only sample variable to be set to a value, the others get set to 0
long mem; private long mem;
long lockHoldingTime; private long lockHoldingTime;
long waitingTime; private long waitingTime;
long minorPageFaults; private long minorPageFaults;
long majorPageFaults; private long majorPageFaults;
int numOfFeatures; private int numOfFeatures;
int numOfBranches; private int numOfBranches;
int numOfChildren; private int numOfChildren;
private List<FreudFeature> features;
public Sample(long time) { public Sample(long time) {
this.time = time; this.time = time;
...@@ -18,10 +22,12 @@ public class Sample { ...@@ -18,10 +22,12 @@ public class Sample {
waitingTime = 0; waitingTime = 0;
minorPageFaults = 0; minorPageFaults = 0;
majorPageFaults = 0; majorPageFaults = 0;
numOfFeatures = 0; numOfFeatures = 1;
numOfBranches = 0; numOfBranches = 0;
numOfChildren = 0; numOfChildren = 0;
features = new LinkedList<>();
long value = 10; // random value
features.add(new FreudFeature(value));
} }
public int getUidR() { public int getUidR() {
...@@ -56,6 +62,10 @@ public class Sample { ...@@ -56,6 +62,10 @@ public class Sample {
return numOfFeatures; return numOfFeatures;
} }
public List<FreudFeature> getFeatures() {
return features;
}
public int getNumOfBranches() { public int getNumOfBranches() {
return numOfBranches; return numOfBranches;
} }
......
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
public class TestClassToSerialize implements Serializable { public class TestClassToSerialize implements Serializable {
...@@ -8,6 +9,7 @@ public class TestClassToSerialize implements Serializable { ...@@ -8,6 +9,7 @@ public class TestClassToSerialize implements Serializable {
private String symbolName; private String symbolName;
// FEATURE NAMES // FEATURE NAMES
private int featureNamesCount; private int featureNamesCount;
List<FreudFeatureName> featureNames;
// TYPE NAMES // TYPE NAMES
int typeNamesCount; int typeNamesCount;
// SAMPLES // SAMPLES
...@@ -18,7 +20,13 @@ public class TestClassToSerialize implements Serializable { ...@@ -18,7 +20,13 @@ public class TestClassToSerialize implements Serializable {
public TestClassToSerialize(String symbolName, int samplesCount) { public TestClassToSerialize(String symbolName, int samplesCount) {
this.symbolNameLen = symbolName.length(); this.symbolNameLen = symbolName.length();
this.symbolName = symbolName; this.symbolName = symbolName;
this.featureNamesCount = 0;
// feature names
this.featureNamesCount = 1;
String myVar = "my var";
featureNames = new LinkedList<>();
featureNames.add(new FreudFeatureName((short) myVar.length(), myVar));
this.typeNamesCount = 0; this.typeNamesCount = 0;
this.samplesCount = samplesCount; this.samplesCount = samplesCount;
this.sampleList = new ArrayList<>(); this.sampleList = new ArrayList<>();
...@@ -55,6 +63,10 @@ public class TestClassToSerialize implements Serializable { ...@@ -55,6 +63,10 @@ public class TestClassToSerialize implements Serializable {
this.featureNamesCount = featureNamesCount; this.featureNamesCount = featureNamesCount;
} }
public List<FreudFeatureName> getFeatureNamesList() {
return featureNames;
}
// TYPE NAMES // TYPE NAMES
public int getTypeNamesCount() { public int getTypeNamesCount() {
return typeNamesCount; return typeNamesCount;
......
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