How to set a flag while the function-call subsystem is executing?
Show older comments
I’m working with a multi‑rate system where task2 can be preempted by task1, and I need to measure the execution time of both tasks accurately.
Since task1 may interrupt task2, I want to subtract the time task1 executes during those interruptions from task2’s total execution time. My idea is to have task2 raise a flag whenever it is running, so task1 can detect the preemption and record how long it ran during that period.
How can I keep a signal high for the entire duration of task2’s execution? Both tasks are implemented inside function‑call subsystems.
Thanks!
Answers (1)
Adarsh
26 minutes ago
0 votes
Hi Valeriy,
As per my understanding, you are working with a Multi rate model where Task 2 can be preempted by Task 1 and your goal is to measure the execution time for both the tasks accurately by subtracting the time Task 1 runs while interrupting Task 2.
A simple and reliable way to implement this is to use two small MATLAB Function blocks inside the Task 2 function-call subsystem along with a shared Data Store Memory variable that acts as a global flag.
You can start by creating a Data Store Memory block at a level visible to both tasks. For example, name it "task2_running", set its data type to a Boolean or "uint8", and initialize it to zero. This block represents the global variable that both tasks will access.
Inside the Task 2 function-call subsystem, add two MATLAB Function blocks. One block should be placed at the logical start of the subsystem and the other at the logical end. To enforce this order clearly, you can set the execution priority of the first MATLAB Function block to "First" and the second one to "Last" using the Block Properties dialog. This makes the execution order explicit and avoids ambiguity.
Register the "task2_running" datastore memory variable in the symbols pane for MATLAB Function block with its scope set to Data Store Memory. In the code for MATLAB Function block, set this variable as global and use this variable as a flag, where the first MATLAB Function block will set it to "1" and the other block sets it to "0" after the execution.
Because both MATLAB Function blocks reference the same Data Store Memory, the flag remains asserted for the entire duration of Task 2, even if Task 1 preempts it.
Inside the Task 1 function-call subsystem, you can register the same Data Store Memory variable "task2_running" in the Symbols pane of the MATLAB Function block that performs timing measurements. By reading its value at the beginning and end of Task 1 execution, Task 1 can detect whether it interrupted Task 2 and accumulate its execution time accordingly.
This approach avoids relying on signal wiring for execution timing and works well with multi-rate and multitasking configurations.
For more information, you can refer to the following links:
- https://www.mathworks.com/help/releases/R2024b/simulink/slref/datastorememory.html
- https://www.mathworks.com/help/releases/R2024b/simulink/slref/matlabfunction.html
Hope this helps!
Categories
Find more on Schedule Model Components in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!