package dcworms.schedframe.scheduling; import gridsim.GridSim; import gridsim.dcworms.DCWormsTags; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.joda.time.DateTime; import org.joda.time.DateTimeUtilsExt; import org.joda.time.ReadableDuration; import org.qcg.broker.schemas.resreqs.ResourceConsumptionProfileType; import org.qcg.broker.schemas.resreqs.ResourceConsumptionType; import org.qcg.broker.schemas.resreqs.StringParameterType; import qcg.shared.constants.BrokerConstants; import schedframe.resources.computing.ComputingResource; import schedframe.resources.units.PEUnit; import schedframe.resources.units.ProcessingElements; import schedframe.resources.units.StandardResourceUnitName; import schedframe.scheduling.ExecutionHistoryItem; import schedframe.scheduling.ResourceItem; import schedframe.scheduling.WorkloadUnitHandler; import schedframe.scheduling.manager.tasks.JobRegistryImpl; import schedframe.scheduling.tasks.AbstractProcesses; import schedframe.scheduling.tasks.AbstractProcessesGroup; import schedframe.scheduling.tasks.Task; import schedframe.scheduling.tasks.phases.ExecutionPhase; import schedframe.scheduling.tasks.phases.ExecutionProfile; import schedframe.scheduling.tasks.requirements.ResourceParameterName; /** * * @author Marcin Krystek * */ public class Executable implements ExecTask{ protected Task task; protected String processesSetId; protected int status; protected Map specificResources; protected String reservationId; protected double completionPercentageTotal; protected int estimatedDuration; protected String schedulerName; protected double submissionTime; protected double arrivalTime; protected double execStartTime; protected double totalCompletionTime; protected double finishTime; private boolean firstTime = true; protected double execStartTimeFirst; protected ExecutionProfile execProfile; protected LinkedList execHistory; protected LinkedList allocatedResources; public Executable(Task t){ this.task = t; this.status = DCWormsTags.CREATED; this.allocatedResources = new LinkedList(); this.execHistory = new LinkedList(); init(); } public Executable(Task t, AbstractProcesses procesesSet){ this.task = t; this.status = DCWormsTags.CREATED; this.processesSetId = procesesSet.getId(); this.allocatedResources = new LinkedList(); this.execHistory = new LinkedList(); init(); } protected void init() { double currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000; this.arrivalTime = currentTime; this.submissionTime = currentTime; this.totalCompletionTime = 0.0; preparePhases(null); } public int getUserId() { return task.getSenderId(); } public String getUserDN() { return task.getUserDN(); } public String getJobId() { return task.getJobId(); } public String getTaskId(){ return this.task.getId(); } public String getId() { if(processesSetId == null) return task.getId(); else return task.getId() + "_" + processesSetId; } public String getProcessesId(){ return this.processesSetId; } public int getUniqueId(){ if(processesSetId == null){ return (task.getJobId() + "_" + task.getId()).hashCode(); } else { return (task.getJobId() + "_" + task.getId() + "_" + processesSetId).hashCode(); } } public List getProcesses() { return task.getProcesses(); } public List getProcesses( AbstractProcessesGroup processGroup) { return task.getProcesses(processGroup); } public List getProcessesGroups() { return task.getProcessesGroups(); } public org.qcg.broker.schemas.resreqs.Task getDescription() { return task.getDescription(); } public String getDocument() throws Exception { return task.getDocument(); } public long getLength() { return task.getLength(); } public int getStatus() { return status; } public boolean isFinished(){ return task.isFinished(); } public void setStatus(int newStatus) throws Exception { int prevStatus = status; if (status == newStatus) { return; } if (newStatus < DCWormsTags.CREATED || newStatus > DCWormsTags.FAILED) { throw new Exception("Executable.setStatuts() : Error - " + "Invalid integer range for Executable status."); } status = newStatus; if(status == DCWormsTags.INEXEC){ loadResourceConsumptionProfile(); task.setStatus((int) BrokerConstants.TASK_STATUS_RUNNING); } else if(status == DCWormsTags.QUEUED){ task.setStatus((int) BrokerConstants.TASK_STATUS_QUEUED); } double currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000; // time in seconds if (newStatus == DCWormsTags.SUCCESS || newStatus == DCWormsTags.CANCELED) { finishTime = currentTime; } /*if(newStatus == DCWormsTags.SUBMITTED){ arrivalTime = GridSim.clock(); }*/ if ((prevStatus == DCWormsTags.INEXEC) && (status == DCWormsTags.CANCELED || status == DCWormsTags.PAUSED || status == DCWormsTags.SUCCESS)){ totalCompletionTime += (currentTime - execStartTime); execStartTime = execStartTimeFirst; } /*if (prevStatus == DCWormsTags.RESUMED && status == DCWormsTags.SUCCESS) { totalCompletionTime += (currentTime - execStartTime); }*/ if(prevStatus == DCWormsTags.PAUSED && status == DCWormsTags.RESUMED){ } if (status == DCWormsTags.INEXEC) { execStartTime = currentTime; if (firstTime){ execStartTimeFirst = currentTime; firstTime = false; } } if(status == DCWormsTags.NEW_EXEC_PHASE && prevStatus == DCWormsTags.INEXEC){ status = DCWormsTags.INEXEC; execProfile.setCompletionPercentage(0.0); execProfile.setCurrentPhase(execProfile.getCurrentPhase() + 1); } } public void addSpecificResource(ResourceParameterName resourceName, Object value){ if(this.specificResources == null) this.specificResources = new HashMap(); this.specificResources.put(resourceName, value); } public boolean expectSpecificResource(ResourceParameterName resourceName){ if(this.specificResources == null) return false; return this.specificResources.containsKey(resourceName); } public Object getExpectedSpecificResource(ResourceParameterName resourceName){ if(this.specificResources == null) return null; return this.specificResources.get(resourceName); } public void setReservationId(String reservationId){ this.reservationId = reservationId; } public boolean requireReservation(){ return (reservationId != null); } public String getReservationId(){ return this.reservationId; } public double getTotalCompletionPercentage() { return completionPercentageTotal; } public void setTotalCompletionPercentage(double completionPercentage) { this.completionPercentageTotal = completionPercentage; } public void setSchedulerName(int resourceId){ this.schedulerName = GridSim.getEntityName(resourceId); } public void setSchedulerName(String resourceId){ this.schedulerName = resourceId; } public String getSchedulerName(){ return schedulerName; } public int getEstimatedDuration(){ return this.estimatedDuration; } public void setEstimatedDuration(int value){ this.estimatedDuration = value; } public boolean isRegistered() { return task.isRegistered(); } public void register(JobRegistryImpl jobRegistry) { task.register(jobRegistry); } public ReadableDuration getExpectedDuration() throws NoSuchFieldException { return task.getExpectedDuration(); } public double getParameterDoubleValue(ResourceParameterName parameterName) throws NoSuchFieldException, IllegalArgumentException { return task.getParameterDoubleValue(parameterName); } public String getParameterStringValue(ResourceParameterName parameterName) throws NoSuchFieldException, IllegalArgumentException { return task.getParameterStringValue(parameterName); } public double getCpuCntRequest() throws NoSuchFieldException{ return getParameterDoubleValue(ResourceParameterName.CPUCOUNT); } public double getMemoryRequest() throws NoSuchFieldException{ return getParameterDoubleValue(ResourceParameterName.MEMORY); } public DateTime getExecutionEndTime() throws NoSuchFieldException { return task.getExecutionEndTime(); } public DateTime getExecutionStartTime() throws NoSuchFieldException { return task.getExecutionStartTime(); } public DateTime getSubmissionTimeToBroker() { return task.getSubmissionTimeToBroker(); } public long getWorkloadLogWaitTime() { return task.getWorkloadLogWaitTime(); } public double getExecStartTime() { return execStartTime; } public double getFinishTime() { return finishTime; } public double getSubmissionTime() { return submissionTime; } public double getWaitingTime() { return execStartTime - submissionTime; } public void finalizeExecutable(){ try { setStatus(DCWormsTags.SUCCESS); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void accept(WorkloadUnitHandler wuh) { wuh.handleExecutable(this); } /*public boolean equals(Object obj){ if(obj instanceof Executable){ Executable t = (Executable) obj; return getId().equals(t.getId()) && getJobId().equals(t.getJobId()); } return false; }*/ public ExecutionProfile getExecutionProfile(){ return execProfile; } public String getApplicationName(){ return task.getApplicationName(); } private void preparePhases(String resourceType) { LinkedList execPhases = new LinkedList(); long usefulWork = -1; if(task.getDescription().getExecution() == null || task.getDescription().getExecution().getResourceConsumptionProfile() == null || task.getDescription().getExecution().getResourceConsumptionProfile().length == 0 ){ ExecutionPhase resConsumption = null; try { resConsumption = new ExecutionPhase(getLength(), task.getComputingResourceRequirements()); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } execPhases.add(resConsumption); } else { boolean found = false; if(resourceType != null){ for(ResourceConsumptionProfileType resConsumptioProfile: task.getDescription().getExecution().getResourceConsumptionProfile()){ if(resConsumptioProfile.getType() != null && resConsumptioProfile.getType().equals(resourceType)){ for(ResourceConsumptionType resConsumption: resConsumptioProfile.getResourceConsumption()){ ExecutionPhase resourceConsumption = new ExecutionPhase(resConsumption); execPhases.add(resourceConsumption); } for(StringParameterType prop: resConsumptioProfile.getProperties()){ if(prop.getName().equals("usefulWork")){ usefulWork = Double.valueOf(prop.getContent()).longValue(); break; } } found = true; break; } } } if(!found){ for(ResourceConsumptionType resConsumption: task.getDescription().getExecution().getResourceConsumptionProfile()[0].getResourceConsumption()){ ExecutionPhase resourceConsumption = new ExecutionPhase(resConsumption); execPhases.add(resourceConsumption); } for(StringParameterType prop: task.getDescription().getExecution().getResourceConsumptionProfile()[0].getProperties()){ if(prop.getName().equals("usefulWork")){ usefulWork = Double.valueOf(prop.getContent()).longValue(); break; } } } } usefulWork = (usefulWork != -1) ? usefulWork : this.getLength(); double tempCompletionPercentage = this.execProfile != null ? this.execProfile.getCompletionPercentage() : 0; this.execProfile = new ExecutionProfile(execPhases); this.execProfile.setUsefulWork(usefulWork); this.execProfile.setCompletionPercentage(tempCompletionPercentage); } private boolean loadResourceConsumptionProfile(){ PEUnit peUnit = (PEUnit)getAllocatedResources().getLast().getResourceUnits() .get(StandardResourceUnitName.PE); if(peUnit instanceof ProcessingElements){ ProcessingElements pes = (ProcessingElements) peUnit; for (ComputingResource resource : pes) { while(resource != null && resource.getResourceCharacteristic().getParameters().get("product") == null){ resource = resource.getParent(); } if(resource != null) { String productName = resource.getResourceCharacteristic().getParameters().get("product").get(0).getContent(); if(productName.equals("Fusion G - T40N")) preparePhases("Fusion G - T40N"); else if(productName.equals("Atom - D510")) preparePhases("Atom - D510"); else if(productName.equals("Atom - N2600")) preparePhases("Atom - N2600"); else if(productName.equals("Core i7 - 2715QE")) preparePhases("Core i7 - 2715QE"); else if(productName.equals("Core i7 - 3615QE")) preparePhases("Core i7 - 3615QE"); else if(productName.equals("Xeon E5-2630")) preparePhases("Xeon E5-2630"); else if(productName.equals("Xeon E5-2603")) preparePhases("Xeon E5-2603"); else if(productName.equals("Xeon L5310")) preparePhases("Xeon L5310"); else preparePhases(productName); } else preparePhases(null); return true; } } return false; } public LinkedList getExecutionHistory() { return execHistory; } public void addExecHistory(ExecutionHistoryItem execHistoryItem) { this.execHistory.add(execHistoryItem); } public LinkedList getAllocatedResources() { return allocatedResources; } public void addAllocatedResources(ResourceItem allocatedResourcesItem) { this.allocatedResources.add(allocatedResourcesItem); } }