arpolicy.java

来自「一个非常著名的网格模拟器,能够运行网格调度算法!」· Java 代码 · 共 603 行 · 第 1/2 页

JAVA
603
字号
        for (int i = 0; i < tagArray_.length; i++) {            tagArray_[i]= GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_1_SEC - i;        }    }    /**     * Sends a result of a create reservation request.     * @param destID   a destination or user ID     * @param tag      a tag to send to the user     * @param expiryTime   reservation expiry time. If no empty slots, then     *        expiryTime should be set to -1     * @param reservID     a reservation ID. If no empty slots, then reservID     *        should be set to one of GridSimTags.AR_CREATE_XXXX tags where     *        XXXX = a specific tag name.     * @pre destID > 0     * @post $none     * @see gridsim.GridSimTags     */    protected void replyCreateReservation(int destID, int tag, long expiryTime,                                          int reservID)    {        long[] sendArray = new long[MAX+1];        sendArray[0] = tag - GridSimTags.RETURN_AR_CREATE;        sendArray[1] = reservID;     // reservation id        sendArray[2] = expiryTime;   // expiry time        int size = (3*8) + 6;  // 3 longs * 8 bytes + overheads        super.sim_schedule(super.outputPort_, 0, GridSimTags.RETURN_AR_CREATE,                           new IO_data(sendArray, size, destID) );    }    /**     * Sends a result of a cancel reservation request.     * @param destID     a destination or user ID     * @param tag        a tag to send to the user     * @param result     a result tag. This tag should be one of     *        GridSimTags.AR_CANCEL_XXXX where XXXX = a specific tag name.     * @pre destID > 0     * @pre tag > 0     * @post $none     * @see gridsim.GridSimTags     */    protected void replyCancelReservation(int destID, int tag, int result)    {        int[] array = new int[MAX];        array[0] = tag - GridSimTags.RETURN_AR_CANCEL;   // transaction ID        array[1] = result;          // outcome of this functionality        // for cancel, just send the tag result        super.sim_schedule(super.outputPort_, 0, GridSimTags.RETURN_AR_CANCEL,                           new IO_data(array, SIZE, destID) );    }    /**     * Sends a result of a commit reservation request.     * @param destID     a destination or user ID     * @param tag        a tag to send to the user     * @param result     a result tag. This tag should be one of     *        GridSimTags.AR_COMMIT_XXXX where XXXX = a specific tag name.     * @pre destID > 0     * @pre tag > 0     * @post $none     * @see gridsim.GridSimTags     */    protected void replyCommitReservation(int destID, int tag, int result)    {        int[] array = new int[MAX];        array[0] = tag - GridSimTags.RETURN_AR_COMMIT;   // transaction ID        array[1] = result;          // outcome of this functionality        // for commit, just send the tag result, same as cancel        super.sim_schedule(super.outputPort_, 0, GridSimTags.RETURN_AR_COMMIT,                           new IO_data(array, SIZE, destID) );    }    /**     * Sends a result of a query reservation request.     * @param destID     a destination or user ID     * @param tag        a tag to send to the user     * @param result     a result tag. This tag should be one of     *        GridSimTags.AR_STATUS_XXXX where XXXX = a specific tag name.     * @pre destID > 0     * @pre tag > 0     * @post $none     * @see gridsim.GridSimTags     */    protected void replyQueryReservation(int destID, int tag, int result)    {        int[] array = new int[MAX];        array[0] = tag - GridSimTags.RETURN_AR_QUERY_STATUS;  // transaction ID        array[1] = result;          // outcome of this functionality        // for query, just send the tag result        super.sim_schedule(super.outputPort_, 0, GridSimTags.RETURN_AR_QUERY_STATUS,                           new IO_data(array, SIZE, destID) );    }    /**     * Sends a result of a modify reservation request.     * @param destID     a destination or user ID     * @param tag        a tag to send to the user     * @param result     a result tag. This tag should be one of     *        GridSimTags.AR_MODIFY_XXXX where XXXX = a specific tag name.     * @pre destID > 0     * @pre tag > 0     * @post $none     * @see gridsim.GridSimTags     */    protected void replyModifyReservation(int destID, int tag, int result)    {        int[] array = new int[MAX];        array[0] = tag - GridSimTags.RETURN_AR_MODIFY;  // transaction ID        array[1] = result;          // outcome of this functionality        // for modification, just send the tag result        super.sim_schedule(super.outputPort_, 0, GridSimTags.RETURN_AR_MODIFY,                           new IO_data(array, SIZE, destID) );    }    // this is in response to queryBusyTime() and queryFreeTime()    /**     * Sends a result of a query busy or free time request.     * @param destID     a destination or user ID     * @param tag        a tag to send to the user     * @param result     a list of results.     * Each object inside ArrayList is <tt>long array[3]</tt>, with:     * <ul>     *    <li> array[0] = start time     *    <li> array[1] = duration time     *    <li> array[2] = number of PEs     * </ul>     *     * @pre destID > 0     * @post $none     * @see gridsim.GridSimTags     */    protected void replyTimeReservation(int destID, int tag, ArrayList result,                            double userTimeZone)    {        int size = SIZE;        if (result != null) {            size = (4*8) + result.size();  // 3 longs + array size + overheads        }        Object[] array = new Object[MAX];        array[0] = new Integer(tag - GridSimTags.RETURN_AR_QUERY_TIME);        array[1] = result;        // NOTE: need to convert all start time into user local time        convertToUserTime(result, userTimeZone);        // for query, just send the result list        super.sim_schedule(super.outputPort_,0,GridSimTags.RETURN_AR_QUERY_TIME,                           new IO_data(array, SIZE, destID) );    }    // only convert the start time (result[0]) from local/resource time to    // user time    private void convertToUserTime(ArrayList result, double userTimeZone)    {        if (result == null) {            return;        }        double resTimeZone = resource_.getResourceTimeZone();        long[] array = null;        long from = 0;        try        {            // convert the start time of a local time to a user time            Iterator it = result.iterator();            while ( it.hasNext() )            {                array = (long[]) it.next();                from = AdvanceReservation.convertTimeZone(array[0], resTimeZone,                            userTimeZone);                array[0] = from;            }        }        catch (Exception e) {            // print error msg        }    }    /**     * Gets the current time. Time calculates from simulation init time +     * (GridSim.clock * MILLI_SEC). MILLI_SEC = 1000.     * @return current time in seconds     * @pre $none     * @post $result > 0     */    protected long getCurrentTime() {        return initTime_ + (int) (GridSim.clock() * MILLI_SEC);    }    /**     * Search for a particular reservation in a data structure     * @param obj   a data structure     * @param reservationID  a reservation ID     * @return location in the data structure or <tt>-1</tt> if not found     * @pre obj != null     * @post $none     */    protected int searchReservation(Collection obj, int reservationID)    {        ARObject arObj = 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() )            {                arObj = (ARObject) iter.next();                // Need to check against the userId as well since                // each user might have same Gridlet id submitted to                // same GridResource                if (arObj.getReservationID() == reservationID)                {                    found = i;                    break;                }                i++;            }        }        catch (Exception e)        {            System.out.println("ARPolicy.findReservedGridlet(): Error occurs.");            System.out.println( e.getMessage() );        }        return found;    }    /**     * Approximates busy time     * @param time   busy time     * @return a time interval tag. This tag should be one of     *         GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_XXXX where     *         XXXX = a specific time interval.     */    protected int approxBusyTime(long time)    {        int SECOND = 1;        int MINUTE = 60 * SECOND;  // 1 minute = 60 secs        int HOUR = 60 * MINUTE;    // 1 hour = 60 minute        int busy = (int) time / MILLI_SEC;   // already in seconds        // find one of the type: sec or min or hour        int type = 0;        if (busy < MINUTE) {            type = SECOND;        }        else if (busy < HOUR) {            type = MINUTE;        }        else {            type = HOUR;        }        // then do a loop to find the nearest time interval. Always rounding        // up, e.g. time is 6 minutes, so nearest time would be 10 minutes.        int temp = 0;        int i = 0;        for (i = 0; i < timeArray_.length; i++)        {            temp = timeArray_[i] * type;            if (busy <= temp) {                break;            }        }        // make sure i is not out of range, since array starts at 0        if (i == timeArray_.length) {            i = timeArray_.length - 1;        }        // then find the exact location to determine the correct tag value        // tag[0 ... (TAG_SIZE-1)] = time interval for seconds        if (type == SECOND) {            temp = 0;        }        // tag[TAG_SIZE ... (TAG_SIZE+TAG_SIZE - 1)] = time interval for minutes        else if (type == MINUTE) {            temp = TAG_SIZE;        }        // the rests are for hour intervals        else {            temp = TAG_SIZE + TAG_SIZE;        }        return tagArray_[i + temp];    }} // end class

⌨️ 快捷键说明

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