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

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