在 Windows 上以编程方式读取程序的页面错误计数

发布于 2024-11-16 11:06:38 字数 262 浏览 3 评论 0原文

我希望我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

淡莣 2024-11-23 11:06:38

afai 可以告诉您,执行此操作的唯一方法是使用 ETW(Windows 事件跟踪)来监视内核硬页面错误。事件负载有一个线程 ID,您可以将其与现有进程关联起来(顺便说一句,这将是非常重要的)以生成正在运行的每个进程计数。我没有看到任何方法来获取每个进程的历史信息。

我可以向您保证这是一个难题,因为 Process Explorer 仅支持页面错误(软或硬)在其每个进程的显示中。

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

当寻找一个页面时会发生页面错误
页表条目无效。如果
需要引入请求的页面
从磁盘来看,它被称为硬页
故障(非常昂贵的操作),
并考虑所有其他类型
软页面错误(较便宜的
手术)。页面错误事件负载
包含虚拟内存地址
发生了页面错误并且
导致的指令指针
它。硬页面错误需要磁盘
访问发生,这可能是
首先访问文件中的内容或
访问内存块
调出。启用页面错误事件
导致记录硬页面错误
作为硬页面类型的页面错误
过错。然而,硬故障通常
有相当大的影响
性能,所以一个单独的事件是
仅适用于以下硬故障
可以独立启用。硬
故障事件负载有更多数据,
例如文件键、偏移量和线程
ID,与页面错误事件相比。

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.

热情消退 2024-11-23 11:06:38

我认为您可以使用 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.

骷髅 2024-11-23 11:06:38

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

深爱成瘾 2024-11-23 11:06:38

这是通过性能计数器来完成的视窗。我已经有一段时间没有和他们一起做任何事了。我不记得您是否需要以管理员身份运行才能查询它们。

[编辑]
我没有提供示例代码,但根据此页面,您可以获得特定进程的此信息:

进程:页面错误/秒。这是一个
页数指示
由于请求而发生的故障
从这个特定的过程。
过多的页面错误
特定过程是一个指示
通常是不良的编码实践。
函数和 DLL 都不是
组织正确,或者数据集
该应用程序正在使用的是
调用效率较低
方式。

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.

醉城メ夜风 2024-11-23 11:06:38

是的,很悲伤。或者您不能假设 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.

始终不够 2024-11-23 11:06:38

我认为您不需要管理凭据来枚举性能计数器。 codeproject 性能计数器枚举器 中的示例

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文