Linux对内存的管理, 以及page fault的概念

▼魔方 西西 提交于 2020-08-09 01:50:31

原文地址:

http://blog.scoutapp.com/articles/2015/04/10/understanding-page-faults-and-memory-swap-in-outs-when-should-you-worry

学习后的总结:

Linux allocates memory to processes by dividing the physical memory into pages, and then mapping those physical pages to the virtual memory needed by a process. It does this in conjunction with the Memory Management Unit (MMU) in the CPU. Typically a page will represent 4KB of physical memory. Statistics and flags are kept about each page to tell Linux the status of that chunk of memory.

These pages can be in different states. Some will be free (unused), some will be used to hold executable code, and some will be allocated as data for a program. There are lots of clever algorithms that manage this list of pages and control how they are cached, freed and loaded.

Linux对于物理内存的管理方法是:

由MMU把物理内存分割成众多个page,每个page是4KB. 然后把page映射到进程的虚拟内存空间. CPU在执行进程中的指令时, 以虚拟内存地址为基础, 通过map映射, 进而找到物理内存中实际存放指令的地址.

What's a page fault?

Imagine a large running program on a Linux system. The program executable size could be measured in megabytes, but not all that code will run at once. Some of the code will only be run during initialization or when a special condition occurs. Over time Linux can discard the pages of memory which hold executable code, if it thinks that they are no longer needed or will be used rarely. As a result not all of the machine code will be held in memory even when the program is running.

A program is executed by the CPU as it steps its way through the machine code. Each instruction is stored in physical memory at a certain address. The MMU handles the mapping from the physical address space to the virtual address space. At some point in the program's execution the CPU may need to address code which isn't in memory. The MMU knows that the page for that code isn't available (because Linux told it) and so the CPU will raise a page fault.

The name sounds more serious than it really is. It isn't an error, but rather a known event where the CPU is telling the operating system that it needs physical access to some more of the code.

Linux will respond by allocating more pages to the process, filling those pages with the code from the binary file, configuring the MMU, and telling the CPU to continue.

page fault, (严格说, 这里指的是major page fault)名字听起来挺严重, 实际上, 并不是什么"错误".
大致是这样, 一个程序可能占几Mb, 但并不是所有的指令都要同时运行, 有些是在初始化时运行, 有些是在特定条件下才会去运行. 因此linux并不会把所有的指令都从磁盘加载到page内存. 那么当cpu在执行指令时, 如果发现下一条要执行的指令不在实际的物理内存page中时, CPU 就会 raise a page fault, 通知MMU把下面要执行的指令从磁盘加载到物理内存page中. 严格说, 这里指的是major fault. 还有另一种, 就是minor fault.

What a Minor page faults?

There is also a special case scenario called a minor page fault which occurs when the code (or data) needed is actually already in memory, but it isn't allocated to that process. For example, if a user is running a web browser then the memory pages with the browser executable code can be shared across multiple users (since the binary is read-only and can't change). If a second user starts the same web browser then Linux won't load all the binary again from disk, it will map the shareable pages from the first user and give the second process access to them. In other words, a minor page fault occurs only when the page list is updated (and the MMU configured) without actually needing to access the disk.

minor page fault, 指的就是CPU要执行的指令实际上已经在物理内存page中了, 只是这个page没有被分配给当前进程, 这时CPU就会raise一个minor page fault, 让MMU把这个page分配给当前进程使用, 因此minor page fault并不需要去访问磁盘.

What a Swap?

当物理内存不够时,把一些物理内存page中的内容写入到磁盘, 以腾出一些空闲的page出来供进程使用, 这就是swap out.(The process of writing pages out to disk to free memory is called swapping-out)
反过来说, 当CPU要执行的指令被发现已经swap out到了磁盘中, 这时就需要从磁盘把这些指令再swap in到物理内存中,让CPU去执行.
swap in和swap out的操作都是比较耗时的, 频繁的swap in和swap out操作很影响系统性能.

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!