Memory-saving embedded software design skills

Source: Internet
Author: User

Now the new Android yuan machine has 2 GB of memory. Do we need to rack our brains to save memory? Yes, it is a feature of high-end processors. Here we are talking about the resource shortage embedded system design method. Generally, the cost of the electronic products of single-chip microcomputer controllers is directly proportional to the memory, especially in the design of SOC chips. Before a large amount of data production, determine the size of the built-in SRAM. The smaller the built-in SRAM, the better. This requires the design and programming skills of the software system. Here, I will only summarize my personal work experience. This involves audio and video multimedia electronic products. Similar systems generally customize their own operating systems. Drivers, middleware, applications, and other modules are available, the so-called Sparrow is small and dirty.

I. memory block time-sharing

Time-sharing is used to manage code in blocks (banks. Its design needs come from:

1. Many electronic products do not run multiple applications at the same time as their current Android phones. They only browse pictures when they listen to songs at most, as do non-smart phones. However, we will also see many applications in electronic products, such as settings, ebook, phone book, and recording. Therefore, applications running at different times occupy the same memory space.

2. Drive Space. Many drivers are not used at the same time. For example, when listening to FM is a FM driver and listening to songs is a decoder, many drivers can also use the same space.

3. Reuse of middleware. For example, the UI and hardware driver are re-encapsulated and used. They are directly called by corresponding applications, and there is usually a need for reuse.

4. Reuse of data segments. Both applications and drivers have data and need to be reused.

In theory, the driver and code can also take space, but there are too many details to consider, and the scalability is not good, so the application will not reuse the space with the driver. Generally, software systems are roughly divided into the following components: startup, driver, operating system, middleware, and application. Start is a one-time execution, and there is no need to think too much about the space for reuse. Operating systems generally require resident memory, such as interrupt management, time management, scheduling management, module code management, and virtual file systems. Of course, some features of the operating system do not require resident memory, it mainly refers to some interfaces with lower call frequency, such as driver installation and uninstallation and application initialization modules. Some interface implementations without resident memory can also reuse space with the driver.

Let's take a look at the features of the Memory Management Unit of the high-end processor. The Memory Management Unit implements memory management in two parts, including the hardware TLB module and the software page table, the hardware TLB automatically matches the high N bits of the virtual address to the high N bits of the physical memory. The matching is based on the page table (TLB is the cache of the page table. It can be considered that the page table is a virtual-physical ing index table. High-end processors generally carry more than m-level memory, often SDRAM, rather than built-in SRAM. Generally, an operating system that supports multiple processes is used, that is, multiple applications can be run at the same time. The virtual address space and physical address space that can be used by these applications are the whole space (some may be used for the operating system, as is true for Linux ), that is, it is used for time-sharing in the whole address space. The code block management mentioned above only refers to the MMU design philosophy. The blocks are implemented in a certain amount of space, and the application and drive block spaces are separated.

Ii. Code segmentation skills

The first point is the requirements and general principles of block management. However, the design of block size greatly considers the skill of the System Architect. Block setup is a big waste, which may lead to frequent code switching and low efficiency. Since it is all Ram, sometimes the data can be placed in the same block as the code segment, and there is no need to add another data block. Of course, these details need to be comprehensively evaluated and designed in detail. In cost-sensitive electronic products, these skills need to be explored.

Iii. Replacing RAM with Rom

This only saves memory resources from the perspective of cost. Some codes need to be stored in the content, but the content does not change with the version update. As mentioned above, the scheduling and management of the operating system are described, you can consider curing the code into the Rom. Theoretically, most of the Code in the operating system that requires resident memory can be solidified. The cost ratio of Ram and Rom is about 6:1, so using ROM can also greatly reduce costs.

4. Cut down unnecessary modules during system Transplantation

This is an important consideration for operating system designers. Each product has unique functions, and the underlying operating system is universal. In resource shortage systems, it is wise to cut unnecessary modules.

V. Operating System Customization

It can also be called to improve the operating system. The systems we have mentioned are generally closed systems. As long as we can efficiently implement functions, we can modify all the code in the system at will. For example, for executable elf files, if the operating system needs to parse and load the elf files according to the standard process, it not only requires a lot of memory data, but also is inefficient. The most important thing about elf loading and execution is. code ,. data ,. const ,. we can extract BSS and other segment information offline to generate a new simple custom file format. The operating system only needs to parse this simple file. This not only saves memory, but also saves external storage space.

6. Programming Skills

This requires daily accumulation. For example, we all know that alignment will be considered during compilation in terms of variable arrangement.

Char A; int B; char C; the memory required to define the order of variables is larger than char a; char C; int B.

VII. Algorithm Design

Good algorithms are generally lightweight and efficient.

8. code compilation Optimization

Select a high optimization level during compilation, so that the size of the generated code is greatly reduced.

9. Compile command mode

For example, in arm, select thumb command and MIPS select mips16e, which is determined by the architecture. The architecture also designs a 16-bit command mode to save code space resources, these CPUs are usually 32 characters long. This method can reduce the amount of code by about 30%.

10. Stack space planning

Each thread has its own stack, and each thread's stack should be set according to its thread call depth. For example, UCOS has a stack usage task, we may use this idea to look at the final stack depth of a thread.

Set an independent interrupt stack to avoid reserving stack space for each task stack.

A flat function call method uses a stack that is generally smaller than a vertical function call. For efficiency and resources, embedded development sometimes does not need to divide the code into blocks too carefully. Functions increase the amount of code and stack, and reduce the running efficiency.

11. properly plan the memory space, adjust the linked files, and make full use of the existing physical space.

For example, when planning a space, we often separate the code segment from the data segment, but the actual code segment may not be used up. In this case, we can locate some variables after the code segment.

12. Be good at using the segment attributes of links

For example, the uboot command format. Each command format is a data structure named _ tbl_s, with names, execution functions, and help information. Considering the management of commands, we will naturally think of Structured arrays. But how can we set the size of arrays? The setting is too wasteful. If the setting is just enough, You need to increase the size of a command. Uboot uses the segment attribute of the link cleverly. As long as it is a command, add section (". u_boot_cmd "), then all the commands will naturally be placed in this segment. You can traverse this section by querying the command. In Linux, the link segment attribute technology is widely used.

 

Memory-saving embedded software design skills

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.