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;
66 public TaskSimulator(TaskDescriptor td, CoreSimulator cs) {
69 this.paraQueues = null;
70 this.objVersionTbl = null;
71 this.currentRun = null;
76 public CoreSimulator getCs() {
80 public TaskDescriptor getTd() {
84 public ExeResult getCurrentRun() {
88 public Vector<Queue<ObjectSimulator>> getParaQueues() {
92 public Hashtable<ObjectSimulator, Integer> getObjVersionTbl() {
96 public int getObjVersion(ObjectSimulator os) {
97 return this.objVersionTbl.get(os).intValue();
100 public void enquePara(ObjectSimulator obj, FlagState fs, int version, boolean inherent) {
101 ClassDescriptor cd = obj.getCd();
102 int paraNum = td.numParameters();
103 for(int i = 0; i < paraNum; i++) {
104 VarDescriptor para = td.getParameter(i);
105 if(cd.equals(para.getType().getClassDesc())) {
106 // check if the status is right
107 FlagExpressionNode fen = td.getFlag(para);
110 cfs = obj.getCurrentFS();
112 if(SchedulingUtil.isTaskTrigger_flag(fen, cfs)) {
113 if(this.paraQueues == null) {
114 this.paraQueues = new Vector<Queue<ObjectSimulator>>();
115 for(int j = 0; j < paraNum; j++) {
116 this.paraQueues.add(null);
119 if(this.paraQueues.elementAt(i) == null) {
120 this.paraQueues.setElementAt(new LinkedList<ObjectSimulator>(), i);
122 if(this.objVersionTbl == null) {
123 this.objVersionTbl = new Hashtable<ObjectSimulator, Integer>();
125 if(!this.paraQueues.elementAt(i).contains(obj)) {
126 this.paraQueues.elementAt(i).add(obj);
128 this.objVersionTbl.put(obj, obj.getVersion());
130 this.objVersionTbl.put(obj, version);
138 public void refreshPara(ObjectSimulator obj, boolean remove) {
139 ClassDescriptor cd = obj.getCd();
140 int paraNum = td.numParameters();
141 for(int i = 0; i < paraNum; i++) {
142 VarDescriptor para = td.getParameter(i);
143 if(cd.equals(para.getType().getClassDesc())) {
145 if((this.paraQueues != null) &&
146 (this.paraQueues.elementAt(i) != null) &&
147 (this.paraQueues.elementAt(i).contains(obj))) {
148 this.paraQueues.elementAt(i).remove(obj);
149 this.objVersionTbl.remove(obj);
152 // check if the status is right
153 FlagExpressionNode fen = td.getFlag(para);
154 if(SchedulingUtil.isTaskTrigger_flag(fen, obj.getCurrentFS())) {
155 if(this.paraQueues == null) {
156 this.paraQueues = new Vector<Queue<ObjectSimulator>>();
157 for(int j = 0; j < paraNum; j++) {
158 this.paraQueues.add(null);
161 if(this.paraQueues.elementAt(i) == null) {
162 this.paraQueues.setElementAt(new LinkedList<ObjectSimulator>(), i);
164 this.paraQueues.elementAt(i).add(obj);
165 if(this.objVersionTbl == null) {
166 this.objVersionTbl = new Hashtable<ObjectSimulator, Integer>();
168 this.objVersionTbl.put(obj, obj.getVersion());
170 if((this.paraQueues != null) &&
171 (this.paraQueues.elementAt(i) != null) &&
172 (this.paraQueues.elementAt(i).contains(obj))){
173 this.paraQueues.elementAt(i).remove(obj);
174 this.objVersionTbl.remove(obj);
182 public void process() {
189 if(this.currentRun == null) {
190 this.currentRun = new ExeResult();
194 // According to runtime statistic information, decide the execution path of this task this time.
195 // Mainly following things:
196 // 1.the result, i.e. the result FlagState reached by each parameter.
200 // First check if all the parameters are still available.
201 // For shared objects, need to first grab the lock and also check if the version is right
202 for(int i = 0; i < paraQueues.size(); i++) {
203 ObjectSimulator tpara = paraQueues.elementAt(i).peek();
204 if(tpara.isShared()) {
206 // shared object held by other tasks
207 finishTime = 1; // TODO currenly assume the effort on requesting locks are only 1
208 this.currentRun.setFinishTime(finishTime);
209 this.currentRun.setExetype(1);
210 paraQueues.elementAt(i).poll();
211 paraQueues.elementAt(i).add(tpara);
212 for(int j = 0; j < i; ++j) {
213 tpara = this.paraQueues.elementAt(j).poll();
214 if(tpara.isShared() && tpara.isHold()) {
215 tpara.setHold(false);
217 this.paraQueues.elementAt(j).add(tpara);
220 } else if (tpara.getVersion() != this.objVersionTbl.get(tpara)) {
221 // shared object has been updated and no longer fitted to this task
222 finishTime = 1; // TODO currenly assume the effort on requesting locks are only 1
223 this.currentRun.setFinishTime(finishTime);
224 this.currentRun.setExetype(2);
225 paraQueues.elementAt(i).poll();
226 // remove this object from the remaining parameter queues
227 for(int j = i + 1; j < paraQueues.size(); j++) {
228 paraQueues.elementAt(j).remove(tpara);
230 for(int j = 0; j < i; ++j) {
231 tpara = this.paraQueues.elementAt(j).poll();
232 if(tpara.isShared() && tpara.isHold()) {
233 tpara.setHold(false);
235 this.paraQueues.elementAt(j).add(tpara);
242 // remove this object from the remaining parameter queues
243 for(int j = i + 1; j < paraQueues.size(); j++) {
244 paraQueues.elementAt(j).remove(tpara);
247 for(int i = 0; i < paraQueues.size(); i++) {
248 ObjectSimulator tpara = paraQueues.elementAt(i).peek();
250 FlagState tfstate = tpara.getCurrentFS();
251 FEdge toexecute = tfstate.process(td);
252 finishTime += toexecute.getExeTime();
253 if((toexecute.getNewObjInfoHashtable() != null) && (toexecute.getNewObjInfoHashtable().size() > 0)) {
255 Iterator it = toexecute.getNewObjInfoHashtable().keySet().iterator();
256 int invokeNum = toexecute.getInvokeNum();
257 while(it.hasNext()) {
258 ClassDescriptor cd = (ClassDescriptor)it.next();
259 NewObjInfo noi = toexecute.getNewObjInfo(cd);
260 if(noi.getInvokeNum() < ((int)Math.round(((noi.getProbability() / 100) * noi.getNewRate() * invokeNum)))) {
261 for(int j = 0; j < noi.getNewRate(); j++) {
262 ObjectSimulator tmpObj = new ObjectSimulator(cd, noi.getRoot());
263 this.currentRun.addNewObj(tmpObj);
269 tpara.applyEdge(toexecute);
270 tpara.increaseVersion();
272 finishTime /= paraQueues.size();
273 this.currentRun.setFinishTime(finishTime);
274 this.currentRun.setExetype(0);
277 public void updateFinishTime(int time) {
278 this.currentRun.setFinishTime(this.currentRun.finishTime - time);
282 public void finish() {