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

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