gridresource.java

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

JAVA
1,005
字号
     *              A Machine must contain one or more PEs.     *      </ul>     * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[],     *          String)     * @pre name != null     * @pre link != null     * @pre resource != null     * @pre calendar != null     * @post $none     */    public GridResource(String name, Link link,                ResourceCharacteristics resource, ResourceCalendar calendar)                throws Exception    {        super(name, link);        resource_ = resource;        resCalendar_ = calendar;        policy_ = null;        init();    }    /**     * Allocates a new GridResource object. When making a different type of     * GridResource object, use this constructor and then overrides     * {@link #processOtherEvent(Sim_event)}.     *     * @param name       the name to be associated with this entity (as     *                   required by Sim_entity class from simjava package)     * @param link       the link that will be used to connect this     *                   GridResource to another Entity or Router.     * @param resource   an object of ResourceCharacteristics     * @param calendar   an object of ResourceCalendar     * @param policy     a scheduling policy for this Grid resource. If no     *                   scheduling policy is defined, the default one is     *                   <tt>SpaceShared</tt>     * @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)     * @see gridsim.AllocPolicy     * @pre name != null     * @pre link != null     * @pre resource != null     * @pre calendar != null     * @pre policy != null     * @post $none     */    public GridResource(String name, Link link,            ResourceCharacteristics resource, ResourceCalendar calendar,            AllocPolicy policy) throws Exception    {        super(name,link);        resource_ = resource;        resCalendar_ = calendar;        policy_ = policy;  // the order between policy and init() is important        init();    }    /////////////////////////////////////////////////    /**     * Sets a regional GridInformationService (GIS) entity for this resource     * to communicate with. This resource will then register its ID to the     * regional GIS entity, rather than {@link GridInformationService} or     * <tt>system GIS</tt>.     * @param regionalGIS   name of regional GIS entity     * @return <tt>true</tt> if it is successful, <tt>false</tt> otherwise     * @pre regionalGIS != null     * @post $none     */    public boolean setRegionalGIS(String regionalGIS)    {        if (regionalGIS == null || GridSim.getEntityId(regionalGIS) == -1) {            return false;        }        regionalGISName_ = regionalGIS;        return true;    }    /**     * Sets a regional GridInformationService (GIS) entity for this resource     * to communicate with. This resource will then register its ID to the     * regional GIS entity, rather than {@link GridInformationService} or     * <tt>system GIS</tt>.     * @param gis   regional GIS entity object     * @return <tt>true</tt> if it is successful, <tt>false</tt> otherwise     * @pre gis != null     * @post $none     */    public boolean setRegionalGIS(AbstractGIS gis)    {        if (gis == null) {            return false;        }        return setRegionalGIS( gis.get_name() );    }    /**     * Handles external events that are coming to this GridResource entity.     * This method also registers the identity of this GridResource entity to     * <tt>GridInformationService</tt> class.     * <p>     * The services or tags available for this resource are:     * <ul>     *      <li> {@link gridsim.GridSimTags#RESOURCE_CHARACTERISTICS} </li>     *      <li> {@link gridsim.GridSimTags#RESOURCE_DYNAMICS} </li>     *      <li> {@link gridsim.GridSimTags#GRIDLET_SUBMIT} </li>     *      <li> {@link gridsim.GridSimTags#GRIDLET_CANCEL} </li>     *      <li> {@link gridsim.GridSimTags#GRIDLET_PAUSE} </li>     *      <li> {@link gridsim.GridSimTags#GRIDLET_RESUME} </li>     *      <li> {@link gridsim.GridSimTags#GRIDLET_MOVE} </li>     *      <li> {@link gridsim.GridSimTags#GRIDLET_STATUS} </li>     * </ul>     * <br>     * This method also calls these methods in the following order:     * <ol>     *      <li> {@link #registerOtherEntity()} method     *      <li> {@link #processOtherEvent(Sim_event)} method     * </ol>     *     * @pre $none     * @post $none     */    public void body()    {        // send the registration to GIS        int register = 0;        if (policyType_ == ResourceCharacteristics.ADVANCE_RESERVATION) {            register = GridSimTags.REGISTER_RESOURCE_AR;        }        else {            register = GridSimTags.REGISTER_RESOURCE;        }        // this resource should register to regional GIS.        // However, if not specified, then register to system GIS (the        // default GridInformationService) entity.        int gisID = GridSim.getEntityId(regionalGISName_);        if (gisID == -1) {            gisID = GridSim.getGridInfoServiceEntityId();        }        // need to wait for few seconds before registering to a regional GIS.        // This is because to allow all routers to fill in their routing tables        else        {            super.sim_pause(GridSim.PAUSE);            System.out.println(super.get_name() + ".body(): wait for " +                GridSim.PAUSE + " seconds before registering to " +                regionalGISName_);        }        // send the registration to GIS        super.send(super.output, GridSimTags.SCHEDULE_NOW, register,                new IO_data(new Integer(super.get_id()), SIZE, gisID) );        // Below method is for a child class to override        registerOtherEntity();        // Process events until END_OF_SIMULATION is received from the        // GridSimShutdown Entity        Sim_event ev = new Sim_event();        while ( Sim_system.running() )        {            super.sim_get_next(ev);            // if the simulation finishes then exit the loop            if (ev.get_tag() == GridSimTags.END_OF_SIMULATION)            {                policy_.setEndSimulation();                break;            }            // process the received event            processEvent(ev);        }        // remove I/O entities created during construction of this entity        super.terminateIOEntities();    }    /**     * As of GridSim 2.2, this method is <b>OBSOLETE</b>.     * Allocates one of the PEs to Gridlet for execution and schedules     * an internal event to be delivered at completion time.     * @param gl a Gridlet to be processed     * @deprecated As of GridSim 2.2, this method is <b>OBSOLETE</b>.     * @pre gl != null     * @post $none     */    public void SpaceShare_AllocatePEtoGridlet(Gridlet gl) {        this.spaceShared_AllocatePEtoGridlet(gl);    }    /**     * As of GridSim 2.2, this method is <b>OBSOLETE</b>.     * Allocates one of the PEs to Gridlet for execution and schedules     * an internal event to be delivered at completion time.     * @param gl a Gridlet to be processed     * @deprecated As of GridSim 2.2, this method is <b>OBSOLETE</b>.     * @pre gl != null     * @post $none     */    public void spaceShared_AllocatePEtoGridlet(Gridlet gl)    {        System.out.println("GridResource.spaceShared_AllocatePEtoGridlet()" +                " is OBSOLETE since GridSim 2.2. Don't use this method.");    }    //////////////////// PROTECTED METHODS ///////////////////////////////////    /**     * Overrides this method when making a new and different type of resource.     * This method is called by {@link #body()} for incoming unknown tags.     * <p>     * Another approach is to override the     * {@link gridsim.AllocPolicy#processOtherEvent(Sim_event)} method.     * This approach is desirable if you do not want to create a new type of     * grid resource.     *     * @param ev   a Sim_event object     * @pre ev != null     * @post $none     */    protected void processOtherEvent(Sim_event ev)    {        if (ev == null)        {            System.out.println(super.get_name() + ".processOtherEvent(): " +                    "Error - an event is null.");            return;        }        /****   // NOTE: now a resource passes a new event to the scheduler        System.out.println(super.get_name()+".processOtherEvent(): Unable to " +                "handle request from GridSimTags with tag number " +                ev.get_tag() );        *****/        policy_.processOtherEvent(ev);    }    /**     * Overrides this method when making a new and different type of resource.     * This method is called by {@link #body()} to register other type to     * {@link gridsim.GridInformationService} entity. In doing so, you     * need to create a new child class extending from     * {@link gridsim.GridInformationService}.     * <br>     * <b>NOTE:</b> You do not need to override {@link #body()} method, if     * you use this method.     *     * @pre $none     * @post $none     * @see gridsim.GridInformationService     */    protected void registerOtherEntity() {        // empty. This should be override by a child class    }    //////////////////// PRIVATE METHODS ///////////////////////////////////    /**     * Initializes the resource allocation policy     * @throws Exception    If number of PEs is zero     * @pre $none     * @post $none     */    private void init() throws Exception    {        // If this resource doesn't have any PEs then no useful at all        if (resource_.getNumPE() == 0)        {            throw new Exception(super.get_name() + " : Error - this entity " +                    "has no PEs. Therefore, can't process any Gridlets.");        }        // stores id of this class        resource_.setResourceID( super.get_id() );        // if internal allocation policy is used        policyType_ = resource_.getResourceAllocationPolicy();        if (policy_ == null)        {            switch (policyType_)            {                case ResourceCharacteristics.TIME_SHARED:                    policy_ = new TimeShared(super.get_name(), "TimeShared");                    break;                case ResourceCharacteristics.SPACE_SHARED:                    policy_ = new SpaceShared(super.get_name(), "SpaceShared");                    break;                default:                    throw new Exception(super.get_name()+" : Error - supports"+                            " only TimeShared or SpaceShared policy.");            }        }        policy_.init(resource_, resCalendar_, super.output);    }    /**     * Processes events or services that are available for this GridResource     * @param ev    a Sim_event object     * @pre ev != null     * @post $none     */    private void processEvent(Sim_event ev)    {        int src_id = -1;        switch ( ev.get_tag() )        {            // Resource characteristics inquiry            case GridSimTags.RESOURCE_CHARACTERISTICS:                src_id = ( (Integer) ev.get_data() ).intValue();                super.send( super.output, 0.0, ev.get_tag(),                        new IO_data(resource_,resource_.getByteSize(),src_id));                break;                // Resource dynamic info inquiry            case GridSimTags.RESOURCE_DYNAMICS:

⌨️ 快捷键说明

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