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

Revision 1415, 13.9 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.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 schedName;
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    {
161                return task.isFinished();
162    }
163       
164        public void setStatus(int newStatus) throws Exception {
165                int prevStatus = status;
166           
167        if (status == newStatus) {
168            return;
169        }
170
171        if (newStatus < DCWormsTags.CREATED || newStatus > DCWormsTags.FAILED) {
172            throw new Exception("Executable.setStatuts() : Error - " +
173                    "Invalid integer range for Executable status.");
174        }
175
176        status = newStatus;
177       
178        if(status == DCWormsTags.INEXEC){
179                loadResourceConsumptionProfile();
180                task.setStatus((int) BrokerConstants.TASK_STATUS_RUNNING);
181        } else if(status == DCWormsTags.QUEUED){
182                task.setStatus((int) BrokerConstants.TASK_STATUS_QUEUED);
183        }
184       
185        double currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000; // time in seconds
186   
187               
188                if (newStatus == DCWormsTags.SUCCESS || newStatus == DCWormsTags.CANCELED) {
189            finishTime = currentTime;
190        }
191               
192                /*if(newStatus == DCWormsTags.SUBMITTED){
193                         arrivalTime = GridSim.clock();
194                }*/
195
196        if ((prevStatus == DCWormsTags.INEXEC) && (status == DCWormsTags.CANCELED || status == DCWormsTags.PAUSED ||
197                status == DCWormsTags.SUCCESS)){
198            totalCompletionTime += (currentTime -  execStartTime);
199            execStartTime = execStartTimeFirst;
200        }
201
202        /*if (prevStatus == DCWormsTags.RESUMED && status == DCWormsTags.SUCCESS) {
203            totalCompletionTime += (currentTime -  execStartTime);
204        }*/
205
206        if(prevStatus == DCWormsTags.PAUSED && status == DCWormsTags.RESUMED){
207               
208        }
209        if (status == DCWormsTags.INEXEC) {
210               
211                execStartTime = currentTime;
212            if (firstTime){
213                execStartTimeFirst = currentTime;
214                firstTime = false;
215            }
216        }
217       
218        if(status == DCWormsTags.NEW_EXEC_PHASE && prevStatus == DCWormsTags.INEXEC){
219                status = DCWormsTags.INEXEC;
220                execProfile.setCompletionPercentage(0.0);
221                execProfile.setCurrentPhase(execProfile.getCurrentPhase() + 1);
222        }
223
224        }
225       
226        public void addSpecificResource(ResourceParameterName resourceName, Object value){
227                if(this.specificResources == null)
228                        this.specificResources = new HashMap<ResourceParameterName, Object>();
229                this.specificResources.put(resourceName, value);
230        }
231
232        public boolean expectSpecificResource(ResourceParameterName resourceName){
233                if(this.specificResources == null)
234                        return false;
235                return this.specificResources.containsKey(resourceName);
236        }
237       
238        public Object getExpectedSpecificResource(ResourceParameterName resourceName){
239                if(this.specificResources == null)
240                        return null;
241               
242                return this.specificResources.get(resourceName);
243        }
244       
245        public void setReservationId(String reservationId){
246                this.reservationId = reservationId;
247        }
248       
249        public boolean requireReservation(){
250                return (reservationId != null);
251        }
252       
253        public String getReservationId(){
254                return this.reservationId;
255        }
256       
257        public double getTotalCompletionPercentage() {
258                return completionPercentageTotal;
259        }
260
261        public void setTotalCompletionPercentage(double completionPercentage) {
262                this.completionPercentageTotal = completionPercentage;
263        }
264
265    public void setSchedulerName(int resourceId){
266        this.schedName = GridSim.getEntityName(resourceId);
267    }
268   
269    public void setSchedulerName(String resourceId){
270        this.schedName = resourceId;
271    }
272
273    public String getSchedulerName(){
274        return schedName;
275    }
276       
277        public int getEstimatedDuration(){
278                return this.estimatedDuration;
279        }
280       
281        public void setEstimatedDuration(int value){
282                this.estimatedDuration = value;
283        }
284
285        public boolean isRegistered() {
286                return task.isRegistered();
287        }
288
289        public void register(JobRegistryImpl jobRegistry) {
290                task.register(jobRegistry);
291        }
292
293        public ReadableDuration getExpectedDuration() throws NoSuchFieldException {
294                return task.getExpectedDuration();
295        }
296
297        public double getParameterDoubleValue(ResourceParameterName parameterName)
298                        throws NoSuchFieldException, IllegalArgumentException {
299                return task.getParameterDoubleValue(parameterName);
300        }
301
302        public String getParameterStringValue(ResourceParameterName parameterName)
303                        throws NoSuchFieldException, IllegalArgumentException {
304                return task.getParameterStringValue(parameterName);
305        }
306       
307        public double getCpuCntRequest() throws NoSuchFieldException{
308                return getParameterDoubleValue(ResourceParameterName.CPUCOUNT);
309        }
310       
311        public double getMemoryRequest() throws NoSuchFieldException{
312                return getParameterDoubleValue(ResourceParameterName.MEMORY);
313        }
314       
315        public DateTime getExecutionEndTime() throws NoSuchFieldException {
316                return task.getExecutionEndTime();
317        }
318
319        public DateTime getExecutionStartTime() throws NoSuchFieldException {
320                return task.getExecutionStartTime();
321        }
322
323        public DateTime getSubmissionTimeToBroker() {
324                return task.getSubmissionTimeToBroker();
325        }
326
327        public long getWorkloadLogWaitTime() {
328                return task.getWorkloadLogWaitTime();
329        }
330
331    public double getExecStartTime() {
332        return execStartTime;
333    }
334
335    public double getFinishTime() {
336        return finishTime;
337    }
338
339    public double getSubmissionTime() {
340        return submissionTime;
341    }
342   
343    public double getWaitingTime() {
344        return execStartTime - submissionTime;
345    }
346   
347    public void finalizeExecutable(){
348                try {
349                        setStatus(DCWormsTags.SUCCESS);
350                } catch (Exception e) {
351                        // TODO Auto-generated catch block
352                        e.printStackTrace();
353                }
354    }
355   
356        public void accept(WorkloadUnitHandler wuh) {
357                wuh.handleExecutable(this);
358        }
359   
360        /*public boolean equals(Object obj){
361                if(obj instanceof Executable){
362                        Executable t = (Executable) obj;
363                        return getId().equals(t.getId()) && getJobId().equals(t.getJobId());
364                }
365                return false;
366        }*/
367       
368        public ExecutionProfile getExecutionProfile(){
369                return execProfile;
370        }
371
372        public String getApplicationName(){
373                return task.getApplicationName();
374        }
375       
376        private void preparePhases(String resourceType) {
377                LinkedList<ExecutionPhase> resourceConsumptionList = new LinkedList<ExecutionPhase>();
378               
379                long usefulWork = -1;
380               
381                if(task.getDescription().getExecution() == null || task.getDescription().getExecution().getResourceConsumptionProfile() == null
382                                || task.getDescription().getExecution().getResourceConsumptionProfile().length == 0 ){
383                        ExecutionPhase resConsumption = null;
384                        try {
385                                resConsumption = new ExecutionPhase(getLength(), task.getComputingResourceRequirements());
386                        } catch (NoSuchFieldException e) {
387                                // TODO Auto-generated catch block
388                                e.printStackTrace();
389                        }
390                        resourceConsumptionList.add(resConsumption);
391                } else {
392                        boolean found = false;
393                        if(resourceType != null){
394                                for(ResourceConsumptionProfileType resConsumptioProfile: task.getDescription().getExecution().getResourceConsumptionProfile()){
395                                        if(resConsumptioProfile.getType() != null && resConsumptioProfile.getType().equals(resourceType)){
396                                                for(ResourceConsumptionType resConsumption: resConsumptioProfile.getResourceConsumption()){
397                                                        ExecutionPhase resourceConsumption = new ExecutionPhase(resConsumption);
398                                                        resourceConsumptionList.add(resourceConsumption);
399                                                }
400                                                for(StringParameterType prop: resConsumptioProfile.getProperties()){
401                                                        if(prop.getName().equals("usefulWork")){
402                                                                usefulWork = Double.valueOf(prop.getContent()).longValue();
403                                                                break;
404                                                        }
405                                                }
406                                                found = true;
407                                                break;
408                                        }
409                                }       
410                        }                       
411                        if(!found){
412                                for(ResourceConsumptionType resConsumption: task.getDescription().getExecution().getResourceConsumptionProfile()[0].getResourceConsumption()){
413                                        ExecutionPhase resourceConsumption = new ExecutionPhase(resConsumption);
414                                        resourceConsumptionList.add(resourceConsumption);
415                                }
416                               
417                                for(StringParameterType prop: task.getDescription().getExecution().getResourceConsumptionProfile()[0].getProperties()){
418                                        if(prop.getName().equals("usefulWork")){
419                                                usefulWork = Double.valueOf(prop.getContent()).longValue();
420                                                break;
421                                        }
422                                }
423                        }
424                }
425               
426                usefulWork = (usefulWork != -1) ? usefulWork : this.getLength();
427                this.execProfile = new ExecutionProfile(resourceConsumptionList);
428                this.execProfile.setUsefulWork(usefulWork);
429
430        }
431
432        private boolean loadResourceConsumptionProfile(){
433
434                PEUnit peUnit = (PEUnit)getAllocatedResources().getLast().getResourceUnits()
435                                .get(StandardResourceUnitName.PE);
436                if(peUnit instanceof ProcessingElements){
437                        ProcessingElements pes = (ProcessingElements) peUnit;
438                        for (ComputingResource resource : pes) {
439                                while(resource != null && resource.getResourceCharacteristic().getParameters().get("product") == null){
440                                        resource = resource.getParent();
441                                }
442                                if(resource != null) {
443                                        String productName = resource.getResourceCharacteristic().getParameters().get("product").get(0).getContent();
444                                        if(productName.equals("Fusion G - T40N"))
445                                                preparePhases("Fusion G - T40N");
446                                        else if(productName.equals("Atom - D510"))
447                                                preparePhases("Atom - D510");
448                                        else if(productName.equals("Atom - N2600"))
449                                                preparePhases("Atom - N2600");
450                                        else if(productName.equals("Core i7 - 2715QE"))
451                                                preparePhases("Core i7 - 2715QE");
452                                        else if(productName.equals("Core i7 - 3615QE"))
453                                                preparePhases("Core i7 - 3615QE");
454                                        else if(productName.equals("Xeon E5-2630"))
455                                                preparePhases("Xeon E5-2630");
456                                        else if(productName.equals("Xeon E5-2603"))
457                                                preparePhases("Xeon E5-2603");
458                                        else if(productName.equals("Xeon L5310"))
459                                                preparePhases("Xeon L5310");
460                                }
461                                return true;
462                        }
463                }
464                return false;
465               
466        }
467
468        public LinkedList<ExecutionHistoryItem> getExecutionHistory() {
469                return execHistory;
470        }
471
472        public void addExecHistory(ExecutionHistoryItem execHistoryItem) {
473                this.execHistory.add(execHistoryItem);
474        }
475
476        public LinkedList<ResourceItem> getAllocatedResources() {
477                return allocatedResources;
478        }
479
480        public void addAllocatedResources(ResourceItem allocatedResourcesItem) {
481                this.allocatedResources.add(allocatedResourcesItem);
482        }
483}
Note: See TracBrowser for help on using the repository browser.