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

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