timesharedwithfailure.java

来自「中間件開發详细说明:清华大学J2EE教程讲义(ppt)-Tsinghua Uni」· Java 代码 · 共 892 行 · 第 1/3 页

JAVA
892
字号
    /**     * Determines the smallest completion time of all Gridlets in the execution     * list. The smallest time is used as an internal event to     * update Gridlets processing in the future.     * <p>     * The algorithm for this method:     * <ul>     *     <li> identify the finish time for each Gridlet in the execution list     *          given the share MIPS rating for all and the remaining Gridlet's     *          length     *     <li> find the smallest finish time in the list     *     <li> send the last Gridlet in the list with     *          <tt>delay =  smallest finish time - current time</tt>     * </ul>     * @pre $none     * @post $none     */    private void forecastGridlet()    {        // if no Gridlets available in exec list, then exit this method        if (gridletInExecList_.size() == 0) {            return;        }        // checks whether Gridlets have finished or not. If yes, then remove        // them since they will effect the MIShare calculation.        checkGridletCompletion();        // Identify MIPS share for all Gridlets for 1 second, considering        // current Gridlets + No of PEs.        MIShares share = getMIShare( 1.0, gridletInExecList_.size() );        ResGridlet rgl = null;        int i = 0;        double time = 0.0;        double rating = 0.0;        double smallestTime = 0.0;        // For each Gridlet, determines their finish time        Iterator iter = gridletInExecList_.iterator();        while ( iter.hasNext() )        {            rgl = (ResGridlet) iter.next();            // If a Gridlet locates before the max count then it will be given            // the max. MIPS rating            if (i < share.maxCount) {                rating = share.max;            }            else {   // otherwise, it will be given the min. MIPS Rating                rating = share.min;            }            time = forecastFinishTime(rating, rgl.getRemainingGridletLength() );            int roundUpTime = (int) (time+1);   // rounding up            rgl.setFinishTime(roundUpTime);            // get the smallest time of all Gridlets            if (i == 0 || smallestTime > time) {                smallestTime = time;            }            i++;        }        // sends to itself as an internal event        super.sendInternalEvent(smallestTime);    }    /**     * Checks all Gridlets in the execution list whether they are finished or     * not.     * @pre $none     * @post $none     */    private void checkGridletCompletion()    {        ResGridlet rgl = null;        // a loop that determine the smallest finish time of a Gridlet        // Don't use an iterator since it causes an exception because if        // a Gridlet is finished, gridletFinish() will remove it from the list.        int i = 0;        while ( i < gridletInExecList_.size() )        {            rgl = (ResGridlet) gridletInExecList_.get(i);            // if a Gridlet has finished, then remove it from the list            if (rgl.getRemainingGridletLength() <= 0.0)            {                gridletFinish(rgl, Gridlet.SUCCESS);                continue;  // not increment i coz the list size also decreases            }            i++;        }    }    /**     * 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.     */    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;    }    /**     * Updates the Gridlet's properties, such as status once a     * Gridlet is considered finished.     * @param rgl     a ResGridlet object     * @param status  the status of this ResGridlet object     * @pre rgl != null     * @post $none     */    private void gridletFinish(ResGridlet rgl, int status)    {        // NOTE: the order is important! Set the status first then finalize        // due to timing issues in ResGridlet class.        rgl.setGridletStatus(status);        rgl.finalizeGridlet();        // sends back the Gridlet with no delay        Gridlet gl = rgl.getGridlet();        super.sendFinishGridlet(gl);        // remove this Gridlet in the execution        gridletInExecList_.remove(rgl);    }    /**     * Handles internal event     * @pre $none     * @post $none     */    private void internalEvent()    {        // this is a constraint that prevents an infinite loop        // Compare between 2 floating point numbers. This might be incorrect        // for some hardware platform.        if ( lastUpdateTime_ == GridSim.clock() ) {            return;        }        // update Gridlets in execution up to this point in time        updateGridletProcessing();        // schedule next event        forecastGridlet();    }    /**     * 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;        // Check whether the Gridlet is in execution list or not        int found = super.findGridlet(gridletInExecList_, gridletId, userId);        // if a Gridlet is in execution list        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            if (rgl.getRemainingGridletLength() == 0.0) {                rgl.setGridletStatus(Gridlet.SUCCESS);            }            else {                rgl.setGridletStatus(Gridlet.CANCELED);            }            // then forecast the next Gridlet to complete            forecastGridlet();        }        // if a Gridlet is not in exec list, then find it in the paused list        else        {            found = super.findGridlet(gridletPausedList_, gridletId, userId);            // if a Gridlet is found in the paused list then remove it            if (found >= 0)            {                rgl = (ResGridlet) gridletPausedList_.remove(found);                rgl.setGridletStatus(Gridlet.CANCELED);            }        }        return rgl;    }    /**     * Sets the status of all Gridlets in this resource to <tt>FAILED</tt>.     * Then sends them back to users, and clean up the relevant lists.     */    public void setGridletsFailed()    {        ResGridlet rgl;        int gridletPausedList_size = gridletPausedList_.size();        int gridletInExecList_size = gridletInExecList_.size();        /*******************        // Uncomment this to get more info on the progress of sims        System.out.println("################# " + super.get_name() +        ". Cleaning gridlets in InExec and Paused lists in the failed resource." +        " gridletInExecList_.size (): " + gridletInExecList_.size() +        " gridletPausedList_.size (): " + gridletPausedList_.size());        *******************/        for (int i = 0; i<gridletPausedList_size; i++)        {            rgl = (ResGridlet) gridletPausedList_.get(0);            int status = rgl.getGridletStatus();            // if the gridlet has already finished, then just send it back.            // Otherwise, set status to FAILED_RESOURCE_UNAVAILABLE            if (status != Gridlet.SUCCESS)            {                rgl.setGridletStatus(Gridlet.FAILED_RESOURCE_UNAVAILABLE);            }            rgl.finalizeGridlet();            super.sendFinishGridlet(rgl.getGridlet());            gridletPausedList_.remove(0);        }        updateGridletProcessing();        // go on with the gridlet in exec list.        for (int i = 0; i < gridletInExecList_size; i++)        {            rgl = (ResGridlet) gridletInExecList_.get(0);            int status = rgl.getGridletStatus();            // if the gridlet has already finished, then just send it back.            // Otherwise, set status to FAILED_RESOURCE_UNAVAILABLE            if (status != Gridlet.SUCCESS)            {                rgl.setGridletStatus(Gridlet.FAILED_RESOURCE_UNAVAILABLE);            }            rgl.finalizeGridlet();            super.sendFinishGridlet(rgl.getGridlet());            gridletInExecList_.remove(0);        }    }    /**     * This method is empty because it is not required by this policy.     * Sets the status of all Gridlets in this machine to <tt>FAILED</tt>.     * Then sends them back to users, and clean up the relevant lists.     * @param failedMachID  the id of the failed machine     */    public void setGridletsFailed(int failedMachID)    {        // Nothing, as in a time_shared machine, all the gridlets use the        // machine in turn, during a period of time.        // So, gridlets are not allocated tightly to a machine or PE.        // Because of that, we don't have to make fail any gridlet        // when a machine fails    }} // end class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?