如何获取进程使用的CPU周期数
我需要使用 C#(或 VB.Net)获取特定进程使用的 CPU 周期数。此信息可在 Sysinternal 的进程资源管理器内的进程属性弹出窗口中找到。例如,我发布此消息时使用的浏览器当前已使用 18,521,360,165 个周期(或多或少有几亿)。有谁知道如何从 .Net 应用程序获取此信息?我知道如何获取 CPU 使用率(百分比),但这不是我想要的。我需要一种方法来比较在不同时间运行的两个不同进程之间的 CPU 使用情况。
谢谢你, 马特
更新:
为什么我需要这个?我是本地 .Net 用户组的领导者,我们正在举办代码挑战赛,开发人员提交代码来解决问题。我需要一种方法来衡量一个条目相对于另一个条目的性能。目前我正在使用计时器来衡量性能。服务器 100% 致力于此,但这并不能保证同时发生其他事情。显然,这充满了各种潜在的问题,但总的来说,它是相当准确的。测量所使用的 CPU 周期数几乎是一种万无一失的方法,可以衡量某个条目相对于另一个条目的性能。我确信有人可以在这一切上打洞——此时无需尝试。 ;-) 我希望这有助于解释我的问题背后的原因以及为什么计时器不足以解决我的问题。
I have a need to get the number of CPU cycles used by a specific process using C# (or VB.Net). This information is available in the Process properties popup within Sysinternal's Process Explorer. For instance, the browser that I'm using the post this message has currently used 18,521,360,165 cyles (give or take a few hundred million). Does anyone know how to get this information from a .Net app? I know how to get the CPU usage (percentage), but this isn't what I'm looking for. I need a way to compare CPU usage between two different processes running at different times.
Thank you,
Matt
Update:
Why do I need this? I'm the leader of the local .Net user group and we're running a code challenge where developers submit code to solve a problem. I need a way to measure the performance of one entry against another. Currently I'm using a timer to measure performance. The server is 100% dedicated to this, but that doesn't guarantee that something else might be happening at the same time. Obviously, this is frought with all kinds of potential issues, but generally speaking, it's fairly accurate. Measuring the number of CPU cycles used would be an almost fool proof way to measure how well someone's entry performs against another. I'm certain that someone can shoot holes all over this - no need to try at this point. ;-) I hope that helps explain the reason behind my question and why a timer is insufficient for solving my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Process Explorer 调用
QueryProcessCycleTime
来获取该信息。您可能必须使用 P/Invoke 来调用它。我希望它的 P/Invoke 签名看起来像:Process Explorer calls
QueryProcessCycleTime
to get that information. You will probably have to use P/Invoke to call it. I would expect its P/Invoke signature to look like:您将需要查询 CPU 内部的性能计数器。这是低级别的并且非常特定于硬件,因此您必须调用本机代码才能获取它。 PAPI 是最接近此任务的可移植库。
请注意,上下文切换可以更改许多内部 CPU 寄存器,因此您需要从进程内部执行此查询。从不同的进程查询 CPU 计数器将会得到虚假结果。
还要记住,CPU 周期计数与“执行的指令数”不同(在 x86 上甚至不相似)。
You will need to query the performance counters inside the CPUs. This is low level and very hardware specific, so you'll have to thunk to native code to get it. PAPI is the closest thing to a portable library for this task.
Be aware that context switches can change many of these internal CPU registers, so you'll need to do this query from inside your process. Querying the CPU counters from a different process will give you spurious results.
Remember also that CPU cycle count is not the same as (and on the x86, isn't even similar to) "number of instructions performed."
这里有一篇 CodeProject 文章: http://www.codeproject.com/KB/system/ processscpuusage.aspx
我认为这篇文章将帮助您准确地完成您想要做的事情。基本上,他们向您展示了两种方法,一种使用托管 System.Diagnostics 工具,第二种使用 Win API 方法调用。
There is a CodeProject article here: http://www.codeproject.com/KB/system/processescpuusage.aspx
I think this article will help you do exactly what you are trying to do. Basically they show you 2 ways to do it, one using the managed System.Diagnostics tools and the second using a Win API method call.