Understand the Priority-Based Scheduling Model (Real-Time Module)

This topic explains how the LabVIEW Real-Time Module handles priorities and execution systems when creating and scheduling threads. By understanding the priority-based scheduling model, you can leverage the strengths of the LabVIEW Real-Time Module to create deterministic applications.

Threads, Priorities, and Execution Systems

LabVIEW is a structured dataflow language that abstracts many of the underlying complexities of computer operation to create an intuitive graphical programming environment. To understand how LabVIEW schedules parallel tasks based on priority, it helps to define some key concepts that underlie the scheduling process in LabVIEW:

  • Thread—The basic unit of concurrency in the LabVIEW Real-Time Module. Operations within a thread execute sequentially, while each thread executes independently.
  • Scheduler—An algorithm the operating system uses to schedule threads. The scheduler determines which thread to run on each CPU in the system at any given time.
  • Run Queue—The queue of threads ready to be scheduled.
  • Preemptive Scheduling—The process whereby the scheduler interrupts a lower-priority thread as soon as a higher-priority thread is ready to run.
  • Round-Robin Scheduling—The process whereby the scheduler switches periodically between threads of equal priority to promote equal sharing of processor time.
  • Execution System—A pool of related threads. Use the Execution page of the VI Properties dialog box to configure the execution system of a VI.
  • VI Priority—The priority you assign to a VI on the Execution page of the VI Properties dialog box.
  • Timed Loop Priority—The priority you assign to a Timed Loop using either the Priority input or the Configure Timed Loop dialog box.
  • OS Priority—The priority assigned to a thread by the real-time operating system. LabVIEW assigns each thread an OS priority based on the priority of the containing VI or Timed Loop.
  • Time-Critical—The highest priority available for a LabVIEW VI. Time-critical threads run above the priority of the scheduler itself. Therefore, time-critical threads always run to completion or until blocked.
  • Priority Inheritance—The process whereby a thread temporarily assumes the priority of the calling thread. Priority inheritance helps to prevent priority inversions.
  • Jitter—A measure of the extent to which execution timing fails to meet deterministic expectations.

The following illustration summarizes the priorities and execution systems available in LabVIEW.

The preceding illustration shows the following facts:

  • Execution systems are independent of priority levels. LabVIEW uses execution systems only as thread pools and does not prioritize threads based on execution system. For each execution system, LabVIEW creates up to four threads at each priority level. This rule extends to each core in a multi-core system, so on a dual-core system, LabVIEW could create up to eight threads at each priority level for each execution system. Because the number of threads per execution system is limited, balancing your execution system assignments can be important. If you assign too many VIs to the same execution system, the VIs must share threads, limiting the potential parallelism of the VIs.
  • Timed structure priorities fall between the high and time-critical VI priority levels.
  • When you place parallel code inside a single Timed Structure, LabVIEW serializes the code because each timed structure comprises one and only one thread.
  • LabVIEW Real-Time Module systems include many system threads running in parallel to the threads that comprise your LabVIEW VIs. These system threads execute at various priorities. For example, the NI Scan Engine thread, when present, runs by default at a priority above time-critical.

Priority Inheritance

LabVIEW implements priority inheritance to prevent unintended priority reductions. If a subVI is configured with a lower priority than that of the calling VI and both run within the same execution system, the subVI inherits the priority of the calling VI. If a subVI is configured with a priority higher than that of the calling VI, the subVI maintains its configured priority.

Note  If a subVI runs in a different execution system than the calling VI, priority inheritance does not occur.

The real-time operating system (RTOS) also implements priority inheritance. When a lower-priority thread holds a shared resource needed by a higher-priority thread, the RTOS temporarily increases the priority of the thread holding the shared resource so that the resource can return promptly to the higher-priority thread.

Time-Critical Priorities

There are two types of time-critical threads:

  • VIs set to time-critical priority—VIs set to time-critical priority run above the priority of the scheduler itself. Therefore, VIs set to time-critical priority always run to completion or until blocked, typically by the inclusion of a timing function or a function that waits for an external event.
  • Timed Structures—A Timed Structure can be interrupted by another Timed Structure of higher priority or by a VI set to time-critical priority.
