[104] | 1 | package schedframe.resources; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | import java.util.Collection; |
---|
| 5 | import java.util.HashMap; |
---|
| 6 | import java.util.Iterator; |
---|
| 7 | import java.util.List; |
---|
| 8 | import java.util.Map; |
---|
| 9 | |
---|
| 10 | import schedframe.resources.units.ResourceUnit; |
---|
| 11 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | * |
---|
| 16 | * @author Marcin Krystek |
---|
| 17 | * |
---|
| 18 | */ |
---|
| 19 | public abstract class ResourceDescription implements Cloneable { |
---|
| 20 | |
---|
| 21 | protected ResourceProvider provider; |
---|
| 22 | |
---|
| 23 | protected Map<ResourceParameterName, List<ResourceUnit>> resUnits; |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * |
---|
| 27 | * @return type of described resource |
---|
| 28 | */ |
---|
| 29 | public abstract ResourceType getType(); |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | protected ResourceDescription(){ |
---|
| 33 | this.resUnits = new HashMap<ResourceParameterName, List<ResourceUnit>>(); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | protected ResourceDescription(ResourceDescription r){ |
---|
| 37 | this(); |
---|
| 38 | Iterator<ResourceParameterName> itr = r.resUnits.keySet().iterator(); |
---|
| 39 | while(itr.hasNext()){ |
---|
| 40 | ResourceParameterName name = itr.next(); |
---|
| 41 | try { |
---|
| 42 | List<ResourceUnit> list = r.getResourceUnitList(name); |
---|
| 43 | List<ResourceUnit> newList = new ArrayList<ResourceUnit>(list.size()); |
---|
| 44 | for(int i = 0; i < list.size(); i++){ |
---|
| 45 | newList.add((ResourceUnit)list.get(i).clone()); |
---|
| 46 | } |
---|
| 47 | resUnits.put(name, newList); |
---|
| 48 | } catch(NoSuchFieldException e){ |
---|
| 49 | e.printStackTrace(); |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | provider = r.provider; |
---|
| 54 | |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * |
---|
| 59 | * @return resource id |
---|
| 60 | */ |
---|
| 61 | public ResourceProvider getProvider(){ |
---|
| 62 | return this.provider; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * |
---|
| 67 | * @param unitName name of the resource unit |
---|
| 68 | * @return resource unit value |
---|
| 69 | * @throws NoSuchFieldException if particular resource unit is not defined |
---|
| 70 | */ |
---|
| 71 | public ResourceUnit getResourceUnit(ResourceParameterName unitName) throws NoSuchFieldException{ |
---|
| 72 | return getResourceUnitList(unitName).get(0); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | public List<ResourceUnit> getResourceUnitList(ResourceParameterName unitName) throws NoSuchFieldException{ |
---|
| 76 | if(resUnits.containsKey(unitName)) |
---|
| 77 | return resUnits.get(unitName); |
---|
| 78 | else |
---|
| 79 | throw new NoSuchFieldException("Resource unit " + unitName + |
---|
| 80 | " is not available in resource " + provider.id); |
---|
| 81 | |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | public Collection<ResourceUnit> getResourceUnit(){ |
---|
| 85 | List<ResourceUnit> values = new ArrayList<ResourceUnit>(); |
---|
| 86 | Collection<List<ResourceUnit>> lists = resUnits.values(); |
---|
| 87 | Iterator<List<ResourceUnit>> itr = lists.iterator(); |
---|
| 88 | |
---|
| 89 | while(itr.hasNext()){ |
---|
| 90 | List<ResourceUnit> list = itr.next(); |
---|
| 91 | values.addAll(list); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | return values; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | public int getProcessorsAmount() throws NoSuchFieldException{ |
---|
| 98 | List<ResourceUnit> list = getResourceUnitList(ResourceParameterName.CPUCOUNT); |
---|
| 99 | int amount = 0; |
---|
| 100 | for(int i = 0; i < list.size(); i++){ |
---|
| 101 | amount += list.get(i).getAmount(); |
---|
| 102 | } |
---|
| 103 | return amount; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | public float getMemoryAmount() throws NoSuchFieldException{ |
---|
| 107 | List<ResourceUnit> list = getResourceUnitList(ResourceParameterName.MEMORY); |
---|
| 108 | int amount = 0; |
---|
| 109 | for(int i = 0; i < list.size(); i++){ |
---|
| 110 | amount += list.get(i).getAmount(); |
---|
| 111 | } |
---|
| 112 | return amount; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | public Object clone(){ |
---|
| 116 | ResourceDescription copy = null; |
---|
| 117 | try { |
---|
| 118 | copy = (ResourceDescription) super.clone(); |
---|
| 119 | copy.resUnits = new HashMap<ResourceParameterName, List<ResourceUnit>>(); |
---|
| 120 | Iterator<ResourceParameterName> itr = this.resUnits.keySet().iterator(); |
---|
| 121 | while(itr.hasNext()){ |
---|
| 122 | ResourceParameterName name = itr.next(); |
---|
| 123 | List<ResourceUnit> list = this.resUnits.get(name); |
---|
| 124 | List<ResourceUnit> newList = new ArrayList<ResourceUnit>(list.size()); |
---|
| 125 | for(int i = 0; i < list.size(); i++){ |
---|
| 126 | newList.add((ResourceUnit)list.get(i).clone()); |
---|
| 127 | } |
---|
| 128 | copy.resUnits.put(name, newList); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | } catch (CloneNotSupportedException e) { |
---|
| 132 | e.printStackTrace(); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | return copy; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | public enum ResourceType { |
---|
| 139 | COMPUTING_RESOURCE, |
---|
| 140 | QUEUING_SYSTEM; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | public abstract long getQueueLoad(String queueName) throws NoSuchFieldException; |
---|
| 144 | |
---|
| 145 | } |
---|