Commit f5b5cd88 authored by Aibek Sarimbekov's avatar Aibek Sarimbekov
Browse files

moved from ConcurrentHashMap to FieldStateList in fia

fixed bug in isInStaticInitializer
parent 42ee0c64
......@@ -47,7 +47,6 @@ public class DiSLClass {
ImmutabilityAnalysis.onObjectInitialization(allocatedObj, allocationSite);
}
//guard = NoClInit.class,
/** FIELD ACCESSES **/
......
......@@ -11,6 +11,6 @@ public class MyStaticContext extends AbstractStaticContext {
}
public boolean isInTheStaticInitializer() {
return (staticContextData.getMethodNode().name.equals("<clinit>") ? false : true);
return (staticContextData.getMethodNode().name.equals("<clinit>") ? true : false);
}
}
......@@ -6,6 +6,6 @@ public abstract class NoClInit {
@GuardMethod
public static boolean isApplicable(MyStaticContext sc) {
return sc.isInTheStaticInitializer();
return (!sc.isInTheStaticInitializer());
}
}
......@@ -2,21 +2,26 @@ package ch.usi.dag.disl.example.fieldsImmutabilityAnalysis.runtime;
import java.io.PrintStream;
public class FieldState {
public class FieldState {
private static final String OFFSET = "\t\t";
private static final String SEPARATOR = "\t";
private enum MyState {VIRGIN, IMMUTABLE, MUTABLE};
private MyState currentState;
private long writeAccesses;
private boolean defaultInit=true;
public FieldState(){
private boolean defaultInit = true;
private final String fieldName;
public FieldState(String fieldName){
this.fieldName = fieldName;
currentState = MyState.VIRGIN;
}
public String getFieldName(){
return fieldName;
}
public synchronized void onRead() {
switch(currentState) {
case VIRGIN:
......@@ -28,7 +33,7 @@ public class FieldState {
public synchronized void onWrite(boolean isInDynamicExtendOfConstructor) {
switch(currentState) {
case VIRGIN:
defaultInit=false;
defaultInit = false;
if(isInDynamicExtendOfConstructor) {
currentState = MyState.IMMUTABLE;
}
......@@ -38,19 +43,14 @@ public class FieldState {
currentState = MyState.MUTABLE;
}
else{
defaultInit=false;
defaultInit = false;
}
break;
}
}
public synchronized void dump(PrintStream ps) {
ps.println(toString());
}
public synchronized String toString() {
return currentState.name() + " : " + defaultInit + " : " + "[writes: " + writeAccesses + "]" ;
ps.println(OFFSET + fieldName + SEPARATOR + currentState.name() + " : " + defaultInit + " : " + "[writes: " + writeAccesses + "]" );
}
}
\ No newline at end of file
}
package ch.usi.dag.disl.example.fieldsImmutabilityAnalysis.runtime;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.LinkedList;
public class FieldStateList {
private final LinkedList<FieldState> myLL;
public FieldStateList() {
myLL = new LinkedList<FieldState>();
}
public FieldState put(String fieldName) {
FieldState newFS;
myLL.add(newFS = new FieldState(fieldName));
return newFS;
}
public FieldState get(String fieldName) {
if (myLL.size() == 0)
return null;
Iterator<FieldState> it = myLL.iterator();
while(it.hasNext()) {
FieldState nextFS;
if ((nextFS = it.next()).getFieldName().equals(fieldName)){
return nextFS;
}
}
return null;
}
public void dump(PrintStream ps) {
for(FieldState fs : myLL) {
fs.dump(ps);
}
}
}
......@@ -5,7 +5,6 @@ import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;
import ch.usi.dag.jborat.runtime.DynamicBypass;
......@@ -13,25 +12,20 @@ import ch.usi.dag.jborat.runtime.DynamicBypass;
@SuppressWarnings("unchecked")
public class ImmutabilityAnalysis {
private static final int NUMBER_OF_TABLES = 40000;
private static MyWeakKeyIdentityHashMap<Object, ConcurrentHashMap<String, FieldState>> bigMap[];
private static MyWeakKeyIdentityHashMap<Object,FieldStateList> bigMap;
private static final String DUMP_FILE = System.getProperty("dump", "dump.log");
private static MyDumper myDumper;
// public static ConcurrentHashMap<String,String>objectTable = new ConcurrentHashMap<String, String>();
static {
boolean oldState = DynamicBypass.getAndSet();
try{
PrintStream ps = new PrintStream(new BufferedOutputStream(new FileOutputStream(DUMP_FILE)));
myDumper = new MyDumper(ps);
bigMap = new MyWeakKeyIdentityHashMap[NUMBER_OF_TABLES];
for(int i=0;i<NUMBER_OF_TABLES;i++){
bigMap[i]= new MyWeakKeyIdentityHashMap<Object, ConcurrentHashMap<String, FieldState>>(8, myDumper);
}
bigMap = new MyWeakKeyIdentityHashMap();
Thread shutdownHook = new Thread(){
public void run() {
......@@ -57,17 +51,8 @@ public class ImmutabilityAnalysis {
String objectID = getObjectID(accessedObj, null);
FieldState fs = getOrCreateInstanceField(accessedObj, objectID, accessedFieldName);
// if(accessedObj instanceof java.io.ByteArrayInputStream) {
// System.out.println("****** " + accessedObj.getClass().getName() + "\t" + System.identityHashCode(accessedObj) + "\t" + os.toString());
// new Exception().printStackTrace();
// }
fs.onRead();
// if(accessedObj instanceof java.io.ByteArrayInputStream) {
// System.out.println("************** " + os.toString() + "\t" + Thread.currentThread().getName() + "\t" + Thread.currentThread().getId());
// }
}
catch(Throwable t){
t.printStackTrace();
......@@ -75,20 +60,16 @@ public class ImmutabilityAnalysis {
}
public static void onObjectInitialization(Object allocatedObj, String allocSite) {
int idx = System.identityHashCode(allocatedObj) % NUMBER_OF_TABLES;
try{
String objectID = getObjectID(allocatedObj, allocSite);
synchronized (bigMap[idx]) {
if(bigMap[idx].get(objectID) == null) {
bigMap[idx].put(allocatedObj, new ConcurrentHashMap<String, FieldState>(), objectID);
synchronized (bigMap) {
if(bigMap.get(allocatedObj) == null) {
bigMap.put(allocatedObj, new FieldStateList(),objectID);
}
else{
System.err.println("object is already in the table!");
}
}
// String allocatedSite;
// synchronized (objectTable) {
// if((allocatedSite = (String)objectTable.get(objectID)) == null)
// objectTable.put(objectID,allocSite);
// }
}
catch(Throwable t){
t.printStackTrace();
......@@ -106,17 +87,7 @@ public class ImmutabilityAnalysis {
}
String objectID = getObjectID(accessedObj, null);
FieldState fs = getOrCreateInstanceField(accessedObj, objectID, accessedFieldName);
// if(accessedObj instanceof java.io.ByteArrayInputStream) {
// System.out.println("~~~~~~ " + accessedObj.getClass().getName() + "\t" + System.identityHashCode(accessedObj) + "\t" + os.toString());
// new Exception().printStackTrace();
// }
fs.onWrite(isInDynamicExtendOfConstructor);
// if(accessedObj instanceof java.io.ByteArrayInputStream) {
// System.out.println("~~~~~~~~~~~~~~ " + os.toString() + "\t" + Thread.currentThread().getName() + "\t" + Thread.currentThread().getId());
// }
}
catch(Throwable t){
t.printStackTrace();
......@@ -132,25 +103,22 @@ public class ImmutabilityAnalysis {
}
private static FieldState getOrCreateInstanceField(Object accessedObj, String objectId, String fieldName) {
int idx = System.identityHashCode(accessedObj) % NUMBER_OF_TABLES;
try{
ConcurrentHashMap<String, FieldState> smallMap;
FieldState fs;
synchronized(bigMap[idx]){
if((smallMap = bigMap[idx].get(accessedObj)) == null) {
bigMap[idx].put(accessedObj, smallMap = new ConcurrentHashMap<String, FieldState>(),objectId);
// if(accessedObj instanceof String)
// new Exception().printStackTrace();
FieldStateList smallMap;
FieldState fs = null;
synchronized(bigMap){
if((smallMap = bigMap.get(accessedObj)) == null) {
bigMap.put(accessedObj, smallMap = new FieldStateList(), objectId);
}
}
if((fs = smallMap.get(fieldName)) == null) {
FieldState oldFS;
if((oldFS = smallMap.putIfAbsent(fieldName, fs = new FieldState())) != null) {
fs = oldFS;
synchronized(smallMap) {
if((fs = smallMap.get(fieldName)) == null) {
fs = smallMap.put(fieldName);
}
}
return fs;
}
catch(Throwable t){
......@@ -159,42 +127,7 @@ public class ImmutabilityAnalysis {
}
}
// private static FieldState createInstanceField(Object accessedObj, String fieldName) {
// try{
// FieldState fs;
// int idx = System.identityHashCode(accessedObj) % NUMBER_OF_TABLES;
// synchronized(instanceFields[idx]) {
// /** SANITY CHECK **/
// if(instanceFields[idx].get(accessedObj) != null) {
// System.err.println("[ImmutabilityAnalysis.createInstanceField] ERROR1!");
// }
// /** END_OF: SANITY CHECK **/
// instanceFields[idx].put(accessedObj, fs = new FieldState(fieldName + " [" + System.identityHashCode(accessedObj) + "]"));
// }
// return fs;
// }
// catch(Throwable t){
// t.printStackTrace();
// return null;
// }
// }
// private static FieldState getInstanceField(Object accessedObj) {
// FieldState os;
// int idx = System.identityHashCode(accessedObj) % NUMBER_OF_TABLES;
// synchronized(instanceFields[idx]) {
// os = (FieldState)instanceFields[idx].get(accessedObj);
// }
// /** SANITY CHECK **/
// if(os == null) {
// System.err.println("[ImmutabilityAnalysis.getInstanceField] ERROR2!");
// System.err.println("OBJ: " + accessedObj.getClass().getName() + "\t" + accessedObj);
// new Exception().printStackTrace();
// System.exit(-1);
// }
// /** END_OF: SANITY CHECK **/
// return os;
// }
public static boolean isIncluded(Stack<Object> stack, Object accessedObject) {
for(Iterator<Object> iter = stack.iterator(); iter.hasNext();) {
......@@ -206,17 +139,14 @@ public class ImmutabilityAnalysis {
}
private static String getObjectID(Object accessedObj, String allocSite) {
return System.identityHashCode(accessedObj) + ":" + accessedObj.getClass().getName() + ":" + allocSite;// + ((accessedObj instanceof Thread && allocSite == null) ? ":" + ((Thread)accessedObj).getName() : "");
return System.identityHashCode(accessedObj) + ":" + accessedObj.getClass().getName() + ":" + allocSite;
}
private static void dump() {
try {
for(int i=0;i<NUMBER_OF_TABLES;i++){
synchronized(bigMap[i]) {
bigMap[i].dump();
}
synchronized(bigMap) {
bigMap.dump();
}
} catch(Throwable e) {
e.printStackTrace();
}
......
package ch.usi.dag.disl.example.fieldsImmutabilityAnalysis.runtime;
import java.io.PrintStream;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import ch.usi.dag.disl.example.fieldsImmutabilityAnalysis.runtime.MyWeakKeyIdentityHashMap.EntryDumper;
public class MyDumper implements EntryDumper<MyWeakReference<Object>, ConcurrentHashMap<String, FieldState>>{
private static final String OFFSET = "\t\t";
private static final String SEPARATOR = "\t";
public class MyDumper implements EntryDumper<MyWeakReference<Object>, FieldStateList>{
private final PrintStream ps;
public MyDumper(PrintStream ps) {
......@@ -20,12 +14,10 @@ public class MyDumper implements EntryDumper<MyWeakReference<Object>, Concurrent
}
@Override
public synchronized void dumpEntry(MyWeakReference<Object> key, ConcurrentHashMap<String, FieldState> value) {
public synchronized void dumpEntry(MyWeakReference<Object> key, FieldStateList value) {
String shortId = (String) key.objectID.subSequence(key.objectID.indexOf(":")+1, key.objectID.length());
ps.println(shortId);
for(Entry<String, FieldState> entry : value.entrySet()) {
ps.println(OFFSET + entry.getKey() + SEPARATOR + entry.getValue().toString());
}
value.dump(ps);
ps.flush();
}
......@@ -33,6 +25,5 @@ public class MyDumper implements EntryDumper<MyWeakReference<Object>, Concurrent
public void close() {
ps.flush();
ps.close();
}
}
......@@ -153,39 +153,6 @@ public final class MyWeakKeyIdentityHashMap<K,V> {
return null;
}
// /**
// * Removes the mapping for this key from this map if present.
// *
// * The implementation is NOT thread safe.
// *
// * @param key key whose mapping is to be removed from the map
// * @return the previous value associated with <tt>key</tt>, or
// * <tt>null</tt> if there was no mapping for <tt>key</tt>.
// * (A <tt>null</tt> return can also indicate that the map
// * previously associated <tt>null</tt> with <tt>key</tt>.)
// */
// public V remove(Object key) {
// Object[] tab = table;
// int index = hash(key, currentSize);
//
// MyWeakReference<K> item;
// while (true) {
// item = (MyWeakReference<K>)tab[index];
// if (item.get() == key) {
// size--;
// V oldValue = (V)tab[index + 1];
// tab[index + 1] = null;
// tab[index] = null;
// //TODO: to be implemented
// closeDeletion(index);
// return oldValue;
// }
// if (item == null)
// return null;
// index = nextKeyIndex(index, currentSize);
// }
// }
/**
* Dumps all entries using the registered dumper.
*
......@@ -270,13 +237,6 @@ public final class MyWeakKeyIdentityHashMap<K,V> {
return registerEntryDumper(null);
}
// /**
// * This method has not been implemented.
// * Throws a RuntimeException.
// */
// public Set<java.util.Map.Entry<K, V>> entrySet() {
// throw new RuntimeException("Method " + this.getClass().getName() + ".entrySet() has not been implemented");
// }
@SuppressWarnings("hiding")
public interface EntryDumper<MyWeakReference, V> {
......
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