What is the Minimal Footprint of uClinux? Print

 

This is perhaps one of the most frequently asked questions about uClinux on Cortex-M.

First thing to say is that external RAM is a must for uClinux. However small a Linux configuration, it still requires at least several MBytes of RAM to run from. All Cortex-M devices Emcraft is aware of limit they internal SRAM to hundreds KBytes at best. There is no way uClinux can be run from a single-chip Cortex-M design, as of this writing.

A two-chip design (Cortex-M + external RAM) is a possibility, however. There are actually two separate "footprint" metrics that need to be considered:

Size of the bootable Linux image. Given the two-chip design goal (i.e. no external device to mount the root file system from), the root file system is embedded in the bootable image as initramfs. More specifically, the bootable image is comprised of two major pieces:

  • Linux kernel itself;
  • cpio representation of the root file system, which gets expanded into a RAM-based initramfs and mounted as rootfs when Linux is booting up.

The size of such a bootable image starts at maybe 0.5 MBytes for truly minimal configurations and ranges to whatever, depending on what you have put into your root file system and, to a lesser extent, what configuration options you have enabled in your kernel. For instance, enabling the TCP/IP stack in the kernel increases the image size somewhat substantially.

The size of a practical bootable image, with Ethernet, TCP/IP and a reasonable set of user-space tools and applications confugured, would be in a 1.5 - 2 MBytes ballpark. With the "two-chips Linux design" concept in mind, a 1.5 MBytes image could possibly fit into internal Flash of today's Cortex-M microcontrollers. One example of a device that can hold an image of the size is the STM32F746 Cortex-M4 microcontroller.

Size of external RAM required for run-time Linux operation. The answer we give to our customers when asked how much RAM is needed is the more the better, but no less than 8 MBytes. Admittedly, it may be possible to run some very basic configurations with rootfs mounted from NFS or some external device even out of 2 MBytes but frankly this is more of a joke than a configuration one can build a practical uClinux design on.

As the rule of thumb, consider at least 32 MB RAM if your intention is to use uClinux in a serious product. When you approach deployment and it becomes clear that you can fit into less RAM, you might want to downsize to a compatible 16 MB or even 8 MB RAM device, however the safe advice is to start with more RAM rather than less. Requirements for embedded applications grow at an incredibly fast rate and it is a sure bet that in a 1-year's time you will be wanting to add new software features to your design. Seeing that you are already considering uClinux for your microcontroller application, you are probably fed up with the "if only I had another 512 MB of RAM" kind of situation MCU developers are used to. If you want Linux and "features", plan for reasonable amounts of RAM.

On a practical side of things, and given the specific context of SDRAM memory used with the advanced STM32F7 microcontrollers, the BOM differences between compatible 8 MB, 16 MB and 32 MB devices are often times quite tolerable. Again, the advice we can give is to play safely and plan for more RAM rather than less.

With that background in mind, here are some footprint numbers for a sample Linux configuration that Emcraft includes in the software distribution. The project is called hello (for "Hello, world") and can be found in the projects/hello directory, relative to the top of the distribution directory.

The kernel configuration of this project is minimalistic, for instance, the TCP/IP stack is disabled. At the application level, this is really a single-process configuration. Specifically, instead of the standard Linux init, the project runs a custom application called hello. The following are relevant snippets from the hello.intramfs specification file that illustrate how the custom application is installed on the target instead of init.

file /bin/hello ${INSTALL_ROOT}/projects/${SAMPLE}/hello/hello 755 0 0
slink /bin/init hello 777 0 0

The application itself is a simple endless-loop Hello, world C program, except that before entering the loop it prints the content of /proc/meminfo to stdout (which is the Linux serial console in this specific configuration). The meminfo printout gives information on how much memory is available when the application is running.

The application source can be found in projects/hello/hello/hello.c. For the sake of completeness, here is the full source of that program:

#include <stdio.h>
#include <unistd.h>
#include "hello.h"
#include <sys/mount.h>

int main(int argc, char **argv)
{
char buff[4096];
int rc;
FILE *fp;

printf("Mounting /proc..\n");

if (mount("proc", "/proc", "proc", MS_MGC_VAL, NULL)) {
perror("Unable to mount /proc");
goto xit;
}
if (!(fp = fopen("/proc/meminfo", "r"))) {
perror("fopen");
goto xit;
}
printf("Reading /proc/meminfo:\n");
while (fgets(buff, sizeof(buff), fp)) {
fputs(buff, stdout);
}
printf("Done\n");

while(1) {
printf(HELLO_STRING);
sleep(3);
}
xit:
return -1;
}

The bootable Linux image ready for installation to embedded Flash of the STM32F746 (hello.uImage) is 594 KBytes in size. That easily fits into the 2 MBytes Flash of the STM32F746 with room to go.

Here is a snapshot of the boot session for that configuration, as run on the STM32F7 Discovery. The kernel runs from the internal Flash and is quite fast (note the 397 BogoMIPS metrics in the kernel output). Boot time to the application is less than a second (we didn't measure specifically, but it is probably around 600 msecs from the point where U-Boot passes control to the kernel). Notice the 6156 KBytes of unused memory:

U-Boot 2010.03-00083-gf9c82a9-cortexm-1.14.1 (Jun 26 2015 - 12:37:23)

CPU : STM32F7 (Cortex-M7)
Freqs: SYSCLK=200MHz,HCLK=200MHz,PCLK1=50MHz,PCLK2=100MHz
Board: STM32F746 Discovery Rev 1.A, www.emcraft.com
DRAM: 8 MB
...
Starting kernel ...

Linux version 2.6.33-arm1 ( This e-mail address is being protected from spambots. You need JavaScript enabled to view it ) (gcc version 4.4.1 (Sourcery G++ Lite 2010q1-189) ) #18 Thu Jun 25 11:22:51 +0400 2015
...
Memory: 8MB = 8MB total
Memory: 6520k/6520k available, 1672k reserved, 0K highmem
...
Calibrating delay loop... 397.31 BogoMIPS (lpj=1986560)
...
Freeing init memory: 64K
Mounting /proc..
Reading /proc/meminfo:
MemTotal:           6584 kB
MemFree:            6156 kB
Buffers:               0 kB
Cached:               32 kB
SwapCached:            0 kB
Active:                0 kB
Inactive:              0 kB
Active(anon):          0 kB
Inactive(anon):        0 kB
Active(file):          0 kB
Inactive(file):        0 kB
Unevictable:           0 kB
Mlocked:               0 kB
MmapCopy:             72 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:             0 kB
Mapped:                0 kB
Shmem:                 0 kB
Slab:                244 kB
SReclaimable:         24 kB
SUnreclaim:          220 kB
KernelStack:          72 kB
PageTables:            0 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:        3292 kB
Committed_AS:          0 kB
VmallocTotal:          0 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
Done
Hello, STM32F7DISCO-Linux!
...