What causes page faults?

前端 未结 7 861
日久生厌
日久生厌 2020-12-12 20:55

According to Wikipedia:

A page fault is a trap to the software raised by the hardware when a program accesses a page that is mapped in the virtual add

7条回答
  •  旧巷少年郎
    2020-12-12 21:31

    (I'm the author of Process Hacker.)

    Firstly:

    A page fault is a trap to the software raised by the hardware when a program accesses a page that is mapped in the virtual address space, but not loaded in physical memory.

    That's not entirely correct, as explained later in the same article (Minor page fault). There are soft page faults, where all the kernel needs to do is add a page to the working set of the process. Here's a table from the Windows Internals book (I've excluded the ones that result in an access violation):

    • Reason for Fault - Result
    • Accessing a page that isn’t resident in memory but is on disk in a page file or a mapped file - Allocate a physical page, and read the desired page from disk and into the relevant working set
    • Accessing a page that is on the standby or modified list - Transition the page to the relevant process, session, or system working set
    • Accessing a demand-zero page - Add a zero-filled page to the relevant working set
    • Writing to a copy-on-write page - Make process-private (or session-private) copy of page, and replace original in process or system working set

    Page faults can occur for a variety of reasons, as you can see above. Only one of them has to do with reading from the disk. If you try to allocate a block from the heap and the heap manager allocates new pages, then accesses those pages, you'll get a demand-zero page fault. If you try to hook a function in kernel32 by writing to kernel32's pages, you'll get a copy-on-write fault because those pages are silently being copied so your changes don't affect other processes.

    Now to answer your question more specifically: Process Hacker only seems to have page faults when updating its service information - that is, when it calls EnumServicesStatusEx, which RPCs to the SCM (services.exe). My guess is that in the process, a lot of memory is being allocated, leading to demand-zero page faults (the service information requires several pages to store, IIRC).

提交回复
热议问题