Code File Location: VisualSim/actor/lib/Scheduler_SW
o Used to define a processing resource that can be called from multiple points in a model.
o Separate the behavior flow from the architecture definition.
o Model multi-threaded applications with preemption between them.
o The entity can be hardware component, software scheduler or RTOS, and physical entities such as people and conveyer belts.
The following is a block diagram that shows the application of a SystemResource block.
This block is a timed resource that combines a single input queue and a processing (server) resource. A timed resource consumes units of time to emulate the processing delay across a entity.
Transactions arrive at the SystemResource from Mappers and Script (using RegEx function called scheduleTask) blocks and are queued based on priority. Each transaction is allocated scheduler time based on the Scheduler_Type selection. The connection between the Mappers and the Scheduler is virtual. This means that no wires are used to make the connections. When the requested Resource time has been consumed, the transaction is sent back to the Mapper. The Mapper then sends the transaction on the output port.
If the SystemResource is not busy, then the task at the Head of the Queue will start to execute. If the SystemResource is busy, then the incoming task will be enqueued if Scheduler_Type is FCFS (First-Come-First_Serve); or the incoming task will be start to execute if Scheduler_Type is FCFS + Preempt if the incoming priority is higher than the executing task, else it will be enqueued. For the Scheduler_Type of Round-Robin, the task will be provided the time share in the order of arrival.
The ports are for plotting (state_plot) and viewing the transaction (output) that has completed. The task is sent to the SystemResource that is identified by the Resource_Name in the Mapper block or the Next_Resource in the transmitting SystemResource. The SystemResource is identified by the field Resource_Name. The incoming transaction must provide the following information- SystemResource_Name, Task Time or processor duration, Task_Priority (optional) and Task_Number (Identifier used for plotting purpose).
To understand the usage of the SystemResource, view the SystemResource Example in the BDE. View the parameters in the SystemResource and Mapper blocks and the plotting.
o Input queue is ordered based on the priority of the transaction. Higher the number signifies higher priority.
o The SystemResource can handle time as a double value (number of seconds) or cycles (divided by the Speed_MHz).
o Three default scheduling schemes and two user-defined are provided.
o Executing tasks can be preempted (Scheduler setting).
o Requesting transaction can arrive from any part of the model.
o Supports two Power_Manager states- Active and Stanby. Standby is when the Scheduler is inactive. Active is when the Scheduler is processing a task.
o User can define additional states for each Scheduler. Using the Power RegEx, the user can move the Scheduler into any state.
o Supports Power_Manager switching, The transition from these states to Active or Active to Standby or Standby to Active will be delayed by the respective Cycles before the changing states.
o getBlockStatus (SystemResource Name”, “Any value”, Type, Any value, Any value). Type can be “length, “copy” and “stats”. Does not require Queue number or Position. To learn the usage, click on getBlockStatus example.
The SystemResource can operate in a Hierarchical mode. In this mode, the incoming Data Structure/request is sent to another SystemResource/SystemResource_Extend for processing. The current Scheduler queues the transaction and when it arrives at the head of it's queue, it sends over to the top-level Scheduler. On return from the other Scheduler, the transaction is sent to the origination block or line of code. If the "Parent_Scheduler_Name" is "none", then the task will execute on this scheduler.
To understand the Hierarchical usage, view the Hierarchical Example in the BDE.
o In the First-Come First_Serve (FCFS) mode, the Data Structure is processed when it reaches the head of the Queue. Look at the FCFS Example.
o In the Round-Robin mode, the Data Structures in the Queue are each allocated a finite piece (Round-Robin Time Slice) of the processing time in each round. The RR scheduler will reduce a remaining task time by the time slice till completed. RR schedulers do not use priority. Look at the Round-Robin Example.
o In FCFS with Preemption, the execution operates similar to the FCFS mode. When a higher priority transaction arrives and the currently executing task has a lower priority, the processing task is placed in a preempt queue. The higher priority starts executing after a context switching time. When the higher priority task completes, the task with the next priority starts processing. The preempted task only processes the time remaining on resumption. The total latency increases but the processing time does not change. Task execution is influenced by the "Task_Priority" field. Look at the FCFS with Preemption Example.
Notice the plot to see how the lower priority task (Y-axis is the task number that is same as the Task Priority in this case) gets preempted and completes after all the higher priority tasks have completed. Also, notice that a preempted tasks itself gets preempted.
o User-defined: users can add additional algorithms to the block.
o If a SystemResource Block is driving a parent SystemResource_Extend Block, the lower or child scheduler type must be FCFS. There are no restrictions for tasks executing at the top-level SystemResource Block.
The latency across the SystemResource consists of the queuing time plus the processing time. The processing time is done in two ways:
o Time_Type = 'Relative Time'. In this case the Task_Time in the Mapper blocks is the delay time at the Server. The Clock_Rate_Mhz of the SystemResource is ignored. In this mode, the Scheduler does not synchronize to clock boundaries. This is the primary difference with the 'Number_Clocks' option.
o Time_Type = Number Clocks. In this case, the Task_Time in the Mapper blocks is the number of cycles. The Clock_Rate_Mhz of the SystemResource is used. The Delay Time = Task_Time / Clock_Rate_Mhz. If the Data Structure arrives out of sync with the clock, the processing will not start until the next clock cycle.
Note: If a Mapper Block sends Task_Time '10.0' anticipating "Number Clocks" block menu attribute, and the block menu attribute is "Relative Time", then one may not see any output till '10.0' seconds!
The Context_Switching time occurs every time there is a new Data Structure processed or a Data Structure is preempted or a preemption Data Structure returns. The "Task_Context_Switch_Time" is used only at the end of a RR task and not applied to each RR time slice.
Features |
SystemResource |
SystemResource_Extend |
Preemption |
Yes |
No |
Hierarchical |
Yes |
No |
Extended Task Processing |
No |
Yes |
Non-Blocking | No | Yes |
o The Preemption will only work if there is one SystemResource or, in the case of a Hierarchical SystemResource, there is the current Resource and one parent Resource.
o If you do not want a certain set or types of Data Structures to be preempted, you can send those through a Software_Mapper with Task_Mutex set to TRUE.
This is typical in a Real Time Operating System (RTOS). The SystemResource responds to the Task_Mutex field set to true, by excluding this task from preemption, irrespective of the priority. Until a mutex task starts it is treated like other tasks. View the Mutual Exclusion Example in the BDE. Compare this model with the FCFS with Preemption model and notice the Plotting for the Task Number = 1. When Mutual Exclusion is turned on, Task 1 is never preempted.
The Scheduler blocks provide individual task timing and scheduler statistics. The user can enable this feature by checking the parameter called Add_Scheduler_Times_to_DS. This enables individual statistics to be added to each data structure processed by the Scheduler. The added fields include TIME_ACTIVATION, TIME_FINISH, TIME_IN_SCHEDULER, TIME_RELEASE and TIME_RESPONSE.
TIME_RELEASE = TIME_ACTIVATION
If the scheduler is busy, and enqueued, and the Scheduler_Type is FCFS then:
TIME_RELEASE > TIME_ACTIVATION.
TIME_RELEASE > TIME_ACTIVATION.
TIME_RESPONSE = TIME_FINISH - TIME_RELEASE
If the TIME_ACTIVATION and TIME_RELEASE timestamps are the same, then the TIME_IN_SCHEDULER and TIME_RESPONSE scheduler statistics are the same for the scheduler. To learn more about the individual statistics in a model, view the Individual Statistics Example in the BDE.
If multiple SystemResource exist in the model and Add_Scheduler_Times_to_DS is checked in multiple SystemResource, then the user must process the added scheduler statistics after each SystemResource completes execution by transferring the values to other data structure fields, or to unique model memories.
The statistics for the SystemResource
are generated using the getBlockStatus RegEx function. This
function can be called in either the Virtual_Machine, Processing or
Decision blocks.
Statistics_A = getBlockStatus(Scheduler_Name,"Any Value","stats",1,"Any
Value") -> To get the statistics
Reset_Stats_A = getBlockStatus(Scheduler_Name,"Any
Value","stats",-1,"Any Value") -> To reset the statistics
There is a pre-created statistics Generator block called
Results/Resource_Statistics/Resource_Statistics_Report and is used in
the Resource_Statistics Example in the BDE.
View the SystemResource Example in the BDE to see the plotting.
Statistics Output
Statistic Name |
Value |
Explanation |
Mathematical Equation |
Type |
Number_Entered |
100 |
Number of transactions entering the queue. |
- |
int |
Number_Exited |
25 |
Number of transactions that left the queue. |
- |
int |
Number_Rejected |
10 |
Number of transactions rejected and output to reject port. |
- |
int |
Queue_Number |
1 |
Queue Number. Value will be 1. |
- |
int |
Occupancy_Min |
4.0 |
Minimum queue size during the simulation. |
If (Xn < Xn – 1) Xmin = Xn |
double |
Occupancy_Mean |
8.0 |
Mean/Average queue size during the simulation |
Xµ = (1 / n) * (X1 + X2 + … + Xn) |
double |
Occupancy_StDev |
3.0 |
Standard Deviation from the Mean queue size during the simulation. |
Xσ = Math.sqrt ((1 / n) * ((X1 - Xµ)2 + (X2 - Xµ)2 + … + (Xn - Xµ)2)) |
double |
Occupancy_Max |
25.0 |
Maximum queue size consumed during the simulation. |
If (Xn > Xn – 1) Xmax = Xn |
double |
Total_Delay_Min |
1.3 |
In seconds. Least time through the queue+server among all transactions. |
If (Xn < Xn – 1) Xmin = Xn |
double |
Total_Delay_Mean |
1.3 |
In seconds. Mean/Average time through the queue+server among all transactions. |
Xµ = (1 / n) * (X1 + X2 + … + Xn) |
double |
Total_Delay_StDev |
1.3 |
In seconds. Standard Deviation from the Mean time through the queue+server among all transactions. |
Xσ = Math.sqrt ((1 / n) * ((X1 - Xµ)2 + (X2 - Xµ)2 + … + (Xn - Xµ)2)) |
double |
Total_Delay_Max |
1.3 |
In seconds. Maximum time through the queue+server among all transactions. |
If (Xn > Xn – 1) Xmax = Xn |
double |
Utilization_Pct_Mean |
0.0 |
Mean/Average utilization of the server portion only. Queue utilization not considered. Unit is in percentage. |
Xµ = (1 / n) * (X1 + X2 + … + Xn) |
double |
Where n is the number of samples and X is occupancy or delay.
Parameter |
Explanation |
Type |
Example |
Resource_Name |
This is the name of this SystemResource block and is used by Mappers, RegEx and other SystemResource block to call this block to execute a transaction. |
String |
"CPU" |
Next_Resource |
The Next_Resource is the name of the next hierarchical SystemResource, which can be SystemResource or SystemResource_Extend block. |
String |
"Core_CPU" or "None" |
Task_Context_Switch_Time |
Task_Context_Switch_Time, used to model the time to switch between one scheduler task and another task. If the value is grater than 0.0 it is added to all new tasks starting, and before and after a task is pre-empted. In RR mode, applied after task completes. If hiearchical schedulers, then lower level scheduler determines context switch time. |
Double |
0.0 |
Round_Robin_Time_Slice |
Quantum time to switch between tasks in Round Robin Scheduler, meaning the time the scheduler will devote to each task incrementally till it completes in the scheduler. |
Double |
1.0E-3 |
Clock_Rate_Mhz |
The Clock_Rate_Mhz is used to model delays, only if the Time_Type is 'Number_Clocks'. Scheduler delay is 'Task_Time' / (Clock_Rate_Mhz * 1000000.0). This value is ignored if the Time_Type is set to "Relative_Time |
Double |
500.0 |
Max_Scheduler_Length |
The Max_Scheduler_Length is the number of outstanding transaction in the queues. If the scheduler length is exceeded, then an exception will be generated. |
Integer |
30 |
Time_Type |
'Relative_Time'
mode can be used for estimating prototype tasks, or partially defined
software tasks. In this case the Task_Time in the Mapper blocks is
the delay time at the Server. The Clock_Rate_Mhz of the
Scheduler_SW is ignored. |
Double |
Relative Time |
Scheduler_Type |
'Scheduler_FCFS' is a First-Come-First-Serve scheduler that supports priority. |
- |
Scheduler_FCFS |
Add_Scheduler_Times_to_DS |
Add_Scheduler_Times_to_DS turns on or off the request for individual statistics. |
- |
- |
Port |
Explanation |
task_output |
This port receives the data structure that was sent from Mapper block once the processing has been completed either at this block or at a Parent SystemResource. This provides a copy and does not affect the timing or the return to the calling Mapper or line of code. |
task_plot |
Output port sending SystemResource task plot information. This is connected to a TimedDataPlotter block. This plot shows the activity of each task on this scheduler. The peak of each rectangle corressponds to the Task_Number of that task. The distance along the X-axis indicates the total latency for the task. For preempted task, you will see a higher priority item in the middle of the rectangle indicating that the task is currently waiting in the preempt queue. |