From: <mar...@us...> - 2008-04-09 01:48:20
|
Revision: 168 http://gridsim.svn.sourceforge.net/gridsim/?rev=168&view=rev Author: marcos_dias Date: 2008-04-08 18:48:26 -0700 (Tue, 08 Apr 2008) Log Message: ----------- This update includes the implementation of gridlet cancellation in the multiple partition easy backfilling resource allocation policy. Modified Paths: -------------- branches/gridsim4.0-branch3/source/gridsim/turbo/EBMultiplePartitions.java branches/gridsim4.0-branch3/source/gridsim/turbo/MPAvailabilityProfile.java Added Paths: ----------- branches/gridsim4.0-branch3/examples/examples/workload/parallel/TurboExampleEBMultiQueuesCancellation.java Added: branches/gridsim4.0-branch3/examples/examples/workload/parallel/TurboExampleEBMultiQueuesCancellation.java =================================================================== --- branches/gridsim4.0-branch3/examples/examples/workload/parallel/TurboExampleEBMultiQueuesCancellation.java (rev 0) +++ branches/gridsim4.0-branch3/examples/examples/workload/parallel/TurboExampleEBMultiQueuesCancellation.java 2008-04-09 01:48:26 UTC (rev 168) @@ -0,0 +1,215 @@ +/* + * Author Marcos Dias de Assuncao + * Date: February 2008 + * + * Description: This example shows how to create a resource using an aggressive + * backfilling policy without advance reservation features but with multiple + * partitions. The jobs are read from a trace file. Additionally, this example + * cancels some gridlets submitted. This example has been implemented in order + * to test the job cancellation features. To test this example, you can use + * one of the traces included in the workloads directory. + */ + +package examples.workload.parallel; + +import examples.QueuePredicateExample; +import examples.WorkloadWithCancellation; +import gridsim.GridResource; +import gridsim.GridSim; +import gridsim.Machine; +import gridsim.MachineList; +import gridsim.ResourceCalendar; +import gridsim.turbo.EBMultiplePartitions; +import gridsim.turbo.TResourceCharacteristics; +import gridsim.util.Workload; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.LinkedList; +import java.util.Random; + +/** + * Test Driver class for this example + */ +public class TurboExampleEBMultiQueuesCancellation +{ + /** + * Creates main() to run this example + */ + public static void main(String[] args) + { + long startTime = System.currentTimeMillis(); + if(args.length == 0){ + System.out.println("Please provide the location of the workload file!"); + System.exit(1); + } + + try { + + ////////////////////////////////////////// + // Get the workload to be used The format should be: + // ASCII text, gzip or zip. + String fileName = args[0]; + ArrayList<GridResource> resources = new ArrayList<GridResource>(); + + ////////////////////////////////////////// + // Initialize the GridSim package. It should be called + // before creating any entities. We can't run this example without + // initializing GridSim first. We will get run-time exception + // error. + + // number of grid user entities + any Workload entities. + int num_user = 1; + Calendar calendar = Calendar.getInstance(); + boolean trace_flag = false; // mean trace GridSim events + + // Initialize the GridSim package without any statistical + // functionalities. Hence, no GridSim_stat.txt file is created. + System.out.println("Initializing GridSim package"); + GridSim.init(num_user, calendar, trace_flag); + + ////////////////////////////////////////// + // Creates one or more GridResource entities + int totalResource = 1; // total number of Grid resources + int rating = 377; // rating of each PE in MIPS + int totalPE = 9; // total number of PEs for each Machine + int totalMachine = 128; // total number of Machines + int i = 0; + + String[] resArray = new String[totalResource]; + for (i = 0; i < totalResource; i++) { + String resName = "Res_" + i; + GridResource resource = createGridResource(resName, rating, totalMachine, totalPE); + resources.add(resource); + + // add a resource name into an array + resArray[i] = resName; + } + + ////////////////////////////////////////// + // Creates one Workload trace entity. + + int resID = 0; + Random r = new Random(); + resID = r.nextInt(totalResource); + WorkloadWithCancellation workload = + new WorkloadWithCancellation("Load_1", fileName, resArray[resID], rating); + + ////////////////////////////////////////// + // Starts the simulation in debug mode + GridSim.startGridSimulation(true); + + // Start the simulation in normal mode +// GridSim.startGridSimulation(); + + ////////////////////////////////////////// + // Final step: Prints the Gridlets when simulation is over + long finishTime = System.currentTimeMillis(); + System.out.println("The simulation took " + (finishTime - startTime) + " milliseconds"); + + // prints the Gridlets inside a Workload entity + // workload.printGridletList(trace_flag); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Creates one Grid resource. A Grid resource contains one or more + * Machines. Similarly, a Machine contains one or more PEs (Processing + * Elements or CPUs). + * @param name a Grid Resource name + * @param peRating rating of each PE + * @param totalMachine total number of Machines + * @param totalPE total number of PEs for each Machine + */ + private static GridResource createGridResource(String name, int peRating, + int totalMachine, int totalPE) + { + + ////////////////////////////////////////// + // Here are the steps needed to create a Grid resource: + // 1. We need to create an object of MachineList to store one or more + // Machines + MachineList mList = new MachineList(); + + for (int i = 0; i < totalMachine; i++) + { + ////////////////////////////////////////// + // 4. Create one Machine with its id and list of PEs or CPUs + mList.add( new Machine(i, totalPE, peRating) ); + } + + ////////////////////////////////////////// + // 5. Create a ResourceCharacteristics object that stores the + // properties of a Grid resource: architecture, OS, list of + // Machines, allocation policy: time- or space-shared, time zone + // and its price (G$/PE time unit). + String arch = "Sun Ultra"; // system architecture + String os = "Solaris"; // operating system + double time_zone = 0.0; // time zone this resource located + double cost = 3.0; // the cost of using this resource + + // this resource will use an aggressive backfilling policy with + // multiple partitions and without advance reservation features + TResourceCharacteristics resConfig = new TResourceCharacteristics( + arch, os, mList, TResourceCharacteristics.EB_MULTI_PARTITIONS, + time_zone, cost); + + EBMultiplePartitions policy = null; + try { + policy = new EBMultiplePartitions(name, "Policy", 3); + } catch (Exception e1) { + e1.printStackTrace(); + } + + // creates three partitions, one for short jobs, one for medium length jobs + // and another for long jobs + QueuePredicateExample express = new QueuePredicateExample(0, 1000, peRating); + QueuePredicateExample medium = new QueuePredicateExample(1000, 10000, peRating); + QueuePredicateExample large = new QueuePredicateExample(10000, Integer.MAX_VALUE, peRating); + + // divide the resources equally amongst the partitions + policy.createPartition(0, resConfig.getNumPE() / 3, express); + policy.createPartition(1, resConfig.getNumPE() / 3, medium); + policy.createPartition(2, resConfig.getNumPE() / 3, large); + + // By default, the partitions will borrow PEs from one another + // when they require and the lending partitions are not using the PEs. + // You can change this behaviour by removing the comment from the line below +// policy.setAllowBorrowing(false); + + ////////////////////////////////////////// + // 6. Finally, we need to create a GridResource object. + double baud_rate = 10000.0; // communication speed + long seed = 11L*13*17*19*23+1; + double peakLoad = 0.0; // the resource load during peak hour + double offPeakLoad = 0.0; // the resource load during off-peak hr + double holidayLoad = 0.0; // the resource load during holiday + + // incorporates weekends so the grid resource is on 7 days a week + LinkedList weekends = new LinkedList(); + weekends.add(new Integer(Calendar.SATURDAY)); + weekends.add(new Integer(Calendar.SUNDAY)); + + // incorporates holidays. However, no holidays are set in this example + LinkedList holidays = new LinkedList(); + GridResource gridRes = null; + + ResourceCalendar resCalendar = new ResourceCalendar(time_zone, + peakLoad, offPeakLoad, holidayLoad, weekends, + holidays, seed); + + try { + gridRes = new GridResource(name, baud_rate, resConfig, + resCalendar, policy); + } + catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("Creates one Grid resource with name = " + name); + return gridRes; + } +} // end class Modified: branches/gridsim4.0-branch3/source/gridsim/turbo/EBMultiplePartitions.java =================================================================== --- branches/gridsim4.0-branch3/source/gridsim/turbo/EBMultiplePartitions.java 2008-04-07 23:06:47 UTC (rev 167) +++ branches/gridsim4.0-branch3/source/gridsim/turbo/EBMultiplePartitions.java 2008-04-09 01:48:26 UTC (rev 168) @@ -444,17 +444,125 @@ return -1; } - /** - * Cancels a Gridlet running or in the waiting queue.<br> - * <b>NOTE: Not implemented YET.</b> + /** + * Cancels a Gridlet running or in the waiting queue. + * This method will search the running and waiting queues. + * The User ID is important as many users might have the same + * Gridlet ID in the lists. If a Gridlet is cancelled and the gridlet was + * either running or was a pivot, then the availability profile is updated. + * After that, the backfilling algorithm is called to check which + * gridlets can be started or scheduled. + * <b>NOTE:</b> + * <ul> + * <li> Before cancelling a Gridlet, this method updates when + * the expected completion time of the Gridlet is. If the + * completion time is smaller than the current time, then + * the Gridlet is considered to be <tt>finished</tt>. + * Therefore the Gridlet cannot be cancelled. + * + * <li> Once a Gridlet has been cancelled, it cannot be resumed + * to execute again since this method will pass the Gridlet + * back to sender, i.e. the <tt>userId</tt>. + * + * <li> If a Gridlet cannot be found in either execution and + * waiting lists, then a <tt>null</tt> Gridlet will be send back + * to sender, i.e. the <tt>userId</tt>. + * </ul> * @param gridletId a Gridlet ID * @param userId the user or owner's ID of this Gridlet * @pre gridletId > 0 * @pre userId > 0 */ public void gridletCancel(int gridletId, int userId) { - System.out.println(super.get_name() + - ".gridletCancel(): not supported at the moment."); + double currentTime = GridSim.clock(); + + // stores the gridlet if found + SSGridlet sgl = null; + boolean found = false; + boolean updateProfile = false; + + // Look for the Gridlet in the running queue + sgl = findSSGridlet(runningGridlets_, gridletId, userId); + if (sgl != null) { + found = true; + + // if the Gridlet's finish time is smaller than the current + // time or the status is FINISHED, it means that the Gridlet + // has finished, but has not been removed from the running + // queue yet. It will probably be done shortly, + // so just send a null to the user. + if(sgl.getStatus() == Gridlet.SUCCESS || + sgl.getActualFinishTime() <= currentTime){ + super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, + null, gridletId, userId); + return; // return as it is impossible to cancel the Gridlet + } + else{ + // remove from the queue before compressing the schedule + runningGridlets_.remove(sgl); + updateProfile = true; + } + } + + if(!found) { + // Look for the gridlet in the waiting queue + sgl = findSSGridlet(queuedGridlets_, gridletId, userId); + if (sgl != null) { + found = true; + + // Get the Gridlet from the waiting queue and + // remove from the queue before compressing the schedule + queuedGridlets_.remove(sgl); + + // if start time has been set, then it means that gridlet is + // a pivot. So, we need to set the pivot of the corresponding + // queue partition to null + if(sgl.getStartTime() > 0) { + EasyBackFillingPartition pt = getPartition(sgl.getPartitionID()); + pt.pivot_ = null; + updateProfile = true; + } + } + } + + // If the Gridlet could not be found in neither queue, then send a null + // Gridlet to the user and return because there is no need to + // compress the schedule + if(!found) { + System.out.println(super.get_name() + ".gridletCancel():" + + " Cannot find Gridlet #" + gridletId + " for User #" + userId); + super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, + null, gridletId, userId); + return; + } + + // check whether the Gridlet is running + sgl.setStatus(Gridlet.CANCELED); + + //----------------- USED FOR DEBUGGING PURPOSES ONLY ------------------- + + // If a gridlet has been cancelled, then inform the listeners + GridSim.notifyListeners(this.get_id(), AllocationAction.ITEM_CANCELLED, true, sgl); + + //---------------------------------------------------------------------- + + // remove/update entries of the gridlet in the profile + if(updateProfile) { + removeGridlet(sgl); + backfillGridlets(currentTime); + + //------------------- USED FOR DEBUGGING PURPOSES ONLY ----------------- + + // Inform the listeners about the new schedule + GridSim.notifyListeners(this.get_id(), AllocationAction.SCHEDULE_CHANGED, true); + + //---------------------------------------------------------------------- + } + + // sends the Gridlet back to user + sgl.finalizeGridlet(); + super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, + sgl.getGridlet(), gridletId, userId); } /** @@ -906,6 +1014,20 @@ return gridletStarted; } + // ------------------------- PRIVATE METHODS ---------------------------- + + /** + * This method removes/updates all the entries of a gridlet from the profile. + * @param gridlet the Gridlet to be removed + */ + private void removeGridlet(SSGridlet gridlet) { + if(!gridlet.hasReserved()) { + + // removes the gridlet from the profile + profile_.updateEntriesAtProfile(gridlet); + } + } + /* * This method obtains the queue from the profile and does a casting */ Modified: branches/gridsim4.0-branch3/source/gridsim/turbo/MPAvailabilityProfile.java =================================================================== --- branches/gridsim4.0-branch3/source/gridsim/turbo/MPAvailabilityProfile.java 2008-04-07 23:06:47 UTC (rev 167) +++ branches/gridsim4.0-branch3/source/gridsim/turbo/MPAvailabilityProfile.java 2008-04-09 01:48:26 UTC (rev 168) @@ -949,14 +949,14 @@ transferPEs(partitionId, allocatedRanges, startTime, finishTime); MPProfileEntry entry = getPrecedingEntry(startTime); - if(entry.getTime() == startTime) { + if(entry != null && entry.getTime() == startTime) { entry.decreaseGridlet(); if(entry.getNumGridlets() == 0) super.remove(entry); } entry = getPrecedingEntry(finishTime); - if(entry.getTime() == startTime) { + if(entry != null && entry.getTime() == finishTime) { entry.decreaseGridlet(); if(entry.getNumGridlets() == 0) super.remove(entry); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |