windows cpu使用率最高的进程名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Name of the process with highest cpu usage
提问by Ash
I have a Samurizeconfig that shows a CPU usage graph similar to Task manager.
我有一个Samurize配置,它显示类似于任务管理器的 CPU 使用率图。
How do I also display the name of the process with the current highest CPU usage percentage?
如何同时显示当前 CPU 使用率最高的进程名称?
I would like this to be updated, at most, once per second. Samurize can call a command line tool and display it's output on screen, so this could also be an option.
我希望最多每秒更新一次。Samurize 可以调用命令行工具并将其输出显示在屏幕上,因此这也可以是一个选项。
Further clarification:
进一步澄清:
I have investigated writing my own command line c# .NET application to enumerate the array returned from System.Diagnostics.Process.GetProcesses(), but the Process instance class does not seem to include a CPU percentage property.
我研究了编写自己的命令行 c# .NET 应用程序来枚举从 System.Diagnostics.Process.GetProcesses() 返回的数组,但 Process 实例类似乎不包含 CPU 百分比属性。
Can I calculate this in some way?
我可以用某种方式计算这个吗?
采纳答案by PabloG
With PowerShell:
使用 PowerShell:
Get-Process | Sort-Object CPU -desc | Select-Object -first 3 | Format-Table CPU,ProcessName -hidetableheader
returns somewhat like:
返回有点像:
16.8641632 System
12.548072 csrss
11.9892168 powershell
回答by Jorge Córdoba
What you want to get its the instant CPU usage (kind of)...
你想得到它的即时 CPU 使用率(种类)...
Actually, the instant CPU usage for a process does not exists. Instead you have to make two measurements and calculate the average CPU usage, the formula is quite simple:
实际上,不存在进程的即时 CPU 使用率。相反,您必须进行两次测量并计算平均 CPU 使用率,公式非常简单:
AvgCpuUsed = [TotalCPUTime(process,time2) - TotalCPUTime(process,time1)] / [time2-time1]
AvgCpuUsed = [TotalCPUTime(process,time2) - TotalCPUTime(process,time1)] / [time2-time1]
The lower Time2 and Time1 difference is, the more "instant" your measurement will be. Windows Task Manager calculate the CPU use with an interval of one second. I've found that is more than enough and you might even consider doing it in 5 seconds intervals cause the act of measuring itself takes up CPU cycles...
Time2 和 Time1 差异越小,您的测量就越“即时”。Windows 任务管理器以一秒为间隔计算 CPU 使用情况。我发现这已经足够了,您甚至可以考虑每隔 5 秒进行一次,因为测量本身会占用 CPU 周期......
So, first, to get the average CPU time
所以,首先,要获得平均 CPU 时间
using System.Diagnostics;
float GetAverageCPULoad(int procID, DateTme from, DateTime, to)
{
// For the current process
//Process proc = Process.GetCurrentProcess();
// Or for any other process given its id
Process proc = Process.GetProcessById(procID);
System.TimeSpan lifeInterval = (to - from);
// Get the CPU use
float CPULoad = (proc.TotalProcessorTime.TotalMilliseconds / lifeInterval.TotalMilliseconds) * 100;
// You need to take the number of present cores into account
return CPULoad / System.Environment.ProcessorCount;
}
now, for the "instant" CPU load you'll need an specialized class:
现在,对于“即时”CPU 负载,您需要一个专门的类:
class ProcLoad
{
// Last time you checked for a process
public Dictionary<int, DateTime> lastCheckedDict = new Dictionary<int, DateTime>();
public float GetCPULoad(int procID)
{
if (lastCheckedDict.ContainsKey(procID))
{
DateTime last = lastCheckedDict[procID];
lastCheckedDict[procID] = DateTime.Now;
return GetAverageCPULoad(procID, last, lastCheckedDict[procID]);
}
else
{
lastCheckedDict.Add(procID, DateTime.Now);
return 0;
}
}
}
You should call that class from a timer (or whatever interval method you like) for each process you want to monitor, if you want all the processes just use the Process.GetProcessesstatic method
如果您希望所有进程只使用Process.GetProcesses静态方法,您应该从计时器(或您喜欢的任何间隔方法)为要监视的每个进程调用该类
回答by Vidar
Building on Frederic's answer and utilizing the code at the bottom of the page here(for an example of usage see thispost) to join the full set of processes gotten from Get-Process
, we get the following:
以 Frederic 的回答为基础,并利用此处页面底部的代码(有关用法示例,请参阅此帖子)加入从Get-Process
获得的全套流程,我们得到以下内容:
$sampleInterval = 3
$process1 = Get-Process |select Name,Id, @{Name="Sample1CPU"; Expression = {$_.CPU}}
Start-Sleep -Seconds $sampleInterval
$process2 = Get-Process | select Id, @{Name="Sample2CPU"; Expression = {$_.CPU}}
$samples = Join-Object -Left $process1 -Right $process2 -LeftProperties Name,Sample1CPU -RightProperties Sample2CPU -Where {$args[0].Id -eq $args[1].Id}
$samples | select Name,@{Name="CPU Usage";Expression = {($_.Sample2CPU-$_.Sample1CPU)/$sampleInterval * 100}} | sort -Property "CPU Usage" -Descending | select -First 10 | ft -AutoSize
Which gives an example output of
这给出了一个示例输出
Name CPU Usage
---- ---------
firefox 20.8333333333333
powershell_ise 5.72916666666667
Battle.net 1.5625
Skype 1.5625
chrome 1.5625
chrome 1.04166666666667
chrome 1.04166666666667
chrome 1.04166666666667
chrome 1.04166666666667
LCore 1.04166666666667
回答by hasseg
You might be able to use Pmon.exefor this. You can get it as part of the Windows Resource Kit tools(the link is to the Server 2003 version, which can apparently be used in XP as well).
您或许可以为此使用Pmon.exe。您可以将其作为Windows Resource Kit 工具的一部分获取(该链接指向 Server 2003 版本,显然也可以在 XP 中使用)。
回答by Ed Guiness
回答by Ayan Mullick
Somehow
不知何故
Get-Process | Sort-Object CPU -desc | Select-Object -first 3 | Format-Table CPU,ProcessName,TotalProcessorTime -hidetableheader
Get-Process | Sort-Object CPU -desc | Select-Object -first 3 | Format-Table CPU,ProcessName,TotalProcessorTime -hidetableheader
wasn't getting the CPU information from the remote machine. I had to come up with this.
没有从远程机器获取 CPU 信息。我不得不想出这个。
Get-Counter '\Process(*)\% Processor Time' | Select-Object -ExpandProperty countersamples | Select-Object -Property instancename, cookedvalue| Sort-Object -Property cookedvalue -Descending| Select-Object -First 10| ft -AutoSize
Get-Counter '\Process(*)\% Processor Time' | Select-Object -ExpandProperty countersamples | Select-Object -Property instancename, cookedvalue| Sort-Object -Property cookedvalue -Descending| Select-Object -First 10| ft -AutoSize
回答by Ambrose Leung
Thanks for the formula, Jorge. I don't quite understand why you have to divide by the number of cores, but the numbers I get match the Task Manager. Here's my powershell code:
谢谢你的公式,豪尔赫。我不太明白为什么你必须除以核心数,但我得到的数字与任务管理器相匹配。这是我的PowerShell代码:
$procID = 4321
$time1 = Get-Date
$cpuTime1 = Get-Process -Id $procID | Select -Property CPU
Start-Sleep -s 2
$time2 = Get-Date
$cpuTime2 = Get-Process -Id $procID | Select -Property CPU
$avgCPUUtil = ($cpuTime2.CPU - $cpuTime1.CPU)/($time2-$time1).TotalSeconds *100 / [System.Environment]::ProcessorCount
回答by MD. Mohiuddin Ahmed
You can also do it this way :-
你也可以这样做:-
public Process getProcessWithMaxCPUUsage()
{
const int delay = 500;
Process[] processes = Process.GetProcesses();
var counters = new List<PerformanceCounter>();
foreach (Process process in processes)
{
var counter = new PerformanceCounter("Process", "% Processor Time", process.ProcessName);
counter.NextValue();
counters.Add(counter);
}
System.Threading.Thread.Sleep(delay);
//You must wait(ms) to ensure that the current
//application process does not have MAX CPU
int mxproc = -1;
double mxcpu = double.MinValue, tmpcpu;
for (int ik = 0; ik < counters.Count; ik++)
{
tmpcpu = Math.Round(counters[ik].NextValue(), 1);
if (tmpcpu > mxcpu)
{
mxcpu = tmpcpu;
mxproc = ik;
}
}
return processes[mxproc];
}
Usage:-
用法:-
static void Main()
{
Process mxp=getProcessWithMaxCPUUsage();
Console.WriteLine(mxp.ProcessName);
}