【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter

简介: 【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter

问题描述

部署在Azure上的VM资源,偶尔CPU飙高,但是发现的时候已经恢复,无法判断当时High CPU原因。

在Windows系统中,有什么方式能记录CPU被进程占用情况,查找出当时是排名前列的进程信息,用于后期分析。

 

问题解答

方式一:Performance Monitor

可以通过Windows系统自带的 Performance Monitor 来获取高CPU的情况。(缺点:Performance Monitor需要长期运行,对于没有规律且短时间无法重现的情况,不太适用)

图形版的操作步骤可以参考博文: 【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式

也可以通过CMD直接调用Performance Monitor的进程(Logman.exe )创建Monitor和开启,停止

第一步:创建Performance Monitor 指标 (Counter)

Logman.exe create counter Perf-1min -f bin -max 500 -c "\LogicalDisk(*)\*" "\Memory\*" "\Network Interface(*)\*" "\Paging File(*)\*" "\PhysicalDisk(*)\*" "\Server\*" "\System\*" "\Process(*)\*" "\Processor(*)\*" "\Cache\*" "\GPU Adapter Memory(*)\*" "\GPU Engine(*)\*" "\GPU Local Adapter Memory(*)\*" "\GPU Non Local Adapter Memory(*)\*" "\GPU Process Memory(*)\*" -si 00:01:00 -cnf 24:00:00 -v mmddhhmm -o C:\PerfMonLogs\Perf-1min.blg

注:counter的名称,-si 间隔时间, -cnf 固定的时间间隔, -o文件输出路径都可以自定义修改。

第二步:开启

Logman start Perf-1min

第三步:停止

Logman stop Perf-1min

CMD运行效果:

 

方式二:Powershell Get-Counter

The Get-Counter cmdlet gets performance counter data directly from the performance monitoring instrumentation in the Windows family of operating systems. Gets performance counter data from local and remote computers.

Get-Counter 直接从 Windows 系列操作系统中的性能监视检测中获取性能计数器数据。

You can use the Get-Counter parameters to specify one or more computers, list the performance counter sets and the instances they contain, set the sample intervals, and specify the maximum number of samples. Without parameters, Get-Counter gets performance counter data for a set of system counters.

可以使用 Get-Counter 参数指定一台或多台计算机,列出性能计数器集及其包含的实例,设置采样间隔,以及指定最大样本数。如果不带参数,Get-Counter 将获取一组系统计数器的性能计数器数据。

Many counter sets are protected by access control lists (ACL). To see all counter sets, open PowerShell with the Run as administrator option.

许多计数器集受访问控制列表 (ACL) 的保护。若要查看所有计数器集,请使用“以管理员身份运行”选项打开 PowerShell。

 

本文中示例为:(保存脚本为 getcpu.ps1,直接运行输入间隔时间(秒)和CPU阈值,脚本会长时间运行)

