📄 timeshared.java
字号:
/* * Title: GridSim Toolkit * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation * of Parallel and Distributed Systems such as Clusters and Grids * Licence: GPL - http://www.gnu.org/copyleft/gpl.html * * $Id: TimeShared.java,v 1.39 2006/03/09 05:56:32 anthony Exp $ */package gridsim;import java.util.Iterator;import gridsim.*;import eduni.simjava.*;/** * TimeShared class is an allocation policy for GridResource that behaves * similar to a round robin algorithm, except that all Gridlets are * executed at the same time. * This is a basic and simple * scheduler that runs each Gridlet to one Processing Element (PE). * If a Gridlet requires more than one PE, then this scheduler only assign * this Gridlet to one PE. * * @author Manzur Murshed and Rajkumar Buyya * @author Anthony Sulistio (re-written this class) * @since GridSim Toolkit 2.2 * @see gridsim.GridSim * @see gridsim.ResourceCharacteristics * @invariant $none */class TimeShared extends AllocPolicy{ private ResGridletList gridletInExecList_; // storing exec Gridlets private ResGridletList gridletPausedList_; // storing Paused Gridlets private double lastUpdateTime_; // a timer to denote the last update time private MIShares share_; // a temp variable /** * Allocates a new TimeShared object * @param resourceName the GridResource entity name that will contain * this allocation policy * @param entityName this object entity name * @throws Exception This happens when one of the following scenarios occur: * <ul> * <li> creating this entity before initializing GridSim package * <li> this entity name is <tt>null</tt> or empty * <li> this entity has <tt>zero</tt> number of PEs (Processing * Elements). <br> * No PEs mean the Gridlets can't be processed. * A GridResource must contain one or more Machines. * A Machine must contain one or more PEs. * </ul> * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], * String) * @pre resourceName != null * @pre entityName != null * @post $none */ TimeShared(String resourceName, String entityName) throws Exception { super(resourceName, entityName); // initialises local data structure this.gridletInExecList_ = new ResGridletList(); this.gridletPausedList_ = new ResGridletList(); this.share_ = new MIShares(); this.lastUpdateTime_ = 0.0; } ////////////////////// INTERNAL CLASS ///////////////////////////////// /** * Gridlets MI share in Time Shared Mode */ private class MIShares { /** maximum amount of MI share Gridlets can get */ public double max; /** minimum amount of MI share Gridlets can get when * it is executed on a PE that runs one extra Gridlet */ public double min; /** Total number of Gridlets that get Max share */ public int maxCount; /** * Default constructor that initializes all attributes to 0 * @pre $none * @post $none */ public MIShares() { max = 0.0; min = 0.0; maxCount = 0; } } // end of internal class /////////////////////// End of Internal Class ///////////////////////// /** * Handles internal events that are coming to this entity. * @pre $none * @post $none */ public void body() { // a loop that is looking for internal events only Sim_event ev = new Sim_event(); while ( Sim_system.running() ) { super.sim_get_next(ev); // if the simulation finishes then exit the loop if (ev.get_tag() == GridSimTags.END_OF_SIMULATION || super.isEndSimulation() == true) { break; } // Internal Event if the event source is this entity if (ev.get_src() == super.myId_) { internalEvent(); } } // CHECK for ANY INTERNAL EVENTS WAITING TO BE PROCESSED while (super.sim_waiting() > 0) { // wait for event and ignore since it is likely to be related to // internal event scheduled to update Gridlets processing super.sim_get_next(ev); System.out.println(super.resName_ + ".TimeShared.body(): ignoring internal events"); } } /** * Schedules a new Gridlet that has been received by the GridResource * entity. * @param gl a Gridlet object that is going to be executed * @param ack an acknowledgement, i.e. <tt>true</tt> if wanted to know * whether this operation is success or not, <tt>false</tt> * otherwise (don't care) * @pre gl != null * @post $none */ public void gridletSubmit(Gridlet gl, boolean ack) { // update Gridlets in execution up to this point in time updateGridletProcessing(); // reset number of PE since at the moment, it is not supported if (gl.getNumPE() > 1) { String userName = GridSim.getEntityName( gl.getUserID() ); System.out.println(); System.out.println(super.get_name() + ".gridletSubmit(): " + " Gridlet #" + gl.getGridletID() + " from " + userName + " user requires " + gl.getNumPE() + " PEs."); System.out.println("--> Process this Gridlet to 1 PE only."); System.out.println(); // also adjusted the length because the number of PEs are reduced int numPE = gl.getNumPE(); double len = gl.getGridletLength(); gl.setGridletLength(len*numPE); gl.setNumPE(1); } // adds a Gridlet to the in execution list ResGridlet rgl = new ResGridlet(gl); rgl.setGridletStatus(Gridlet.INEXEC); // set the Gridlet status to exec gridletInExecList_.add(rgl); // add into the execution list // sends back an ack if required if (ack == true) { super.sendAck(GridSimTags.GRIDLET_SUBMIT_ACK, true, gl.getGridletID(), gl.getUserID() ); } // forecast all Gridlets in the execution list forecastGridlet(); } /** * Finds the status of a specified Gridlet ID. * @param gridletId a Gridlet ID * @param userId the user or owner's ID of this Gridlet * @return the Gridlet status or <tt>-1</tt> if not found * @see gridsim.Gridlet * @pre gridletId > 0 * @pre userId > 0 * @post $none */ public int gridletStatus(int gridletId, int userId) { ResGridlet rgl = null; // Find in EXEC List first int found = super.findGridlet(gridletInExecList_, gridletId, userId); if (found >= 0) { // Get the Gridlet from the execution list rgl = (ResGridlet) gridletInExecList_.get(found); return rgl.getGridletStatus(); } // if not found then find again in Paused List found = super.findGridlet(gridletPausedList_, gridletId, userId); if (found >= 0) { // Get the Gridlet from the execution list rgl = (ResGridlet) gridletPausedList_.get(found); return rgl.getGridletStatus(); } // if not found in all lists return -1; } /** * Cancels a Gridlet running in this entity. * This method will search the execution and paused list. The User ID is * important as many users might have the same Gridlet ID in the lists. * <b>NOTE:</b> * <ul> * <li> Before canceling a Gridlet, this method updates all the * Gridlets in the execution list. If the Gridlet has no more MIs * to be executed, then it is considered to be <tt>finished</tt>. * Hence, the Gridlet can't be canceled. * * <li> Once a Gridlet has been canceled, it can't 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 can't be found in both execution and paused list, * 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 * @post $none */ public void gridletCancel(int gridletId, int userId) { // Finds the gridlet in execution and paused list ResGridlet rgl = cancel(gridletId, userId); // If not found in both lists then report an error and sends back // an empty Gridlet if (rgl == null) { System.out.println(super.resName_ + ".TimeShared.gridletCancel(): Cannot find " + "Gridlet #" + gridletId + " for User #" + userId); super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, null, gridletId, userId); return; } // if a Gridlet is found rgl.finalizeGridlet(); // finalise Gridlet // if a Gridlet has finished execution before canceling, the reports // an error msg if (rgl.getGridletStatus() == Gridlet.SUCCESS) { System.out.println(super.resName_ + ".TimeShared.gridletCancel(): Cannot cancel" + " Gridlet #" + gridletId + " for User #" + userId + " since it has FINISHED."); } // sends the Gridlet back to sender super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, rgl.getGridlet(), gridletId, userId); } /** * Pauses a Gridlet only if it is currently executing. * This method will search in the execution list. The User ID is * important as many users might have the same Gridlet ID in the lists. * @param gridletId a Gridlet ID * @param userId the user or owner's ID of this Gridlet * @param ack an acknowledgement, i.e. <tt>true</tt> if wanted to know * whether this operation is success or not, <tt>false</tt> * otherwise (don't care) * @pre gridletId > 0 * @pre userId > 0 * @post $none */ public void gridletPause(int gridletId, int userId, boolean ack) { boolean status = false; // find this Gridlet in the execution list int found = super.findGridlet(gridletInExecList_, gridletId, userId); if (found >= 0) { // update Gridlets in execution list up to this point in time updateGridletProcessing(); // get a Gridlet from execution list ResGridlet rgl = (ResGridlet) gridletInExecList_.remove(found); // if a Gridlet is finished upon pausing, then set it to success // instead. if (rgl.getRemainingGridletLength() == 0.0) { System.out.println(super.resName_ + ".TimeShared.gridletPause(): Cannot pause" + " Gridlet #" + gridletId + " for User #" + userId + " since it is FINISHED."); gridletFinish(rgl, Gridlet.SUCCESS); } else { status = true; rgl.setGridletStatus(Gridlet.PAUSED); // add the Gridlet into the paused list gridletPausedList_.add(rgl); System.out.println(super.resName_ + ".TimeShared.gridletPause(): Gridlet #" + gridletId + " with User #" + userId + " has been sucessfully PAUSED."); } // forecast all Gridlets in the execution list forecastGridlet(); } else // if not found in the execution list { System.out.println(super.resName_ + ".TimeShared.gridletPause(): Cannot find " + "Gridlet #" + gridletId + " for User #" + userId); } // sends back an ack if (ack == true) { super.sendAck(GridSimTags.GRIDLET_PAUSE_ACK, status, gridletId, userId); } } /** * Moves a Gridlet from this GridResource entity to a different one. * This method will search in both the execution and paused list. * The User ID is important as many Users might have the same Gridlet ID * in the lists. * <p> * If a Gridlet has finished beforehand, then this method will send back * the Gridlet to sender, i.e. the <tt>userId</tt> and sets the * acknowledgment to false (if required). * * @param gridletId a Gridlet ID * @param userId the user or owner's ID of this Gridlet * @param destId a new destination GridResource ID for this Gridlet * @param ack an acknowledgement, i.e. <tt>true</tt> if wanted to know * whether this operation is success or not, <tt>false</tt> * otherwise (don't care) * @pre gridletId > 0 * @pre userId > 0 * @pre destId > 0 * @post $none */ public void gridletMove(int gridletId, int userId, int destId, boolean ack) { // cancel the Gridlet first ResGridlet rgl = cancel(gridletId, userId); // If no found then print an error msg if (rgl == null) { System.out.println(super.resName_ + ".TimeShared.gridletMove(): Cannot find " + "Gridlet #" + gridletId + " for User #" + userId); if (ack == true) // sends ack that this operation fails { super.sendAck(GridSimTags.GRIDLET_SUBMIT_ACK, false, gridletId, userId); } return; } // if found rgl.finalizeGridlet(); // finalise Gridlet Gridlet gl = rgl.getGridlet(); // if a Gridlet has finished execution if (gl.getGridletStatus() == Gridlet.SUCCESS) { System.out.println(super.resName_ + ".TimeShared.gridletMove(): Cannot move"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -