如何通过“任务列表”获取进程的CPU使用率?在Windows中 [英] How can I get the CPU usage of a process with "tasklist" in Windows
问题描述
我正在用Java编写程序,以定期显示给定进程ID的CPU和内存使用情况。我的实现调用任务列表。通过以下命令获取内存使用情况非常简单:
I am writing a program in Java to periodically display the CPU and memory usage of a given process ID. My implementation invokes tasklist. It is pretty straightforward to get the memory usage by the following command:
任务列表/ fi memusage ge 0 / fi pid eq 2076 / v
这将返回进程ID 2076的内存使用情况,我可以将其用于我的任务。通过调用以下命令,我可以提取CPU时间。
This will return the memory usage of process id 2076 and i can use this for my task. By invoking the following command, I can extract the CPU Time.
任务列表/ fi pid eq 2076 / fi CPUTIME ge 00:00:00 / v
我的问题是,我将如何获取该进程的CPU使用率?
My question is, how would I go about getting the CPU usage of this process?
我在StackOverflow上找到了我的问题的帖子,但答案不清楚,而且我不知道在命令中键入什么来获得所需的信息。问题在2008年得到了回答,有人在2013年要求澄清,但回答问题的人没有回答。
I found a post on StackOverflow for my question but the answer isn't clear and I don't understand what to type in the command to get what I need. The question was answered in 2008 and someone asked for clarification in 2013 but the person that answered the question hasn't replied.
Here is the post that I have found.
推荐答案
内存就像一个茶杯,它可能已满也可能是空的,瞬间查看杯子可以让您看到杯子有多充满就是茶(这是您的记忆命令)。
Memory is like a tea cup, it maybe full or empty, an instantaneous look at the cup allows you to see how full of tea it is (that is your "memusage" command).
CPU就像滑雪缆车一样。无论您是否乘坐电梯,它都以合理的恒定速度移动。不可能通过一个瞬时观测值来确定您的使用情况-我们需要知道您骑了多长时间(这就是 cputime命令)。您必须至少使用两次 cputime命令!
CPU is like a ski lift. It moves at a reasonably constant rate irrespective of whether your are riding the lift or not. It is not possible to determine your usage in a single instantaneous observation - we need to know how long you were riding it for (that is your "cputime" command). You have to use the "cputime" command at least twice!
例如:
- 在7:09 pm,您在进程上运行cputime命令,它返回 28分钟
- 在7:17 pm,您在进程上运行cputime命令再次返回 32分钟。
从第一次运行cputime命令到第二次,使用量增加了从28分钟到32分钟-进程已使用4分钟的CPU时间。
From the first time you ran the cputime command to the second time, the usage has increased from 28 minutes to 32 minutes -- the process has used 4 minutes of CPU time.
从7:09 pm到7:17 pm持续8分钟-A总共有8分钟的时间可用,但是您的进程仅用了4分钟:4/8 = 50%的平均系统使用率。
From 7:09pm to 7:17pm is a duration of 8 minutes -- A total of 8 minutes of time were available, but your process just used 4 minutes: 4 / 8 = 50% average system usage.
如果您的系统有多个处理器,则您可以将CPU总数除以得到每个CPU的平均值-例如50%/ 2 =双CPU系统中的平均值为25%。
If your system has multiple processors, then you can divide by the total number of CPUs to get an average per CPU - e.g. 50% / 2 = 25% average in a dual cpu system.
我在上面花了几分钟的时间来编写文字,实际上,您可能在看多少CPU纳秒。在仅几毫秒长的时间窗口内对过程进行计时。
I used minutes above for ease of writing - in reality you may be looking at how many nanoseconds of CPU time the process used during a time window that is just milliseconds long.
这篇关于如何通过“任务列表”获取进程的CPU使用率?在Windows中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!