|
| 1 | +--- |
| 2 | +title: Tracking system resource |
| 3 | +weight: 3 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | +## Sampling video decoding resource usage |
| 10 | +A PowerShell script does all the work. It launches the video decoding task, samples CPU and memory usage, and outputs sampled data to a file with format. |
| 11 | + |
| 12 | +Open your code editor, copy content below and save it as `sample_decoding.ps1`. |
| 13 | +```PowerShell { line_numbers = true } |
| 14 | +param ( |
| 15 | + [string]$exePath = "path\to\ffplay.exe", |
| 16 | + [string[]]$argList = @("-loop", "15", "-autoexit", "D:\RaceNight_1080p.mp4"), |
| 17 | + [int]$interval = 2, |
| 18 | + [string]$outputFile = "usage_log.csv" |
| 19 | +) |
| 20 | +
|
| 21 | +"" | Out-File -FilePath $outputFile |
| 22 | +
|
| 23 | +if (-Not (Test-Path $exePath)) { |
| 24 | + Write-Host "Executable not found at path: $exePath" |
| 25 | + exit 1 |
| 26 | +} |
| 27 | +
|
| 28 | +$zoneIdentifier = "$exePath`:Zone.Identifier" |
| 29 | +if (Test-Path $zoneIdentifier) { |
| 30 | + Write-Host "exe is locked. Trying to unlock..." |
| 31 | + try { |
| 32 | + Unblock-File -Path $exePath |
| 33 | + Write-Host "Unlocked exe file." |
| 34 | + } catch { |
| 35 | + Write-Host "Failed to unlock exe: $($_.Exception.Message)" |
| 36 | + } |
| 37 | +} else { |
| 38 | + Write-Host "exe is not locked." |
| 39 | +} |
| 40 | +
|
| 41 | +try { |
| 42 | + $cmdLine = "`"$exePath`" $argList" |
| 43 | + Write-Host "Executing: $cmdLine" |
| 44 | + $process = Start-Process -FilePath $exePath -ArgumentList $argList -PassThru |
| 45 | +} catch { |
| 46 | + Write-Host "Failed to start process. Error: $_" |
| 47 | + exit 1 |
| 48 | +} |
| 49 | +
|
| 50 | +$appPid = $process.Id |
| 51 | +Write-Host "Parent PID: $appPid" |
| 52 | +
|
| 53 | +Start-Sleep -Seconds 2 |
| 54 | +$childProcess = Get-CimInstance -ClassName Win32_Process | Where-Object { $_.ParentProcessId -eq $appPid } |
| 55 | +
|
| 56 | +$index = 1 |
| 57 | +$outHead = @() |
| 58 | +$outHead += "Timestamp,CPU Sum (s),Memory Sum (MB),Memory Private Sum (MB),CPU0 (s),Memory0 (MB),Memory Private0 (MB)" |
| 59 | +foreach ($child in $childProcess) { |
| 60 | + $childPid = $child.ProcessID |
| 61 | + Write-Host " - Child: $childPid" |
| 62 | + $outHead += "CPU$index (s),Memory$index (MB),Memory Private$index (MB)" |
| 63 | + $index++ |
| 64 | +} |
| 65 | +$outHead -join "," | Out-File -Encoding utf8 $outputFile |
| 66 | +
|
| 67 | +Write-Host "Sampling start..." |
| 68 | +
|
| 69 | +while (-not $process.HasExited) { |
| 70 | + $cpu = @() |
| 71 | + $mem = @() |
| 72 | + $memPriv = @() |
| 73 | + $outLine = @() |
| 74 | +
|
| 75 | + $timestamp = Get-Date -Format o |
| 76 | + $outLine += $timestamp |
| 77 | + $proc = Get-Process -Id $appPid -ErrorAction SilentlyContinue |
| 78 | + if ($proc) { |
| 79 | + $cpu += $proc.CPU |
| 80 | + $mem += $proc.WorkingSet64 / 1MB |
| 81 | + $memPriv += $proc.PrivateMemorySize64 / 1MB |
| 82 | +
|
| 83 | + foreach ($child in $childProcess) { |
| 84 | + $procChild = Get-Process -Id $child.ProcessId -ErrorAction SilentlyContinue |
| 85 | + $cpu += $procChild.CPU |
| 86 | + $mem += $procChild.WorkingSet64 / 1MB |
| 87 | + $memPriv += $procChild.PrivateMemorySize64 / 1MB |
| 88 | + } |
| 89 | +
|
| 90 | + $outLine += ($cpu | Measure-Object -Sum).Sum |
| 91 | + $outLine += "{0:F2}" -f ($mem | Measure-Object -Sum).Sum |
| 92 | + $outLine += "{0:F2}" -f ($memPriv | Measure-Object -Sum).Sum |
| 93 | + for ($i = 0; $i -lt $cpu.Count; $i++) { |
| 94 | + $outLine += $cpu[$i] |
| 95 | + $outLine += $mem[$i] |
| 96 | + $outLine += $memPriv[$i] |
| 97 | + } |
| 98 | +
|
| 99 | + $outLine -join "," | Out-File -Append -Encoding utf8 $outputFile |
| 100 | + } |
| 101 | +
|
| 102 | + Start-Sleep -Seconds $interval |
| 103 | + $process.Refresh() |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +{{% notice Note %}} |
| 108 | +Modify the path to `ffplay.exe` on line 2 accordingly. |
| 109 | +{{% /notice %}} |
| 110 | + |
| 111 | +Run the script: |
| 112 | +```console |
| 113 | +Set-ExecutionPolicy -Scope Process RemoteSigned |
| 114 | +.\sample_decoding.ps1 |
| 115 | +``` |
| 116 | +A video starts playing. It ends in 3 minutes. And then you can find the sample results file **usage_log.csv** in current directory. |
| 117 | + |
| 118 | +{{% notice Note %}} |
| 119 | +Script execution can be blocked due to policy configuration. The `Set-ExecutionPolicy` line allows local script to run during this session. |
| 120 | +{{% /notice %}} |
| 121 | + |
| 122 | +### Script explained |
| 123 | +The `param` section defines variables including binary path, video playback arguments, sampling interval and result file path. |
| 124 | + |
| 125 | +Line 15 - Line 26 check and modify binary file attribute. The binaries in use are downloaded from the web. They can be blocked to run due to lack of signature. These lines unlock the binaries. |
| 126 | + |
| 127 | +Line 41 gets all the child processes of the main process. The statistic data include resources used by all the processes spawned by the main process. |
| 128 | + |
| 129 | +The `while` setction collects processes' CPU and memory usage periodically until the application exits. The CPU usage is accumulated time length that the process runs on CPU. And the memory usage is size of memory occupation with or without shared spaces accounted. |
| 130 | + |
| 131 | +### View result |
| 132 | +Shown below is example sample result from running x86_64 version ffplay.exe: |
| 133 | +```output |
| 134 | +Timestamp,CPU Sum (s),Memory Sum (MB),Memory Private Sum (MB),CPU0 (s),Memory0 (MB),Memory Private0 (MB),CPU1 (s),Memory1 (MB),Memory Private1 (MB) |
| 135 | +2025-08-18T10:40:12.3480939+08:00,3.6875,378.65,342.16,3.671875,366.3515625,340.33984375,0.015625,12.296875,1.82421875 |
| 136 | +...... |
| 137 | +2025-08-18T10:43:09.7262439+08:00,396.375,391.71,355.00,396.359375,379.453125,353.2421875,0.015625,12.2578125,1.7578125 |
| 138 | +``` |
| 139 | + |
| 140 | +Example result from running Arm64 native ffplay.exe: |
| 141 | +```output |
| 142 | +Timestamp,CPU Sum (s),Memory Sum (MB),Memory Private Sum (MB),CPU0 (s),Memory0 (MB),Memory Private0 (MB),CPU1 (s),Memory1 (MB),Memory Private1 (MB) |
| 143 | +2025-08-18T10:36:04.3654823+08:00,3.296875,340.51,328.17,3.28125,328.18359375,326.359375,0.015625,12.32421875,1.8125 |
| 144 | +...... |
| 145 | +2025-08-18T10:39:01.7856168+08:00,329.109375,352.53,339.96,329.09375,340.23046875,338.20703125,0.015625,12.30078125,1.75390625 |
| 146 | +``` |
| 147 | + |
| 148 | +The sample result file is in **csv** format. You can open it with spreadsheet applications like Microsoft Excel for a better view and plot lines for data analysis. |
0 commit comments