一个循环采集CPU的etl日志的脚本

一个循环采集CPU的etl日志的脚本,wpr和xperf都可以完成功能,但是实测xperf采集的数据更少,对于只需要获取进程的时间片来估算cpu占用率的目的来讲,xperf的方式占用更少磁盘空间,解析也会更快。

%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
md D:\\temp
del /s /q D:\temp
set TargetDriveEtl=D:\\temp

@echo off
SET /A "index=1"
SET /A "count=12"
:while
if %index% leq %count% (
    echo The value of index is %index%
    cd 
    wmic process where name="wprui.exe" terminate
	xperf -d tmp.etl
    rem "C:\\Program Files (x86)\\Windows Kits\\10\\Windows Performance Toolkit\\wpr.exe" -cancel
	xperf -on PROC_THREAD+LOADER+CSWITCH -stackWalk CSwitch -buffersize 2048 -maxFile 2048 -filemode circular
    rem "C:\\Program Files (x86)\\Windows Kits\\10\\Windows Performance Toolkit\\wpr.exe" -start CPU
    timeout /T 600 /NOBREAK
	xperf -d %TargetDriveEtl%\temp%index%.etl
    rem "C:\\Program Files (x86)\\Windows Kits\\10\\Windows Performance Toolkit\\wpr.exe" -stop %TargetDriveEtl%\temp%index%.etl
    
    SET /A "index=index + 1"
    goto :while
)

解析脚本,执行通过命令行获取某个进程消耗的cpu时间片:
初次可能不允许执行ps1脚本,打开powershell管理员,输入set-executionpolicy remotesigned后即可运行。

param(
[string] $ProcName = $(throw "Parameter missing: -ProcName ProcName"))#,
#[string] $Dir = $(throw "Parameter missing: -Dir Dir"))

$Dir = "D:\\temp"

Get-ChildItem $Dir *.etl| ForEach-Object -Process{
    if($_ -is [System.IO.FileInfo])
    {
        Write-Host($_.name);
        echo "xperf -i $Dir\$_ -a cswitch -process | select-string -Pattern "$ProcName""
        xperf -i $Dir\$_ -a cswitch -process | select-string -Pattern $ProcName >>$Dir\CpuResult_$ProcName.txt
    }
}
(gc $Dir\CpuResult_$ProcName.txt) | ? {$_.trim() -ne "" } | set-content $Dir\CpuResult_$ProcName.txt

如果powershell脚本没有达到预期效果,可以打开powershell的调试开关,这样就能看到实际执行的powershell命令, 或者在PowerShell ISE中进行调试:

# 开启调试开关
Set-PSDebug -Trace 2
# 关闭调试开关
Set-PSDebug -Off

补充另一种解析脚本,将所有的etl Merge到一起,然后再解析某个进程消耗的时间片,这样就能得到该进程在整个测试期间的时间片消耗,不需要再用额外的脚本做累加处理:

$Dir="D:\\temp"
cd $Dir
$num = 0
rm summary.etl
rm summary_temp.etl
Get-ChildItem $Dir *.etl| ForEach-Object -Process{
    if($_ -is [System.IO.FileInfo])
    {
        $num++
    }
}
Write-Host($num)

#$index=1
xperf -merge .\temp1.etl summary.etl
for($index =2;$index -le $num;$index++){
    Write-Host($index)
    xperf -merge summary.etl temp$index.etl summary_temp.etl
    rm .\summary.etl
    mv summary_temp.etl summary.etl 
}
posted @ 2023-04-01 18:32  mooooonlight  阅读(74)  评论(0编辑  收藏  举报