Preempt an Entity in a Server

Definition of Preemption

Preemption from a server is the replacement of an entity in the server by a different entity that satisfies certain criteria. The Single Server block supports preemption. The preempted entity immediately departs from the block through the P entity output port instead of through the usual OUT port.

Criteria for Preemption

Whether preemption occurs depends on attribute values of the entity in the server and of the entity attempting to arrive at the server. You specify the attribute using the Sorting attribute name parameter in the Single Server block's dialog box. You use the Sorting direction parameter to indicate whether the preempting entity has a smaller (Ascending) or larger (Descending) value of the attribute, compared to the entity being replaced. (Both parameters are available after you select Permit preemption based on attribute.) To assign values of the sorting attribute for each entity, you can use the Set Attribute block, as described in Set Entity Attributes. Valid values for the sorting attribute are any real numbers, Inf, and -Inf.

If the attribute values are equal, no preemption occurs.

When preemption is supposed to occur, the P port must not be blocked. Consider connecting the P port to a queue or server with infinite capacity, to prevent a blockage during the simulation.

    Note:   You can interpret the value of the sorting attribute as an entity priority. However, this value has nothing to do with event priorities or block priorities.

Residual Service Time

A preempted entity might or might not have completed its service time. The remaining service time the entity would have required if it had not been preempted is called the entity's residual service time. If you select Write residual service time to attribute in the Single Server block, then the block records the residual service time of each preempted entity in an attribute of that entity. If the entity completes its service time before preemption occurs, then the residual service time is zero.

For entities that depart from the block's OUT entity output port (that is, entities that are not preempted), the block records a residual service time only if the entity already has an attribute whose name matches the Residual service time attribute name parameter value. In this case, the block sets that attribute to zero when the entity departs from the OUT port.

Queuing Disciplines for Preemptive Servers

When you permit preemption in a Single Server block preceded by a queue, only the entity at the head of the queue can preempt an entity in the server.

The Priority Queue block is particularly appropriate for use with the preemption feature of the Single Server block. When an entity with sufficiently high priority arrives at the Priority Queue block, the entity goes to the head of the queue and immediately advances to the server.

Preemption by High-Priority Entities

The following example (open modelmodel) generates two classes of entities, most with an EntityPriority attribute value of 0 and some with an EntityPriority attribute value of -Inf. The sorting direction in the Priority Queue and Single Server blocks is Ascending, so entities with sorting attribute values of -Inf go to the head of the priority queue and immediately preempt any entity in the server except another entity whose sorting attribute value is -Inf.

One plot shows when nonpreemptive departures occur, while another plot indicates the residual service time whenever preemptive departures occur.

Appearance of Preemption-Related Operations in Debugger

To see how operations related to preemption appear in the SimEvents® debugger, first zoom in on the plot of residual service time to find approximate times when preemptions occur. For example, the second preemption occurs shortly after T=17. Then, use the debugger:

  1. Begin a debugger session for the example model. At the MATLAB® command prompt, enter:

    simeventsdocex('doc_preemptiveserver');
    sedebug('doc_preemptiveserver')
  2. Proceed in the simulation. At the sedebug>> prompt, enter:

    tbreak 17
    cont

    The partial output indicates that an event is about to execute shortly after T=17:

    Hit b1 : Breakpoint for first operation at or after time 17
    
    %==============================================================================%
    Executing EntityGeneration Event (ev43)               Time = 17.043502632805254
    : Entity = <none>                                     Priority = 300
    : Block  = Time-Based Entity Generator1
    

    The event is the generation of the entity that preempts an entity in the server, but you cannot see that level of detail yet.

  3. Proceed in the simulation to see what happens as a result of the event execution:

    step over
    step over
    step

    The output shows that a new entity with identifier en19 advances to the head (position 1) of the priority queue and preempts the entity in the server.

        %..........................................................................%
        Generating Entity (en19)
        : Block = Time-Based Entity Generator1
        %..........................................................................%
        Entity Advancing (en19)
        : From = Time-Based Entity Generator1
        : To   = Set Attribute1
        %..........................................................................%
        Setting Attribute on Entity (en19)
        : EntityPriority = -Inf
        : Block = Set Attribute1
        %..........................................................................%
        Entity Advancing (en19)
        : From = Set Attribute1
        : To   = Path Combiner
        %..........................................................................%
        Entity Advancing (en19)
        : From = Path Combiner
        : To   = Priority Queue
        %..........................................................................%
        Queuing Entity (en19)
        : Priority Pos = 1 of 3
        : Capacity = 25
        : Block    = Priority Queue
        %..........................................................................%
        Scheduling NewHeadOfQueue Event (ev68)
        : EventTime = 17.043502632805254 (Now)
        : Priority  = SYS2
        : Entity    = <none>
        : Block     = Priority Queue
        %..........................................................................%
        Scheduling EntityGeneration Event (ev69)
        : EventTime = 29.662073718098188
        : Priority  = 300
        : Entity    = <none>
        : Block     = Time-Based Entity Generator1
    %==============================================================================%
    Executing NewHeadOfQueue Event (ev68)                 Time = 17.043502632805254
    : Entity = <none>                                     Priority = SYS2
    : Block  = Priority Queue
        %..........................................................................%
        Scheduling NewHeadOfQueue Event (ev70)
        : EventTime = 17.043502632805254 (Now)
        : Priority  = SYS2
        : Entity    = <none>
        : Block     = Priority Queue
        %..........................................................................%
        Entity Advancing (en19)
        : From = Priority Queue
        : To   = Single Server
        %..........................................................................%
        Preempting Entity (en16)
        : NewEntity = en19 (EntityPriority = -Inf)
        : OldEntity = en16 (EntityPriority = 0)
        : Block = Single Server
        %..........................................................................%
        Canceling ServiceCompletion Event (ev66)
        : EventTime = 17.094640781246184
        : Priority  = 500
        : Entity    = en16
        : Block     = Single Server

    Further output reflects the effect on the preempted entity: its service completion event no longer applies, it carries the residual service time in an attribute, and it advances to the block that connects to the P port of the server.

        %..........................................................................%
        Canceling ServiceCompletion Event (ev66)
        : EventTime = 17.094640781246184
        : Priority  = 500
        : Entity    = en16
        : Block     = Single Server
        %..........................................................................%
        Setting Attribute on Entity (en16)
        : ResidualServiceTime = 0.0511381484409306
        : Block = Single Server
        %..........................................................................%
        Entity Advancing (en16)
        : From = Single Server
        : To   = Attribute Scope
        %..........................................................................%
        Executing Scope
        : Block = Attribute Scope
        %..........................................................................%
        Destroying Entity (en16)
        : Block = Attribute Scope
    
  4. End the debugger session. At the sedebug>> prompt, enter:

    sedb.quit
Was this topic helpful?