MATLAB: What is the meaning of the task overrun detected output

Embedded Codersimulinksimulink coder

After code-generating my Simulink model to a deployed executable and running it on in a Linux environment, I receive the following messages printed out over stdout:
"Overrun detected: The sample time for the rate 0 is too short."
My model is a multi-rate model and each rate group is as follows:
  • Discrete 1: 0.0005 (2 kHz)
  • Discrete 2: 0.004 (250 Hz)
  • Discrete 3: 0.032 (~31 Hz)
  • Discrete 4: 0.128
  • Discrete 5: 0.512
  • Discrete 6: 3.072
My question is two-fold:
  1. Does "rate 0" (from the error message) refer to the base rate of the model?
  2. Why can't this output be more specific and tell me what part of the model is executing when the overrun occurs?
My question is not about how to fix task overruns (just the above), so please refrain from linking me to posts/articles that tell me to increase my sample time, etc.
Thanks, Tyler

Best Answer

Overruns do not necessarily correspond to a particular rate group. Overruns happen when the combination of all of the functions to be executed at a particular time takes longer than the time until the next step. Your minimum step is 0.0005, so at some point the combination of tasks is taking more than 0.0005 s.
t = [0.0005, 0.004, 0.032, 0.12, 0.512, 3.072];
t./min(t)
ans = 1 8 64 240 1024 6144
so the clock will fire every 0.0005 s, and each time it will run everything in the first group; every 8 ticks it will also run everything in the second group; every 64 ticks it will run everything in the first group together with everything in the second group together with everything in the third group (the counts for those all factor 64); every 240 ticks it would run the first group together with the second group together with the fourth group (all factor into 240), and so on. At
fold(@lcm, t./min(t))
ans = 30720
every 30720 ticks it would need to run all 8 groups and finish that before tick 30721, 0.0005 seconds later.
The question is not whether there is one group that runs too slow: the question is whether there is a combination of groups that cannot finish before the next tick... and since all of the blocks will eventually run in a single tick, at lcm() of their times, the question becomes whether all of the blocks together can finish in a single tick.
Certainly if you have a single group that is much more complex than the others then you should address it first, but everything counts.
You can sometimes postpone the effect of running multiple groups by carefully arranging the timings to be relatively prime. For example if the second group ran at every 9 ticks instead of every 8, then instead of groups 1, 2, and 3 all executing every 64 ticks, it would be postponed to every 576 ticks. But no matter what you do, if the simulation runs long enough, the lcm() of all of the timings will arrive and require that all of the groups run in a single tick.