add scheduling simulator
[IRC.git] / Robust / src / Analysis / Scheduling / TaskSimulator.java
1 package Analysis.Scheduling;
2
3 import java.util.Iterator;
4 import java.util.LinkedList;
5 import java.util.Queue;
6 import java.util.Vector;
7
8 import Analysis.TaskStateAnalysis.FEdge;
9 import Analysis.TaskStateAnalysis.FlagState;
10 import Analysis.TaskStateAnalysis.FEdge.NewObjInfo;
11 import IR.ClassDescriptor;
12 import IR.TaskDescriptor;
13 import IR.VarDescriptor;
14 import IR.Tree.FlagExpressionNode;
15
16 public class TaskSimulator {
17     TaskDescriptor td;
18     Vector<Queue<ObjectSimulator>> paraQueues;
19     ExeResult currentRun;
20     CoreSimulator cs;
21     boolean finish;
22     
23     public class ExeResult {
24         int finishTime;
25         Vector<ObjectSimulator> newObjs;
26         
27         public ExeResult() {
28             finishTime = 0;
29             newObjs = null;
30         }
31
32         public int getFinishTime() {
33             return finishTime;
34         }
35
36         public void setFinishTime(int finishTime) {
37             this.finishTime = finishTime;
38         }
39
40         public Vector<ObjectSimulator> getNewObjs() {
41             return newObjs;
42         }
43
44         public void addNewObj(ObjectSimulator newObj) {
45             if(this.newObjs == null) {
46                 this.newObjs = new Vector<ObjectSimulator>();
47             }
48             
49             this.newObjs.add(newObj);
50         }
51         
52     }
53     
54     public TaskSimulator(TaskDescriptor td, CoreSimulator cs) {
55         super();
56         this.td = td;
57         this.paraQueues = null;
58         this.currentRun = null;
59         this.cs = cs;
60         this.finish = true;
61     }
62     
63     public CoreSimulator getCs() {
64         return cs;
65     }
66
67     public TaskDescriptor getTd() {
68         return td;
69     }
70
71     public ExeResult getCurrentRun() {
72         return currentRun;
73     }
74
75     public Vector<Queue<ObjectSimulator>> getParaQueues() {
76         return paraQueues;
77     }
78
79     public void enquePara(ObjectSimulator obj) {
80         ClassDescriptor cd = obj.getCd();
81         int paraNum = td.numParameters();
82         for(int i = 0; i < paraNum; i++) {
83             VarDescriptor para = td.getParameter(i);
84             if(cd.equals(para.getType().getClassDesc())) {
85                 // check if the status is right
86                 FlagExpressionNode fen = td.getFlag(para);
87                 if(SchedulingUtil.isTaskTrigger_flag(fen, obj.getCurrentFS())) {
88                     if(this.paraQueues == null) {
89                         this.paraQueues = new Vector<Queue<ObjectSimulator>>();
90                         for(int j = 0; j < paraNum; j++) {
91                             this.paraQueues.add(null);
92                         }
93                     }
94                     if(this.paraQueues.elementAt(i) == null) {
95                         this.paraQueues.setElementAt(new LinkedList<ObjectSimulator>(), i);
96                     }
97                     this.paraQueues.elementAt(i).add(obj);
98                 }
99             }
100         }
101     }
102     
103     public void refreshPara(ObjectSimulator obj, boolean remove) {
104         ClassDescriptor cd = obj.getCd();
105         int paraNum = td.numParameters();
106         for(int i = 0; i < paraNum; i++) {
107             VarDescriptor para = td.getParameter(i);
108             if(cd.equals(para.getType().getClassDesc())) {
109                 if(remove) {
110                     if((this.paraQueues != null) &&
111                             (this.paraQueues.elementAt(i) != null)  && 
112                             (this.paraQueues.elementAt(i).contains(obj))){
113                         this.paraQueues.elementAt(i).remove(obj);
114                     }
115                 } else {
116                     // check if the status is right
117                     FlagExpressionNode fen = td.getFlag(para);
118                     if(SchedulingUtil.isTaskTrigger_flag(fen, obj.getCurrentFS())) {
119                         if(this.paraQueues == null) {
120                             this.paraQueues = new Vector<Queue<ObjectSimulator>>();
121                             for(int j = 0; j < paraNum; j++) {
122                                 this.paraQueues.add(null);
123                             }
124                         }
125                         if(this.paraQueues.elementAt(i) == null) {
126                             this.paraQueues.setElementAt(new LinkedList<ObjectSimulator>(), i);
127                         }
128                         this.paraQueues.elementAt(i).add(obj);
129                     } else {
130                         if((this.paraQueues != null) &&
131                                 (this.paraQueues.elementAt(i) != null)  && 
132                                 (this.paraQueues.elementAt(i).contains(obj))){
133                             this.paraQueues.elementAt(i).remove(obj);
134                         }
135                     }
136                 }
137             }
138         }
139     }
140
141     public void process() {
142         if(!finish) {
143             return;
144         } else {
145             finish = false;
146         }
147         
148         if(this.currentRun == null) {
149             this.currentRun = new ExeResult();
150         }
151         
152         int finishTime = 0;
153         // According to runtime statistic information, decide the execution path of this task this time.
154         // Mainly following things:
155         //    1.the result, i.e. the result FlagState reached by each parameter.
156         //    2.the finish time
157         //    3.any new objects
158         for(int i = 0; i < paraQueues.size(); i++) {
159             ObjectSimulator tpara = paraQueues.elementAt(i).peek();
160             // remove this object from the remaining parameter queues
161             for(int j = i + 1; j < paraQueues.size(); j++) {
162                 paraQueues.elementAt(j).remove(tpara);
163             }
164             FlagState tfstate = tpara.getCurrentFS();
165             FEdge toexecute = tfstate.process(td);
166             finishTime += toexecute.getExeTime();
167             if((toexecute.getNewObjInfoHashtable() != null) && (toexecute.getNewObjInfoHashtable().size() > 0)) {
168                 // have new objects
169                 Iterator it = toexecute.getNewObjInfoHashtable().keySet().iterator();
170                 int invokeNum = toexecute.getInvokeNum();
171                 while(it.hasNext()) {
172                     ClassDescriptor cd = (ClassDescriptor)it.next();
173                     NewObjInfo noi = toexecute.getNewObjInfo(cd);
174                     if(noi.getInvokeNum() < ((int)Math.round(((noi.getProbability() / 100) * noi.getNewRate() * invokeNum)))) {
175                         for(int j = 0; j < noi.getNewRate(); j++) { 
176                             ObjectSimulator tmpObj = new ObjectSimulator(cd, noi.getRoot());
177                             this.currentRun.addNewObj(tmpObj);
178                             noi.incInvokeNum();
179                         }
180                     }
181                 }
182             }
183             //FlagState tFState = (FlagState)toexecute.getTarget();
184             //tpara.setCurrentFS(tFState);
185             tpara.applyEdge(toexecute);
186         }
187         finishTime /= paraQueues.size();
188         this.currentRun.setFinishTime(finishTime);
189     }
190     
191     public void updateFinishTime(int time) {
192         this.currentRun.setFinishTime(this.currentRun.finishTime - time);
193         finish = false;
194     }
195     
196     public void finish() {
197         this.finish = true;
198     }
199 }