📄 arsimplespaceshared.java
字号:
if (index == NOT_FOUND) { return GridSimTags.AR_COMMIT_FAIL_INVALID_BOOKING_ID; } // need to check the expiry time whether it has passed or not ARObject ar = (ARObject) reservList_.get(index); long currentTime = super.getCurrentTime(); // get curret time // if a reservation is already expired if (ar.hasCommitted() == false && ar.getExpiryTime() < currentTime) { // change the status of this reservation into expired ar.setStatus(GridSimTags.AR_STATUS_EXPIRED); // move the reservation from reservation list into expired list expiryList_.add(ar); reservList_.remove(ar); return GridSimTags.AR_COMMIT_FAIL_EXPIRED; } // if committing after a reservation slot has finished long endTime = ar.getStartTime()+(ar.getDurationTime()*super.MILLI_SEC); if (endTime < currentTime) { // change the status of this reservation into expired ar.setStatus(GridSimTags.AR_STATUS_EXPIRED); // move the reservation from reservation list into expired list expiryList_.add(ar); reservList_.remove(ar); return GridSimTags.AR_COMMIT_FAIL_EXPIRED; } int result = 0; // storing a result of this operation // if a user wants to commit only without any Gridlet(s) submitted if (obj == null) { result = GridSimTags.AR_COMMIT_SUCCESS; } else // if a user commits one or more Gridlet objects { Gridlet gl = null; try { // a case where a user commits with only 1 Gridlet gl = (Gridlet) obj; // only able to handle 1 PE = 1 Gridlet if (ar.getTotalGridlet() == ar.getNumPE()) { return GridSimTags.AR_COMMIT_FAIL; } ResGridlet rgl = new ResGridlet(gl, ar.getStartTime(), ar.getDurationTime(), ar.getReservationID()); // set the result into success result = GridSimTags.AR_COMMIT_SUCCESS; // store the Gridlet index = findPosition(gridletWaitingList_, ar.getStartTime()); // put into waiting list gridletWaitingList_.add(index, rgl); ar.addTotalGridlet(1); // add total Gridlet executed by 1 } catch (ClassCastException c) { // a case where a user commits with more than 1 Gridlets try { // creates a new ResGridlet object for each Gridlet object // in the list GridletList list = (GridletList) obj; // only able to handle 1 PE = 1 Gridlet, so if committing // more Gridlets, then need some adjustment. int size = list.size(); if (ar.getTotalGridlet() + size > ar.getNumPE()) { size = ar.getNumPE() - ar.getTotalGridlet(); } for (int i = 0; i < size; i++) { gl = (Gridlet) list.get(i); ResGridlet rgl = new ResGridlet(gl, ar.getStartTime(), ar.getDurationTime(), ar.getReservationID()); // find the exact location to store these Gridlets if (i == 0) { index = findPosition(gridletWaitingList_, ar.getStartTime()); } // store each gridlet into a waiting list gridletWaitingList_.add(index, rgl); } // set the result into success result = GridSimTags.AR_COMMIT_SUCCESS; // add total Gridlet executed by this reservation ar.addTotalGridlet(size); } catch (Exception again) { result = GridSimTags.AR_COMMIT_ERROR; } } catch (Exception e) { result = GridSimTags.AR_COMMIT_ERROR; } } // if commit is successful, then set the internal event to process // a reservation in later time if (result == GridSimTags.AR_COMMIT_SUCCESS) { int status = GridSimTags.AR_STATUS_NOT_STARTED; int start = (int) ((ar.getStartTime()-currentTime)/super.MILLI_SEC); // start can be -ve for immediate reservation if (start <= 0) { status = GridSimTags.AR_STATUS_ACTIVE; start = 0; } ar.setCommitted(); ar.setStatus(status); // set AR status super.sendInternalEvent(start, PERFORM_RESERVATION); } return result; } /** * Finds the location in the list based on a given start time. * This is useful for doing an insertion sort based on a reservation's * start time. * @param list a list containing reservation objects * @param startTime a reservation start time * @return position number in the list * @pre list != null * @pre startTime > 0 * @post $none */ private int findPosition(LinkedList list, long startTime) { int index = 0; // if the waiting list is empty, then add to it straightaway if (list.size() == 0) { return index; } Iterator it = list.iterator(); ResGridlet rgl = null; // iterates a loop to find the location based on start time while ( it.hasNext() ) { rgl = (ResGridlet) it.next(); // exit the loop if object's reservation start time is greater if (rgl.getStartTime() > startTime) { break; } index++; } // if append at the end of the list if ( index > list.size() ) { index = list.size(); } return index; } /** * Finds Gridlets that are ready for execution. * @pre $none * @post $none */ private void performReservation() { // update the current Gridlets in exec list up to this point in time updateGridletProcessing(); // if no Gridlets in the waiting list, then exit if (gridletWaitingList_.size() == 0) { return; } // get current time. NOTE: add 5 seconds for buffer long currentTime = super.getCurrentTime() + (5 * super.MILLI_SEC); // trying to execute Gridlets from 0 up to index-1. // NOTE: "up to" means including int index = findPosition(gridletWaitingList_, currentTime); // index is 0 means no Gridlets executing if (index <= 0) { return; } executeReservedGridlet(index); } /** * Schedules Gridlets to empty PEs. * @param index a position number in the reservation list. This * variable also refers to number of PEs required. * @pre index > 0 * @post $none */ private void executeReservedGridlet(int index) { boolean success = false; // if there are empty PEs, then assign straight away if (gridletInExecList_.size() + index <= super.totalPE_) { success = true; } // if no available PE then put unreserved Gridlets into queue list if (success == false) { clearExecList(index); } // a loop to execute Gridlets and remove them from waiting list ResGridlet rgl = null; int i = 0; int totalAllocate = 0; try { while (totalAllocate < index) { rgl = (ResGridlet) gridletWaitingList_.get(i); success = allocatePEtoGridlet(rgl); // only if a Gridlet has been successfully allocated, then // remove it from the waiting list if (success == true) { gridletWaitingList_.remove(i); totalAllocate++; continue; // don't increment i } i++; } } catch (Exception e) { // .... do nothing } } /** * Removes Gridlets in the execution list to make room for new ones. * NOTE: a reservation might contain many Gridlets. Hence, only few * Gridlets executing now. The rest are later. * @param totalPE total number of free PEs needed. * @return total number of free PEs available. * @pre totalPE > 0 * @post $none */ private int clearExecList(int totalPE) { int removeSoFar = 0; // number of PEs removed so far ResGridlet obj = null; // first, remove unreserved Gridlets int i = 0; while ( i < gridletInExecList_.size() ) { obj = (ResGridlet) gridletInExecList_.get(i); if (removeSoFar <= totalPE && obj.hasReserved() == false) { obj.setGridletStatus(Gridlet.QUEUED); gridletInExecList_.remove(i); gridletQueueList_.add(obj); super.resource_.setStatusPE(PE.FREE, obj.getMachineID(), obj.getPEID()); removeSoFar++; continue; } i++; } // if enough, then exit if (removeSoFar == totalPE) { return removeSoFar; } // if still not enough, then remove Gridlets that are running longer long currentTime = super.getCurrentTime(); long finishTime = 0; // searching through the loop again i = 0; while ( i < gridletInExecList_.size() ) { // exit the loop if there are enough space already if (removeSoFar > totalPE) { break; } obj = (ResGridlet) gridletInExecList_.get(i); // look for AR Gridlets that are running longer than estimated if (removeSoFar <= totalPE && obj.hasReserved() == true) { finishTime = obj.getStartTime()+(obj.getDurationTime()*MILLI_SEC); if (finishTime <= currentTime) { obj.setGridletStatus(Gridlet.QUEUED); gridletInExecList_.remove(i); gridletQueueList_.add(obj); super.resource_.setStatusPE(PE.FREE, obj.getMachineID(), obj.getPEID()); removeSoFar++; continue; } } i++; } // end for return removeSoFar; } /** * Handles a query reservation request. * @param reservationID a reservation ID * @param senderID a sender or user ID * @param sendTag a tag to send to the user * @pre reservationID > 0 * @pre senderID > 0 * @post $none */ public void handleQueryReservation(int reservationID, int senderID, int sendTag) { ARObject obj = null; int result = 0; // firstly, look in the reservation list int index = super.searchReservation(reservList_, reservationID); if (index == NOT_FOUND) { // if not found, then look in the expiry list index = super.searchReservation(expiryList_, reservationID); if (index == NOT_FOUND) { result = GridSimTags.AR_STATUS_RESERVATION_DOESNT_EXIST; } else // if found then get the object status { obj = (ARObject) expiryList_.get(index); result = obj.getStatus(); } } else // if found then get the object status { obj = (ARObject) reservList_.get(index);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -