首页 / 知识

关于Windows:CPU使用率最高的进程的名称

2023-04-14 16:26:00

关于Windows:CPU使用率最高的进程的名称

Name of the process with highest cpu usage

我有一个Samurize配置,该配置显示了类似于任务管理器的CPU使用率图。

如何显示当前具有最高CPU使用率的进程的名称?

我希望最多每秒更新一次。 Samurize可以调用命令行工具,并将其输出显示在屏幕上,因此也可以选择。

进一步说明:

我已经研究过编写自己的命令行c#.NET应用程序以枚举从System.Diagnostics.Process.GetProcesses()返回的数组,但是Process实例类似乎未包含CPU百分比属性。

我可以用某种方式计算吗?


您想要获得的即时CPU使用率(有点)...

实际上,进程的即时CPU使用率不存在。相反,您必须进行两次测量并计算平均CPU使用率,公式很简单:

AvgCpuUsed = [TotalCPUTime(process,time2) - TotalCPUTime(process,time1)] / [time2-time1]

Time2和Time1的差越小,您的测量的"即时"越多。 Windows任务管理器以一秒为间隔计算CPU使用率。我发现这已经绰绰有余了,您甚至可以考虑每隔5秒进行一次测量,因为测量本身占用了CPU周期...

因此,首先要获取平均CPU时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    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;
}

现在,对于"即时" CPU负载,您将需要一个专门的类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 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;
    }
  }
}

如果要所有进程仅使用Process.GetProcesses静态方法,则应从计时器(或所需的任何间隔方法)为要监视的每个进程调用该类。


以Frederic的答案为基础,并利用页面底部的代码(有关用法示例,请参见此文章)加入从Get-Process获得的完整过程集,我们得到以下信息:铅>

1
2
3
4
5
6
7
8
9
10
11
$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

其中给出了

的示例输出

1
2
3
4
5
6
7
8
9
10
11
12
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

感谢公式Jorge。我不太明白为什么必须除以内核数,但是我得到的数字与任务管理器匹配。这是我的powershell代码:

1
2
3
4
5
6
7
8
9
10
11
$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

不知何故

Get-Process | Sort-Object CPU -desc | Select-Object -first 3 | Format-Table CPU,ProcessName,TotalProcessorTime -hidetableheader

没有从远程计算机获取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


1
Process.TotalProcessorTime

http://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.totalprocessortime.aspx


您也许可以为此使用Pmon.exe。您可以将其作为Windows资源工具包工具的一部分来获得(链接指向Server 2003版本,该版本显然也可以在XP中使用)。


您也可以这样:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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];
    }

用法:-

1
2
3
4
5
static void Main()
    {
        Process mxp=getProcessWithMaxCPUUsage();
        Console.WriteLine(mxp.ProcessName);
    }

使用PowerShell:

1
Get-Process | Sort-Object CPU -desc | Select-Object -first 3 | Format-Table CPU,ProcessName -hidetableheader

返回类似:

1
2
3
  16.8641632 System
   12.548072 csrss
  11.9892168 powershell

进程显示配置任务管理器

最新内容

相关内容

猜你喜欢