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