性能计数器按进程 ID 而不是名称?
我正在跟踪同一应用程序的多个实例,并且需要获取两个进程的内存和 CPU 使用情况。但是,我似乎无法找出使用性能计数器并知道哪个结果适用于哪个进程的方法。我已经看到我可以将 #1 等附加到名称末尾以获得每个结果,但这并不能告诉我哪个进程适用于哪个进程。
如何确定 ProcessId 或将进程 ID 传递给计数器以获取每个同名进程的结果?
PerformanceCounterCPU.CategoryName = "Process";
PerformanceCounterCPU.CounterName = "% Processor Time";
PerformanceCounterCPU.InstanceName = proc.ProcessHandle.ProcessName;
PerformanceCounterMemory.CategoryName = "Process";
PerformanceCounterMemory.CounterName = "Working Set - Private";
PerformanceCounterMemory.InstanceName = proc.ProcessHandle.ProcessName;
I am tracking multiple instances of the same application and need to get the memory and cpu use of both processes. However, I cant seem to figure out a way to use the performance counter and know which result is for which process. I have seen that I can append #1 and such to the end of the name to get results for each, but that doesn't tell me which one is for which process.
How can I determine the ProcessId or pass the process ID to the counter to get the result per each process with same name?
PerformanceCounterCPU.CategoryName = "Process";
PerformanceCounterCPU.CounterName = "% Processor Time";
PerformanceCounterCPU.InstanceName = proc.ProcessHandle.ProcessName;
PerformanceCounterMemory.CategoryName = "Process";
PerformanceCounterMemory.CounterName = "Working Set - Private";
PerformanceCounterMemory.InstanceName = proc.ProcessHandle.ProcessName;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相关问题的此答案可能有效:
This answer to a related question might work:
如果您不介意更改计算机范围的注册表,您可以 将 Windows 配置为使用 ProcessName_ProcessID 形式作为 Perf Counter 实例名称,而不是附加 #1、#2 等:
创建 DWORD
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PerfProc\Performance\ProcessNameFormat
并将其值设置为 2。如果您坚持使用 #1、#2 等形式,请注意 实例名称给定的进程可以在进程的生命周期内发生变化!
If you don't mind a machine-wide registry change, you can configure Windows to use the form ProcessName_ProcessID for Perf Counter instance names, rather than appending #1, #2, etc:
Create DWORD
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PerfProc\Performance\ProcessNameFormat
and set its value to 2.If you do stick with the #1, #2 etc form, beware that the instance name for a given process can change during the process' lifetime!