1 package Analysis.TaskStateAnalysis;
3 import Analysis.Scheduling.ScheduleEdge;
4 import Analysis.TaskStateAnalysis.*;
10 import Util.GraphNode;
12 /** This class is used to hold the flag states that a class in the Bristlecone
13 * program can exist in, during runtime.
15 public class FlagState extends GraphNode implements Cloneable {
16 public static final int ONETAG=1;
17 public static final int NOTAGS=0;
18 public static final int MULTITAGS=-1;
19 public static final int MAXTIME=10;
22 private static int nodeid=0;
24 private final HashSet flagstate;
25 private final ClassDescriptor cd;
26 private final Hashtable<TagDescriptor,Integer> tags;
27 private boolean issourcenode;
29 public static final int KLIMIT=2;
32 // for static scheduling
33 private int executeTime;
34 private int visited4time;
35 private int invokeNum;
36 // for building multicore codes
38 private int checkmask;
39 private boolean setmask;
41 //private boolean isolate;
42 //private Vector<ScheduleEdge> allys;
45 * Creates a new flagstate with all flags set to false.
46 * @param cd ClassDescriptor
48 public FlagState(ClassDescriptor cd) {
49 this.flagstate=new HashSet();
51 this.tags=new Hashtable<TagDescriptor,Integer>();
52 this.uid=FlagState.nodeid++;
53 this.issourcenode=false;
54 this.executeTime = -1;
55 this.visited4time = -1;
61 //this.isolate = true;
66 * Creates a new flagstate with flags set according to the HashSet.
67 * If the flag exists in the hashset, it's set to true else set to false.
68 * @param cd ClassDescriptor
69 * @param flagstate a <CODE>HashSet</CODE> containing FlagDescriptors
71 private FlagState(HashSet flagstate, ClassDescriptor cd,Hashtable<TagDescriptor,Integer> tags) {
72 this.flagstate=flagstate;
75 this.uid=FlagState.nodeid++;
76 this.issourcenode=false;
77 this.executeTime = -1;
78 this.visited4time = -1;
86 public int getiuid() {
90 public boolean isSetmask() {
94 public void setSetmask(boolean setmask) {
95 this.setmask = setmask;
99 * @param fd FlagDescriptor
100 * @return true if the flagstate contains fd else false.
102 public boolean get(FlagDescriptor fd) {
103 return flagstate.contains(fd);
106 /** Checks if the flagstate is a source node.
107 * @return true if the flagstate is a sourcenode(i.e. Is the product of an allocation site).
110 public boolean isSourceNode(){
114 /** Sets the flagstate as a source node.
116 public void setAsSourceNode(){
119 this.tasks=new Vector();
123 public void addAllocatingTask(TaskDescriptor task){
127 public Vector getAllocatingTasks(){
132 public String toString() {
133 return cd.toString()+getTextLabel();
136 /** @return Iterator over the flags in the flagstate.
139 public Iterator getFlags() {
140 return flagstate.iterator();
143 public int numFlags(){
144 return flagstate.size();
147 public FlagState[] setTag(TagDescriptor tag, boolean set){
148 HashSet newset1=(HashSet)flagstate.clone();
149 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
153 if (tags.containsKey(tag))
154 count=tags.get(tag).intValue();
157 newtags1.put(tag, new Integer(count));
158 return new FlagState[] {new FlagState(newset1, cd, newtags1)};
161 if (tags.containsKey(tag))
162 count=tags.get(tag).intValue();
163 newtags1.put(tag, new Integer(count));
164 if ((count+1)==KLIMIT)
165 return new FlagState[] {this, new FlagState(newset1, cd, newtags1)};
167 return new FlagState[] {new FlagState(newset1, cd, newtags1)};
171 public FlagState[] setTag(TagDescriptor tag){
172 HashSet newset1=(HashSet)flagstate.clone();
173 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
175 if (tags.containsKey(tag)){
176 //Code could try to remove flag that doesn't exist
178 switch (tags.get(tag).intValue()){
180 newtags1.put(tag,new Integer(MULTITAGS));
181 return new FlagState[] {this, new FlagState(newset1, cd, newtags1)};
183 return new FlagState[] {this};
188 newtags1.put(tag,new Integer(ONETAG));
189 return new FlagState[] {new FlagState(newset1,cd,newtags1)};
193 public int getTagCount(TagDescriptor tag) {
194 if (tags.containsKey(tag))
195 return tags.get(tag).intValue();
199 public int getTagCount(String tagtype){
200 return getTagCount(new TagDescriptor(tagtype));
203 public FlagState[] clearTag(TagDescriptor tag){
204 if (tags.containsKey(tag)){
205 switch(tags.get(tag).intValue()){
207 HashSet newset=(HashSet)flagstate.clone();
208 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
210 return new FlagState[]{new FlagState(newset,cd,newtags)};
213 //two possibilities - count remains 2 or becomes 1
215 HashSet newset1=(HashSet)flagstate.clone();
216 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
219 HashSet newset2=(HashSet)flagstate.clone();
220 Hashtable<TagDescriptor,Integer> newtags2=(Hashtable<TagDescriptor,Integer>)tags.clone();
221 newtags1.put(tag,new Integer(ONETAG));
222 return new FlagState[] {new FlagState(newset1, cd, newtags2),
223 new FlagState(newset2, cd, newtags2)};
228 throw new Error("Invalid Operation: Can not clear a tag that doesn't exist.");
232 /** Creates a string description of the flagstate
233 * e.g. a flagstate with five flags could look like 01001
234 * @param flags an array of flagdescriptors.
235 * @return string representation of the flagstate.
237 public String toString(FlagDescriptor[] flags)
239 StringBuffer sb = new StringBuffer(flagstate.size());
240 for(int i=0;i < flags.length; i++)
248 return new String(sb);
252 * @return returns the classdescriptor of the flagstate.
255 public ClassDescriptor getClassDescriptor(){
259 /** Sets the status of a specific flag in a flagstate after cloning it.
260 * @param fd FlagDescriptor of the flag whose status is being set.
261 * @param status boolean value
262 * @return the new flagstate with <CODE>fd</CODE> set to <CODE>status</CODE>.
265 public FlagState setFlag(FlagDescriptor fd, boolean status) {
266 HashSet newset=(HashSet) flagstate.clone();
267 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
270 else if (newset.contains(fd)){
274 return new FlagState(newset, cd, newtags);
277 /** Tests for equality of two flagstate objects.
280 public boolean equals(Object o) {
281 if (o instanceof FlagState) {
282 FlagState fs=(FlagState)o;
285 return (fs.flagstate.equals(flagstate) & fs.tags.equals(tags));
290 public int hashCode() {
291 return cd.hashCode()^flagstate.hashCode()^tags.hashCode();
294 public String getLabel() {
298 public String getTextLabel() {
300 for(Iterator it=getFlags();it.hasNext();) {
301 FlagDescriptor fd=(FlagDescriptor) it.next();
305 label+=", "+fd.toString();
307 for (Enumeration en_tags=getTags();en_tags.hasMoreElements();){
308 TagDescriptor td=(TagDescriptor)en_tags.nextElement();
309 switch (tags.get(td).intValue()){
312 label=td.toString()+"(1)";
314 label+=", "+td.toString()+"(1)";
318 label=td.toString()+"(n)";
320 label+=", "+td.toString()+"(n)";
331 public Enumeration getTags(){
335 public int getExeTime() {
337 if(this.executeTime == -1) {
338 if(this.visited4time == -1) {
339 this.visited4time = 0;
342 // visited, this node is in a loop
344 // currently set 10 as the largest time
345 this.executeTime = FlagState.MAXTIME;
348 } catch (Exception e) {
352 return this.executeTime;
355 public void setExeTime(int exeTime) {
356 this.executeTime = exeTime;
359 public int getAndmask() {
363 public void setAndmask(int andmask) {
364 this.andmask = andmask;
367 public int getCheckmask() {
371 public void setCheckmask(int checkmask) {
372 this.checkmask = checkmask;
375 public void calExeTime() throws Exception {
376 Iterator it = this.edges();
378 FEdge fe = (FEdge)it.next();
379 while((fe != null) && (fe.getTarget().equals(this))) {
381 fe = (FEdge)it.next();
387 this.executeTime = 0;
389 if(fe.getExeTime() == -1) {
390 throw new Exception("Error: Uninitiate FEdge!");
392 this.executeTime = fe.getExeTime() + ((FlagState)fe.getTarget()).getExeTime();
395 this.executeTime = 0;
397 while(it.hasNext()) {
398 FEdge fe = (FEdge)it.next();
399 int temp = fe.getExeTime() + ((FlagState)fe.getTarget()).getExeTime();
400 if(temp < this.executeTime) {
401 this.executeTime = temp;
406 public Object clone() {
409 o = (FlagState)super.clone();
410 } catch(CloneNotSupportedException e){
413 o.uid = FlagState.nodeid++;
414 o.edges = new Vector();
415 for(int i = 0; i < this.edges.size(); i++) {
416 o.edges.addElement(this.edges.elementAt(i));
418 o.inedges = new Vector();
419 for(int i = 0; i < this.inedges.size(); i++) {
420 o.inedges.addElement(this.inedges.elementAt(i));
425 public void init4Simulate() {
429 public FEdge process(TaskDescriptor td) {
432 // refresh all the expInvokeNum of each edge
433 for(int i = 0; i < this.edges.size(); i++) {
434 next = (FEdge)this.edges.elementAt(i);
435 next.setExpInvokeNum((int)Math.round(this.invokeNum * (next.getProbability() / 100)));
438 // find the one with the biggest gap between its actual invoke time and the expected invoke time
439 // and associated with task td
442 for(int i = 0; i < this.edges.size(); i++) {
443 int temp = ((FEdge)this.edges.elementAt(index)).getInvokeNumGap();
444 if((temp > gap) && (next.getTask().equals(td))){
449 next = (FEdge)this.edges.elementAt(index);
455 /*public Vector<ScheduleEdge> getAllys() {
459 public void addAlly(ScheduleEdge se) {
460 if(this.allys == null) {
461 assert(this.isolate == true);
462 this.isolate = false;
463 this.allys = new Vector<ScheduleEdge>();
465 this.allys.addElement(se);
468 public boolean isIsolate() {