MATLAB: Do I see high task execution times in the beginning of execution of the model in xPC Target

cacheperformancerealreal timeSimulink Real-Timetettime

I am using a model that contains a State-Space block, with the following state-space parameters:
A = diag(.005:.005:.5)+.001;
B = ones(100,8);
C = ones(8,100);
D = .001*ones(8,8);
I am generating real-time code using xPC Target, downloading and running the application on the target machine.
Sometimes when I am starting my model, it causes a CPU overload error.
Once the model has started successfully, the computational load is easily handled by the CPU. In effect, I am only using about half of the cycle-time.
It seems like the task execution time (TET) is very high in the beginning of execution of my real-time application.

Best Answer

High task execution time (TET) in the beginning of the model's execution could be related to a caching issue.
For instance, if your model has large amount of states, and you access those states in your real-time code, the first time you reference those states in your code, those states would need to be cached, so the execution time will increase.
The next time you access those states, they will probably be in your system's cache, so the execution time will be much lower.
If you do not want the caching mechanism to influence your real-time execution, you need to reference those states before the real-time execution begins, for instance, in the MdlStart function.
The idea is that the states will get cached outside of the real-time execution constraint.
Related Question