Code and data size for an RTOS-based design

xxx.png

May 13, 2022

This article is written by Jean J. Labrosse, RTOS Expert.

An RTOS consists of a set of services (i.e., functions) that a developer adds to an application. These services will consume both flash (code space) and RAM (data space).

The actual amount of code space consumed by an RTOS is typically quite small, ranging from about 8 kilobytes to 24 kilobytes. Code space is generally not a concern with today’s 32-bit CPUs and MCUs. On the other hand, an RTOS-based application requires more RAM than a single threaded application (i.e., super loop).

An RTOS uses internal variables to perform its duties. Typically, this ranges from a few hundred bytes up to a kilobyte or so. Each task in an application also requires a data structure called a task control block (TCB) which the RTOS uses to keep track of the status of each task. A TCB can consume between 50-250 bytes of RAM depending on the features provided by the RTOS as well as whether the RTOS collects statistics about each task.

Probably the biggest consumer of RAM in an RTOS-based application is the stack that each task requires, the size of which depends on the task’s requirements (e.g., function call nesting, arguments passed to functions, local variables, etc.). To avoid stack overflows, a developer needs to over-allocate stack space, yet not too much, to avoid wasting RAM. The linker in IAR Embedded Workbench has a neat feature which helps determine how much stack space each task will require. You simply check boxes in the linker’s configuration (see screen shots below), build your code and examine the link map (.MAP file) to reveal the call stack depth (in bytes) for each function in your application. You then simply note the task’s call stack size for each of your tasks since we still have to add one or two values to that number.

1_RTOS_article_image1.png


The stack for each task needs to hold enough storage to hold the context of the task. This consist of a copy of the CPU registers as well as the FPU registers (if an FPU is used). The simple equation below is used to determine the total task stack size (in bytes) needed for a given task and, adds an extra 33% to provide some wiggle room. In fact, in most embedded applications, it’s desirable to keep run-time stack usage below the 70% mark. You can certainly be more conservative as needed by your requirements.

 

As an example, let’s assume one of the task’s call stack in your application is 256 bytes deep and you are using a Cortex-M4.  In this case, the CPU context is 64 bytes while the FPU is another 132 bytes. The above equation would thus yield the follow task stack size:

 

 

You can confirm the actual stack usage at run-time by examining the kernel awareness feature provided in the C-SPY Debugger in IAR Embedded Workbench as shown below for a uC/OS-III based application.

 

1_RTOS_article_image2.png

 

Finally, tasks can either share resources or communicate with each other, and do so through other data structures that are called RTOS objects. RTOS objects are small, and typically consume between 16 to 64 bytes each.

 

 

For example, let’s assume an application has 10 tasks, and on average a task stack is 1 kilobyte (typical task need less). If the application also has 5 RTOS objects (each 64 bytes), then this is the calculation for the required RAM:

 

or:

 

However, this RAM is solely for the RTOS’ needs; it does not include the amount of RAM needed for the application code itself. 

About the author

This article is part of a series on the topic of developing applications with RTOS.

Jean Labrosse, Micrium Founder and author of the widely popular uC/OS-II and uC/OS-III kernels, remains actively involved in the evolution of the uC/ line of embedded software.

Given his wealth of experience and deep understanding of the embedded systems market, Jean serves as a principal advisor and consultant to Weston Embedded Solutions helping to chart the future direction of the current RTOS offerings. Weston Embedded Solutions specializes in the support and development of the highly reliable Cesium RTOS family of products that are derived from the Micrium codebase. 

You can contact him at Jean.Labrosse@Weston-Embedded.com.

jean_labrosse.jpg

죄송하지만, 이 콘텐츠는 한국어로 제공되지 않습니다.

당사 웹 사이트 중 한국어가 제공되는 않는 페이지는 기본적으로 영어로 제공됩니다. 보다 정확한 정보를 위해 영어로 제공되는 글로벌 웹 사이트를 방문하실 것을 권장합니다.

죄송하지만, 당사 사이트에서는 Internet Explorer를 지원하지 않습니다.보다 편안한 사이트를 위해 Chrome, Edge, Firefox 등과 같은 최신 브라우저를 사용해 주시길 부탁드립니다.