在 Windows 上以编程方式读取程序的页面错误计数
我希望我的 Windows C++ 程序能够读取它引起的硬页面错误的数量。该程序未以管理员身份运行。 编辑添加:要明确的是,我对整个系统的页面错误总数并不感兴趣。
看起来 ETW 可能会为此导出计数器,但我有很多弄清楚 API 很困难,并且不清楚普通用户与管理员相比可以访问哪些内容。
有人有这个功能的例子吗?或者在 Windows 上根本不可能?
(OT,但在 *nix 上这要容易得多,这不是很遗憾吗? gerusage() 就完成了。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
afai 可以告诉您,执行此操作的唯一方法是使用 ETW(Windows 事件跟踪)来监视内核硬页面错误。事件负载有一个线程 ID,您可以将其与现有进程关联起来(顺便说一句,这将是非常重要的)以生成正在运行的每个进程计数。我没有看到任何方法来获取每个进程的历史信息。
我可以向您保证这是一个难题,因为 Process Explorer 仅支持页面错误(软或硬)在其每个进程的显示中。
http://msdn.microsoft.com/en-us/magazine/ee412263.aspx
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
我认为您可以使用 GetProcessMemoryInfo() - 请参阅 http://msdn.microsoft.com/en-us/library/ms683219(v=vs.85).aspx 了解更多信息。
I think you can use GetProcessMemoryInfo() - Please refer to http://msdn.microsoft.com/en-us/library/ms683219(v=vs.85).aspx for more information.
Microsoft 网站上有一个 C/C++ 示例,解释了如何读取性能计数器: INFO: PDH枚举性能计数器和实例的示例代码
您可以复制/粘贴它,我认为您对“内存”/“页面读取/秒”计数器感兴趣,如这篇有趣的文章中所述: 页面错误的基础知识
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
这是通过性能计数器来完成的视窗。我已经有一段时间没有和他们一起做任何事了。我不记得您是否需要以管理员身份运行才能查询它们。
[编辑]
我没有提供示例代码,但根据此页面,您可以获得特定进程的此信息:
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:
是的,很悲伤。或者您不能假设 Windows 是如此简单以至于它甚至不提供页面错误计数器并查找它: Win32_PerfFormattedData_PerfOS_Memory。
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.
我认为您不需要管理凭据来枚举性能计数器。 codeproject 性能计数器枚举器 中的示例
I don't think you need administrative credential to enumerate the performance counters. A sample at codeproject Performance Counters Enumerator