【问题标题】:Name of the process with highest cpu usagecpu使用率最高的进程名
【发布时间】:2010-09-08 06:39:56
【问题描述】:

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

如何同时显示当前 CPU 使用百分比最高的进程名称?

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


进一步说明:

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

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

【问题讨论】:

  • 这是一个关于如何使用 samurize 包的问题吗?也许您可以将其改写为一个编程问题?

标签: windows performance operating-system


【解决方案1】:

你想要得到它的即时 CPU 使用率(某种)......

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

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

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

所以,首先,要获得平均 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;
}

现在,对于“即时”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;
    }
  }
}

您应该为您想要监控的每个进程从计时器(或任何您喜欢的间隔方法)调用该类,如果您希望所有进程只需使用Process.GetProcesses 静态方法 p>

【讨论】:

    【解决方案2】:

    基于 Frederic 的回答并利用页面底部的代码 here(有关用法示例,请参阅 this 帖子)加入从 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
    

    其中给出了

    的示例输出
    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
    

    【讨论】:

      【解决方案3】:

      您也许可以为此使用Pmon.exe。您可以将它作为Windows Resource Kit tools 的一部分获取(链接指向Server 2003 版本,显然也可以在XP 中使用)。

      【讨论】:

        【解决方案4】:
        【解决方案5】:

        不知何故

        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

        【讨论】:

          【解决方案6】:

          感谢您的公式,豪尔赫。我不太明白为什么你必须除以核心数量,但我得到的数字与任务管理器相匹配。这是我的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
          

          【讨论】:

          • 感谢工作示例!终于意识到 CPU 属性是进程的 TotalProcessorTime 的 TotalSeconds 并且 TotalSeconds 只能作为 TimeSpan 对象而不是 DateTime 对象。
          【解决方案7】:

          你也可以这样做:-

          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];
              }
          

          用法:-

          static void Main()
              {
                  Process mxp=getProcessWithMaxCPUUsage();
                  Console.WriteLine(mxp.ProcessName);
              }
          

          【讨论】:

            【解决方案8】:

            使用 PowerShell:

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

            返回有点像:

              16.8641632 System
               12.548072 csrss
              11.9892168 powershell
            

            【讨论】:

            • 另外,很明显,CPU 并没有给你使用 CPU 的百分比。它是进程处于活动状态的总处理器时间(以秒为单位)。 powershell "get-process | get-member | Select-Object Name,Definition | sort-object Name | format-list" CPU 定义为 {get=$this.TotalProcessorTime.TotalSeconds;}
            猜你喜欢
            • 2015-06-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多