1 | package dcworms.schedframe.scheduling; |
---|
2 | |
---|
3 | |
---|
4 | import gridsim.GridSim; |
---|
5 | import gridsim.dcworms.DCWormsTags; |
---|
6 | |
---|
7 | import java.util.HashMap; |
---|
8 | import java.util.LinkedList; |
---|
9 | import java.util.List; |
---|
10 | import java.util.Map; |
---|
11 | |
---|
12 | import org.joda.time.DateTime; |
---|
13 | import org.joda.time.DateTimeUtilsExt; |
---|
14 | import org.joda.time.ReadableDuration; |
---|
15 | import org.qcg.broker.schemas.resreqs.ResourceConsumptionProfileType; |
---|
16 | import org.qcg.broker.schemas.resreqs.ResourceConsumptionType; |
---|
17 | import org.qcg.broker.schemas.resreqs.StringParameterType; |
---|
18 | |
---|
19 | import qcg.shared.constants.BrokerConstants; |
---|
20 | |
---|
21 | import schedframe.resources.computing.ComputingResource; |
---|
22 | import schedframe.resources.units.PEUnit; |
---|
23 | import schedframe.resources.units.ProcessingElements; |
---|
24 | import schedframe.resources.units.StandardResourceUnitName; |
---|
25 | import schedframe.scheduling.ExecutionHistoryItem; |
---|
26 | import schedframe.scheduling.ResourceItem; |
---|
27 | import schedframe.scheduling.WorkloadUnitHandler; |
---|
28 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
29 | import schedframe.scheduling.tasks.AbstractProcesses; |
---|
30 | import schedframe.scheduling.tasks.AbstractProcessesGroup; |
---|
31 | import schedframe.scheduling.tasks.Task; |
---|
32 | import schedframe.scheduling.tasks.phases.ExecutionPhase; |
---|
33 | import schedframe.scheduling.tasks.phases.ExecutionProfile; |
---|
34 | import schedframe.scheduling.tasks.requirements.ResourceParameterName; |
---|
35 | |
---|
36 | /** |
---|
37 | * |
---|
38 | * @author Marcin Krystek |
---|
39 | * |
---|
40 | */ |
---|
41 | public 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 | this.execProfile = new ExecutionProfile(execPhases); |
---|
427 | this.execProfile.setUsefulWork(usefulWork); |
---|
428 | |
---|
429 | } |
---|
430 | |
---|
431 | private boolean loadResourceConsumptionProfile(){ |
---|
432 | |
---|
433 | PEUnit peUnit = (PEUnit)getAllocatedResources().getLast().getResourceUnits() |
---|
434 | .get(StandardResourceUnitName.PE); |
---|
435 | if(peUnit instanceof ProcessingElements){ |
---|
436 | ProcessingElements pes = (ProcessingElements) peUnit; |
---|
437 | for (ComputingResource resource : pes) { |
---|
438 | while(resource != null && resource.getResourceCharacteristic().getParameters().get("product") == null){ |
---|
439 | resource = resource.getParent(); |
---|
440 | } |
---|
441 | if(resource != null) { |
---|
442 | String productName = resource.getResourceCharacteristic().getParameters().get("product").get(0).getContent(); |
---|
443 | if(productName.equals("Fusion G - T40N")) |
---|
444 | preparePhases("Fusion G - T40N"); |
---|
445 | else if(productName.equals("Atom - D510")) |
---|
446 | preparePhases("Atom - D510"); |
---|
447 | else if(productName.equals("Atom - N2600")) |
---|
448 | preparePhases("Atom - N2600"); |
---|
449 | else if(productName.equals("Core i7 - 2715QE")) |
---|
450 | preparePhases("Core i7 - 2715QE"); |
---|
451 | else if(productName.equals("Core i7 - 3615QE")) |
---|
452 | preparePhases("Core i7 - 3615QE"); |
---|
453 | else if(productName.equals("Xeon E5-2630")) |
---|
454 | preparePhases("Xeon E5-2630"); |
---|
455 | else if(productName.equals("Xeon E5-2603")) |
---|
456 | preparePhases("Xeon E5-2603"); |
---|
457 | else if(productName.equals("Xeon L5310")) |
---|
458 | preparePhases("Xeon L5310"); |
---|
459 | } |
---|
460 | return true; |
---|
461 | } |
---|
462 | } |
---|
463 | return false; |
---|
464 | |
---|
465 | } |
---|
466 | |
---|
467 | public LinkedList<ExecutionHistoryItem> getExecutionHistory() { |
---|
468 | return execHistory; |
---|
469 | } |
---|
470 | |
---|
471 | public void addExecHistory(ExecutionHistoryItem execHistoryItem) { |
---|
472 | this.execHistory.add(execHistoryItem); |
---|
473 | } |
---|
474 | |
---|
475 | public LinkedList<ResourceItem> getAllocatedResources() { |
---|
476 | return allocatedResources; |
---|
477 | } |
---|
478 | |
---|
479 | public void addAllocatedResources(ResourceItem allocatedResourcesItem) { |
---|
480 | this.allocatedResources.add(allocatedResourcesItem); |
---|
481 | } |
---|
482 | } |
---|