source: DCWoRMS/trunk/src/schedframe/resources/units/ProcessingElements.java @ 477

Revision 477, 7.0 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package schedframe.resources.units;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Iterator;
6import java.util.List;
7import java.util.ListIterator;
8
9import schedframe.resources.ResourceStatus;
10import schedframe.resources.ResourceType;
11import schedframe.resources.computing.ComputingResource;
12
13public class ProcessingElements extends PEUnit implements List<ComputingResource> {
14
15        protected List<ComputingResource> resources;
16
17        public ProcessingElements(){
18                super();
19                resources =  new ArrayList<ComputingResource>(1);
20                provisioner = new ProcessingElementsResourceUnitProvisioner(ResourceUnitState.FREE, 0);
21        }
22       
23        public ProcessingElements(String resName){
24                this();
25                resourceId = resName;
26        }
27       
28        public ProcessingElements(List<ComputingResource> resources){
29                this();
30                this.resources = resources;
31        }
32       
33        public ProcessingElements(String resName, List<ComputingResource> resources){
34                this(resName);
35                this.resources = resources;
36        }
37
38        public ResourceType getResourceType(){
39                if(resources != null && resources.size() > 0)
40                        return resources.get(0).getType();
41                else return null;
42        }
43       
44        public int getAmount(){
45                return this.resources.size();
46        }
47       
48        public int getSpeed(){
49                int peCnt = getAmount();
50               
51                double avgSpeed = 0;
52                for(int i = 0; i < peCnt; i++){
53                        try {
54                                avgSpeed += resources.get(i).getResourceCharacteristic().getResourceUnit(StandardResourceUnitName.CPUSPEED).getAmount();
55                        } catch (NoSuchFieldException e) {
56                                // TODO Auto-generated catch block
57                                e.printStackTrace();
58                        }
59                }
60                avgSpeed = avgSpeed / peCnt;
61                int speed = (int) Math.round(avgSpeed);
62                return speed;
63        }
64
65        @Override
66        public boolean add(ComputingResource e) {
67                if(resources == null)
68                        resources = new ArrayList<ComputingResource>(1);
69                return resources.add(e);
70
71        }
72
73        @Override
74        public void add(int index, ComputingResource element) {
75                if(resources == null)
76                        resources = new ArrayList<ComputingResource>(1);
77                resources.add(index, element);
78        }
79
80        @Override
81        public boolean addAll(Collection<? extends ComputingResource> c) {
82                if(resources == null)
83                        resources = new ArrayList<ComputingResource>(c.size());
84                return resources.addAll(c);
85        }
86
87        @Override
88        public boolean addAll(int index, Collection<? extends ComputingResource> c) {
89                return resources.addAll(index, c);
90        }
91
92        @Override
93        public void clear() {
94                // TODO Auto-generated method stub
95
96        }
97
98        @Override
99        public boolean contains(Object o) {
100                return resources.contains(o);
101        }
102
103        @Override
104        public boolean containsAll(Collection<?> c) {
105                return resources.containsAll(c);
106        }
107
108        @Override
109        public ComputingResource get(int index) {
110                return resources.get(index);
111        }
112
113        @Override
114        public int indexOf(Object o) {
115                return resources.indexOf(o);
116        }
117
118        @Override
119        public boolean isEmpty() {
120                return resources.isEmpty();
121        }
122
123        @Override
124        public Iterator<ComputingResource> iterator() {
125                return resources.iterator();
126        }
127
128        @Override
129        public int lastIndexOf(Object o) {
130                return resources.lastIndexOf(o);
131        }
132
133        @Override
134        public ListIterator<ComputingResource> listIterator() {
135                return resources.listIterator();
136        }
137
138        @Override
139        public ListIterator<ComputingResource> listIterator(int index) {
140                return resources.listIterator(index);
141        }
142
143        @Override
144        public boolean remove(Object o) {
145                // TODO Auto-generated method stub
146                return false;
147        }
148
149        @Override
150        public ComputingResource remove(int index) {
151                // TODO Auto-generated method stub
152                return null;
153        }
154
155        @Override
156        public boolean removeAll(Collection<?> c) {
157                // TODO Auto-generated method stub
158                return false;
159        }
160
161        @Override
162        public boolean retainAll(Collection<?> c) {
163                // TODO Auto-generated method stub
164                return false;
165        }
166
167        @Override
168        public ComputingResource set(int index, ComputingResource element) {
169                // TODO Auto-generated method stub
170                return null;
171        }
172
173        @Override
174        public int size() {
175                return resources.size();
176        }
177
178        @Override
179        public List<ComputingResource> subList(int fromIndex, int toIndex) {
180                return resources.subList(fromIndex, toIndex);
181        }
182
183        @Override
184        public Object[] toArray() {
185                return resources.toArray();
186        }
187
188        @Override
189        public <T> T[] toArray(T[] a) {
190                return resources.toArray(a);
191        }
192
193       
194        protected int getAmount(ResourceStatus status){
195                int sum = 0;
196                for(int i = 0; i < this.resources.size(); i++){
197                        if(resources.get(i).getStatus() == status){
198                                sum++;
199                        }
200                }
201                return sum;
202        }
203       
204        public int getFreeAmount(){
205                return getAmount(ResourceStatus.FREE);
206        }
207
208        public int getUsedAmount(){
209                return getAmount(ResourceStatus.BUSY);
210        }
211       
212        public void setUsedAmount(int amount) {
213                int cnt = getAmount(ResourceStatus.BUSY);
214                int delta = amount - cnt;
215
216                //allocate resources
217                if(delta > 0){
218                                for(int i = 0; i < this.resources.size() && delta > 0; i++){
219                                        ComputingResource r = this.resources.get(i);
220                                        if(r.getStatus() == ResourceStatus.FREE || r.getStatus() == ResourceStatus.PENDING){
221                                                r.setStatus(ResourceStatus.BUSY);
222                                                delta--;
223                                        }
224                                }
225                }
226                //free resources
227                else if(delta < 0) {
228                        for(int i = this.resources.size() - 1; i >= 0 && delta < 0; i--){
229                                ComputingResource r = this.resources.get(i);
230                                if(r.getStatus() == ResourceStatus.BUSY){
231                                        r.setStatus(ResourceStatus.FREE);
232                                        delta++;
233                                }
234                        }
235                }
236        }
237       
238        public ProcessingElements replicate(int amount){
239                List<ComputingResource> compResources =  new ArrayList<ComputingResource>(amount);
240                Iterator<ComputingResource> it = resources.iterator();
241                amount = Math.min(resources.size(), amount);
242                while(it.hasNext() && amount > 0){
243                        ComputingResource compRes = it.next();
244                        if(compRes.getStatus() == ResourceStatus.FREE){
245                                compResources.add(compRes);
246                                amount--;
247                        }
248                }
249                return new ProcessingElements(compResources);
250        }
251
252        public ResourceUnitProvisioner getProvisioner() {
253                if(provisioner == null){
254                        provisioner = new ProcessingElementsResourceUnitProvisioner(ResourceUnitState.FREE, 0);
255                }
256        return provisioner;
257        }
258       
259        class ProcessingElementsResourceUnitProvisioner implements ResourceUnitProvisioner{
260
261                protected ResourceUnitState state;
262                protected int pending;
263
264                public ProcessingElementsResourceUnitProvisioner (ResourceUnitState state, int pending) {
265                        this.state = state;
266                        this.pending = pending;
267                }
268
269                public ResourceUnitState getState() {
270                        return state;
271                }
272
273                public void setState(ResourceUnitState newState) {
274
275                        if(newState == ResourceUnitState.FREE){
276                                setUsedAmount(getUsedAmount() - getAmount());
277                        } else if(newState == ResourceUnitState.PENDING){
278                                for (int i = 0; i < resources.size(); i++) {
279                                        ComputingResource pe = resources.get(i);
280                                        if (pe.getStatus() == ResourceStatus.FREE) {
281                                                pe.setStatus(ResourceStatus.PENDING);
282                                        }
283                                }
284                                setPending(getPending() + getAmount());
285                        } else if(state == ResourceUnitState.PENDING && newState == ResourceUnitState.BUSY){
286                                setPending(getProvisioner().getPending() - getAmount());
287                                setUsedAmount(getUsedAmount() + getAmount());
288                        } else if(state == ResourceUnitState.FREE && newState == ResourceUnitState.BUSY){
289                                setUsedAmount(getUsedAmount() + getAmount());
290                        }
291                        state = newState;
292                }
293               
294                public int getPending() {
295                        return pending;
296                }
297
298                public void setPending(int pending) {
299                        this.pending = pending;
300                }
301               
302        }
303       
304
305}
Note: See TracBrowser for help on using the repository browser.