通过processID获取特定进程的CPU使用率

时间:2014-09-16 14:46:57

标签: java windows powershell process wmic

我正在尝试使用PID获取特定进程的确切cpu使用情况。

使用Wmic,我可以获得cpu内存,threadcount但不能获得CPU使用率。

使用typeperf,我们可以获得cpu时间[我不确定时间是多么激动]

typeperf -sc 5 "\process(w3wp)\% processor time"

输出

"09/16/2014 10:43:34.441","1.556741" "09/16/2014 10:43:35.443","3.113481" "09/16/2014 10:43:36.446","0.000000" "09/16/2014 10:43:37.449","0.000000" "09/16/2014 10:43:38.452","0.000000"

有没有办法找到运行的windows / java进程的确切CPU [%]用法?

2 个答案:

答案 0 :(得分:1)

以下是使用PID或名称(使用选项A或选项B)在Powershell中执行此操作的相对简单的方法

# Option A: This is if you just have the name
$ProcessName = "ProcessName"

# Option B: This is for if you just have the PID; it will get the name for you
$ProcessPID = "3348"
$ProcessName = (Get-Process -Id $ProcessPID).Name


$CpuCores = (Get-WmiObject -Class Win32_Processor).NumberOfCores
$CpuValue = ((Get-Counter "\Process($ProcessName)\% Processor Time").CounterSamples.CookedValue)/$CpuCores
[Decimal]::Round($CpuValue, 3)

我希望这有助于解决您的问题或者至少指出您正确的方向。

干杯,

编辑:我没有意识到使用早期版本的PowerShell和不同的VM机器或具有多个物理CPU的机器时弹出的问题,所以...这是我对剧本的编辑。

# Option A: This is if you just have the name
$ProcessName = "ProcessName"

# Option B: This is for if you just have the PID; it will get the name for you
$ProcessPID = "3348"
$ProcessName = (Get-Process -Id $ProcessPID).Name

$CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
$CpuValue = ((Get-Counter "\Process($ProcessName)\% Processor Time").CounterSamples | Select-Object -Property CookedValue).CookedValue
[Decimal]::Round($CpuValue, 3)

答案 1 :(得分:0)

我找到了一个解决方案,可以使用PowerShell获取与ProcessID相关的所有与流程相关的详细信息

以下是获取所需流程详细信息的powershell代码,包括特定流程的cpu使用情况。

 param(
[String]$ProcID = "0" ,
[String[]]$Get = "0"
)
$CPUPercent = @{
  Name = 'CPUPercent'
  Expression = {
    $TotalSec = (New-TimeSpan -Start $_.StartTime).TotalSeconds
    [Math]::Round( ($_.CPU * 100 / $TotalSec), 2)
  }
}
function getParameters($Query)
{
    switch($Query)
    {
        "Name" {
                (Get-Process -ID $ProcID).Name | Format-List
               }
        "Pid" {
                (Get-Process -ID $ProcID).ProcessId | Format-List
               }
        "CommandLine" {
                        (Get-WmiObject Win32_Process -filter "ProcessId=$ProcID").CommandLine | Format-List
                      }
        "CSName" {
                        (Get-WmiObject Win32_Process -filter "ProcessId=$ProcID").CSName | 
                        Format-List
                      }
        "Description" {
                        (Get-WmiObject Win32_Process -filter "ProcessId=$ProcID").Description |                         
                        Format-List
                      }
        "ExecutablePath" {
                        (Get-WmiObject Win32_Process -filter "ProcessId=$ProcID").ExecutablePath |                      
                        Format-List
                      }
        "MaximumWorkingSetSize" {
                        (Get-WmiObject Win32_Process -filter "ProcessId=$ProcID").MaximumWorkingSetSize |                       
                        Format-List
                      }
        "MinimumWorkingSetSize" {
                        (Get-WmiObject Win32_Process -filter "ProcessId=$ProcID").MinimumWorkingSetSize |                       
                        Format-List
                      } 
        "PageFaults" {
                        (Get-WmiObject Win32_Process -filter "ProcessId=$ProcID").PageFaults |                      
                        Format-List
                      }
        "PeakVirtualSize" {
                        (Get-WmiObject Win32_Process -filter "ProcessId=$ProcID").PeakVirtualSize |                         
                        Format-List
                      }             
        "ProcessName" {
                        (Get-WmiObject Win32_Process -filter "ProcessId=$ProcID").ProcessName |                         
                        Format-List
                      }
        "ThreadCount" {
                        (Get-WmiObject Win32_Process -filter "ProcessId=$ProcID").ThreadCount | 
                        Format-list
                      } 
        "CPU"       {
                        (Get-Process -ID $ProcID) |
                        Select-Object -Property $CPUPercent|
                        Format-list
                    }       
        "PSVersion" {
                        $PSversiontable.psversion.major |
                        Format-list
                      }                       
        default {
                        "Not Enough Parameters for Get"
                        "Usage : example : ProcessDetails.ps1 -ProcID 5988 -Get cpu"
                }
    }
}
if(!($ProcID -eq "0") -or !($Get -eq "0"))
{
    foreach($getParam in $Get)
    {
        getParameters($getParam)
    }
}
else
{
    "Not Enough Parameters"
    "Usage : example : XXX.ps1 -ProcID PPPP -Get cpu"
}

用法:只需将此代码复制到文件并使用power shell

执行

ex:。\ XXX.ps1 -ProcID 1000 -Get cpu

ex:。\ XXX.ps1 -ProcID 1000 -Get ExecutablePath

ex:。\ XXX.ps1 -ProcID 1000 -Get ExecutablePath,cpu