簡體   English   中英

通過processID獲取特定進程的CPU使用率

[英]Get the CPU usage of a particular process by processID

我正在嘗試使用PID獲取特定進程的確切cpu用法。

使用Wmic,我可以獲取cpu內存,線程數,但不能獲取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 [%]使用率?

這是在Powershell中使用PID或名稱(使用選項A或選項B)的相對簡單的方法

# 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以及虛擬機或具有多個物理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)

我找到了一種使用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執行

例如:。\\ XXX.ps1 -ProcID 1000-獲取cpu

例如:。\\ XXX.ps1 -ProcID 1000 -Get ExecutablePath

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM