如何获取当前进程的 Windows 性能计数器 [英] How do I get a Windows Performance Counter for the current process

查看:92
本文介绍了如何获取当前进程的 Windows 性能计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 C++ 应用程序 foo.exe 编写代码.在应用程序内部,我想记录其地址空间使用情况.所以我想查看的性能计数器是\Process(foo)\Virtual Bytes".我遇到的问题是系统上可能运行了多个 foo.exe 实例.在 perfmon 中,我可以看到这些被引用为\Process(foo#2)\Virtual Bytes",其中 #2 可以是任何数字,具体取决于进程数.

I am writing code for a C++ application foo.exe. Inside the application, I want to log its address space usage. So the performance counter I want to look at is "\Process(foo)\Virtual Bytes". The problem I am encountering is that there may be multiple instances of foo.exe running on the system. In perfmon, I can see that these are referenced as "\Process(foo#2)\Virtual Bytes" where the #2 could be any number depending on the number of processes.

如何构造计数器路径来获取当前进程的虚拟字节,以处理存在多个进程实例的情况?

How can I construct a counter path to get the virtual bytes for the current process that handles the case where there are multiples instances of the process?

推荐答案

您的实例是具有与您当前的进程 ID 匹配的 ID Process 计数器值的实例.不幸的是,除了获取所有实例并枚举它们,直到找到属于您的实例为止,别无他法,但这很简单.

Your instance is the one that has the ID Process counter value that matches your current process ID. Unfortunately there is no other way than to get all instances and enumerate through them untill you find the instance that is yours, but that is fairly trivial to do.

这篇关于如何获取当前进程的 Windows 性能计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