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

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