📄 spaceshared.java
字号:
if (ack == true) // sends back an ack if required { super.sendAck(GridSimTags.GRIDLET_SUBMIT_ACK, false, gridletId, userId); } return; } // if the Gridlet has finished beforehand if (rgl.getGridletStatus() == Gridlet.SUCCESS) { System.out.println(super.resName_ + ".SpaceShared.gridletMove(): Cannot move Gridlet #" + gridletId + " for User #" + userId + " since it has FINISHED."); if (ack == true) // sends back an ack if required { super.sendAck(GridSimTags.GRIDLET_SUBMIT_ACK, false, gridletId, userId); } gridletFinish(rgl, Gridlet.SUCCESS); } else // otherwise moves this Gridlet to a different GridResource { rgl.finalizeGridlet(); // Set PE on which Gridlet finished to FREE super.resource_.setStatusPE( PE.FREE, rgl.getMachineID(), rgl.getPEID() ); super.gridletMigrate(rgl.getGridlet(), destId, ack); allocateQueueGridlet(); } } /** * Resumes a Gridlet only in the paused 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 gridletResume(int gridletId, int userId, boolean ack) { boolean status = false; // finds the Gridlet in the execution list first int found = super.findGridlet(gridletPausedList_, gridletId, userId); if (found >= 0) { // removes the Gridlet ResGridlet rgl = (ResGridlet) gridletPausedList_.remove(found); rgl.setGridletStatus(Gridlet.RESUMED); // update the Gridlets up to this point in time updateGridletProcessing(); status = true; // if there is an available PE slot, then allocate immediately boolean success = false; if ( gridletInExecList_.size() < super.totalPE_ ) { success = allocatePEtoGridlet(rgl); } // otherwise put into Queue list if (success == false) { rgl.setGridletStatus(Gridlet.QUEUED); gridletQueueList_.add(rgl); } System.out.println(super.resName_ + "TimeShared.gridletResume():" + " Gridlet #" + gridletId + " with User ID #" + userId + " has been sucessfully RESUMED."); } else { System.out.println(super.resName_ + "TimeShared.gridletResume(): Cannot find " + "Gridlet #" + gridletId + " for User #" + userId); } // sends back an ack if required if (ack == true) { super.sendAck(GridSimTags.GRIDLET_RESUME_ACK, status, gridletId, userId); } } ///////////////////////////// PRIVATE METHODS ///////////////////// /** * Allocates the first Gridlet in the Queue list (if any) to execution list * @pre $none * @post $none */ private void allocateQueueGridlet() { // if there are many Gridlets in the QUEUE, then allocate a // PE to the first Gridlet in the list since it follows FCFS // (First Come First Serve) approach. Then removes the Gridlet from // the Queue list if (gridletQueueList_.size() > 0 && gridletInExecList_.size() < super.totalPE_) { ResGridlet obj = (ResGridlet) gridletQueueList_.get(0); // allocate the Gridlet into an empty PE slot and remove it from // the queue list boolean success = allocatePEtoGridlet(obj); if (success == true) { gridletQueueList_.remove(obj); } } } /** * Updates the execution of all Gridlets for a period of time. * The time period is determined from the last update time up to the * current time. Once this operation is successfull, then the last update * time refers to the current time. * @pre $none * @post $none */ private void updateGridletProcessing() { // Identify MI share for the duration (from last event time) double time = GridSim.clock(); double timeSpan = time - lastUpdateTime_; // if current time is the same or less than the last update time, // then ignore if (timeSpan <= 0.0) { return; } // Update Current Time as Last Update lastUpdateTime_ = time; // update the GridResource load int size = gridletInExecList_.size(); double load = super.calculateTotalLoad(size); super.addTotalLoad(load); // if no Gridlets in execution then ignore the rest if (size == 0) { return; } ResGridlet obj = null; // a loop that allocates MI share for each Gridlet accordingly Iterator iter = gridletInExecList_.iterator(); while ( iter.hasNext() ) { obj = (ResGridlet) iter.next(); // Updates the Gridlet length that is currently being executed load = getMIShare( timeSpan, obj.getMachineID() ); obj.updateGridletFinishedSoFar(load); } } /** * Identifies MI share (max and min) each Gridlet gets for * a given timeSpan * @param timeSpan duration * @param machineId machine ID that executes this Gridlet * @return the total MI share that a Gridlet gets for a given * <tt>timeSpan</tt> * @pre timeSpan >= 0.0 * @pre machineId > 0 * @post $result >= 0.0 */ private double getMIShare(double timeSpan, int machineId) { // 1 - localLoad_ = available MI share percentage double localLoad = super.resCalendar_.getCurrentLoad(); // each Machine might have different PE Rating compare to another // so much look at which Machine this PE belongs to double totalMI = machineRating_[machineId] * timeSpan * (1 - localLoad); return totalMI; } /** * Allocates a Gridlet into a free PE and sets the Gridlet status into * INEXEC and PE status into busy afterwards * @param rgl a ResGridlet object * @return <tt>true</tt> if there is an empty PE to process this Gridlet, * <tt>false</tt> otherwise * @pre rgl != null * @post $none */ private boolean allocatePEtoGridlet(ResGridlet rgl) { // IDENTIFY MACHINE which has a free PE and add this Gridlet to it. Machine myMachine = resource_.getMachineWithFreePE(); // If a Machine is empty then ignore the rest if (myMachine == null) { return false; } // gets the list of PEs and find one empty PE PEList MyPEList = myMachine.getPEList(); int freePE = MyPEList.getFreePEID(); // ALLOCATE IMMEDIATELY rgl.setGridletStatus(Gridlet.INEXEC); // change Gridlet status rgl.setMachineAndPEID(myMachine.getMachineID(), freePE); // add this Gridlet into execution list gridletInExecList_.add(rgl); // Set allocated PE to BUSY status super.resource_.setStatusPE(PE.BUSY, rgl.getMachineID(), freePE); // Identify Completion Time and Set Interrupt int rating = machineRating_[ rgl.getMachineID() ]; double time = forecastFinishTime( rating , rgl.getRemainingGridletLength() ); int roundUpTime = (int) (time+1); // rounding up rgl.setFinishTime(roundUpTime); // then send this into itself super.sendInternalEvent(roundUpTime); return true; } /** * Forecast finish time of a Gridlet. * <tt>Finish time = length / available rating</tt> * @param availableRating the shared MIPS rating for all Gridlets * @param length remaining Gridlet length * @return Gridlet's finish time. * @pre availableRating >= 0.0 * @pre length >= 0.0 * @post $none */ private double forecastFinishTime(double availableRating, double length) { double finishTime = (length / availableRating); // This is as a safeguard since the finish time can be extremely // small close to 0.0, such as 4.5474735088646414E-14. Hence causing // some Gridlets never to be finished and consequently hang the program if (finishTime < 1.0) { finishTime = 1.0; } return finishTime; } /** * Checks all Gridlets in the execution list whether they are finished or * not. * @pre $none * @post $none */ private void checkGridletCompletion() { ResGridlet obj = null; int i = 0; // NOTE: This one should stay as it is since gridletFinish() // will modify the content of this list if a Gridlet has finished. // Can't use iterator since it will cause an exception while ( i < gridletInExecList_.size() ) { obj = (ResGridlet) gridletInExecList_.get(i); if (obj.getRemainingGridletLength() == 0.0) { gridletInExecList_.remove(obj); gridletFinish(obj, Gridlet.SUCCESS); continue; } i++; } // if there are still Gridlets left in the execution // then send this into itself for an hourly interrupt // NOTE: Setting the internal event time too low will make the // simulation more realistic, BUT will take longer time to // run this simulation. Also, size of sim_trace will be HUGE! if (gridletInExecList_.size() > 0) { super.sendInternalEvent(60.0*60.0); } } /** * Updates the Gridlet's properties, such as status once a * Gridlet is considered finished. * @param rgl a ResGridlet object * @param status the Gridlet status * @pre rgl != null * @pre status >= 0 * @post $none */ private void gridletFinish(ResGridlet rgl, int status) { // Set PE on which Gridlet finished to FREE super.resource_.setStatusPE(PE.FREE, rgl.getMachineID(), rgl.getPEID()); // the order is important! Set the status first then finalize // due to timing issues in ResGridlet class rgl.setGridletStatus(status); rgl.finalizeGridlet(); super.sendFinishGridlet( rgl.getGridlet() ); allocateQueueGridlet(); // move Queued Gridlet into exec list } /** * Handles an operation of canceling a Gridlet in either execution list * or paused list. * @param gridletId a Gridlet ID * @param userId the user or owner's ID of this Gridlet * @param an object of ResGridlet or <tt>null</tt> if this Gridlet is not * found * @pre gridletId > 0 * @pre userId > 0 * @post $none */ private ResGridlet cancel(int gridletId, int userId) { ResGridlet rgl = null; // Find in EXEC List first int found = super.findGridlet(gridletInExecList_, gridletId, userId); if (found >= 0) { // update the gridlets in execution list up to this point in time updateGridletProcessing(); // Get the Gridlet from the execution list rgl = (ResGridlet) gridletInExecList_.remove(found); // if a Gridlet is finished upon cancelling, then set it to success // instead. if (rgl.getRemainingGridletLength() == 0.0) { rgl.setGridletStatus(Gridlet.SUCCESS); } else { rgl.setGridletStatus(Gridlet.CANCELED); } // Set PE on which Gridlet finished to FREE super.resource_.setStatusPE( PE.FREE, rgl.getMachineID(), rgl.getPEID() ); allocateQueueGridlet(); return rgl; } // Find in QUEUE list found = super.findGridlet(gridletQueueList_, gridletId, userId); if (found >= 0) { rgl = (ResGridlet) gridletQueueList_.remove(found); rgl.setGridletStatus(Gridlet.CANCELED); } // if not, then find in the Paused list else { found = super.findGridlet(gridletPausedList_, gridletId, userId); // if found in Paused list if (found >= 0) { rgl = (ResGridlet) gridletPausedList_.remove(found); rgl.setGridletStatus(Gridlet.CANCELED); } } return rgl; }} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -