4

I'd like to my Windows C++ program to be able to read the number of hard page faults it has caused. The program isn't running as administrator. Edited to add: To be clear, I'm not as interested in the aggregate page fault count of the whole system.

It looks like ETW might export counters for this, but I'm having a lot of difficulty figuring out the API, and it's not clear what's accessible by regular users as compared to administrators.

Does anyone have an example of this functionality lying around? Or is it simply not possible on Windows?

(OT, but isn't it sad how much easier this is on *nix? gerusage() and you're done.)

Justin L.
  • 3,957
  • 3
  • 31
  • 29

6 Answers6

4

afai can tell the only way to do this would be to use ETW (Event Tracing for Windows) to monitor kernel Hard Page Faults. The event payload has a thread ID that you might be able to correlate with an existing process (this is going to be non-trivial btw) to produce a running per-process count. I don't see any way to get historical information per process.

I can guarantee you that this is A Hard Problem because Process Explorer supports only Page Faults (soft or hard) in its per-process display.

http://msdn.microsoft.com/en-us/magazine/ee412263.aspx

A page fault occurs when a sought-out page table entry is invalid. If the requested page needs to be brought in from disk, it is called a hard page fault (a very expensive operation), and all other types are considered soft page faults (a less expensive operation). A Page Fault event payload contains the virtual memory address for which a page fault happened and the instruction pointer that caused it. A hard page fault requires disk access to occur, which could be the first access to contents in a file or accesses to memory blocks that were paged out. Enabling Page Fault events causes a hard page fault to be logged as a page fault with a type Hard Page Fault. However, a hard fault typically has a considerably larger impact on performance, so a separate event is available just for a hard fault that can be enabled independently. A Hard Fault event payload has more data, such as file key, offset and thread ID, compared with a Page Fault event.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • As far as I can tell Process Explorer uses performance counters which I suppose is why it shows hard and soft page faults. I've never used ETW but from what I've been reading it looks to be much more powerful. – Dave Rager Jun 21 '11 at 19:50
  • @Dave - yes, ETW is great but even harder to use than Perf Counters. – Steve Townsend Jun 22 '11 at 12:40
2

I think you can use GetProcessMemoryInfo() - Please refer to http://msdn.microsoft.com/en-us/library/ms683219(v=vs.85).aspx for more information.

istudy0
  • 1,313
  • 5
  • 14
  • 22
1

Yes, quite sad. Or you could just not assume Windows is so gimp that it doesn't even provide a page fault counter and look it up: Win32_PerfFormattedData_PerfOS_Memory.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • I understood that to give system-wide, rather than per-process, information. http://glandium.org/blog/?p=1963 Am I mistaken? – Justin L. Jun 20 '11 at 19:46
1

This is done with performance counters in windows. It's been a while since I've done anything with them. I don't recall whether or not you need to run as administrator to query them.

[Edit] I don't have example code to provide but according to this page, you can get this information for a particular process:

Process : Page Faults/sec. This is an indication of the number of page faults that occurred due to requests from this particular process. Excessive page faults from a particular process are an indication usually of bad coding practices. Either the functions and DLLs are not organized correctly, or the data set that the application is using is being called in a less than efficient manner.

Dave Rager
  • 8,002
  • 3
  • 33
  • 52
1

There is a C/C++ sample on Microsoft's site that explain how to read performance counters: INFO: PDH Sample Code to Enumerate Performance Counters and Instances

You can copy/paste it and I think you're interested by the "Memory" / "Page Reads/sec" counters, as stated in this interesting article: The Basics of Page Faults

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • Can I get per-application information this way? It looks like this provides only system-wide data. – Justin L. Jun 20 '11 at 20:13
  • 1
    @Justin - Hmm... I missed that part in your question. I don't think so, because when you browse perf counters, using Perfmon for example, you'll only see "Page Faults / sec" under the "Process" category. Even undocumented apis (NtQueryInformationProcess, VM_COUNTERS) don't do it. see another related thread here: http://www.cpplc.net/forum/index.php?topic=580.0 – Simon Mourier Jun 20 '11 at 20:30
0

I don't think you need administrative credential to enumerate the performance counters. A sample at codeproject Performance Counters Enumerator

mox
  • 6,084
  • 2
  • 23
  • 35