start of new file
[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     int activeTime;
19     
20     public CoreSimulator(RuntimeSchedule schedule, int coreNum) {
21         super();
22         reset();
23         this.rSchedule = schedule;
24         this.coreNum = coreNum;
25     }
26     
27     public CoreSimulator(int coreNum) {
28         super();
29         reset();
30         this.coreNum = coreNum;
31     }
32     
33     public void reset() {
34         this.activeTime = 0;
35         this.tasks = null;
36         this.targetCSimulator = null;
37         this.targetFState = null;
38         this.rSchedule = null;
39         this.rtask = null;
40     }
41     
42     public void deployTasks(Vector<TaskDescriptor> tasks) {
43         if(tasks == null) {
44             return;
45         }
46         
47         if(this.tasks == null) {
48             this.tasks = new Vector<TaskSimulator>();
49         } else {
50             this.tasks.clear();
51         }
52         
53         for(int i = 0; i < tasks.size(); i++) {
54             TaskDescriptor td = tasks.elementAt(i);
55             this.tasks.add(new TaskSimulator(td, this));
56         }
57     }
58     
59     public Queue<Integer> getTargetCores(FlagState fstate) {
60         if(targetCSimulator == null) {
61             return null;
62         }
63         return targetCSimulator.get(fstate);
64     }
65
66     public void setTargetCSimulator(Hashtable<FlagState, Queue<Integer>> targetCSimulator) {
67         this.targetCSimulator = targetCSimulator;
68     }
69     
70     public Vector<Integer> getAllyCores(FlagState fstate) {
71         if(allyCSimulator == null) {
72             return null;
73         }
74         return allyCSimulator.get(fstate);
75     }
76
77     public void setAllyCSimulator(Hashtable<FlagState, Vector<Integer>> allyCSimulator) {
78         this.allyCSimulator = allyCSimulator;
79     }
80     
81     public FlagState getTargetFState(FlagState fstate) {
82         if(targetFState == null) {
83             return fstate;
84         }
85         return targetFState.get(fstate);
86     }
87
88     public void setTargetFState(Hashtable<FlagState, FlagState> targetFState) {
89         this.targetFState = targetFState;
90     }
91
92     public int getActiveTime() {
93         return activeTime;
94     }
95
96     public int getCoreNum() {
97         return coreNum;
98     }
99     
100     public Vector<TaskSimulator> getTasks() {
101         return tasks;
102     }
103
104     public RuntimeSchedule getRSchedule() {
105         return rSchedule;
106     }
107
108     public void setRSchedule(RuntimeSchedule schedule) {
109         rSchedule = schedule;
110     }
111
112     public TaskSimulator getRtask() {
113         return rtask;
114     }
115
116     public void addObject(ObjectSimulator newObj) {
117         if(this.tasks == null) {
118             return;
119         }
120         for(int i = 0; i < this.tasks.size(); i++) {
121             this.tasks.elementAt(i).enquePara(newObj, null, 0, true);
122         }
123     }
124     
125     public void addObject(ObjectSimulator newObj, FlagState fs, int version) {
126         if(this.tasks == null) {
127             return;
128         }
129         for(int i = 0; i < this.tasks.size(); i++) {
130             this.tasks.elementAt(i).enquePara(newObj, fs, version, false);
131         }
132     }
133     
134     public Vector<ObjectSimulator> finishTask() {
135         assert(this.rtask != null);
136
137         Vector<ObjectSimulator> transObjs = null;
138         Vector<Queue<ObjectSimulator>> paraQueues = this.rtask.getParaQueues();
139         for(int i = 0; i < paraQueues.size(); i++) {
140             ObjectSimulator obj = paraQueues.elementAt(i).poll();
141             obj.setHold(false);
142             boolean remove = false;
143             if((this.targetFState != null) && (this.targetFState.containsKey(obj.getCurrentFS()))) {
144                 if(transObjs == null) {
145                     transObjs = new Vector<ObjectSimulator>();
146                 }
147                 if(!transObjs.contains(obj)) {
148                     transObjs.add(obj);
149                 }
150                 remove = true;
151             }
152             // check if this object becoming shared or not
153             Vector<Integer> allycores = this.getAllyCores(obj.getCurrentFS());
154             if(allycores != null) {
155                 obj.setShared(true);
156                 for(int k = 0; k < allycores.size(); ++k) {
157                     Integer allyCore = allycores.elementAt(k);
158                     if(transObjs == null) {
159                         transObjs = new Vector<ObjectSimulator>();
160                     }
161                     if(!transObjs.contains(obj)) {
162                         transObjs.add(obj);
163                     }
164                     remove = false; 
165                 }
166             }
167             for(int j = 0; j < this.tasks.size(); j++) {
168                 this.tasks.elementAt(j).refreshPara(obj, remove);
169             }
170         }
171         this.activeTime += this.rtask.getCurrentRun().getFinishTime();
172         this.rtask.finish();
173         this.rtask = null;
174         return transObjs;
175     }
176     
177     public void updateTask(int time) {
178         this.activeTime += time;
179         this.rtask.updateFinishTime(time);
180     }
181
182     public TaskSimulator process() {
183         TaskSimulator next = rSchedule.schedule(tasks);
184         this.rtask = next;
185         return next;
186     }
187     
188 }