#获取总的cpu
function all_cpu(){
$total = Get-Counter "\Process(*)\% Processor Time" -ErrorAction SilentlyContinue | select -ExpandProperty CounterSamples | where InstanceName -eq _total
$idle = Get-Counter "\Process(*)\% Processor Time" -ErrorAction SilentlyContinue | select -ExpandProperty CounterSamples | where InstanceName -eq idle
$cpu_total = ($total.cookedvalue-$idle.cookedvalue)/100/$env:NUMBER_OF_PROCESSORS
return $cpu_total.tostring("P")
}
#获取前五cpu占用
function get_top_5(){
Get-Counter "\Process(*)\% Processor Time" -ErrorAction SilentlyContinue `
  | select -ExpandProperty CounterSamples `
  | where {$_.Status -eq 0 -and $_.instancename -notin "_total", "idle"} `
  | sort CookedValue -Descending `
  | select TimeStamp,
    @{N="Name";E={
        $friendlyName = $_.InstanceName
        try {
            $procId = [System.Diagnostics.Process]::GetProcessesByName($_.InstanceName)[0].Id
            $proc = Get-WmiObject -Query "SELECT ProcessId, ExecutablePath FROM Win32_Process WHERE ProcessId=$procId"
            $procPath = ($proc | where { $_.ExecutablePath } | select -First 1).ExecutablePath
            $friendlyName = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($procPath).FileDescription
        } catch { }
        $friendlyName
    }}, 
    @{N="ID";E={
        $friendlyName = $_.InstanceName
        [System.Diagnostics.Process]::GetProcessesByName($_.InstanceName)[0].Id}} ,
    @{N="CPU";E={($_.CookedValue/100/$env:NUMBER_OF_PROCESSORS).ToString("P")}} -First 5 `
 | ft -a 
}
#主函数
#入参,间隔时间, CPU阈值
function main{
param(
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()][int] $sleeptime,
    [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()][int] $cpu_set
    )
$count = 0
while(1){
$count += 1
echo "Check CPU times : $count"
if ($(all_cpu)*100 -gt $cpu_set){
echo "===== ===== Start Logging ====== =====" >> C:\checkcpu_all_cpu.log
echo "CPU : $(all_cpu)" >>  C:\checkcpu_all_cpu.log
echo $(get_top_5) >>  C:\checkcpu_top_5.log
}
#每隔多少秒运行一次
start-sleep -s $sleeptime
}
}
#执行主函数
main

执行效果:

 

参考资料

Get-Counter: https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.diagnostics/get-counter?view=powershell-7.2

 

[END]

相关实践学习
部署Stable Diffusion玩转AI绘画(GPU云服务器)
本实验通过在ECS上从零开始部署Stable Diffusion来进行AI绘画创作,开启AIGC盲盒。
相关文章
|
3天前
|
存储 监控
【Azure Cloud Service】在Azure云服务中收集CPU监控指标和IIS进程的DUMP方法
在使用Cloud Service服务时,发现服务的CPU占用很高,在业务请求并不大的情况下,需要直到到底是什么进程占用了大量的CPU资源,已经如何获取IIS进程(w3wp.exe)的DUMP文件?
|
1天前
|
Python
惊!Python进程间通信IPC,让你的程序秒变社交达人,信息畅通无阻
【9月更文挑战第13天】在编程的世界中,进程间通信(IPC)如同一场精彩的社交舞会,每个进程通过优雅的IPC机制交换信息,协同工作。本文将带你探索Python中的IPC奥秘,了解它是如何让程序实现无缝信息交流的。IPC如同隐形桥梁,连接各进程,使其跨越边界自由沟通。Python提供了多种IPC机制,如管道、队列、共享内存及套接字,适用于不同场景。通过一个简单的队列示例,我们将展示如何使用`multiprocessing.Queue`实现进程间通信,使程序如同社交达人般高效互动。掌握IPC,让你的程序在编程舞台上大放异彩。
8 3
|
6天前
|
安全 开发者 Python
揭秘Python IPC:进程间的秘密对话,让你的系统编程更上一层楼
【9月更文挑战第8天】在系统编程中,进程间通信(IPC)是实现多进程协作的关键技术。IPC机制如管道、队列、共享内存和套接字,使进程能在独立内存空间中共享信息,提升系统并发性和灵活性。Python提供了丰富的IPC工具,如`multiprocessing.Pipe()`和`multiprocessing.Queue()`,简化了进程间通信的实现。本文将从理论到实践,详细介绍各种IPC机制的特点和应用场景,帮助开发者构建高效、可靠的多进程应用。掌握Python IPC,让系统编程更加得心应手。
12 4
|
11天前
|
NoSQL
gdb中获取进程收到的最近一个信号的信息
gdb中获取进程收到的最近一个信号的信息
|
12天前
|
Linux
查看进程的内存使用信息
查看进程的内存使用信息
|
16天前
|
存储 监控 Docker
如何限制docker使用的cpu,内存,存储
如何限制docker使用的cpu,内存,存储
|
25天前
|
缓存 Kubernetes 数据中心
在Docker中,如何控制容器占用系统资源(CPU,内存)的份额?
在Docker中,如何控制容器占用系统资源(CPU,内存)的份额?
|
1月前
|
KVM 虚拟化
[kvm]cpu内存硬盘配置
[kvm]cpu内存硬盘配置
|
16天前
|
缓存 Linux 调度
Linux服务器如何查看CPU占用率、内存占用、带宽占用
Linux服务器如何查看CPU占用率、内存占用、带宽占用
54 0
|
22天前
|
存储 Java API
【Azure Developer】通过Azure提供的Azue Java JDK 查询虚拟机的CPU使用率和内存使用率
【Azure Developer】通过Azure提供的Azue Java JDK 查询虚拟机的CPU使用率和内存使用率

相关实验场景

更多