switch to spaces only..
[IRC.git] / Robust / src / Analysis / Scheduling / CoreSimulator.java
1 package Analysis.Scheduling;
2
3 import java.util.Hashtable;
4 import java.util.Queue;
5 import java.util.Vector;
6
7 import Analysis.TaskStateAnalysis.FlagState;
8 import IR.TaskDescriptor;
9
10 public class CoreSimulator {
11   Vector<TaskSimulator> tasks;
12   RuntimeSchedule rSchedule;
13   TaskSimulator rtask;
14   Hashtable<FlagState, Queue<Integer>> targetCSimulator;
15   Hashtable<FlagState, Vector<Integer>> allyCSimulator;
16   Hashtable<FlagState, FlagState> targetFState;
17   int coreNum;
18   long activeTime;
19
20   public CoreSimulator(RuntimeSchedule schedule,
21                        int coreNum) {
22     super();
23     reset();
24     this.rSchedule = schedule;
25     this.coreNum = coreNum;
26   }
27
28   public CoreSimulator(int coreNum) {
29     super();
30     reset();
31     this.coreNum = coreNum;
32   }
33
34   public void reset() {
35     this.activeTime = 0;
36     this.tasks = null;
37     this.targetCSimulator = null;
38     this.targetFState = null;
39     this.rSchedule = null;
40     this.rtask = null;
41   }
42
43   public void deployTasks(Vector<TaskDescriptor> tasks) {
44     if(tasks == null) {
45       return;
46     }
47
48     if(this.tasks == null) {
49       this.tasks = new Vector<TaskSimulator>();
50     } else {
51       this.tasks.clear();
52     }
53
54     for(int i = 0; i < tasks.size(); i++) {
55       TaskDescriptor td = tasks.elementAt(i);
56       this.tasks.add(new TaskSimulator(td, this));
57     }
58   }
59
60   public Queue<Integer> getTargetCores(FlagState fstate) {
61     if(targetCSimulator == null) {
62       return null;
63     }
64     return targetCSimulator.get(fstate);
65   }
66
67   public void setTargetCSimulator(Hashtable<FlagState,
68                                   Queue<Integer>> targetCSimulator) {
69     this.targetCSimulator = targetCSimulator;
70   }
71
72   public Vector<Integer> getAllyCores(FlagState fstate) {
73     if(allyCSimulator == null) {
74       return null;
75     }
76     return allyCSimulator.get(fstate);
77   }
78
79   public void setAllyCSimulator(Hashtable<FlagState,
80                                 Vector<Integer>> allyCSimulator) {
81     this.allyCSimulator = allyCSimulator;
82   }
83
84   public FlagState getTargetFState(FlagState fstate) {
85     if(targetFState == null) {
86       return fstate;
87     }
88     return targetFState.get(fstate);
89   }
90
91   public void setTargetFState(Hashtable<FlagState, FlagState> targetFState) {
92     this.targetFState = targetFState;
93   }
94
95   public long getActiveTime() {
96     return activeTime;
97   }
98
99   public int getCoreNum() {
100     return coreNum;
101   }
102
103   public Vector<TaskSimulator> getTasks() {
104     return tasks;
105   }
106
107   public RuntimeSchedule getRSchedule() {
108     return rSchedule;
109   }
110
111   public void setRSchedule(RuntimeSchedule schedule) {
112     rSchedule = schedule;
113   }
114
115   public TaskSimulator getRtask() {
116     return rtask;
117   }
118
119   public void addObject(ObjectSimulator newObj) {
120     if(this.tasks == null) {
121       return;
122     }
123     for(int i = 0; i < this.tasks.size(); i++) {
124       this.tasks.elementAt(i).enquePara(newObj, null, 0, true);
125     }
126   }
127
128   public void addObject(ObjectSimulator newObj,
129                         FlagState fs,
130                         int version) {
131     if(this.tasks == null) {
132       return;
133     }
134     for(int i = 0; i < this.tasks.size(); i++) {
135       this.tasks.elementAt(i).enquePara(newObj, fs, version, false);
136     }
137   }
138
139   public Vector<ObjectSimulator> finishTask() {
140     assert(this.rtask != null);
141
142     Vector<ObjectSimulator> transObjs = null;
143     if(this.rtask.currentRun.getExetype() == 0) {
144       Vector<Queue<ObjectSimulator>> paraQueues = this.rtask.getParaQueues();
145       for(int i = 0; i < paraQueues.size(); i++) {
146         ObjectSimulator obj = paraQueues.elementAt(i).poll();
147         obj.setHold(false);
148         boolean remove = false;
149         if((this.targetFState != null)
150            && (this.targetFState.containsKey(obj.getCurrentFS()))) {
151           if(transObjs == null) {
152             transObjs = new Vector<ObjectSimulator>();
153           }
154           if(!transObjs.contains(obj)) {
155             transObjs.add(obj);
156           }
157           remove = true;
158         }
159         // check if this object becoming shared or not
160         Vector<Integer> allycores = this.getAllyCores(obj.getCurrentFS());
161         if(allycores != null) {
162           obj.setShared(true);
163           //for(int k = 0; k < allycores.size(); ++k) {
164           //Integer allyCore = allycores.elementAt(k);
165           if(transObjs == null) {
166             transObjs = new Vector<ObjectSimulator>();
167           }
168           if(!transObjs.contains(obj)) {
169             transObjs.add(obj);
170           }
171           remove = false;
172           //}
173           allycores = null;
174         }
175         // check if need to transfer to other cores
176         Queue<Integer> targetcores = this.getTargetCores(obj.getCurrentFS());
177         if(targetcores != null) {
178           if(transObjs == null) {
179             transObjs = new Vector<ObjectSimulator>();
180           }
181           if(!transObjs.contains(obj)) {
182             transObjs.add(obj);
183           }
184           remove = true;
185         }
186         for(int j = 0; j < this.tasks.size(); j++) {
187           this.tasks.elementAt(j).refreshPara(obj, remove);
188         }
189       }
190       paraQueues = null;
191     }
192     this.activeTime += this.rtask.getCurrentRun().getFinishTime();
193     this.rtask.finish();
194     this.rtask = null;
195     return transObjs;
196   }
197
198   public void updateTask(long time) {
199     this.activeTime += time;
200     this.rtask.updateFinishTime(time);
201   }
202
203   public TaskSimulator process() {
204     TaskSimulator next = rSchedule.schedule(tasks);
205     this.rtask = next;
206     return next;
207   }
208
209 }