【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]

相关实践学习
在云上部署ChatGLM2-6B大模型(GPU版)
ChatGLM2-6B是由智谱AI及清华KEG实验室于2023年6月发布的中英双语对话开源大模型。通过本实验,可以学习如何配置AIGC开发环境,如何部署ChatGLM2-6B大模型。
相关文章
|
5月前
|
弹性计算 定位技术 数据中心
阿里云服务器配置选择方法:付费类型、地域及CPU内存配置全解析
阿里云服务器怎么选?2025最新指南:就近选择地域,降低延迟;长期使用选包年包月,短期灵活选按量付费;企业选2核4G5M仅199元/年,个人选2核2G3M低至99元/年,高性价比爆款推荐,轻松上云。
543 11
|
监控 测试技术 数据库
详解Hyper-V虚拟机CPU分配方法
在Hyper-V环境中,合理分配虚拟机的CPU资源至关重要。vCPU是物理CPU的虚拟化表示,管理员可通过指定处理器数量、核心数、设置兼容性和亲和性、启用动态分配等方法优化性能。使用性能监视工具监控并调整CPU资源,避免过度分配,确保虚拟机稳定运行。定期评估和优化资源分配策略,以适应业务变化,保持最佳性能。
|
存储 监控
【Azure Cloud Service】在Azure云服务中收集CPU监控指标和IIS进程的DUMP方法
在使用Cloud Service服务时,发现服务的CPU占用很高,在业务请求并不大的情况下,需要直到到底是什么进程占用了大量的CPU资源,已经如何获取IIS进程(w3wp.exe)的DUMP文件?
220 8
|
网络安全 Windows
window powershell 查看ssh
window powershell 查看ssh
1237 0
在Linux中,如何找出占用CPU或内存最多的进程?
在Linux中,如何找出占用CPU或内存最多的进程?
|
6月前
|
缓存 人工智能 算法
不同业务怎么选服务器?CPU / 内存 / 带宽配置表
本文详解了服务器三大核心配置——CPU、内存、带宽,帮助读者快速理解服务器性能原理。结合不同业务场景,如个人博客、电商、数据库、直播等,提供配置选择建议,并强调合理搭配的重要性,避免资源浪费或瓶颈限制。内容实用,适合初学者和业务选型参考。
1029 0
|
6月前
|
存储 消息中间件 缓存
从纳秒到毫秒的“时空之旅”:CPU是如何看待内存与硬盘的?
在数据爆炸的时代,如何高效存储与管理海量数据成为系统设计的核心挑战。本文从计算机存储体系结构出发,解析B+树、LSM树与Kafka日志结构在不同数据库中的应用与优化策略,帮助你深入理解高性能存储背后的原理。
221 0
|
8月前
|
存储
阿里云轻量应用服务器收费标准价格表:200Mbps带宽、CPU内存及存储配置详解
阿里云香港轻量应用服务器,200Mbps带宽,免备案,支持多IP及国际线路,月租25元起,年付享8.5折优惠,适用于网站、应用等多种场景。
2820 0
|
7月前
|
弹性计算 前端开发 NoSQL
2025最新阿里云服务器配置选择攻略:CPU、内存、带宽与系统盘全解析
本文详解2025年阿里云服务器ECS配置选择策略,涵盖CPU、内存、带宽与系统盘推荐,助你根据业务需求精准选型,提升性能与性价比。
|
存储 设计模式 监控
快速定位并优化CPU 与 JVM 内存性能瓶颈
本文介绍了 Java 应用常见的 CPU & JVM 内存热点原因及优化思路。
1236 166

热门文章

最新文章