1 package Analysis.Scheduling;
3 import java.util.Hashtable;
4 import java.util.Iterator;
5 import java.util.LinkedList;
6 import java.util.Queue;
7 import java.util.Vector;
9 import Analysis.TaskStateAnalysis.FEdge;
10 import Analysis.TaskStateAnalysis.FlagState;
11 import Analysis.TaskStateAnalysis.FEdge.NewObjInfo;
12 import IR.ClassDescriptor;
13 import IR.TaskDescriptor;
14 import IR.VarDescriptor;
15 import IR.Tree.FlagExpressionNode;
17 public class TaskSimulator {
19 Vector<Queue<ObjectSimulator>> paraQueues;
20 Hashtable<ObjectSimulator, Integer> objVersionTbl;
25 public class ExeResult {
27 Vector<ObjectSimulator> newObjs;
28 int exetype; // 0--normal executing
29 // 1--abort due to fail on grabbing locks
30 // 2--out of date task
37 public int getFinishTime() {
41 public void setFinishTime(int finishTime) {
42 this.finishTime = finishTime;
45 public Vector<ObjectSimulator> getNewObjs() {
49 public void addNewObj(ObjectSimulator newObj) {
50 if(this.newObjs == null) {
51 this.newObjs = new Vector<ObjectSimulator>();
54 this.newObjs.add(newObj);
57 public int getExetype() {
61 public void setExetype(int exetype) {
62 this.exetype = exetype;
73 public TaskSimulator(TaskDescriptor td, CoreSimulator cs) {
76 this.paraQueues = null;
77 this.objVersionTbl = null;
78 this.currentRun = null;
83 public CoreSimulator getCs() {
87 public TaskDescriptor getTd() {
91 public ExeResult getCurrentRun() {
95 public Vector<Queue<ObjectSimulator>> getParaQueues() {
99 public Hashtable<ObjectSimulator, Integer> getObjVersionTbl() {
100 return objVersionTbl;
103 public int getObjVersion(ObjectSimulator os) {
104 return this.objVersionTbl.get(os).intValue();
107 public void enquePara(ObjectSimulator obj, FlagState fs, int version, boolean inherent) {
108 ClassDescriptor cd = obj.getCd();
109 int paraNum = td.numParameters();
110 for(int i = 0; i < paraNum; i++) {
111 VarDescriptor para = td.getParameter(i);
112 if(cd.equals(para.getType().getClassDesc())) {
113 // check if the status is right
114 FlagExpressionNode fen = td.getFlag(para);
117 cfs = obj.getCurrentFS();
119 if(SchedulingUtil.isTaskTrigger_flag(fen, cfs)) {
120 if(this.paraQueues == null) {
121 this.paraQueues = new Vector<Queue<ObjectSimulator>>();
122 for(int j = 0; j < paraNum; j++) {
123 this.paraQueues.add(null);
126 if(this.paraQueues.elementAt(i) == null) {
127 this.paraQueues.setElementAt(new LinkedList<ObjectSimulator>(), i);
129 if(this.objVersionTbl == null) {
130 this.objVersionTbl = new Hashtable<ObjectSimulator, Integer>();
132 if(!this.paraQueues.elementAt(i).contains(obj)) {
133 this.paraQueues.elementAt(i).add(obj);
135 this.objVersionTbl.put(obj, obj.getVersion());
137 this.objVersionTbl.put(obj, version);
145 public void refreshPara(ObjectSimulator obj, boolean remove) {
146 ClassDescriptor cd = obj.getCd();
147 int paraNum = td.numParameters();
148 for(int i = 0; i < paraNum; i++) {
149 VarDescriptor para = td.getParameter(i);
150 if(cd.equals(para.getType().getClassDesc())) {
152 if((this.paraQueues != null) &&
153 (this.paraQueues.elementAt(i) != null) &&
154 (this.paraQueues.elementAt(i).contains(obj))) {
155 this.paraQueues.elementAt(i).remove(obj);
156 this.objVersionTbl.remove(obj);
159 // check if the status is right
160 FlagExpressionNode fen = td.getFlag(para);
161 if(SchedulingUtil.isTaskTrigger_flag(fen, obj.getCurrentFS())) {
162 if(this.paraQueues == null) {
163 this.paraQueues = new Vector<Queue<ObjectSimulator>>();
164 for(int j = 0; j < paraNum; j++) {
165 this.paraQueues.add(null);
168 if(this.paraQueues.elementAt(i) == null) {
169 this.paraQueues.setElementAt(new LinkedList<ObjectSimulator>(), i);
171 this.paraQueues.elementAt(i).add(obj);
172 if(this.objVersionTbl == null) {
173 this.objVersionTbl = new Hashtable<ObjectSimulator, Integer>();
175 this.objVersionTbl.put(obj, obj.getVersion());
177 if((this.paraQueues != null) &&
178 (this.paraQueues.elementAt(i) != null) &&
179 (this.paraQueues.elementAt(i).contains(obj))) {
180 this.paraQueues.elementAt(i).remove(obj);
181 this.objVersionTbl.remove(obj);
189 public void process() {
196 if(this.currentRun == null) {
197 this.currentRun = new ExeResult();
199 this.currentRun.init();
203 // According to runtime statistic information, decide the execution path of this task this time.
204 // Mainly following things:
205 // 1.the result, i.e. the result FlagState reached by each parameter.
209 // First check if all the parameters are still available.
210 // For shared objects, need to first grab the lock and also check if the version is right
211 for(int i = 0; i < paraQueues.size(); i++) {
212 ObjectSimulator tpara = paraQueues.elementAt(i).peek();
213 if(tpara.isShared()) {
215 // shared object held by other tasks
216 finishTime = 800; // TODO currenly assume the effort on requesting locks are only 800
217 /*this.currentRun.setFinishTime(finishTime);
218 this.currentRun.setExetype(1);
219 paraQueues.elementAt(i).poll();
220 paraQueues.elementAt(i).add(tpara);
221 for(int j = 0; j < i; ++j) {
222 tpara = this.paraQueues.elementAt(j).poll();
223 if(tpara.isShared() && tpara.isHold()) {
224 tpara.setHold(false);
226 this.paraQueues.elementAt(j).add(tpara);
229 this.currentRun.setFinishTime(finishTime);
230 this.currentRun.setExetype(2);
231 paraQueues.elementAt(i).poll();
232 // remove this object from the remaining parameter queues
233 for(int j = i + 1; j < paraQueues.size(); j++) {
234 paraQueues.elementAt(j).remove(tpara);
236 for(int j = 0; j < i; ++j) {
237 tpara = this.paraQueues.elementAt(j).poll();
238 if(tpara.isShared() && tpara.isHold()) {
239 tpara.setHold(false);
241 this.paraQueues.elementAt(j).add(tpara);
244 } else if (tpara.getVersion() != this.objVersionTbl.get(tpara)) {
245 // shared object has been updated and no longer fitted to this task
246 finishTime = 800; // TODO currenly assume the effort on requesting locks are only 800
247 this.currentRun.setFinishTime(finishTime);
248 this.currentRun.setExetype(2);
249 paraQueues.elementAt(i).poll();
250 // remove this object from the remaining parameter queues
251 for(int j = i + 1; j < paraQueues.size(); j++) {
252 paraQueues.elementAt(j).remove(tpara);
254 for(int j = 0; j < i; ++j) {
255 tpara = this.paraQueues.elementAt(j).poll();
256 if(tpara.isShared() && tpara.isHold()) {
257 tpara.setHold(false);
259 this.paraQueues.elementAt(j).add(tpara);
266 // remove this object from the remaining parameter queues
267 for(int j = i + 1; j < paraQueues.size(); j++) {
268 paraQueues.elementAt(j).remove(tpara);
271 for(int i = 0; i < paraQueues.size(); i++) {
272 ObjectSimulator tpara = paraQueues.elementAt(i).peek();
274 FlagState tfstate = tpara.getCurrentFS();
275 FEdge toexecute = tfstate.process(td);
276 finishTime += toexecute.getExeTime();
277 if((toexecute.getNewObjInfoHashtable() != null) && (toexecute.getNewObjInfoHashtable().size() > 0)) {
279 Iterator it = toexecute.getNewObjInfoHashtable().keySet().iterator();
280 int invokeNum = toexecute.getInvokeNum();
281 while(it.hasNext()) {
282 ClassDescriptor cd = (ClassDescriptor)it.next();
283 NewObjInfo noi = toexecute.getNewObjInfo(cd);
284 if(noi.getInvokeNum() < ((int)Math.round(((noi.getProbability() / 100) * noi.getNewRate() * invokeNum)))) {
285 for(int j = 0; j < noi.getNewRate(); j++) {
286 ObjectSimulator tmpObj = new ObjectSimulator(cd, noi.getRoot());
287 this.currentRun.addNewObj(tmpObj);
293 tpara.applyEdge(toexecute);
294 tpara.increaseVersion();
296 finishTime /= paraQueues.size();
297 this.currentRun.setFinishTime(finishTime);
298 this.currentRun.setExetype(0);
301 public void updateFinishTime(int time) {
302 this.currentRun.setFinishTime(this.currentRun.finishTime - time);
306 public void finish() {