[477] | 1 | package schedframe.resources.units; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | import java.util.Collection; |
---|
| 5 | import java.util.Iterator; |
---|
| 6 | import java.util.List; |
---|
| 7 | import java.util.ListIterator; |
---|
| 8 | |
---|
| 9 | import schedframe.resources.ResourceStatus; |
---|
| 10 | import schedframe.resources.ResourceType; |
---|
| 11 | import schedframe.resources.computing.ComputingResource; |
---|
[1392] | 12 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
[477] | 13 | |
---|
| 14 | public class ProcessingElements extends PEUnit implements List<ComputingResource> { |
---|
| 15 | |
---|
| 16 | protected List<ComputingResource> resources; |
---|
| 17 | |
---|
| 18 | public ProcessingElements(){ |
---|
| 19 | super(); |
---|
[1384] | 20 | this.resources = new ArrayList<ComputingResource>(1); |
---|
| 21 | this.provisioner = new ProcessingElementsResourceUnitProvisioner(ResourceUnitState.FREE, 0); |
---|
[477] | 22 | } |
---|
| 23 | |
---|
| 24 | public ProcessingElements(String resName){ |
---|
| 25 | this(); |
---|
| 26 | resourceId = resName; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | public ProcessingElements(List<ComputingResource> resources){ |
---|
| 30 | this(); |
---|
| 31 | this.resources = resources; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | public ProcessingElements(String resName, List<ComputingResource> resources){ |
---|
| 35 | this(resName); |
---|
| 36 | this.resources = resources; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | public ResourceType getResourceType(){ |
---|
| 40 | if(resources != null && resources.size() > 0) |
---|
| 41 | return resources.get(0).getType(); |
---|
| 42 | else return null; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | public int getAmount(){ |
---|
| 46 | return this.resources.size(); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | public int getSpeed(){ |
---|
| 50 | int peCnt = getAmount(); |
---|
| 51 | |
---|
| 52 | double avgSpeed = 0; |
---|
| 53 | for(int i = 0; i < peCnt; i++){ |
---|
| 54 | try { |
---|
| 55 | avgSpeed += resources.get(i).getResourceCharacteristic().getResourceUnit(StandardResourceUnitName.CPUSPEED).getAmount(); |
---|
| 56 | } catch (NoSuchFieldException e) { |
---|
[677] | 57 | avgSpeed +=1; |
---|
[477] | 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){ |
---|
[1392] | 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--; |
---|
[477] | 223 | } |
---|
[1392] | 224 | } |
---|
[477] | 225 | } |
---|
| 226 | //free resources |
---|
| 227 | else if(delta < 0) { |
---|
| 228 | for(int i = this.resources.size() - 1; i >= 0 && delta < 0; i--){ |
---|
[1434] | 229 | ComputingResource cr = this.resources.get(i); |
---|
| 230 | if(cr.getStatus() == ResourceStatus.BUSY){ |
---|
| 231 | if(new JobRegistryImpl(cr).getRunningTasks().size() == 0){ |
---|
| 232 | cr.setStatus(ResourceStatus.FREE); |
---|
[1392] | 233 | delta++; |
---|
| 234 | } |
---|
[477] | 235 | } |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | public ProcessingElements replicate(int amount){ |
---|
[1423] | 241 | List<ComputingResource> compResources = new ArrayList<ComputingResource>(amount); |
---|
[477] | 242 | Iterator<ComputingResource> it = resources.iterator(); |
---|
| 243 | amount = Math.min(resources.size(), amount); |
---|
| 244 | while(it.hasNext() && amount > 0){ |
---|
| 245 | ComputingResource compRes = it.next(); |
---|
| 246 | if(compRes.getStatus() == ResourceStatus.FREE){ |
---|
| 247 | compResources.add(compRes); |
---|
| 248 | amount--; |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | return new ProcessingElements(compResources); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | public ResourceUnitProvisioner getProvisioner() { |
---|
| 255 | if(provisioner == null){ |
---|
| 256 | provisioner = new ProcessingElementsResourceUnitProvisioner(ResourceUnitState.FREE, 0); |
---|
| 257 | } |
---|
| 258 | return provisioner; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | class ProcessingElementsResourceUnitProvisioner implements ResourceUnitProvisioner{ |
---|
| 262 | |
---|
| 263 | protected ResourceUnitState state; |
---|
| 264 | protected int pending; |
---|
| 265 | |
---|
| 266 | public ProcessingElementsResourceUnitProvisioner (ResourceUnitState state, int pending) { |
---|
| 267 | this.state = state; |
---|
| 268 | this.pending = pending; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | public ResourceUnitState getState() { |
---|
| 272 | return state; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | public void setState(ResourceUnitState newState) { |
---|
| 276 | |
---|
| 277 | if(newState == ResourceUnitState.FREE){ |
---|
| 278 | setUsedAmount(getUsedAmount() - getAmount()); |
---|
| 279 | } else if(newState == ResourceUnitState.PENDING){ |
---|
| 280 | for (int i = 0; i < resources.size(); i++) { |
---|
| 281 | ComputingResource pe = resources.get(i); |
---|
| 282 | if (pe.getStatus() == ResourceStatus.FREE) { |
---|
| 283 | pe.setStatus(ResourceStatus.PENDING); |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | setPending(getPending() + getAmount()); |
---|
| 287 | } else if(state == ResourceUnitState.PENDING && newState == ResourceUnitState.BUSY){ |
---|
| 288 | setPending(getProvisioner().getPending() - getAmount()); |
---|
| 289 | setUsedAmount(getUsedAmount() + getAmount()); |
---|
| 290 | } else if(state == ResourceUnitState.FREE && newState == ResourceUnitState.BUSY){ |
---|
| 291 | setUsedAmount(getUsedAmount() + getAmount()); |
---|
| 292 | } |
---|
| 293 | state = newState; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | public int getPending() { |
---|
| 297 | return pending; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | public void setPending(int pending) { |
---|
| 301 | this.pending = pending; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | |
---|
| 307 | } |
---|