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 long getFinishTime() {
41 public void setFinishTime(long 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,
77 this.paraQueues = null;
78 this.objVersionTbl = null;
79 this.currentRun = null;
84 public CoreSimulator getCs() {
88 public TaskDescriptor getTd() {
92 public ExeResult getCurrentRun() {
96 public Vector<Queue<ObjectSimulator>> getParaQueues() {
100 public Hashtable<ObjectSimulator, Integer> getObjVersionTbl() {
101 return objVersionTbl;
104 public int getObjVersion(ObjectSimulator os) {
105 return this.objVersionTbl.get(os).intValue();
108 public void enquePara(ObjectSimulator obj,
112 ClassDescriptor cd = obj.getCd();
113 int paraNum = td.numParameters();
114 for(int i = 0; i < paraNum; i++) {
115 VarDescriptor para = td.getParameter(i);
116 if(cd.equals(para.getType().getClassDesc())) {
117 // check if the status is right
118 FlagExpressionNode fen = td.getFlag(para);
121 cfs = obj.getCurrentFS();
123 if(SchedulingUtil.isTaskTrigger_flag(fen, cfs)) {
124 if(this.paraQueues == null) {
125 this.paraQueues = new Vector<Queue<ObjectSimulator>>();
126 for(int j = 0; j < paraNum; j++) {
127 this.paraQueues.add(null);
130 if(this.paraQueues.elementAt(i) == null) {
131 this.paraQueues.setElementAt(new LinkedList<ObjectSimulator>(), i);
133 if(this.objVersionTbl == null) {
134 this.objVersionTbl = new Hashtable<ObjectSimulator, Integer>();
136 if(!this.paraQueues.elementAt(i).contains(obj)) {
137 this.paraQueues.elementAt(i).add(obj);
139 this.objVersionTbl.put(obj, obj.getVersion());
141 this.objVersionTbl.put(obj, version);
149 public void refreshPara(ObjectSimulator obj,
151 ClassDescriptor cd = obj.getCd();
152 int paraNum = td.numParameters();
153 for(int i = 0; i < paraNum; i++) {
154 VarDescriptor para = td.getParameter(i);
155 if(cd.equals(para.getType().getClassDesc())) {
157 if((this.paraQueues != null) &&
158 (this.paraQueues.elementAt(i) != null) &&
159 (this.paraQueues.elementAt(i).contains(obj))) {
160 this.paraQueues.elementAt(i).remove(obj);
161 this.objVersionTbl.remove(obj);
164 // check if the status is right
165 FlagExpressionNode fen = td.getFlag(para);
166 if(SchedulingUtil.isTaskTrigger_flag(fen, obj.getCurrentFS())) {
167 if(this.paraQueues == null) {
168 this.paraQueues = new Vector<Queue<ObjectSimulator>>();
169 for(int j = 0; j < paraNum; j++) {
170 this.paraQueues.add(null);
173 if(this.paraQueues.elementAt(i) == null) {
174 this.paraQueues.setElementAt(new LinkedList<ObjectSimulator>(), i);
176 this.paraQueues.elementAt(i).add(obj);
177 if(this.objVersionTbl == null) {
178 this.objVersionTbl = new Hashtable<ObjectSimulator, Integer>();
180 this.objVersionTbl.put(obj, obj.getVersion());
182 if((this.paraQueues != null) &&
183 (this.paraQueues.elementAt(i) != null) &&
184 (this.paraQueues.elementAt(i).contains(obj))) {
185 this.paraQueues.elementAt(i).remove(obj);
186 this.objVersionTbl.remove(obj);
194 public void process() {
201 if(this.currentRun == null) {
202 this.currentRun = new ExeResult();
204 this.currentRun.init();
208 // According to runtime statistic information, decide the execution path of this task this time.
209 // Mainly following things:
210 // 1.the result, i.e. the result FlagState reached by each parameter.
214 // First check if all the parameters are still available.
215 // For shared objects, need to first grab the lock and also check if the version is right
216 for(int i = 0; i < paraQueues.size(); i++) {
217 ObjectSimulator tpara = paraQueues.elementAt(i).peek();
219 // the parameter is already removed, delete this task too
221 this.currentRun.setFinishTime(finishTime);
222 this.currentRun.setExetype(2);
223 for(int j = 0; j < i; ++j) {
224 tpara = this.paraQueues.elementAt(j).poll();
225 if(tpara.isShared() && tpara.isHold()) {
226 tpara.setHold(false);
228 this.paraQueues.elementAt(j).add(tpara);
232 if(tpara.isShared()) {
234 // shared object held by other tasks
235 finishTime = 800; // TODO currenly assume the effort on requesting locks are only 800
236 /*this.currentRun.setFinishTime(finishTime);
237 this.currentRun.setExetype(1);
238 paraQueues.elementAt(i).poll();
239 paraQueues.elementAt(i).add(tpara);
240 for(int j = 0; j < i; ++j) {
241 tpara = this.paraQueues.elementAt(j).poll();
242 if(tpara.isShared() && tpara.isHold()) {
243 tpara.setHold(false);
245 this.paraQueues.elementAt(j).add(tpara);
248 this.currentRun.setFinishTime(finishTime);
249 this.currentRun.setExetype(2);
250 paraQueues.elementAt(i).poll();
251 // remove this object from the remaining parameter queues
252 for(int j = i + 1; j < paraQueues.size(); j++) {
253 paraQueues.elementAt(j).remove(tpara);
255 for(int j = 0; j < i; ++j) {
256 tpara = this.paraQueues.elementAt(j).poll();
257 if(tpara.isShared() && tpara.isHold()) {
258 tpara.setHold(false);
260 this.paraQueues.elementAt(j).add(tpara);
263 } else if (tpara.getVersion() != this.objVersionTbl.get(tpara)) {
264 // shared object has been updated and no longer fitted to this task
265 finishTime = 800; // TODO currenly assume the effort on requesting locks are only 800
266 this.currentRun.setFinishTime(finishTime);
267 this.currentRun.setExetype(2);
268 paraQueues.elementAt(i).poll();
269 // remove this object from the remaining parameter queues
270 for(int j = i + 1; j < paraQueues.size(); j++) {
271 paraQueues.elementAt(j).remove(tpara);
273 for(int j = 0; j < i; ++j) {
274 tpara = this.paraQueues.elementAt(j).poll();
275 if(tpara.isShared() && tpara.isHold()) {
276 tpara.setHold(false);
278 this.paraQueues.elementAt(j).add(tpara);
285 // remove this object from the remaining parameter queues
286 for(int j = i + 1; j < paraQueues.size(); j++) {
287 paraQueues.elementAt(j).remove(tpara);
291 for(int i = 0; i < paraQueues.size(); i++) {
292 ObjectSimulator tpara = paraQueues.elementAt(i).peek();
294 FlagState tfstate = tpara.getCurrentFS();
295 FEdge toexecute = tfstate.process(td);
296 finishTime += toexecute.getExeTime();
299 ftime = toexecute.getExeTime();
300 } else if(ftime != toexecute.getExeTime()) {
301 //System.err.println("error for simulation: " + td.getSymbol());
304 /*if(td.getSymbol().equals("addIYLM")) {
305 System.err.println("# " + i + " time: " + toexecute.getExeTime());
307 if((toexecute.getNewObjInfoHashtable() != null)
308 && (toexecute.getNewObjInfoHashtable().size() > 0)) {
310 Iterator it = toexecute.getNewObjInfoHashtable().keySet().iterator();
311 int invokeNum = toexecute.getInvokeNum();
312 while(it.hasNext()) {
313 ClassDescriptor cd = (ClassDescriptor)it.next();
314 NewObjInfo noi = toexecute.getNewObjInfo(cd);
315 if(noi.getInvokeNum() < ((int)Math.round(((noi.getProbability() / 100) * noi.getNewRate() * invokeNum)))) {
316 for(int j = 0; j < noi.getNewRate(); j++) {
317 ObjectSimulator tmpObj = new ObjectSimulator(cd, noi.getRoot());
318 this.currentRun.addNewObj(tmpObj);
324 tpara.applyEdge(toexecute);
325 tpara.increaseVersion();
327 finishTime /= paraQueues.size();
329 /*if(td.getSymbol().equals("addIYLM")) {
330 System.err.println("total time: " + finishTime);
331 System.err.println("=====");
333 this.currentRun.setFinishTime(finishTime);
334 this.currentRun.setExetype(0);
337 public void updateFinishTime(long time) {
338 this.currentRun.setFinishTime(this.currentRun.finishTime - time);
342 public void finish() {