X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;ds=sidebyside;f=Robust%2Fsrc%2FAnalysis%2FTaskStateAnalysis%2FSafetyAnalysis.java;fp=Robust%2Fsrc%2FAnalysis%2FTaskStateAnalysis%2FSafetyAnalysis.java;h=0000000000000000000000000000000000000000;hb=refs%2Ftags%2Fbuildscript;hp=220843ef4407921439328c7b6eee376297801e55;hpb=84e434cf05530d0b929a849e5c54da93d595381a;p=IRC.git diff --git a/Robust/src/Analysis/TaskStateAnalysis/SafetyAnalysis.java b/Robust/src/Analysis/TaskStateAnalysis/SafetyAnalysis.java deleted file mode 100644 index 220843ef..00000000 --- a/Robust/src/Analysis/TaskStateAnalysis/SafetyAnalysis.java +++ /dev/null @@ -1,568 +0,0 @@ -package Analysis.TaskStateAnalysis; -import java.util.*; -import IR.*; -import IR.Tree.*; -import IR.Flat.*; -import java.io.*; -import java.io.File; -import java.io.FileWriter; -import java.io.FileOutputStream; -import Util.Edge; - -public class SafetyAnalysis { - private Hashtable executiongraph; - private Hashtable>> safeexecution; //to use to build code - private State state; - private TaskAnalysis taskanalysis; - private Hashtable> optionaltaskdescriptors; - private Hashtable>> fstotimap; - - private ClassDescriptor processedclass; - - public Hashtable>> getResult() { - return safeexecution; - } - - public Hashtable> getOptionalTaskDescriptors() { - return optionaltaskdescriptors; - } - - /* Structure that stores a possible optional task which would be - safe to execute and the possible flagstates the object could be - in before executing the task during an execution without - failure*/ - - /*Constructor*/ - public SafetyAnalysis(Hashtable executiongraph, State state, TaskAnalysis taskanalysis) { - this.executiongraph = executiongraph; - this.safeexecution = new Hashtable(); - this.state = state; - this.taskanalysis = taskanalysis; - this.optionaltaskdescriptors = new Hashtable(); - this.fstotimap=new Hashtable>>(); - } - - /* Builds map of fs -> EGTasknodes that can fire on fs for class cd */ - - private Hashtable> buildMap(ClassDescriptor cd) { - Hashtable> table=new Hashtable>(); - for(Iterator it=((Set)executiongraph.get(cd)).iterator(); it.hasNext();) { - EGTaskNode node=(EGTaskNode)it.next(); - if (node.getFS()!=null) { - if (!table.containsKey(node.getFS())) - table.put(node.getFS(), new HashSet()); - table.get(node.getFS()).add(node); - } - } - return table; - } - - - public Set getOptions(FlagState fs, TaskDescriptor td, int index) { - return fstotimap.get(fs).get(new TaskIndex(td, index)); - } - - public Set getOptions(FlagState fs, TaskIndex ti) { - return fstotimap.get(fs).get(ti); - } - - public Set getTaskIndex(FlagState fs) { - return fstotimap.get(fs).keySet(); - } - - - /* Builds map of fs -> set of fs that depend on this fs */ - - private Hashtable> buildUseMap(ClassDescriptor cd) { - Hashtable> table=new Hashtable>(); - for(Iterator it=((Set)executiongraph.get(cd)).iterator(); it.hasNext();) { - EGTaskNode node=(EGTaskNode)it.next(); - if (node.getFS()!=null) { - if (!table.containsKey(node.getPostFS())) - table.put(node.getPostFS(), new HashSet()); - table.get(node.getPostFS()).add(node.getFS()); - } - } - return table; - } - - public void doAnalysis() { - Enumeration classit=taskanalysis.flagstates.keys(); - - while (classit.hasMoreElements()) { - ClassDescriptor cd=(ClassDescriptor)classit.nextElement(); - if (!executiongraph.containsKey(cd)) - continue; - Hashtable> fstootd=new Hashtable>(); - safeexecution.put(cd, fstootd); - - optionaltaskdescriptors.put(cd, new Hashtable()); - - Hashtable> fstoegmap=buildMap(cd); - Hashtable> fsusemap=buildUseMap(cd); - - HashSet tovisit=new HashSet(); - tovisit.addAll(taskanalysis.getFlagStates(cd)); - - while(!tovisit.isEmpty()) { - FlagState fs=tovisit.iterator().next(); - tovisit.remove(fs); - if (!fstoegmap.containsKey(fs)) - continue; //This FS has no task that can trigger on it - Set nodeset=fstoegmap.get(fs); - analyzeFS(fs, nodeset, fstootd, fsusemap, tovisit); - } - } - printTEST(); - } - - public void analyzeFS(FlagState fs, Set egset, Hashtable> fstootd, Hashtable> fsusemap, HashSet tovisit) { - Hashtable> timap=new Hashtable>(); - Set tiselfloops=new HashSet(); - - for(Iterator egit=egset.iterator(); egit.hasNext();) { - EGTaskNode egnode=egit.next(); - Set setotd; - if (egnode.isOptional()) { - setotd=new HashSet(); - HashSet enterfsset=new HashSet(); - enterfsset.add(fs); - ClassDescriptor cd=fs.getClassDescriptor(); - OptionalTaskDescriptor newotd=new OptionalTaskDescriptor(egnode.getTD(), egnode.getIndex(), enterfsset, new Predicate()); - if(optionaltaskdescriptors.get(cd).containsKey(newotd)) { - newotd = optionaltaskdescriptors.get(cd).get(newotd); - } else { - newotd.setuid(); - resultingFS(newotd); - optionaltaskdescriptors.get(cd).put(newotd, newotd); - } - setotd.add(newotd); - } else if (tagChange(egnode)) { - //Conservatively handle tag changes - setotd=new HashSet(); - } else if(egnode.isMultipleParams()) { - if( goodMultiple(egnode)) { - Predicate p=returnPredicate(egnode); - Set oldsetotd; - if (fstootd.containsKey(egnode.getPostFS())) - oldsetotd=fstootd.get(egnode.getPostFS()); - else - oldsetotd=new HashSet(); - setotd=new HashSet(); - for(Iterator otdit=oldsetotd.iterator(); otdit.hasNext();) { - OptionalTaskDescriptor oldotd=otdit.next(); - Predicate newp=combinePredicates(oldotd.predicate, p); - OptionalTaskDescriptor newotd=new OptionalTaskDescriptor(oldotd.td, oldotd.getIndex(), oldotd.enterflagstates, newp); - ClassDescriptor cd=fs.getClassDescriptor(); - if(optionaltaskdescriptors.get(cd).containsKey(newotd)) { - newotd = optionaltaskdescriptors.get(cd).get(newotd); - } else { - newotd.setuid(); - resultingFS(newotd); - optionaltaskdescriptors.get(cd).put(newotd, newotd); - } - setotd.add(newotd); - } - } else { - //Can't propagate anything - setotd=new HashSet(); - } - } else { - if (fstootd.containsKey(egnode.getPostFS())) - setotd=fstootd.get(egnode.getPostFS()); - else - setotd=new HashSet(); - } - TaskIndex ti=egnode.isRuntime() ? new TaskIndex() : new TaskIndex(egnode.getTD(), egnode.getIndex()); - if (!ti.runtime) { - //runtime edges don't do anything...don't have to take - //them, can't predict when we can. - if (state.selfloops.contains(egnode.getTD().getSymbol())) { - System.out.println("Self loop for: "+egnode.getTD()+" "+egnode.getIndex()); - if (timap.containsKey(ti)) { - if (egnode.getPostFS()!=fs) { - if (tiselfloops.contains(ti)) { - //dump old self loop - timap.put(ti, setotd); - tiselfloops.remove(ti); - } else { - //standard and case - timap.put(ti, createIntersection(timap.get(ti), setotd, fs.getClassDescriptor())); - } - } - } else { - //mark as self loop - timap.put(ti, setotd); - if (egnode.getPostFS()==fs) { - tiselfloops.add(ti); - } - } - } else if (timap.containsKey(ti)) { - //AND case - timap.put(ti, createIntersection(timap.get(ti), setotd, fs.getClassDescriptor())); - } else { - timap.put(ti, setotd); - } - } - } - - //Combine all options - HashSet set=new HashSet(); - for(Iterator> it=timap.values().iterator(); it.hasNext();) { - Set otdset=it.next(); - set.addAll(otdset); - } - - if (!fstootd.containsKey(fs)|| - !fstootd.get(fs).equals(set)) { - fstootd.put(fs, set); - //Requeue all flagstates that may use our updated results - if (fsusemap.containsKey(fs)) { - tovisit.addAll(fsusemap.get(fs)); - } - } - fstotimap.put(fs, timap); - } - - private HashSet createIntersection(Set A, Set B, ClassDescriptor cd) { - HashSet result = new HashSet(); - for(Iterator b_it = B.iterator(); b_it.hasNext();) { - OptionalTaskDescriptor otd_b = (OptionalTaskDescriptor)b_it.next(); - for(Iterator a_it = A.iterator(); a_it.hasNext();) { - OptionalTaskDescriptor otd_a = (OptionalTaskDescriptor)a_it.next(); - if(otd_a.td==otd_b.td&& - otd_a.getIndex()==otd_b.getIndex()) { - HashSet newfs = new HashSet(); - newfs.addAll(otd_a.enterflagstates); - newfs.addAll(otd_b.enterflagstates); - OptionalTaskDescriptor newotd = new OptionalTaskDescriptor(otd_b.td, otd_b.getIndex(), newfs, combinePredicates(otd_a.predicate, otd_b.predicate)); - if(optionaltaskdescriptors.get(cd).get(newotd)!=null) { - newotd = optionaltaskdescriptors.get(cd).get(newotd); - } else { - newotd.setuid(); - resultingFS(newotd); - optionaltaskdescriptors.get(cd).put(newotd, newotd); - } - result.add(newotd); - } - } - } - return result; - } - - // This method returns true if the only parameter whose flag is - // modified is the tracked one - - private boolean goodMultiple(EGTaskNode tn) { - TaskDescriptor td = tn.getTD(); - FlatMethod fm = state.getMethodFlat(td); - TempDescriptor tmp=fm.getParameter(tn.getIndex()); - - Set nodeset=fm.getNodeSet(); - - for(Iterator nodeit=nodeset.iterator(); nodeit.hasNext();) { - FlatNode fn=nodeit.next(); - if (fn.kind()==FKind.FlatFlagActionNode) { - FlatFlagActionNode ffan=(FlatFlagActionNode)fn; - if (ffan.getTaskType() == FlatFlagActionNode.TASKEXIT) { - for(Iterator it_tfp=ffan.getTempFlagPairs(); it_tfp.hasNext();) { - TempFlagPair tfp=(TempFlagPair)it_tfp.next(); - TempDescriptor tempd = tfp.getTemp(); - if(tempd!=tmp) - return false; //return false if a taskexit modifies one of the other parameters - } - } - } - } - return true; - } - - private Predicate returnPredicate(EGTaskNode tn) { - Predicate result = new Predicate(); - TaskDescriptor td = tn.getTD(); - for(int i=0; i flaglist = new HashSet(); - flaglist.add(td.getFlag(vd)); - result.flags.put(vd, flaglist); - if (td.getTag(vd)!=null) - result.tags.put(vd, td.getTag(vd)); - } - } - return result; - } - - private Predicate combinePredicates(Predicate A, Predicate B) { - Predicate result = new Predicate(); - result.vardescriptors.addAll(A.vardescriptors); - result.flags.putAll(A.flags); - result.tags.putAll(A.tags); - Collection c = B.vardescriptors; - for(Iterator varit = c.iterator(); varit.hasNext();) { //maybe change that - VarDescriptor vd = (VarDescriptor)varit.next(); - if(result.vardescriptors.contains(vd)) - System.out.println("Already in "); - else { - result.vardescriptors.add(vd); - } - } - Collection vardesc = result.vardescriptors; - for(Iterator varit = vardesc.iterator(); varit.hasNext();) { - VarDescriptor vd = (VarDescriptor)varit.next(); - HashSet bflags = B.flags.get(vd); - if( bflags == null ) { - continue; - } else { - if (result.flags.containsKey(vd)) - ((HashSet)result.flags.get(vd)).addAll(bflags); - else - result.flags.put(vd, bflags); - } - TagExpressionList btags = B.tags.get(vd); - if( btags != null ) { - if (result.tags.containsKey(vd)) - System.out.println("Tag found but there should be nothing to do because same tag"); - else - result.tags.put(vd, btags); - } - } - return result; - } - - //////////////////// - /* returns a set of the possible sets of flagstates - resulting from the execution of the optional task. - To do it with have to look for TaskExit FlatNodes - in the IR. - */ - private void resultingFS(OptionalTaskDescriptor otd) { - Stack stack = new Stack(); - HashSet result = new HashSet(); - FlatMethod fm = state.getMethodFlat((TaskDescriptor)otd.td); - FlatNode fn = (FlatNode)fm; - - Stack nodestack=new Stack(); - HashSet discovered=new HashSet(); - nodestack.push(fm); - discovered.add(fm); - TempDescriptor temp=fm.getParameter(otd.getIndex()); - - //Iterating through the nodes - while(!nodestack.isEmpty()) { - FlatNode fn1 = (FlatNode) nodestack.pop(); - if (fn1.kind()==FKind.FlatFlagActionNode) { - FlatFlagActionNode ffan=(FlatFlagActionNode)fn1; - if (ffan.getTaskType() == FlatFlagActionNode.TASKEXIT) { - HashSet tempset = new HashSet(); - for(Iterator it_fs = otd.enterflagstates.iterator(); it_fs.hasNext();) { - FlagState fstemp = (FlagState)it_fs.next(); - Vector processed=new Vector(); - - for(Iterator it_tfp=ffan.getTempFlagPairs(); it_tfp.hasNext();) { - TempFlagPair tfp=(TempFlagPair)it_tfp.next(); - if (tfp.getTemp()==temp) - fstemp=fstemp.setFlag(tfp.getFlag(),ffan.getFlagChange(tfp)); - } - - processed.add(fstemp); - //Process clears first - - for(Iterator it_ttp=ffan.getTempTagPairs(); it_ttp.hasNext();) { - TempTagPair ttp=(TempTagPair)it_ttp.next(); - - if (temp==ttp.getTemp()) { - Vector oldprocess=processed; - processed=new Vector(); - - for (Enumeration en=oldprocess.elements(); en.hasMoreElements();) { - FlagState fsworking=(FlagState)en.nextElement(); - if (!ffan.getTagChange(ttp)) { - processed.addAll(Arrays.asList(fsworking.clearTag(ttp.getTag()))); - } else processed.add(fsworking); - } - } - } - //Process sets next - for(Iterator it_ttp=ffan.getTempTagPairs(); it_ttp.hasNext();) { - TempTagPair ttp=(TempTagPair)it_ttp.next(); - - if (temp==ttp.getTemp()) { - Vector oldprocess=processed; - processed=new Vector(); - - for (Enumeration en=oldprocess.elements(); en.hasMoreElements();) { - FlagState fsworking=(FlagState)en.nextElement(); - if (ffan.getTagChange(ttp)) { - processed.addAll(Arrays.asList(fsworking.setTag(ttp.getTag()))); - } else processed.add(fsworking); - } - } - } - //Add to exit states - tempset.addAll(processed); - } - result.add(tempset); - continue; // avoid queueing the return node if reachable - } - } else if (fn1.kind()==FKind.FlatReturnNode) { - result.add(otd.enterflagstates); - } - - /* Queue other nodes past this one */ - for(int i=0; i "+tn2.getLabel()+";"); - } - } - } -}