Main Content

Optimize Memory Usage for Time Counters

This example shows how to optimize the amount of memory that the code generator allocates for time counters. The example optimizes the memory that stores elapsed time, the interval of time between two events.

代码生成器代表时间计数器”igned integers. The word size of time counters is based on the setting of the model configuration parameterApplication lifespan (days), which specifies the expected maximum duration of time the application runs. You can use this parameter to prevent time counter overflows. The default size is 64 bits.

The number of bits that a time counter uses depends on the setting of theApplication lifespan (days)parameter. For example, if a time counter increments at a rate of 1 kHz, to avoid an overflow, the counter has the following number of bits:

  • Lifespan < 0.25 sec: 8 bits

  • Lifespan < 1 min: 16 bits

  • Lifespan < 49 days: 32 bits

  • Lifespan > 50 days: 64 bits

A 64-bit time counter does not overflow for 590 million years.

Open Example Model

Open the example modelrtwdemo_abstime.

The model consists of three subsystemsSS1,SS2, andSS3. On theMath and Data Typespane, theApplication lifespan (days)parameter is set to the default, which isinf.

The three subsystems contain a discrete-time integrator that requires elapsed time as input to compute its output value. The subsystems vary as follows:

  • SS1 - Clocked at 1 kHz. Does not require a time counter.Sample time typeparameter for trigger port is set toperiodic. Elapsed time is inlined as 0.001.

  • SS2 - Clocked at 100 Hz. Requires a time counter. Based on a lifespan of 1 day, a 32-bit counter stores the elapsed time.

  • SS3 - Clocked at 0.5 Hz. Requires a time counter. Based on a lifespan of 1 day, a 16-bit counter stores the elapsed time.

Simulate the Model

Simulate the model. By default, the model is configured to show sample times in different colors. Discrete sample times for the three subsystems appear red, green, and blue. Triggered subsystems are blue-green.

Generate Code and Report

1. Create a temporary folder for the build and inspection process.

2. Configure the model for the code generator to use the GRT system target file and a lifespan ofinfdays.

3. Build the model.

# # #开始构建过程:rtwdemo_abstime### Successful completion of build procedure for: rtwdemo_abstime Build Summary Top model targets built: Model Action Rebuild Reason ================================================================================================ rtwdemo_abstime Code generated and compiled Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 18.569s

Review Generated Code

Open the generated source filertwdemo_abstime.c.

struct tag_RTM_rtwdemo_abstime_T { const char_T *errorStatus; /* * Timing: * The following substructure contains information regarding * the timing information for the model. */ struct { uint32_T clockTick1; uint32_T clockTickH1; uint32_T clockTick2; uint32_T clockTickH2; struct { uint16_T TID[3]; uint16_T cLimit[3]; } TaskCounters; } Timing; }; /* Block states (default storage) */ extern DW_rtwdemo_abstime_T rtwdemo_abstime_DW; /* Zero-crossing (trigger) state */ extern PrevZCX_rtwdemo_abstime_T rtwdemo_abstime_PrevZCX; /* External inputs (root inport signals with default storage) */ extern ExtU_rtwdemo_abstime_T rtwdemo_abstime_U; /* External outputs (root outports fed by signals with default storage) */ extern ExtY_rtwdemo_abstime_T rtwdemo_abstime_Y; /* Model entry point functions */ extern void rtwdemo_abstime_initialize(void); extern void rtwdemo_abstime_step0(void); extern void rtwdemo_abstime_step1(void); extern void rtwdemo_abstime_step2(void); extern void rtwdemo_abstime_terminate(void); /* Real-time Model object */ extern RT_MODEL_rtwdemo_abstime_T *const rtwdemo_abstime_M; /*- * The generated code includes comments that allow you to trace directly * back to the appropriate location in the model. The basic format * is /block_name, where system is the system number (uniquely * assigned by Simulink) and block_name is the name of the block. * * Use the MATLAB hilite_system command to trace the generated code back * to the model. For example, * * hilite_system('') - opens system 3 * hilite_system('/Kp') - opens and selects block Kp which resides in S3 * * Here is the system hierarchy for this model * * '' : 'rtwdemo_abstime' * '' : 'rtwdemo_abstime/SS1' * '' : 'rtwdemo_abstime/SS2' * '' : 'rtwdemo_abstime/SS3' */ #endif /* RTW_HEADER_rtwdemo_abstime_h_ */

Four 32-bit unsigned integers,clockTick1,clockTickH1,clockTick2, andclockTickH2are counters for storing the elapsed time of subsystemsSS2andSS3.

Enable Optimization and Regenerate Code

1. Reconfigure the model to set the lifespan to 1 day.

2. Build the model.

# # #开始构建过程:rtwdemo_abstime### Successful completion of build procedure for: rtwdemo_abstime Build Summary Top model targets built: Model Action Rebuild Reason ================================================================================= rtwdemo_abstime Code generated and compiled Incremental checksum has changed. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 12.249s

Review the Regenerated Code

struct tag_RTM_rtwdemo_abstime_T { const char_T *errorStatus; /* * Timing: * The following substructure contains information regarding * the timing information for the model. */ struct { uint32_T clockTick1; uint16_T clockTick2; struct { uint16_T TID[3]; uint16_T cLimit[3]; } TaskCounters; } Timing; }; /* Block states (default storage) */ extern DW_rtwdemo_abstime_T rtwdemo_abstime_DW; /* Zero-crossing (trigger) state */ extern PrevZCX_rtwdemo_abstime_T rtwdemo_abstime_PrevZCX; /* External inputs (root inport signals with default storage) */ extern ExtU_rtwdemo_abstime_T rtwdemo_abstime_U; /* External outputs (root outports fed by signals with default storage) */ extern ExtY_rtwdemo_abstime_T rtwdemo_abstime_Y; /* Model entry point functions */ extern void rtwdemo_abstime_initialize(void); extern void rtwdemo_abstime_step0(void); extern void rtwdemo_abstime_step1(void); extern void rtwdemo_abstime_step2(void); extern void rtwdemo_abstime_terminate(void); /* Real-time Model object */ extern RT_MODEL_rtwdemo_abstime_T *const rtwdemo_abstime_M; /*- * The generated code includes comments that allow you to trace directly * back to the appropriate location in the model. The basic format * is /block_name, where system is the system number (uniquely * assigned by Simulink) and block_name is the name of the block. * * Use the MATLAB hilite_system command to trace the generated code back * to the model. For example, * * hilite_system('') - opens system 3 * hilite_system('/Kp') - opens and selects block Kp which resides in S3 * * Here is the system hierarchy for this model * * '' : 'rtwdemo_abstime' * '' : 'rtwdemo_abstime/SS1' * '' : 'rtwdemo_abstime/SS2' * '' : 'rtwdemo_abstime/SS3' */ #endif /* RTW_HEADER_rtwdemo_abstime_h_ */

The new setting for theApplication lifespan (days)parameter instructs the code generator to set aside less memory for the time counters. The regenerated code includes:

  • 32-bit unsigned integer,clockTick1,存储时间f the task forSS2

  • 16-bit unsigned integer,clockTick2,存储时间f the task forSS3

Related Information

Related Topics