Note  National Instruments recommends creating no more than one time-critical task per CPU.

Timed Structure Priorities

LabVIEW uses two separate but related priority schemes: VI priorities and timed structure priorities. Timed structure priorities are numeric, and a higher value represents a higher priority relative to other timed structures executing on the target. However, all timed structure priorities fall between the high and time-critical VI priority levels.

Note  (Real-Time Linux) Do not use more than 32 timed structure priority levels in a VI. LabVIEW returns an error if you attempt to set more than 32 timed structure priority levels.

Avoiding Errors When Using Priority Schemes

National Instruments recommends using only one priority scheme in your application, as shown in the following examples:

  • If the application uses timed structures, keep all VIs at normal priority.
  • If the application uses VIs set to time-critical priority, do not place timed structures inside the VIs set to time-critical priority.

Using a single priority scheme makes the application easier to understand and less error prone.

Scheduling

Scheduling is the process of determining which task to run at a given time. Scheduling is a key task for any modern operating system, but especially for a real-time operating system (RTOS). In addition to threads spawned by your VIs, an RT target runs numerous OS and driver threads at various priorities. To ensure that your tasks execute according to your timing requirements, it helps to understand how LabVIEW Real-Time schedules threads.

Basic RT Scheduling Rules

The following rules summarize the operation of the LabVIEW Real-Time scheduler when a new thread enters the run queue:

  1. Perform preemptive scheduling among threads of different priority.
  2. Perform round-robin scheduling among threads of equal priority.
  3. Time-critical threads always run to completion.

Understanding How LabVIEW Real-Time Module Scheduling Works

At any given time, each thread in the system is either running or blocked:

  • Running—The thread is in the run queue, which means it is either ready to execute or is currently executing.
  • Blocked—The thread cannot execute until some event occurs, such as the completion of an I/O operation or a timing function.

When a thread begins executing, it runs until one of the following conditions arises:

  • The thread becomes blocked by a Wait VI within a While Loop, the built-in timing mechanism of a Timed Loop, or a blocking function such as the Read Variable with Timeout function.
  • The thread is interrupted by a higher-priority thread.
  • The thread finishes executing.

When a thread becomes unblocked, it enters the run queue. The run queue is always sorted in priority order, so when a thread enters this queue, it immediately moves ahead of all lower-priority threads. If a thread entering the run queue is higher-priority than a currently-running thread, the higher-priority thread either runs on a different CPU core or interrupts the currently-running thread in a process called preemptive scheduling.

Preemptive Scheduling

Preemptive scheduling is the process of ordering thread execution based on the relative priorities of the threads. When a thread of higher priority than the current thread enters the run queue, the scheduler interrupts the current thread if necessary so that the higher-priority thread can execute immediately. The interrupted thread then returns to the run queue behind the higher-priority thread.

Round Robin Scheduling

Round robin scheduling is the process of alternating between threads such that each thread receives roughly equivalent CPU time. During round robin scheduling, the scheduler interrupts and switches between the threads at regular intervals, keeping track of the CPU time given to each thread.

The LabVIEW Real-Time Module performs round robin scheduling among threads of equal priority. However, the LabVIEW Real-Time Module does not perform round-robin scheduling among VIs set to time-critical priority.

Setting Loop Priorities Based on Timing Requirements

Although it seems intuitive to set the priority of each loop based on the perceived importance of the loop, this strategy can cause jitter. Set the priority of each loop not based on how critical the task is within the application but rather based on how important it is that the task fulfill a certain timing guarantee. For example, in a data logging application with a data acquisition loop and a logging loop, you might be tempted to make the logging loop the higher-priority loop. However, data acquisition must occur at regular intervals to avoid missed data points. However, it does not matter when you log each data point to disk, as long as you log each point eventually. In this case, you should assign a higher priority to the data acquisition loop, even though data logging is the primary purpose of the application.



Timed Structures Home

WAS THIS ARTICLE HELPFUL?

Not Helpful