[104] | 1 | /* |
---|
| 2 | * Title: GridSim Toolkit |
---|
| 3 | * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation |
---|
| 4 | * of Parallel and Distributed Systems such as Clusters and Grids |
---|
| 5 | * Licence: GPL - http://www.gnu.org/copyleft/gpl.html |
---|
| 6 | * |
---|
| 7 | * Copyright (c) 2003, The University of Melbourne, Australia |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | package gridsim.gssim.network; |
---|
| 11 | |
---|
| 12 | import eduni.simjava.Sim_event; |
---|
| 13 | import eduni.simjava.Sim_system; |
---|
| 14 | import gridsim.AllocPolicy; |
---|
| 15 | import gridsim.GridSim; |
---|
| 16 | import gridsim.GridSimTags; |
---|
| 17 | import gridsim.Gridlet; |
---|
| 18 | import gridsim.Machine; |
---|
| 19 | import gridsim.MachineList; |
---|
| 20 | import gridsim.PE; |
---|
| 21 | import gridsim.PEList; |
---|
| 22 | import gridsim.ResGridlet; |
---|
| 23 | import gridsim.ResGridletList; |
---|
| 24 | |
---|
| 25 | import java.util.Calendar; |
---|
| 26 | import java.util.Iterator; |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * SpaceShared class is an allocation policy for GridResource that behaves |
---|
| 31 | * exactly like First Come First Serve (FCFS). This is a basic and simple |
---|
| 32 | * scheduler that runs each Gridlet to one Processing Element (PE). |
---|
| 33 | * If a Gridlet requires more than one PE, then this scheduler only assign |
---|
| 34 | * this Gridlet to one PE. |
---|
| 35 | * |
---|
| 36 | * @author Manzur Murshed and Rajkumar Buyya |
---|
| 37 | * @author Anthony Sulistio (re-written this class) |
---|
| 38 | * @since GridSim Toolkit 2.2 |
---|
| 39 | * @see gridsim.GridSim |
---|
| 40 | * @see gridsim.ResourceCharacteristics |
---|
| 41 | * @invariant $none |
---|
| 42 | */ |
---|
| 43 | class SpaceShared extends AllocPolicy |
---|
| 44 | { |
---|
| 45 | private ResGridletList gridletQueueList_; // Queue list |
---|
| 46 | private ResGridletList gridletInExecList_; // Execution list |
---|
| 47 | private ResGridletList gridletPausedList_; // Pause list |
---|
| 48 | private double lastUpdateTime_; // the last time Gridlets updated |
---|
| 49 | private int[] machineRating_; // list of machine ratings available |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Allocates a new SpaceShared object |
---|
| 54 | * @param resourceName the GridResource entity name that will contain |
---|
| 55 | * this allocation policy |
---|
| 56 | * @param entityName this object entity name |
---|
| 57 | * @throws Exception This happens when one of the following scenarios occur: |
---|
| 58 | * <ul> |
---|
| 59 | * <li> creating this entity before initializing GridSim package |
---|
| 60 | * <li> this entity name is <tt>null</tt> or empty |
---|
| 61 | * <li> this entity has <tt>zero</tt> number of PEs (Processing |
---|
| 62 | * Elements). <br> |
---|
| 63 | * No PEs mean the Gridlets can't be processed. |
---|
| 64 | * A GridResource must contain one or more Machines. |
---|
| 65 | * A Machine must contain one or more PEs. |
---|
| 66 | * </ul> |
---|
| 67 | * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], |
---|
| 68 | * String) |
---|
| 69 | * @pre resourceName != null |
---|
| 70 | * @pre entityName != null |
---|
| 71 | * @post $none |
---|
| 72 | */ |
---|
| 73 | SpaceShared(String resourceName, String entityName) throws Exception |
---|
| 74 | { |
---|
| 75 | super(resourceName, entityName); |
---|
| 76 | |
---|
| 77 | // initialises local data structure |
---|
| 78 | this.gridletInExecList_ = new ResGridletList(); |
---|
| 79 | this.gridletPausedList_ = new ResGridletList(); |
---|
| 80 | this.gridletQueueList_ = new ResGridletList(); |
---|
| 81 | this.lastUpdateTime_ = 0.0; |
---|
| 82 | this.machineRating_ = null; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Handles internal events that are coming to this entity. |
---|
| 87 | * @pre $none |
---|
| 88 | * @post $none |
---|
| 89 | */ |
---|
| 90 | public void body() |
---|
| 91 | { |
---|
| 92 | // Gets the PE's rating for each Machine in the list. |
---|
| 93 | // Assumed one Machine has same PE rating. |
---|
| 94 | MachineList list = super.resource_.getMachineList(); |
---|
| 95 | int size = list.size(); |
---|
| 96 | machineRating_ = new int[size]; |
---|
| 97 | for (int i = 0; i < size; i++) { |
---|
| 98 | machineRating_[i] = super.resource_.getMIPSRatingOfOnePE(i, 0); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | // a loop that is looking for internal events only |
---|
| 102 | Sim_event ev = new Sim_event(); |
---|
| 103 | while ( Sim_system.running() ) |
---|
| 104 | { |
---|
| 105 | super.sim_get_next(ev); |
---|
| 106 | |
---|
| 107 | // if the simulation finishes then exit the loop |
---|
| 108 | if (ev.get_tag() == GridSimTags.END_OF_SIMULATION || |
---|
| 109 | super.isEndSimulation() == true) |
---|
| 110 | { |
---|
| 111 | break; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | // Internal Event if the event source is this entity |
---|
| 115 | if (ev.get_src() == super.myId_ && gridletInExecList_.size() > 0) |
---|
| 116 | { |
---|
| 117 | updateGridletProcessing(); // update Gridlets |
---|
| 118 | checkGridletCompletion(); // check for finished Gridlets |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | // CHECK for ANY INTERNAL EVENTS WAITING TO BE PROCESSED |
---|
| 123 | while (super.sim_waiting() > 0) |
---|
| 124 | { |
---|
| 125 | // wait for event and ignore since it is likely to be related to |
---|
| 126 | // internal event scheduled to update Gridlets processing |
---|
| 127 | super.sim_get_next(ev); |
---|
| 128 | System.out.println(super.resName_ + |
---|
| 129 | ".SpaceShared.body(): ignore internal events"); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * Schedules a new Gridlet that has been received by the GridResource |
---|
| 135 | * entity. |
---|
| 136 | * @param gl a Gridlet object that is going to be executed |
---|
| 137 | * @param ack an acknowledgement, i.e. <tt>true</tt> if wanted to know |
---|
| 138 | * whether this operation is success or not, <tt>false</tt> |
---|
| 139 | * otherwise (don't care) |
---|
| 140 | * @pre gl != null |
---|
| 141 | * @post $none |
---|
| 142 | */ |
---|
| 143 | public void gridletSubmit(Gridlet gl, boolean ack) |
---|
| 144 | { |
---|
| 145 | // update the current Gridlets in exec list up to this point in time |
---|
| 146 | updateGridletProcessing(); |
---|
| 147 | |
---|
| 148 | // reset number of PE since at the moment, it is not supported |
---|
| 149 | if (gl.getNumPE() > 1) |
---|
| 150 | { |
---|
| 151 | String userName = GridSim.getEntityName( gl.getUserID() ); |
---|
| 152 | System.out.println(); |
---|
| 153 | System.out.println(super.get_name() + ".gridletSubmit(): " + |
---|
| 154 | " Gridlet #" + gl.getGridletID() + " from " + userName + |
---|
| 155 | " user requires " + gl.getNumPE() + " PEs."); |
---|
| 156 | System.out.println("--> Process this Gridlet to 1 PE only."); |
---|
| 157 | System.out.println(); |
---|
| 158 | |
---|
| 159 | // also adjusted the length because the number of PEs are reduced |
---|
| 160 | int numPE = gl.getNumPE(); |
---|
| 161 | double len = gl.getGridletLength(); |
---|
| 162 | gl.setGridletLength(len*numPE); |
---|
| 163 | gl.setNumPE(1); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | ResGridlet rgl = new ResGridlet(gl); |
---|
| 167 | boolean success = false; |
---|
| 168 | |
---|
| 169 | // if there is an available PE slot, then allocate immediately |
---|
| 170 | if (gridletInExecList_.size() < super.totalPE_) { |
---|
| 171 | success = allocatePEtoGridlet(rgl); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | // if no available PE then put the ResGridlet into a Queue list |
---|
| 175 | if (success == false) |
---|
| 176 | { |
---|
| 177 | rgl.setGridletStatus(Gridlet.QUEUED); |
---|
| 178 | gridletQueueList_.add(rgl); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | // sends back an ack if required |
---|
| 182 | if (ack == true) |
---|
| 183 | { |
---|
| 184 | super.sendAck(GridSimTags.GRIDLET_SUBMIT_ACK, true, |
---|
| 185 | gl.getGridletID(), gl.getUserID() |
---|
| 186 | ); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | * Finds the status of a specified Gridlet ID. |
---|
| 192 | * @param gridletId a Gridlet ID |
---|
| 193 | * @param userId the user or owner's ID of this Gridlet |
---|
| 194 | * @return the Gridlet status or <tt>-1</tt> if not found |
---|
| 195 | * @see gridsim.Gridlet |
---|
| 196 | * @pre gridletId > 0 |
---|
| 197 | * @pre userId > 0 |
---|
| 198 | * @post $none |
---|
| 199 | */ |
---|
| 200 | public int gridletStatus(int gridletId,int userId) |
---|
| 201 | { |
---|
| 202 | ResGridlet rgl = null; |
---|
| 203 | |
---|
| 204 | // Find in EXEC List first |
---|
| 205 | int found = super.findGridlet(gridletInExecList_, gridletId, userId); |
---|
| 206 | if (found >= 0) |
---|
| 207 | { |
---|
| 208 | // Get the Gridlet from the execution list |
---|
| 209 | rgl = (ResGridlet) gridletInExecList_.get(found); |
---|
| 210 | return rgl.getGridletStatus(); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | // Find in Paused List |
---|
| 214 | found = super.findGridlet(gridletPausedList_, gridletId, userId); |
---|
| 215 | if (found >= 0) |
---|
| 216 | { |
---|
| 217 | // Get the Gridlet from the execution list |
---|
| 218 | rgl = (ResGridlet) gridletPausedList_.get(found); |
---|
| 219 | return rgl.getGridletStatus(); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | // Find in Queue List |
---|
| 223 | found = super.findGridlet(gridletQueueList_, gridletId, userId); |
---|
| 224 | if (found >= 0) |
---|
| 225 | { |
---|
| 226 | // Get the Gridlet from the execution list |
---|
| 227 | rgl = (ResGridlet) gridletQueueList_.get(found); |
---|
| 228 | return rgl.getGridletStatus(); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | // if not found in all 3 lists then no found |
---|
| 232 | return -1; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | /** |
---|
| 236 | * Cancels a Gridlet running in this entity. |
---|
| 237 | * This method will search the execution, queued and paused list. |
---|
| 238 | * The User ID is |
---|
| 239 | * important as many users might have the same Gridlet ID in the lists. |
---|
| 240 | * <b>NOTE:</b> |
---|
| 241 | * <ul> |
---|
| 242 | * <li> Before canceling a Gridlet, this method updates all the |
---|
| 243 | * Gridlets in the execution list. If the Gridlet has no more MIs |
---|
| 244 | * to be executed, then it is considered to be <tt>finished</tt>. |
---|
| 245 | * Hence, the Gridlet can't be canceled. |
---|
| 246 | * |
---|
| 247 | * <li> Once a Gridlet has been canceled, it can't be resumed to |
---|
| 248 | * execute again since this method will pass the Gridlet back to |
---|
| 249 | * sender, i.e. the <tt>userId</tt>. |
---|
| 250 | * |
---|
| 251 | * <li> If a Gridlet can't be found in both execution and paused list, |
---|
| 252 | * then a <tt>null</tt> Gridlet will be send back to sender, |
---|
| 253 | * i.e. the <tt>userId</tt>. |
---|
| 254 | * </ul> |
---|
| 255 | * |
---|
| 256 | * @param gridletId a Gridlet ID |
---|
| 257 | * @param userId the user or owner's ID of this Gridlet |
---|
| 258 | * @pre gridletId > 0 |
---|
| 259 | * @pre userId > 0 |
---|
| 260 | * @post $none |
---|
| 261 | */ |
---|
| 262 | public void gridletCancel(int gridletId, int userId) |
---|
| 263 | { |
---|
| 264 | // cancels a Gridlet |
---|
| 265 | ResGridlet rgl = cancel(gridletId, userId); |
---|
| 266 | |
---|
| 267 | // if the Gridlet is not found |
---|
| 268 | if (rgl == null) |
---|
| 269 | { |
---|
| 270 | System.out.println(super.resName_ + |
---|
| 271 | ".SpaceShared.gridletCancel(): Cannot find " + |
---|
| 272 | "Gridlet #" + gridletId + " for User #" + userId); |
---|
| 273 | |
---|
| 274 | super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, null, |
---|
| 275 | gridletId, userId); |
---|
| 276 | return; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | // if the Gridlet has finished beforehand then prints an error msg |
---|
| 280 | if (rgl.getGridletStatus() == Gridlet.SUCCESS) |
---|
| 281 | { |
---|
| 282 | System.out.println(super.resName_ |
---|
| 283 | + ".SpaceShared.gridletCancel(): Cannot cancel" |
---|
| 284 | + " Gridlet #" + gridletId + " for User #" + userId |
---|
| 285 | + " since it has FINISHED."); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | // sends the Gridlet back to sender |
---|
| 289 | rgl.finalizeGridlet(); |
---|
| 290 | super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, rgl.getGridlet(), |
---|
| 291 | gridletId, userId); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | /** |
---|
| 295 | * Pauses a Gridlet only if it is currently executing. |
---|
| 296 | * This method will search in the execution list. The User ID is |
---|
| 297 | * important as many users might have the same Gridlet ID in the lists. |
---|
| 298 | * @param gridletId a Gridlet ID |
---|
| 299 | * @param userId the user or owner's ID of this Gridlet |
---|
| 300 | * @param ack an acknowledgement, i.e. <tt>true</tt> if wanted to know |
---|
| 301 | * whether this operation is success or not, <tt>false</tt> |
---|
| 302 | * otherwise (don't care) |
---|
| 303 | * @pre gridletId > 0 |
---|
| 304 | * @pre userId > 0 |
---|
| 305 | * @post $none |
---|
| 306 | */ |
---|
| 307 | public void gridletPause(int gridletId, int userId, boolean ack) |
---|
| 308 | { |
---|
| 309 | boolean status = false; |
---|
| 310 | |
---|
| 311 | // Find in EXEC List first |
---|
| 312 | int found = super.findGridlet(gridletInExecList_, gridletId, userId); |
---|
| 313 | if (found >= 0) |
---|
| 314 | { |
---|
| 315 | // updates all the Gridlets first before pausing |
---|
| 316 | updateGridletProcessing(); |
---|
| 317 | |
---|
| 318 | // Removes the Gridlet from the execution list |
---|
| 319 | ResGridlet rgl = (ResGridlet) gridletInExecList_.remove(found); |
---|
| 320 | |
---|
| 321 | // if a Gridlet is finished upon cancelling, then set it to success |
---|
| 322 | // instead. |
---|
| 323 | if (rgl.getRemainingGridletLength() == 0.0) |
---|
| 324 | { |
---|
| 325 | found = -1; // meaning not found in Queue List |
---|
| 326 | gridletFinish(rgl, Gridlet.SUCCESS); |
---|
| 327 | System.out.println(super.resName_ |
---|
| 328 | + ".SpaceShared.gridletPause(): Cannot pause" |
---|
| 329 | + " Gridlet #" + gridletId + " for User #" + userId |
---|
| 330 | + " since it has FINISHED."); |
---|
| 331 | } |
---|
| 332 | else |
---|
| 333 | { |
---|
| 334 | status = true; |
---|
| 335 | rgl.setGridletStatus(Gridlet.PAUSED); // change the status |
---|
| 336 | gridletPausedList_.add(rgl); // add into the paused list |
---|
| 337 | |
---|
| 338 | // Set the PE on which Gridlet finished to FREE |
---|
| 339 | super.resource_.setStatusPE( PE.FREE, rgl.getMachineID(), |
---|
| 340 | rgl.getPEID() ); |
---|
| 341 | |
---|
| 342 | // empty slot is available, hence process a new Gridlet |
---|
| 343 | allocateQueueGridlet(); |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | else { // Find in QUEUE list |
---|
| 347 | found = super.findGridlet(gridletQueueList_, gridletId, userId); |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | // if found in the Queue List |
---|
| 351 | if (status == false && found >= 0) |
---|
| 352 | { |
---|
| 353 | status = true; |
---|
| 354 | |
---|
| 355 | // removes the Gridlet from the Queue list |
---|
| 356 | ResGridlet rgl = (ResGridlet) gridletQueueList_.remove(found); |
---|
| 357 | rgl.setGridletStatus(Gridlet.PAUSED); // change the status |
---|
| 358 | gridletPausedList_.add(rgl); // add into the paused list |
---|
| 359 | } |
---|
| 360 | // if not found anywhere in both exec and paused lists |
---|
| 361 | else if (found == -1) |
---|
| 362 | { |
---|
| 363 | System.out.println(super.resName_ + |
---|
| 364 | ".SpaceShared.gridletPause(): Error - cannot " + |
---|
| 365 | "find Gridlet #" + gridletId + " for User #" + userId); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | // sends back an ack if required |
---|
| 369 | if (ack == true) |
---|
| 370 | { |
---|
| 371 | super.sendAck(GridSimTags.GRIDLET_PAUSE_ACK, status, |
---|
| 372 | gridletId, userId); |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | /** |
---|
| 377 | * Moves a Gridlet from this GridResource entity to a different one. |
---|
| 378 | * This method will search in both the execution and paused list. |
---|
| 379 | * The User ID is important as many Users might have the same Gridlet ID |
---|
| 380 | * in the lists. |
---|
| 381 | * <p> |
---|
| 382 | * If a Gridlet has finished beforehand, then this method will send back |
---|
| 383 | * the Gridlet to sender, i.e. the <tt>userId</tt> and sets the |
---|
| 384 | * acknowledgment to false (if required). |
---|
| 385 | * |
---|
| 386 | * @param gridletId a Gridlet ID |
---|
| 387 | * @param userId the user or owner's ID of this Gridlet |
---|
| 388 | * @param destId a new destination GridResource ID for this Gridlet |
---|
| 389 | * @param ack an acknowledgement, i.e. <tt>true</tt> if wanted to know |
---|
| 390 | * whether this operation is success or not, <tt>false</tt> |
---|
| 391 | * otherwise (don't care) |
---|
| 392 | * @pre gridletId > 0 |
---|
| 393 | * @pre userId > 0 |
---|
| 394 | * @pre destId > 0 |
---|
| 395 | * @post $none |
---|
| 396 | */ |
---|
| 397 | public void gridletMove(int gridletId, int userId, int destId, boolean ack) |
---|
| 398 | { |
---|
| 399 | // cancels the Gridlet |
---|
| 400 | ResGridlet rgl = cancel(gridletId, userId); |
---|
| 401 | |
---|
| 402 | // if the Gridlet is not found |
---|
| 403 | if (rgl == null) |
---|
| 404 | { |
---|
| 405 | System.out.println(super.resName_ + |
---|
| 406 | ".SpaceShared.gridletMove(): Cannot find " + |
---|
| 407 | "Gridlet #" + gridletId + " for User #" + userId); |
---|
| 408 | |
---|
| 409 | if (ack == true) // sends back an ack if required |
---|
| 410 | { |
---|
| 411 | super.sendAck(GridSimTags.GRIDLET_SUBMIT_ACK, false, |
---|
| 412 | gridletId, userId); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | return; |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | // if the Gridlet has finished beforehand |
---|
| 419 | if (rgl.getGridletStatus() == Gridlet.SUCCESS) |
---|
| 420 | { |
---|
| 421 | System.out.println(super.resName_ |
---|
| 422 | + ".SpaceShared.gridletMove(): Cannot move Gridlet #" |
---|
| 423 | + gridletId + " for User #" + userId |
---|
| 424 | + " since it has FINISHED."); |
---|
| 425 | |
---|
| 426 | if (ack == true) // sends back an ack if required |
---|
| 427 | { |
---|
| 428 | super.sendAck(GridSimTags.GRIDLET_SUBMIT_ACK, false, |
---|
| 429 | gridletId, userId); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | gridletFinish(rgl, Gridlet.SUCCESS); |
---|
| 433 | } |
---|
| 434 | else // otherwise moves this Gridlet to a different GridResource |
---|
| 435 | { |
---|
| 436 | rgl.finalizeGridlet(); |
---|
| 437 | |
---|
| 438 | // Set PE on which Gridlet finished to FREE |
---|
| 439 | super.resource_.setStatusPE( PE.FREE, rgl.getMachineID(), |
---|
| 440 | rgl.getPEID() ); |
---|
| 441 | |
---|
| 442 | super.gridletMigrate(rgl.getGridlet(), destId, ack); |
---|
| 443 | allocateQueueGridlet(); |
---|
| 444 | } |
---|
| 445 | } |
---|
| 446 | |
---|
| 447 | /** |
---|
| 448 | * Resumes a Gridlet only in the paused list. |
---|
| 449 | * The User ID is important as many Users might have the same Gridlet ID |
---|
| 450 | * in the lists. |
---|
| 451 | * @param gridletId a Gridlet ID |
---|
| 452 | * @param userId the user or owner's ID of this Gridlet |
---|
| 453 | * @param ack an acknowledgement, i.e. <tt>true</tt> if wanted to know |
---|
| 454 | * whether this operation is success or not, <tt>false</tt> |
---|
| 455 | * otherwise (don't care) |
---|
| 456 | * @pre gridletId > 0 |
---|
| 457 | * @pre userId > 0 |
---|
| 458 | * @post $none |
---|
| 459 | */ |
---|
| 460 | public void gridletResume(int gridletId, int userId, boolean ack) |
---|
| 461 | { |
---|
| 462 | boolean status = false; |
---|
| 463 | |
---|
| 464 | // finds the Gridlet in the execution list first |
---|
| 465 | int found = super.findGridlet(gridletPausedList_, gridletId, userId); |
---|
| 466 | if (found >= 0) |
---|
| 467 | { |
---|
| 468 | // removes the Gridlet |
---|
| 469 | ResGridlet rgl = (ResGridlet) gridletPausedList_.remove(found); |
---|
| 470 | rgl.setGridletStatus(Gridlet.RESUMED); |
---|
| 471 | |
---|
| 472 | // update the Gridlets up to this point in time |
---|
| 473 | updateGridletProcessing(); |
---|
| 474 | status = true; |
---|
| 475 | |
---|
| 476 | // if there is an available PE slot, then allocate immediately |
---|
| 477 | boolean success = false; |
---|
| 478 | if ( gridletInExecList_.size() < super.totalPE_ ) { |
---|
| 479 | success = allocatePEtoGridlet(rgl); |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | // otherwise put into Queue list |
---|
| 483 | if (success == false) |
---|
| 484 | { |
---|
| 485 | rgl.setGridletStatus(Gridlet.QUEUED); |
---|
| 486 | gridletQueueList_.add(rgl); |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | System.out.println(super.resName_ + "TimeShared.gridletResume():" + |
---|
| 490 | " Gridlet #" + gridletId + " with User ID #" + |
---|
| 491 | userId + " has been sucessfully RESUMED."); |
---|
| 492 | } |
---|
| 493 | else |
---|
| 494 | { |
---|
| 495 | System.out.println(super.resName_ + |
---|
| 496 | "TimeShared.gridletResume(): Cannot find " + |
---|
| 497 | "Gridlet #" + gridletId + " for User #" + userId); |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | // sends back an ack if required |
---|
| 501 | if (ack == true) |
---|
| 502 | { |
---|
| 503 | super.sendAck(GridSimTags.GRIDLET_RESUME_ACK, status, |
---|
| 504 | gridletId, userId); |
---|
| 505 | } |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | ///////////////////////////// PRIVATE METHODS ///////////////////// |
---|
| 509 | |
---|
| 510 | /** |
---|
| 511 | * Allocates the first Gridlet in the Queue list (if any) to execution list |
---|
| 512 | * @pre $none |
---|
| 513 | * @post $none |
---|
| 514 | */ |
---|
| 515 | private void allocateQueueGridlet() |
---|
| 516 | { |
---|
| 517 | // if there are many Gridlets in the QUEUE, then allocate a |
---|
| 518 | // PE to the first Gridlet in the list since it follows FCFS |
---|
| 519 | // (First Come First Serve) approach. Then removes the Gridlet from |
---|
| 520 | // the Queue list |
---|
| 521 | if (gridletQueueList_.size() > 0 && |
---|
| 522 | gridletInExecList_.size() < super.totalPE_) |
---|
| 523 | { |
---|
| 524 | ResGridlet obj = (ResGridlet) gridletQueueList_.get(0); |
---|
| 525 | |
---|
| 526 | // allocate the Gridlet into an empty PE slot and remove it from |
---|
| 527 | // the queue list |
---|
| 528 | boolean success = allocatePEtoGridlet(obj); |
---|
| 529 | if (success == true) { |
---|
| 530 | gridletQueueList_.remove(obj); |
---|
| 531 | } |
---|
| 532 | } |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | /** |
---|
| 536 | * Updates the execution of all Gridlets for a period of time. |
---|
| 537 | * The time period is determined from the last update time up to the |
---|
| 538 | * current time. Once this operation is successfull, then the last update |
---|
| 539 | * time refers to the current time. |
---|
| 540 | * @pre $none |
---|
| 541 | * @post $none |
---|
| 542 | */ |
---|
| 543 | private void updateGridletProcessing() |
---|
| 544 | { |
---|
| 545 | // Identify MI share for the duration (from last event time) |
---|
| 546 | double time = GridSim.clock(); |
---|
| 547 | double timeSpan = time - lastUpdateTime_; |
---|
| 548 | |
---|
| 549 | // if current time is the same or less than the last update time, |
---|
| 550 | // then ignore |
---|
| 551 | if (timeSpan <= 0.0) { |
---|
| 552 | return; |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | // Update Current Time as Last Update |
---|
| 556 | lastUpdateTime_ = time; |
---|
| 557 | |
---|
| 558 | // update the GridResource load |
---|
| 559 | int size = gridletInExecList_.size(); |
---|
| 560 | double load = super.calculateTotalLoad(size); |
---|
| 561 | super.addTotalLoad(load); |
---|
| 562 | |
---|
| 563 | // if no Gridlets in execution then ignore the rest |
---|
| 564 | if (size == 0) { |
---|
| 565 | return; |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | ResGridlet obj = null; |
---|
| 569 | |
---|
| 570 | // a loop that allocates MI share for each Gridlet accordingly |
---|
| 571 | Iterator iter = gridletInExecList_.iterator(); |
---|
| 572 | while ( iter.hasNext() ) |
---|
| 573 | { |
---|
| 574 | obj = (ResGridlet) iter.next(); |
---|
| 575 | |
---|
| 576 | // Updates the Gridlet length that is currently being executed |
---|
| 577 | load = getMIShare( timeSpan, obj.getMachineID() ); |
---|
| 578 | obj.updateGridletFinishedSoFar(load); |
---|
| 579 | } |
---|
| 580 | } |
---|
| 581 | |
---|
| 582 | /** |
---|
| 583 | * Identifies MI share (max and min) each Gridlet gets for |
---|
| 584 | * a given timeSpan |
---|
| 585 | * @param timeSpan duration |
---|
| 586 | * @param machineId machine ID that executes this Gridlet |
---|
| 587 | * @return the total MI share that a Gridlet gets for a given |
---|
| 588 | * <tt>timeSpan</tt> |
---|
| 589 | * @pre timeSpan >= 0.0 |
---|
| 590 | * @pre machineId > 0 |
---|
| 591 | * @post $result >= 0.0 |
---|
| 592 | */ |
---|
| 593 | private double getMIShare(double timeSpan, int machineId) |
---|
| 594 | { |
---|
| 595 | // 1 - localLoad_ = available MI share percentage |
---|
| 596 | double localLoad = super.resCalendar_.getCurrentLoad(); |
---|
| 597 | |
---|
| 598 | // each Machine might have different PE Rating compare to another |
---|
| 599 | // so much look at which Machine this PE belongs to |
---|
| 600 | double totalMI = machineRating_[machineId] * timeSpan * (1 - localLoad); |
---|
| 601 | return totalMI; |
---|
| 602 | } |
---|
| 603 | |
---|
| 604 | /** |
---|
| 605 | * Allocates a Gridlet into a free PE and sets the Gridlet status into |
---|
| 606 | * INEXEC and PE status into busy afterwards |
---|
| 607 | * @param rgl a ResGridlet object |
---|
| 608 | * @return <tt>true</tt> if there is an empty PE to process this Gridlet, |
---|
| 609 | * <tt>false</tt> otherwise |
---|
| 610 | * @pre rgl != null |
---|
| 611 | * @post $none |
---|
| 612 | */ |
---|
| 613 | private boolean allocatePEtoGridlet(ResGridlet rgl) |
---|
| 614 | { |
---|
| 615 | // IDENTIFY MACHINE which has a free PE and add this Gridlet to it. |
---|
| 616 | Machine myMachine = resource_.getMachineWithFreePE(); |
---|
| 617 | |
---|
| 618 | // If a Machine is empty then ignore the rest |
---|
| 619 | if (myMachine == null) { |
---|
| 620 | return false; |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | // gets the list of PEs and find one empty PE |
---|
| 624 | PEList MyPEList = myMachine.getPEList(); |
---|
| 625 | int freePE = MyPEList.getFreePEID(); |
---|
| 626 | |
---|
| 627 | // ALLOCATE IMMEDIATELY |
---|
| 628 | rgl.setGridletStatus(Gridlet.INEXEC); // change Gridlet status |
---|
| 629 | rgl.setMachineAndPEID(myMachine.getMachineID(), freePE); |
---|
| 630 | |
---|
| 631 | // add this Gridlet into execution list |
---|
| 632 | gridletInExecList_.add(rgl); |
---|
| 633 | |
---|
| 634 | // Set allocated PE to BUSY status |
---|
| 635 | super.resource_.setStatusPE(PE.BUSY, rgl.getMachineID(), freePE); |
---|
| 636 | |
---|
| 637 | // Identify Completion Time and Set Interrupt |
---|
| 638 | int rating = machineRating_[ rgl.getMachineID() ]; |
---|
| 639 | double time = forecastFinishTime( rating , |
---|
| 640 | rgl.getRemainingGridletLength() ); |
---|
| 641 | |
---|
| 642 | int roundUpTime = (int) (time+1); // rounding up |
---|
| 643 | rgl.setFinishTime(roundUpTime); |
---|
| 644 | |
---|
| 645 | // then send this into itself |
---|
| 646 | super.sendInternalEvent(roundUpTime); |
---|
| 647 | return true; |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | /** |
---|
| 651 | * Forecast finish time of a Gridlet. |
---|
| 652 | * <tt>Finish time = length / available rating</tt> |
---|
| 653 | * @param availableRating the shared MIPS rating for all Gridlets |
---|
| 654 | * @param length remaining Gridlet length |
---|
| 655 | * @return Gridlet's finish time. |
---|
| 656 | * @pre availableRating >= 0.0 |
---|
| 657 | * @pre length >= 0.0 |
---|
| 658 | * @post $none |
---|
| 659 | */ |
---|
| 660 | private double forecastFinishTime(double availableRating, double length) |
---|
| 661 | { |
---|
| 662 | double finishTime = (length / availableRating); |
---|
| 663 | |
---|
| 664 | // This is as a safeguard since the finish time can be extremely |
---|
| 665 | // small close to 0.0, such as 4.5474735088646414E-14. Hence causing |
---|
| 666 | // some Gridlets never to be finished and consequently hang the program |
---|
| 667 | if (finishTime < 1.0) { |
---|
| 668 | finishTime = 1.0; |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | return finishTime; |
---|
| 672 | } |
---|
| 673 | |
---|
| 674 | /** |
---|
| 675 | * Checks all Gridlets in the execution list whether they are finished or |
---|
| 676 | * not. |
---|
| 677 | * @pre $none |
---|
| 678 | * @post $none |
---|
| 679 | */ |
---|
| 680 | private void checkGridletCompletion() |
---|
| 681 | { |
---|
| 682 | ResGridlet obj = null; |
---|
| 683 | int i = 0; |
---|
| 684 | |
---|
| 685 | // NOTE: This one should stay as it is since gridletFinish() |
---|
| 686 | // will modify the content of this list if a Gridlet has finished. |
---|
| 687 | // Can't use iterator since it will cause an exception |
---|
| 688 | while ( i < gridletInExecList_.size() ) |
---|
| 689 | { |
---|
| 690 | obj = (ResGridlet) gridletInExecList_.get(i); |
---|
| 691 | |
---|
| 692 | if (obj.getRemainingGridletLength() == 0.0) |
---|
| 693 | { |
---|
| 694 | gridletInExecList_.remove(obj); |
---|
| 695 | gridletFinish(obj, Gridlet.SUCCESS); |
---|
| 696 | continue; |
---|
| 697 | } |
---|
| 698 | |
---|
| 699 | i++; |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | // if there are still Gridlets left in the execution |
---|
| 703 | // then send this into itself for an hourly interrupt |
---|
| 704 | // NOTE: Setting the internal event time too low will make the |
---|
| 705 | // simulation more realistic, BUT will take longer time to |
---|
| 706 | // run this simulation. Also, size of sim_trace will be HUGE! |
---|
| 707 | if (gridletInExecList_.size() > 0) { |
---|
| 708 | super.sendInternalEvent(60.0*60.0); |
---|
| 709 | } |
---|
| 710 | } |
---|
| 711 | |
---|
| 712 | /** |
---|
| 713 | * Updates the Gridlet's properties, such as status once a |
---|
| 714 | * Gridlet is considered finished. |
---|
| 715 | * @param rgl a ResGridlet object |
---|
| 716 | * @param status the Gridlet status |
---|
| 717 | * @pre rgl != null |
---|
| 718 | * @pre status >= 0 |
---|
| 719 | * @post $none |
---|
| 720 | */ |
---|
| 721 | private void gridletFinish(ResGridlet rgl, int status) |
---|
| 722 | { |
---|
| 723 | // Set PE on which Gridlet finished to FREE |
---|
| 724 | super.resource_.setStatusPE(PE.FREE, rgl.getMachineID(), rgl.getPEID()); |
---|
| 725 | |
---|
| 726 | // the order is important! Set the status first then finalize |
---|
| 727 | // due to timing issues in ResGridlet class |
---|
| 728 | rgl.setGridletStatus(status); |
---|
| 729 | rgl.finalizeGridlet(); |
---|
| 730 | super.sendFinishGridlet( rgl.getGridlet() ); |
---|
| 731 | |
---|
| 732 | allocateQueueGridlet(); // move Queued Gridlet into exec list |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | /** |
---|
| 736 | * Handles an operation of canceling a Gridlet in either execution list |
---|
| 737 | * or paused list. |
---|
| 738 | * @param gridletId a Gridlet ID |
---|
| 739 | * @param userId the user or owner's ID of this Gridlet |
---|
| 740 | * @param an object of ResGridlet or <tt>null</tt> if this Gridlet is not |
---|
| 741 | * found |
---|
| 742 | * @pre gridletId > 0 |
---|
| 743 | * @pre userId > 0 |
---|
| 744 | * @post $none |
---|
| 745 | */ |
---|
| 746 | private ResGridlet cancel(int gridletId, int userId) |
---|
| 747 | { |
---|
| 748 | ResGridlet rgl = null; |
---|
| 749 | |
---|
| 750 | // Find in EXEC List first |
---|
| 751 | int found = super.findGridlet(gridletInExecList_, gridletId, userId); |
---|
| 752 | if (found >= 0) |
---|
| 753 | { |
---|
| 754 | // update the gridlets in execution list up to this point in time |
---|
| 755 | updateGridletProcessing(); |
---|
| 756 | |
---|
| 757 | // Get the Gridlet from the execution list |
---|
| 758 | rgl = (ResGridlet) gridletInExecList_.remove(found); |
---|
| 759 | |
---|
| 760 | // if a Gridlet is finished upon cancelling, then set it to success |
---|
| 761 | // instead. |
---|
| 762 | if (rgl.getRemainingGridletLength() == 0.0) { |
---|
| 763 | rgl.setGridletStatus(Gridlet.SUCCESS); |
---|
| 764 | } |
---|
| 765 | else { |
---|
| 766 | rgl.setGridletStatus(Gridlet.CANCELED); |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | // Set PE on which Gridlet finished to FREE |
---|
| 770 | super.resource_.setStatusPE( PE.FREE, rgl.getMachineID(), |
---|
| 771 | rgl.getPEID() ); |
---|
| 772 | allocateQueueGridlet(); |
---|
| 773 | return rgl; |
---|
| 774 | } |
---|
| 775 | |
---|
| 776 | // Find in QUEUE list |
---|
| 777 | found = super.findGridlet(gridletQueueList_, gridletId, userId); |
---|
| 778 | if (found >= 0) |
---|
| 779 | { |
---|
| 780 | rgl = (ResGridlet) gridletQueueList_.remove(found); |
---|
| 781 | rgl.setGridletStatus(Gridlet.CANCELED); |
---|
| 782 | } |
---|
| 783 | |
---|
| 784 | // if not, then find in the Paused list |
---|
| 785 | else |
---|
| 786 | { |
---|
| 787 | found = super.findGridlet(gridletPausedList_, gridletId, userId); |
---|
| 788 | |
---|
| 789 | // if found in Paused list |
---|
| 790 | if (found >= 0) |
---|
| 791 | { |
---|
| 792 | rgl = (ResGridlet) gridletPausedList_.remove(found); |
---|
| 793 | rgl.setGridletStatus(Gridlet.CANCELED); |
---|
| 794 | } |
---|
| 795 | |
---|
| 796 | } |
---|
| 797 | return rgl; |
---|
| 798 | } |
---|
| 799 | |
---|
| 800 | } // end class |
---|
| 801 | |
---|