嵌入式Linux物理内存映射

2019-07-12 19:58发布

The physical memory map for Linux is completely independent from the virtual map and is designed to maximize contiguous space. Given that the kernel image will always be at the start of DRAM, the Linux kernel maximizes contiguous space by allocating runtime memory from the end of physical DRAM moving downward.

The kernel starts by breaking available memory out into large, contiguous blocks (typically 4MB or more).It then maintains memory using the buddy system, where physical memory is always allocated in combinations of blocks of 2^n pages (where n is the order, that is, 4K is a 0 order block, 8K is a 1st order block, 16K is a 2nd order block, etc).

    Linux物理内存的映射完全独立于虚拟内存的映射,而且尽可能映射到连续的空间。假定内核映像总是位于DRAM的开始处,Linux内核尽可能让空间连续,这是通过从物理DRAM的末端向下移动来分配运行时内存而达到的。
   内核一开始把可用内存分割为大而连续的块(通常为4MB 或更多)。此后,内核利用伙伴算法来管理内存,这里,物理内存的分配总是2^n个页所形成块的组合(这里,n是幂,4k就是幂为0的块,8K是幂为1块,16K为幂为2的块,如此等等)。
   
             
When physical memory is allocated, that is, on process start, or when malloc’ed memory is written to (copy-on-write), the kernel scans for the smallest order block that will fill fit starting from the top of DRAM. As the number of running processes increases, or new allocations are spawned from drivers or processes, the physical memory used grows downward.


当物理内存分配时,也就是在进程刚开始时,或者说当分配的内存被写入时(写时复制),内核扫描最小幂次的块(从DRAM顶部开始寻找最合适的)。随着进程数的增加,或者来自驱动程序或进程新分配的蔓延,所使用的物理内存向下延伸。

When a process exits or a large enough memory block is freed, its DRAM space is unmapped and becomes a gap in memory. This process is called memory fragmentation and becomes more and more prevalent the longer a device is used and the more frequently the use case changes (such as checking email, playing an mp3, watching a video, etc). As new processes and allocations are created, the gaps are filled whenever possible, but fragmentation is still an inevitable part of any OS memory map.

   当进程退出或者大块内存块被释放时,DRAM空间的映射被解除,从而在内存中形成空白区。这样的过程就是所谓内存碎片,设备使用的时间越长,使用场景变化越频繁(例如收发邮件,播放MP3,观看视频等等),碎片会越来越多。