source: DCWoRMS/branches/coolemall/src/dcworms/schedframe/scheduling/Executable.java @ 1315

Revision 1315, 15.4 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package dcworms.schedframe.scheduling;
2
3
4import gridsim.GridSim;
5import gridsim.dcworms.DCWormsTags;
6
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.LinkedList;
10import java.util.List;
11import java.util.Map;
12
13import org.apache.commons.lang.ArrayUtils;
14import org.joda.time.DateTime;
15import org.joda.time.DateTimeUtilsExt;
16import org.joda.time.ReadableDuration;
17import org.qcg.broker.schemas.resreqs.ResourceConsumptionProfileType;
18import org.qcg.broker.schemas.resreqs.ResourceConsumptionType;
19import org.qcg.broker.schemas.resreqs.StringParameterType;
20
21import qcg.shared.constants.BrokerConstants;
22
23import schedframe.resources.computing.ComputingResource;
24import schedframe.resources.units.PEUnit;
25import schedframe.resources.units.ProcessingElements;
26import schedframe.resources.units.StandardResourceUnitName;
27import schedframe.scheduling.ResourceHistoryItem;
28import schedframe.scheduling.UsedResourcesList;
29import schedframe.scheduling.WorkloadUnitHandler;
30import schedframe.scheduling.manager.tasks.JobRegistryImpl;
31import schedframe.scheduling.tasks.AbstractProcesses;
32import schedframe.scheduling.tasks.AbstractProcessesGroup;
33import schedframe.scheduling.tasks.Task;
34import schedframe.scheduling.tasks.phases.ResourceConsumption;
35import schedframe.scheduling.tasks.phases.ResourceConsumptionProfile;
36import schedframe.scheduling.tasks.requirements.ResourceParameterName;
37
38/**
39 *
40 * @author Marcin Krystek
41 *
42 */
43public class Executable implements ExecTask{
44
45        protected Task task;
46        protected String processesSetId;
47       
48        protected int status;
49        protected Map<ResourceParameterName, Object> specificResources;
50       
51        protected String reservationId;
52        protected double completionPercentage;
53       
54        protected int estimatedDuration;
55        //TO DO remove and benefit from visitedResources
56        protected String schedName;
57        protected UsedResourcesList usedResources;
58        //TO DO consider removing
59        protected List<String> visitedResources;
60
61        protected double submissionTime;
62    protected double arrivalTime;
63        protected double execStartTime ;
64    protected double totalCompletionTime;
65        protected double finishTime;
66       
67        private ResourceConsumptionProfile resourceConsumptionProfile;
68       
69        public Executable(Task t){
70                this.task = t;
71                this.status = DCWormsTags.CREATED;
72               
73                this.usedResources = new UsedResourcesList();
74                this.visitedResources = new ArrayList<String>();
75                init();
76        }
77       
78        public Executable(Task t, AbstractProcesses procesesSet){
79                this.task = t;
80                this.status = DCWormsTags.CREATED;
81                this.processesSetId = procesesSet.getId();
82               
83                this.usedResources = new UsedResourcesList();
84                this.visitedResources = new ArrayList<String>();
85                init();
86        }
87       
88        protected void init() {
89                double currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000;
90                this.submissionTime =  currentTime;
91        this.totalCompletionTime = 0.0;
92        preparePhases(null);
93         }
94       
95        public int getUserId() {
96                return task.getSenderId();
97        }
98        public String getUserDN() {
99                return task.getUserDN();
100        }
101
102        public String getJobId() {
103                return task.getJobId();
104        }
105       
106        public String getTaskId(){
107                return this.task.getId();
108        }
109       
110        public String getId() {
111                if(processesSetId == null)
112                        return task.getId();
113                else
114                        return task.getId() + "_" + processesSetId;
115        }
116       
117        public String getProcessesId(){
118                return this.processesSetId;
119        }
120       
121    public int getUniqueId(){
122        if(processesSetId == null){
123                return (task.getJobId() + "_" + task.getId()).hashCode();
124        } else {
125                return (task.getJobId() + "_" + task.getId() + "_" + processesSetId).hashCode();
126        }
127    }
128   
129        public List<AbstractProcesses> getProcesses() {
130                return task.getProcesses();
131        }
132
133        public List<AbstractProcesses> getProcesses(
134                        AbstractProcessesGroup processGroup) {
135                return task.getProcesses(processGroup);
136        }
137
138        public List<AbstractProcessesGroup> getProcessesGroups() {
139                return task.getProcessesGroups();
140        }
141
142        public org.qcg.broker.schemas.resreqs.Task getDescription() {
143                return task.getDescription();
144        }
145
146        public String getDocument() throws Exception {
147                return task.getDocument();
148        }
149       
150        public long getLength() {
151                return task.getLength();
152        }
153
154        public int getStatus() {
155                return status;
156        }
157       
158        public boolean isFinished()
159    {
160                return task.isFinished();
161    }
162       
163        public void setStatus(int newStatus) throws Exception {
164                int prevStatus = status;
165           
166        if (status == newStatus) {
167            return;
168        }
169
170        if (newStatus < DCWormsTags.CREATED || newStatus > DCWormsTags.NEW_EXEC_PHASE) {
171            throw new Exception("Executable.setStatuts() : Error - " +
172                    "Invalid integer range for Execiutable status.");
173        }
174
175        status = newStatus;
176       
177        if(status == DCWormsTags.INEXEC){
178                loadResourceConsumptionProfile();
179                task.setStatus((int) BrokerConstants.TASK_STATUS_RUNNING);
180        } else if(status == DCWormsTags.QUEUED){
181                task.setStatus((int) BrokerConstants.TASK_STATUS_QUEUED);
182        }
183       
184        double currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000; // time in seconds
185   
186               
187                if (newStatus == DCWormsTags.SUCCESS || newStatus == DCWormsTags.CANCELED) {
188            finishTime = DateTimeUtilsExt.currentTimeMillis() / 1000;
189        }
190               
191                if(newStatus == DCWormsTags.SUBMITTED){
192                         arrivalTime = GridSim.clock();
193                }
194
195        if (prevStatus == DCWormsTags.INEXEC) {
196            if (status == DCWormsTags.CANCELED || status == DCWormsTags.PAUSED ||
197                status == DCWormsTags.SUCCESS) {
198                totalCompletionTime += (currentTime -  execStartTime);
199            }
200        }
201
202        if (prevStatus == DCWormsTags.RESUMED && status == DCWormsTags.SUCCESS) {
203            totalCompletionTime += (currentTime -  execStartTime);
204        }
205
206        if (status == DCWormsTags.INEXEC ||
207            (prevStatus == DCWormsTags.PAUSED && status == DCWormsTags.RESUMED) ) {
208                execStartTime = currentTime;
209               
210                ProcessingElements pes = (ProcessingElements) getUsedResources().getLast().getResourceUnits().get(StandardResourceUnitName.PE);
211                for (ComputingResource resource : pes) {
212
213                                LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>();
214                                toExamine.push(resource);
215
216                                while (!toExamine.isEmpty()) {
217                                        ComputingResource compResource = toExamine.pop();
218                                        List<ComputingResource> resources = compResource.getChildren();
219                                        int numberOfRes = resources.size();
220                                        for (int i = 0; i < numberOfRes; i++) {
221                                                ComputingResource resourceChild = resources.get(i);
222                                                trackResource(resourceChild.getFullName());
223                                                toExamine.addLast(resourceChild);
224                                        }
225                                }
226
227                       
228                        trackResource(resource.getFullName());
229                       
230                        ComputingResource parent = resource.getParent();
231                                List<String> visitedResource = getVisitedResources();
232                                String [] visitedResourcesArray = visitedResource.toArray(new String[visitedResource.size()]);
233                        while (parent != null && !ArrayUtils.contains(visitedResourcesArray, parent.getFullName())) {
234                                trackResource(parent.getFullName());
235                                parent = parent.getParent();
236                        }
237                }
238        }
239       
240        if(status == DCWormsTags.NEW_EXEC_PHASE){
241                if(prevStatus == DCWormsTags.INEXEC){
242                        status = DCWormsTags.INEXEC;
243                        setCompletionPercentage(0);
244                        resourceConsumptionProfile.setCurrentPhase(resourceConsumptionProfile.getCurrentPhase() + 1);
245                       
246                        DateTime currentDateTime = new DateTime();
247                       
248                        if(getUsedResources().getLast().getTimeStamp().getMillis() == currentDateTime.getMillis()){
249                                return;
250                        }
251                        ResourceHistoryItem resHistItem = new ResourceHistoryItem(getUsedResources().getLast().getResourceUnits(), currentDateTime);
252                        resHistItem.setCompletionPercentage(0);
253                        addUsedResources(resHistItem);
254                }
255        }
256        }
257       
258        public void addSpecificResource(ResourceParameterName resourceName, Object value){
259                if(this.specificResources == null)
260                        this.specificResources = new HashMap<ResourceParameterName, Object>();
261                this.specificResources.put(resourceName, value);
262        }
263
264        public boolean expectSpecificResource(ResourceParameterName resourceName){
265                if(this.specificResources == null)
266                        return false;
267                return this.specificResources.containsKey(resourceName);
268        }
269       
270        public Object getExpectedSpecificResource(ResourceParameterName resourceName){
271                if(this.specificResources == null)
272                        return null;
273               
274                return this.specificResources.get(resourceName);
275        }
276       
277        public void setReservationId(String reservationId){
278                this.reservationId = reservationId;
279        }
280       
281        public boolean requireReservation(){
282                return (reservationId != null);
283        }
284       
285        public String getReservationId(){
286                return this.reservationId;
287        }
288       
289        public double getCompletionPercentage() {
290                return completionPercentage;
291        }
292
293        public void setCompletionPercentage(double completionPercentage) {
294                this.completionPercentage = completionPercentage;
295        }
296
297        public void addUsedResources(ResourceHistoryItem usedResources){
298                this.usedResources.add(usedResources);
299        }
300       
301        public UsedResourcesList getUsedResources(){
302                return this.usedResources;
303        }
304       
305    public void setSchedulerName(int resourceId){
306        this.schedName = GridSim.getEntityName(resourceId);
307    }
308
309    public String getSchedulerName(){
310        return schedName;
311    }
312       
313        public int getEstimatedDuration(){
314                return this.estimatedDuration;
315        }
316       
317        public void setEstimatedDuration(int value){
318                this.estimatedDuration = value;
319        }
320
321        public boolean isRegistered() {
322                return task.isRegistered();
323        }
324
325        public void register(JobRegistryImpl jobRegistry) {
326                task.register(jobRegistry);
327        }
328       
329        public void trackResource(String resName){
330                visitedResources.add(resName);
331        }
332       
333        public List<String> getVisitedResources(){
334                return visitedResources;
335        }
336       
337        public ReadableDuration getExpectedDuration() throws NoSuchFieldException {
338                return task.getExpectedDuration();
339        }
340
341        public double getParameterDoubleValue(ResourceParameterName parameterName)
342                        throws NoSuchFieldException, IllegalArgumentException {
343                return task.getParameterDoubleValue(parameterName);
344        }
345
346        public String getParameterStringValue(ResourceParameterName parameterName)
347                        throws NoSuchFieldException, IllegalArgumentException {
348                return task.getParameterStringValue(parameterName);
349        }
350       
351        public double getCpuCntRequest() throws NoSuchFieldException{
352                return getParameterDoubleValue(ResourceParameterName.CPUCOUNT);
353        }
354       
355        public double getMemoryRequest() throws NoSuchFieldException{
356                return getParameterDoubleValue(ResourceParameterName.MEMORY);
357        }
358       
359        public DateTime getExecutionEndTime() throws NoSuchFieldException {
360                return task.getExecutionEndTime();
361        }
362
363        public DateTime getExecutionStartTime() throws NoSuchFieldException {
364                return task.getExecutionStartTime();
365        }
366
367        public DateTime getSubmissionTimeToBroker() {
368                return task.getSubmissionTimeToBroker();
369        }
370
371        public long getWorkloadLogWaitTime() {
372                return task.getWorkloadLogWaitTime();
373        }
374
375    public double getExecStartTime() {
376        return execStartTime;
377    }
378
379    public double getFinishTime() {
380        return finishTime;
381    }
382
383    public double getSubmissionTime() {
384        return submissionTime;
385    }
386   
387    public double getWaitingTime() {
388        return execStartTime - submissionTime;
389    }
390   
391    public void finalizeExecutable(){
392                try {
393                        setStatus(DCWormsTags.SUCCESS);
394                } catch (Exception e) {
395                        // TODO Auto-generated catch block
396                        e.printStackTrace();
397                }
398    }
399   
400        public void accept(WorkloadUnitHandler wuh) {
401                wuh.handleExecutable(this);
402        }
403   
404        /*public boolean equals(Object obj){
405                if(obj instanceof Executable){
406                        Executable t = (Executable) obj;
407                        return getId().equals(t.getId()) && getJobId().equals(t.getJobId());
408                }
409                return false;
410        }*/
411       
412        public ResourceConsumptionProfile getResourceConsumptionProfile(){
413                return resourceConsumptionProfile;
414        }
415
416        public ResourceConsumption getCurrentResourceConsumption(){
417                return resourceConsumptionProfile.getCurrentResourceConsumption();
418        }
419       
420        public String getApplicationName(){
421                return task.getApplicationName();
422        }
423       
424        private void preparePhases(String resourceType) {
425                LinkedList<ResourceConsumption> resourceConsumptionList = new LinkedList<ResourceConsumption>();
426               
427                long usefulWork = -1;
428               
429                if(task.getDescription().getExecution() == null || task.getDescription().getExecution().getResourceConsumptionProfile() == null
430                                || task.getDescription().getExecution().getResourceConsumptionProfile().length == 0 ){
431                        ResourceConsumption resConsumption = null;
432                        try {
433                                resConsumption = new ResourceConsumption(getLength(), task.getComputingResourceRequirements());
434                        } catch (NoSuchFieldException e) {
435                                // TODO Auto-generated catch block
436                                e.printStackTrace();
437                        }
438                        resourceConsumptionList.add(resConsumption);
439                }else{
440                        boolean found = false;
441                        if(resourceType != null){
442                                for(ResourceConsumptionProfileType resConsumptioProfile: task.getDescription().getExecution().getResourceConsumptionProfile()){
443                                        if(resConsumptioProfile.getType() != null && resConsumptioProfile.getType().equals(resourceType)){
444                                                for(ResourceConsumptionType resConsumption: resConsumptioProfile.getResourceConsumption()){
445                                                        ResourceConsumption resourceConsumption = new ResourceConsumption(resConsumption);
446                                                        resourceConsumptionList.add(resourceConsumption);
447                                                }
448                                                for(StringParameterType prop: resConsumptioProfile.getProperties()){
449                                                        if(prop.getName().equals("usefulWork")){
450                                                                usefulWork = Double.valueOf(prop.getContent()).longValue();
451                                                                break;
452                                                        }
453                                                }
454                                                found = true;
455                                                break;
456                                        }
457                                }       
458                        }                       
459                        if(!found){
460                                for(ResourceConsumptionType resConsumption: task.getDescription().getExecution().getResourceConsumptionProfile()[0].getResourceConsumption()){
461                                        ResourceConsumption resourceConsumption = new ResourceConsumption(resConsumption);
462                                        resourceConsumptionList.add(resourceConsumption);
463                                }
464                               
465                                for(StringParameterType prop: task.getDescription().getExecution().getResourceConsumptionProfile()[0].getProperties()){
466                                        if(prop.getName().equals("usefulWork")){
467                                                usefulWork = Double.valueOf(prop.getContent()).longValue();
468                                                break;
469                                        }
470                                }
471                        }
472                }
473               
474                usefulWork = (usefulWork != -1) ? usefulWork : this.getLength();
475                this.resourceConsumptionProfile = new ResourceConsumptionProfile(resourceConsumptionList);
476                this.resourceConsumptionProfile.setUsefulWork(usefulWork);
477
478        }
479
480        private boolean loadResourceConsumptionProfile(){
481
482                PEUnit peUnit = (PEUnit)getUsedResources().getLast().getResourceUnits()
483                                .get(StandardResourceUnitName.PE);
484                if(peUnit instanceof ProcessingElements){
485                        ProcessingElements pes = (ProcessingElements) peUnit;
486                        for (ComputingResource resource : pes) {
487                                while(resource != null && resource.getResourceCharacteristic().getParameters().get("product") == null){
488                                        resource = resource.getParent();
489                                }
490                                if(resource != null) {
491                                        String productName = resource.getResourceCharacteristic().getParameters().get("product").get(0).getContent();
492                                        if(productName.equals("Fusion G - T40N"))
493                                                preparePhases("Fusion G - T40N");
494                                        else if(productName.equals("Atom - D510"))
495                                                preparePhases("Atom - D510");
496                                        else if(productName.equals("Atom - N2600"))
497                                                preparePhases("Atom - N2600");
498                                        else if(productName.equals("Core i7 - 2715QE"))
499                                                preparePhases("Core i7 - 2715QE");
500                                        else if(productName.equals("Core i7 - 3615QE"))
501                                                preparePhases("Core i7 - 3615QE");
502                                        else if(productName.equals("Xeon E5-2630"))
503                                                preparePhases("Xeon E5-2630");
504                                        else if(productName.equals("Xeon E5-2603"))
505                                                preparePhases("Xeon E5-2603");
506                                        else if(productName.equals("Xeon L5310"))
507                                                preparePhases("Xeon L5310");
508                                }
509                                return true;
510                        }
511                }
512                return false;
513               
514        }
515}
Note: See TracBrowser for help on using the repository browser.