source: xssim/trunk/src/gridsim/gssim/ResourceUnitsManagerImpl.java @ 128

Revision 128, 15.9 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package gridsim.gssim;
2
3import gridsim.ResourceCharacteristics;
4import gridsim.gssim.resource.ResourceProcessors;
5import gssim.schedframe.resources.profile.ComputingNodePowerProfile;
6import gssim.schedframe.scheduling.ExecTaskInterface;
7
8import java.util.Collection;
9import java.util.HashMap;
10import java.util.Iterator;
11import java.util.List;
12import java.util.Map;
13
14import org.apache.commons.logging.Log;
15import org.apache.commons.logging.LogFactory;
16
17import schedframe.resources.ComputingNode;
18import schedframe.resources.PowerInterface;
19import schedframe.resources.ExecutingResourceDescription;
20import schedframe.resources.units.Memory;
21import schedframe.resources.units.Processor;
22import schedframe.resources.units.Processors;
23import schedframe.resources.units.ResourceUnit;
24import schedframe.resources.units.ResourceUnitType;
25import schedframe.scheduling.Reservation;
26import schedframe.scheduling.plugin.local.ResourceAllocationInterface;
27import schedframe.scheduling.plugin.local.ResourceUnitsManagerInterface;
28import schedframe.scheduling.utils.ResourceParameterName;
29
30
31/**
32 * This class represents resource characteristics of a resource
33 *
34 * @author Marcin Krystek
35 *
36 */
37
38public class ResourceUnitsManagerImpl extends ResourceCharacteristics implements ResourceUnitsManagerInterface, ResourceAllocationInterface {
39
40        public static final int NOT_ADVANCE_RESERVATION = ADVANCE_RESERVATION + 1;
41       
42        private Log log = LogFactory.getLog(ResourceUnitsManagerImpl.class);
43
44        protected ResourceProcessors processors;
45
46        protected Memory memory;
47
48        protected Map<String, ComputingNode> nodeMap;
49       
50        protected Map<String, Map<ResourceParameterName, ResourceUnit>> reservedResources;
51
52        protected Map<String, PowerInterface> nodePowerProfiles;
53
54        /**
55         * Creates new characteristic. resConfig parameter is a map that
56         * contains resource units in a following manner:
57         * <ul>
58         * <li>memory is an Integer stored with a {@link ResourceParameterName#MEMORY}key
59         * <li>Processing elements are stored with a {@link link ResourceParameterName#CPUCOUNT}key
60         * <li>cost per sec is a Double stored with a {@link link ResourceParameterName#COST} key
61         * </ul>
62         *
63         * @param resConfig
64         *            resource units configuration
65         * @param timeZone
66         *            timezone of resource
67         * @param resReqMatcher
68         *            resource units manager
69         */
70        public ResourceUnitsManagerImpl(double timeZone, ExecutingResourceDescription execResDesc){
71                super(null, null, null, -1, timeZone, 0);
72               
73                Collection<ResourceUnit> resConfig = execResDesc.getResourceUnit();
74               
75                this.nodePowerProfiles = execResDesc.getNodePowerProfiles();
76                this.reservedResources = new HashMap<String, Map<ResourceParameterName, ResourceUnit>>();
77                this.nodeMap = null;
78               
79                Iterator<ResourceUnit> itr = resConfig.iterator();
80                while(itr.hasNext()){
81                        ResourceUnit resUnit = itr.next();
82                       
83                        switch(resUnit.getName()){
84                                case COST: this.costPerSec_ = resUnit.getAmount();
85                                        break;
86                                       
87                                case MEMORY: if(resUnit instanceof Memory){
88                                                                this.memory = (Memory) resUnit;
89                                                        }
90                                        break;
91                                       
92                                case CPUCOUNT: if(resUnit instanceof ResourceProcessors){
93                                                                this.processors = (ResourceProcessors) resUnit;
94                                                        }
95                                        break;
96                        }
97                }
98               
99        }
100       
101        /**
102         * Retrieves free resource units that satisfy given resource units
103         * requirements.
104         *
105         * @param resReq resource units requirements
106         * @return free resource units or null
107         */
108        public Map<ResourceParameterName, ResourceUnit> chooseResourcesFor(ExecTaskInterface task) {
109               
110                if(!(task instanceof SubmittedTask)){
111                        return null;
112                }
113               
114                // check if there are resources previously reserved for this task.
115                String jt = task.getJobId() + "_" + task.getId();
116                Map<ResourceParameterName, ResourceUnit> rr = this.reservedResources.get(jt);
117                if(rr != null){
118                        this.reservedResources.remove(jt);
119                        return rr;
120                }
121               
122                HashMap<ResourceParameterName, ResourceUnit> map =
123                                                new HashMap<ResourceParameterName, ResourceUnit>();
124
125                int cpuRequest;
126                try {
127                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
128                } catch (NoSuchFieldException e) {
129                        cpuRequest = 1;
130                }
131               
132                if(cpuRequest != 0){
133                       
134                        if(processors.getFreeAmount() < cpuRequest) {
135                                log.error("Task requires more cpus than is availiable in this moment.");
136                                return null;
137                        }
138                       
139                       
140                        if(task.expectSpecificResource(ResourceParameterName.FREECPUS)){
141                                String names[] = (String[])task.getExpectedSpecificResource(ResourceParameterName.FREECPUS);
142                                int cpuIds[] = new int[names.length];
143                                for(int i = 0; i < names.length; i++){
144                                        cpuIds[i] = Integer.parseInt(names[i]);
145                                }
146                               
147                                ResourceProcessors p = this.processors.clone();
148                                p.reset();
149                                for(int i = 0; i < processors.getAmount() && cpuRequest > 0; i++){
150                                        Processor cpu = processors.get(i);
151                                        boolean isChosen = false;
152                                        for(int j = 0; j < cpuIds.length && !isChosen; j++){
153                                                if(cpu.getId() == cpuIds[j])
154                                                        isChosen = true;
155                                        }
156                                        if(isChosen && cpu.getStatus() == Processor.Status.FREE){
157                                                p.get(i).setStatus(Processor.Status.BUSY);
158                                                cpuRequest--;
159                                        }
160                                }
161                               
162                                if(cpuRequest > 0)
163                                        return null;
164                               
165                                map.put(ResourceParameterName.CPUCOUNT, p);
166                               
167                        } else {
168                       
169                        //      ResourceProcessors p = this.processors.clone();
170                                //p.reset();
171                               
172                                for(int i = 0; i < processors.getAmount() && cpuRequest > 0; i++){
173                                        if(processors.get(i).getStatus() == Processor.Status.FREE){
174                                                processors.get(i).setStatus(Processor.Status.BUSY);
175                                                cpuRequest--;
176                                        }
177                                }
178                       
179                                map.put(ResourceParameterName.CPUCOUNT, processors);
180                        }
181                }
182               
183               
184                int memoryRequest;
185                try {
186                        memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue();
187                } catch (NoSuchFieldException e) {
188                        memoryRequest = 0;
189                }
190                if(memoryRequest != 0 && isMemRequirementSatisfied(memoryRequest)){
191                        Memory m = new Memory(this.memory);
192                        m.setUsedAmount(memoryRequest);
193                        map.put(ResourceParameterName.MEMORY, m);
194                }
195                       
196                return map;
197               
198        }
199
200        /**
201         * Checks if given resource units requirements can ever be satisfied by
202         * resource.
203         *
204         * @param resReq
205         *            resource units requirements
206         * @return true if these resource units can be satisfied, false otherwise
207         */
208        public boolean isReqSatisfied(Map<ResourceParameterName, ResourceUnit> resReq) {
209
210                Processors p = (Processors) resReq.get(ResourceParameterName.CPUCOUNT);
211                if(p != null){
212                        if(processors.getProcessorSpeed() < p.getProcessorSpeed())
213                                return false;
214                        if(processors.getAmount() < p.getUsedAmount())
215                                return false;
216                }
217               
218                Memory m = (Memory) resReq.get(ResourceParameterName.MEMORY);
219                if(m != null){
220                        if(memory.getAmount() < m.getUsedAmount())
221                                return false;
222                }
223               
224                return true;
225        }
226
227        /**
228         *
229         * @return number of PE this resource consists of.
230         */
231        public int getNumPE() {
232                return processors.getAmount();
233        }
234
235        /**
236         * Returns speed of single (first) processing element.
237         */
238        public int getPESpeed() {
239                return processors.getProcessorSpeed();
240        }
241       
242        /**
243         * Allocates free resource units. This function delegates processing to
244         * {@link gridsim.gssim.interfaces.ResUnitsManagerInterface#allocateResources(Map)}
245         *
246         * @param freeRes
247         *            resource units
248         */
249        public void allocateResources(Map<ResourceParameterName, ResourceUnit> resources) {
250                ResourceUnit unit = resources.get(ResourceParameterName.CPUCOUNT);
251               
252                if(unit != null) {
253                       
254                        if(unit instanceof ResourceProcessors){
255                                ResourceProcessors peUnit = (ResourceProcessors) unit;
256                               
257                                for(int i = 0; i < peUnit.getAmount(); i++){
258                                        if(peUnit.get(i).getStatus() == Processor.Status.BUSY){
259                                                //Processor p = processors.get(i);
260                                                //p.setStatus(Processor.Status.BUSY);
261                                        }
262                                }
263                        }
264                }
265
266                unit = resources.get(ResourceParameterName.MEMORY);
267                if(unit != null)
268                        memory.setUsedAmount(memory.getUsedAmount() + unit.getUsedAmount());
269               
270        }
271
272        /**
273         * Free used resources.
274         *
275         * @param lastUsedResources
276         *            resource units
277         */
278        public void freeResources(Map<ResourceParameterName, ResourceUnit> lastUsedResources) {
279                ResourceUnit unit = lastUsedResources.get(ResourceParameterName.CPUCOUNT);
280               
281                if(unit instanceof ResourceProcessors){
282                        ResourceProcessors peUnit = (ResourceProcessors) unit;
283                       
284                        for(int i = 0; i < peUnit.getAmount(); i++){
285                                if(peUnit.get(i).getStatus() == Processor.Status.BUSY){
286                                        processors.get(i).setStatus(Processor.Status.FREE);
287                                }
288                        }
289                }
290
291                unit = lastUsedResources.get(ResourceParameterName.MEMORY);
292                if(unit != null){
293                        memory.setUsedAmount(memory.getUsedAmount() - unit.getUsedAmount());
294                }
295               
296        }
297       
298        public Processors chooseProcessors(Processors cpus){
299                ResourceProcessors p = this.processors.clone();
300                p.reset();
301                int cpuRequest = cpus.getUsedAmount();
302
303                if(cpus instanceof List){
304                        log.error("CHOOSING SPECIFIC CPUS IN ADVANCE RESERVATION MODEL IS NOT SUPPOERTED YET.");
305                } else {
306               
307                        for(int i = 0; i < processors.getAmount() && cpuRequest > 0; i++){
308                                if(processors.get(i).getStatus() == Processor.Status.FREE){
309                                        p.get(i).setStatus(Processor.Status.BUSY);
310                                        cpuRequest--;
311                                }
312                        }
313                }
314                return p;
315        }
316       
317        public void createResourceReservation(Reservation reservation, Processors processors){
318                try {
319                        ResourceProcessors cpus = (ResourceProcessors) processors;
320                        for(int i = 0; i < cpus.size(); i++){
321                                Processor p = cpus.get(i);
322                                Processor local_p = this.processors.get(i);
323                                if(p.getStatus() == Processor.Status.BUSY &&
324                                        local_p.getStatus() == Processor.Status.FREE){
325                                        local_p.setStatus(Processor.Status.RESERVED);
326                                }
327                        }
328                       
329                        HashMap<ResourceParameterName, ResourceUnit> map =
330                                new HashMap<ResourceParameterName, ResourceUnit>();
331                        map.put(ResourceParameterName.CPUCOUNT, processors);
332                       
333                        this.reservedResources.put(reservation.getJobId()+"_"+reservation.getTaskId(), map);
334                       
335                        }catch(Exception e){
336                                e.printStackTrace();
337                        }
338        }
339       
340        public void createResourceReservation(ExecTaskInterface task, Map<ResourceParameterName, ResourceUnit> resources){
341                try {
342                ResourceProcessors cpus = (ResourceProcessors) resources.get(ResourceParameterName.CPUCOUNT);
343                for(int i = 0; i < cpus.size(); i++){
344                        Processor p = cpus.get(i);
345                        Processor local_p = this.processors.get(i);
346                        if(p.getStatus() == Processor.Status.BUSY &&
347                                local_p.getStatus() == Processor.Status.FREE){
348                                local_p.setStatus(Processor.Status.RESERVED);
349                        }
350                }
351               
352                this.reservedResources.put(task.getJobId()+"_"+task.getId(), resources);
353                }catch(Exception e){
354                        e.printStackTrace();
355                }
356        }
357       
358        public void removeResourceReservation(Map<ResourceParameterName, ResourceUnit> lastUsedResources){
359                ResourceUnit unit = lastUsedResources.get(ResourceParameterName.CPUCOUNT);
360               
361                if(unit instanceof ResourceProcessors){
362                        ResourceProcessors peUnit = (ResourceProcessors) unit;
363                       
364                        for(int i = 0; i < peUnit.getAmount(); i++){
365                                Processor.Status cpuStatus = peUnit.get(i).getStatus();
366                                if(cpuStatus == Processor.Status.BUSY){
367                                        processors.get(i).setStatus(Processor.Status.FREE);
368                                }
369                        }
370                }
371        }
372       
373        public boolean areResourcesReserved(ExecTaskInterface task){
374                return this.reservedResources.containsKey(task.getJobId()+"_"+task.getId());
375        }
376
377       
378        public boolean areFreeResourcesSufficient(Map<ResourceParameterName, ResourceUnit> resReq){
379                ResourceUnit requirement = null;
380               
381                // check memory requirements
382                if(resReq.containsKey(ResourceParameterName.MEMORY)){
383                        requirement = resReq.get(ResourceParameterName.MEMORY);
384
385                        if(memory.getFreeAmount() < requirement.getUsedAmount())
386                                return false;
387                }
388               
389                // check cpu requirements
390                if(resReq.containsKey(ResourceParameterName.CPUCOUNT)){
391                        requirement = resReq.get(ResourceParameterName.CPUCOUNT);
392                       
393                        if(requirement.getResourceUnitType() == ResourceUnitType.DISCRETE_RESOURCE){
394                               
395                                List<Processor> peRequirement = (List<Processor>) requirement;
396                                for(int i = 0; i < peRequirement.size(); i++){
397                                        if(peRequirement.get(i).getStatus() == Processor.Status.BUSY &&
398                                                processors.get(i).getStatus() == Processor.Status.BUSY) {
399                                                return false;
400                                        }
401                                }
402                               
403                        } else {
404                               
405                                int amount = requirement.getUsedAmount();
406                                for(int i = 0; i < processors.getAmount() && amount > 0; i++){
407                                        if(processors.get(i).getStatus() == Processor.Status.FREE)
408                                                amount--;
409                                }
410                                if(amount > 0)
411                                        return false;
412                        }
413                }
414               
415                return true;
416        }
417
418
419        /**
420         * @author Stanislaw Szczepanowski
421         * @return the current available memory amount
422         */
423        public int getFreeMemory() {
424                return memory.getFreeAmount();
425        }
426       
427        /**
428         * @author Stanislaw Szczepanowski
429         * @return the total available memory amount
430         */
431        public int getTotalMemory() {
432                return memory.getAmount();
433        }
434
435       
436        /**
437         * Checks if memory requirements can be satisfied
438         *
439         * @param memoryReq
440         * @return true if memory requirements can be satisfied,false otherwise
441         */
442        protected boolean isMemRequirementSatisfied(int memoryReq) {
443                if (memory.getFreeAmount() < memoryReq)
444                        return false;
445               
446                return true;
447        }
448       
449        /**
450         * Checks if given processing element requirement can be satisfied
451         *
452         * @param peReq
453         *            processing element to be checked
454         * @return true if given processing element requirement can be satisfied,
455         *         false otherwise
456         */
457        protected boolean isPeRequirementSatisfied(Processors req) {
458
459                if(processors.getProcessorSpeed() < req.getProcessorSpeed())
460                        return false;
461               
462                if(processors.getFreeAmount() < req.getUsedAmount()){
463                        return false;
464                }
465
466                return true;
467        }
468
469        public Map<ResourceParameterName, ResourceUnit> getResourceParameters(){
470                Map<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>();
471                if(this.processors != null)
472                        map.put(ResourceParameterName.CPUCOUNT, this.processors);
473                if(this.memory != null)
474                map.put(ResourceParameterName.MEMORY, this.memory);
475               
476                return map;
477        }
478       
479        public ResourceUnit getRequestedUnitInstance(ResourceParameterName name){
480                if(name == ResourceParameterName.CPUCOUNT){
481                        Processors p = null;
482                        if(processors.areValuesUnique()){
483                                p = processors.clone();
484                                p.reset();
485                               
486                        } else {
487                                p = new Processors(processors.getResourceId(), processors.getAmount(), 0);
488                        }
489                        return p;
490                }
491               
492                if(name == ResourceParameterName.MEMORY){
493                        return new Memory(memory.getResourceId(), 0, 0);
494                }
495                return null;
496        }
497       
498        public void printProcessorsState(){
499                for(int i = 0; i < this.processors.size(); i++){
500                        System.err.println(this.processors.get(i).getStatus() + " ");
501                }
502        }
503
504        @Override
505        public Memory getMemory() {
506                return this.memory;
507        }
508
509        @Override
510        public ResourceProcessors getProcessors() {
511                return this.processors;
512        }
513
514        @Override
515        public Map<ResourceParameterName, ResourceUnit> getReservedResources(
516                        String id) {
517                return this.reservedResources.get(id);
518        }
519
520        @Override
521        public void removeResourceReservation(String id) {
522                this.reservedResources.remove(id);
523        }
524
525        @Override
526        public Collection<ComputingNode> getComputingNode() {
527                if(this.nodeMap == null)
528                        createComputingNodes();
529               
530                return this.nodeMap.values();
531        }
532
533        @Override
534        public ComputingNode getComputingNode(String id) {
535                if(this.nodeMap == null)
536                        createComputingNodes();
537               
538                return this.nodeMap.get(id);
539        }
540               
541        protected void createComputingNodes() {
542                String nodeId = this.processors.get(0).getComputingNodeId();
543                int fromIndex = 0;
544                boolean checkEnMap = (this.nodePowerProfiles != null ? true : false);
545               
546                this.nodeMap = new HashMap<String, ComputingNode>();
547               
548                int last = this.processors.size() - 1;
549                for(int i = 0; i < this.processors.size(); i++){
550                        Processor p = this.processors.get(i);
551                        String currentNodeId = p.getComputingNodeId();
552                        if(!nodeId.equals(currentNodeId) || i == last){
553                                if(i == last) i++;
554                                ComputingNode computingNode = new ComputingNode(nodeId);
555                                computingNode.setProcessors(this.processors.subList(fromIndex, i));
556                                if(checkEnMap){
557                                        PowerInterface enProfile = this.nodePowerProfiles.get(nodeId);
558                                        if(enProfile != null)
559                                                computingNode.accept(enProfile);
560                                        else
561                                                computingNode.accept(new ComputingNodePowerProfile());
562                                } else {
563                                        computingNode.accept(new ComputingNodePowerProfile());
564                                }
565                               
566                                this.nodeMap.put(nodeId, computingNode);
567                               
568                                fromIndex = i;
569                                nodeId = currentNodeId;
570                        }
571                       
572                }
573        }
574
575}
Note: See TracBrowser for help on using the repository browser.