Main Content

Optimize Memory Usage and Prevent Overflows for Time Counters

You can optimize the amount of memory that the code generator allocates for time counters and prevent time counter overflows. The code generator stores time counters associated with entry-point functions as unsigned integers that have a word size of 8, 16, 32, or 64 bits. The code generator determines the word size by computing the largest number of clock ticks needed by the application target environment, which involves dividing the product of the model application lifespan and 86,400, the number of seconds in one day, by the clock resolution.

Largest number of ticks = ((Application lifespan x 86400)/Clock resolution)

  • Application lifespan is the value specified by model configuration parameter Application lifespan (days). The parameter specifies how long, in days, an application that contains blocks or Stateflow® charts that depend on absolute or elapsed time can execute before a timer overflow occurs. If you set the parameter to inf, the code generator uses a word size of 64 bits, which meets the 64-bit integer data type requirement for continuous execution (since R2024a).

  • Clock resolution is the value specified by model configuration parameter Clock resolution (seconds, -1 inherited). The parameter specifies the smallest increment of a clock value in seconds. For example, if a clock increments its value once per second, the clock resolution is 1 second. When the parameter is set to the default setting of -1 (inherit), the code generator initializes clock resolutions based on scheduling properties for the model style and type of an entry-point function. For example, the code generator sets the clock resolution for an aperiodic function in an export-function model to the model fixed-step size (fundamental sample time). For periodic functions, the clock resolution is set to the function sample time.

A time counter overflow occurs when a time counter reaches its maximum value and wraps around to its minimum value. This type of event is commonly encountered in systems that use fixed-size counters to measure time or keep track of elapsed time. The occurrence of a time counter overflow depends on the word size of the counter and the frequency at which it increments. For example, if a 16-bit counter increments at a frequency of 1 kHz, it will overflow every 65.536 seconds (216/1000). When the counter reaches its maximum value of 65,535, it resets to zero, and the overflow event is triggered.

To control the word size of generated time counters to optimize memory usage or to prevent time counter overflows, adjust the setting of the Application lifespan (days) parameter and, if relevant, the Clock resolution (seconds, -1 inherited) parameter, so that the largest number of ticks is in the range of maximum clock ticks for the word size required by your application target environment. For instance, to minimize the RAM used for time counters for an aperiodic entry-point function, specify the smallest application lifespan possible and the largest fixed-step size possible.

For details on how the code generator uses the Application lifespan (days) and Clock resolution (seconds, -1 inherited) parameter settings and for a list of the ranges of maximum clock ticks and how they map to generated time counter data types, see Timer Data Types and Memory Allocation.

See Also

|

Related Topics