⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 allocpolicy.java

📁 一个非常著名的网格模拟器,能够运行网格调度算法!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        int simTimeZone = calendar.getTimeZone().getRawOffset() /                          AdvanceReservation.HOUR;        // then convert into the local resource time        initTime_ = AdvanceReservation.convertTimeZone( simTime, simTimeZone,                           resource_.getResourceTimeZone() );    }    ////////////////////// PROTECTED METHODS //////////////////////////////    /**     * Allocates a new AllocPolicy object. A child class should call this method     * during its constructor. The name of this entity (or the child class that     * inherits this class) will be <tt>"resName_entityName"</tt>.     *     * @param resName    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 resName != null     * @pre entityName != null     * @post $none     */    protected AllocPolicy(String resName, String entityName) throws Exception    {        super(resName + "_" + entityName);        myId_ = GridSim.getEntityId(resName + "_" + entityName);        resName_ = resName;        initTime_ = 0;        // default value        outputPort_ = null;        resource_ = null;        resCalendar_ = null;        endSimulation_ = false;        totalPE_ = 0;        accTotalLoad_ = new Accumulator();    }    /**     * Adds the given load into the overall total load for this entity     * @param load   current GridResource load     * @pre load >= 0.0     * @post $none     */    protected void addTotalLoad(double load) {        accTotalLoad_.add(load);    }    /**     * Finds a Gridlet inside a given list. This method needs a combination of     * Gridlet Id and User Id because each Grid User might have exactly the     * same Gridlet Id submitted to this GridResource.     *     * @param obj   a Collection object that contains a list of ResGridlet     * @param gridletId    a Gridlet Id     * @param userId       an User Id     * @return the location of a Gridlet or <tt>-1</tt> if not found     * @pre obj != null     * @pre gridletId >= 0     * @pre userId >= 0     * @post $none     */    protected int findGridlet(Collection obj, int gridletId, int userId)    {        ResGridlet rgl = null;        int found = -1;     // means the Gridlet is not in the list        try        {            // Search through the list to find the given Gridlet object            int i = 0;            Iterator iter = obj.iterator();            while ( iter.hasNext() )            {                rgl = (ResGridlet) iter.next();                // Need to check against the userId as well since                // each user might have same Gridlet id submitted to                // same GridResource                if (rgl.getGridletID()==gridletId && rgl.getUserID()==userId)                {                    found = i;                    break;                }                i++;            }        }        catch (Exception e)        {            System.out.println(super.get_name() +                               ".findGridlet(): Exception error occurs.");            System.out.println( e.getMessage() );        }        return found;    }    /**     * Calculates the current load of a GridResource for a given number of     * Gridlets currently in execution. This method can be overridden by     * child class, if the below algorithm doesn't suitable for a particular     * type of scheduling:     * <code>     * <br> <br>     * // totalPE = total PEs that this GridResource has. <br>     * // It can be found out using ResourceCharacteristics.getNumPE(); <br>     * int numGridletPerPE = (totalGridletSize + 1) / totalPE;  <br><br>     * // load is between [0.0, 1.0] where 1.0 is busy and 0.0 is not busy<br>     * double localLoad = resCalendar_.getCurrentLoad(); <br>     * double totalLoad = 1.0 - ( (1 - localLoad) / numGridletPerPE ); <br>     * <br>     * </code>     *     * @param size  total Gridlets in execution size     * @return total load between range [0.0, 1.0]     * @see gridsim.ResourceCharacteristics#getNumPE()     * @see gridsim.ResourceCalendar#getCurrentLoad()     * @pre $none     * @post $none     */    protected double calculateTotalLoad(int size)    {        double val = (size + 1.0) / totalPE_;        int numGridletPerPE = (int) Math.ceil(val);        // load is between [0.0, 1.0] where 1.0 is busy and 0.0 is not busy        double localLoad = resCalendar_.getCurrentLoad();        double load = 1.0 - ( (1 - localLoad) / numGridletPerPE );        if (load < 0.0) {            load = 0.0;        }        return load;    }    /**     * Sends an acknowledgement to the sender. This method is only suitable     * for the following tags:     * <ul>     *     <li> GridSimTags.GRIDLET_PAUSE_ACK     *     <li> GridSimTags.GRIDLET_RESUME_ACK     *     <li> case GridSimTags.GRIDLET_SUBMIT_ACK     * </ul>     *     * <p>     * At the receiving end, <tt>gridletPause()</tt>, <tt>gridletResume()</tt>     * and <tt>gridletSubmit()</tt> will be responsible for this acknowledgment     * data.     *     * @param tag   event tag as described above     * @param status   <tt>true</tt> if the operation has been completed     *                 successfully, <tt>false</tt> otherwise     * @param gridletId   the Gridlet ID     * @param destId      the sender ID. This can also be the user or owner's     *                    ID for this Gridlet     * @return <tt>true</tt> if an acknowledgment has been sent successfully,     *         <tt>false</tt> otherwise     * @see gridsim.GridSim#gridletPause(int, int, int, double, boolean)     * @see gridsim.GridSim#gridletResume(int, int, int, double, boolean)     * @see gridsim.GridSim#gridletSubmit(Gridlet, int, double, boolean)     * @pre tag >= 0     * @pre gridletId >= 0     * @pre destId >= 0     * @post $none     */    protected boolean sendAck(int tag,boolean status,int gridletId,int destId)    {        boolean success = false;        switch (tag)        {            case GridSimTags.GRIDLET_PAUSE_ACK:            case GridSimTags.GRIDLET_RESUME_ACK:            case GridSimTags.GRIDLET_SUBMIT_ACK:                int[] array = new int[ARRAY_SIZE];                array[0] = gridletId;                if (status == true) {                    array[1] = GridSimTags.TRUE;                }                else {                    array[1] = GridSimTags.FALSE;                }                super.sim_schedule( outputPort_, GridSimTags.SCHEDULE_NOW, tag,                                    new IO_data(array, 8, destId) );                success = true;                break;            default:                System.out.println(super.get_name() +                                   ".sendAck(): Invalid tag ID.");                break;        }        return success;    }    /**     * Sends the canceled Gridlet back to sender. This method is only valid     * for GridSimTags.GRIDLET_CANCEL.     *     * @param tag   event tag as described above     * @param gl    a Gridlet object     * @param gridletId   the Gridlet ID     * @param destId      the sender ID. This can also be the user or owner's     *                    ID for this Gridlet     * @return <tt>true</tt> if the Gridlet has been sent successfully,     *         <tt>false</tt> otherwise     * @see gridsim.GridSim#gridletCancel(int, int, int, double)     * @pre tag >= 0     * @pre gridletId >= 0     * @pre destId >= 0     * @post $none     */    protected boolean sendCancelGridlet(int tag, Gridlet gl, int gridletId,                                        int destId)    {        if (tag != GridSimTags.GRIDLET_CANCEL) {            return false;        }        long gridletSize = 0;        if (gl != null) {            gridletSize = gl.getGridletOutputSize();        }        // if no Gridlet found, then create a new Gridlet but set its status        // to FAILED. Then, most importantly, set the resource parameters        // because the user will search/filter based on a resource ID.        else if (gl == null)        {            try            {                gridletSize = 100;                gl = new Gridlet(gridletId, 0, gridletSize, gridletSize);                gl.setGridletStatus(Gridlet.FAILED);                gl.setResourceParameter(resId_, resource_.getCostPerSec());            }            catch(Exception e) {                // empty ...            }        }        super.sim_schedule( outputPort_, GridSimTags.SCHEDULE_NOW, tag,                            new IO_data(gl, gridletSize, destId) );        return true;    }    /**     * Migrates a Gridlet from this GridResource ID to the destination ID     * @param   gl    a Gridlet object that is going to be executed     * @param destId  a new destination GridResource Id     * @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)     * @return <tt>true</tt> if the Gridlet has been sent successfully,     *         <tt>false</tt> otherwise     * @see gridsim.GridSim#gridletMove(int, int, int, int, double, boolean)     * @pre gl != null     * @pre destId >= 0     * @post $none     */    protected boolean gridletMigrate(Gridlet gl, int destId, boolean ack)    {        if (gl == null) {            return false;        }        IO_data data = new IO_data(gl, gl.getGridletOutputSize(), destId);        int tag = 0;        if (ack == true) {            tag = GridSimTags.GRIDLET_SUBMIT_ACK;        }        else {            tag = GridSimTags.GRIDLET_SUBMIT;        }        super.sim_schedule(outputPort_, GridSimTags.SCHEDULE_NOW, tag, data);        return true;    }    /**     * Sends the completed Gridlet back to sender or Gridlet's user ID     * @param gl  a completed Gridlet object     * @return <tt>true</tt> if the Gridlet has been sent successfully,     *         <tt>false</tt> otherwise     * @pre gl != null     * @post $none     */    protected boolean sendFinishGridlet(Gridlet gl)    {        IO_data obj = new IO_data(gl,gl.getGridletOutputSize(),gl.getUserID());        super.sim_schedule(outputPort_, 0, GridSimTags.GRIDLET_RETURN, obj);        return true;    }    /**     * Sends an internal event to itself     * @param time   the simulation delay time     * @return <tt>true</tt> if the event has been sent successfully,     *         <tt>false</tt> otherwise     * @pre time >= 0.0     * @post $none     */    protected boolean sendInternalEvent(double time)    {        if (time < 0.0) {            time = 0.0;        }        super.sim_schedule(myId_, time, GridSimTags.INSIGNIFICANT);        return true;    }    /**     * Sends an internal event to itself with a certain tag     * @param time    the simulation delay time     * @param tag     a tag ID     * @return <tt>true</tt> if the event has been sent successfully,     *         <tt>false</tt> otherwise     * @pre time >= 0.0     * @post $none     */    protected boolean sendInternalEvent(double time, int tag)    {        if (time < 0.0) {            time = 0.0;        }        super.sim_schedule(myId_, time, tag);        return true;    }} // end class

⌨️ 快捷键说明

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