forked from ComfyFluffy/Caustica
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofileMinecraft.ps1
More file actions
122 lines (103 loc) · 3.11 KB
/
Copy pathprofileMinecraft.ps1
File metadata and controls
122 lines (103 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
param(
[int]$TargetPid = 0,
[string]$RecordingName = "mc",
[string]$Settings = "profile"
)
$ErrorActionPreference = "Stop"
$jcmdCommand = Get-Command jcmd -ErrorAction SilentlyContinue
if ($null -eq $jcmdCommand -and $env:JAVA_HOME) {
$javaHomeJcmd = Join-Path $env:JAVA_HOME "bin\jcmd.exe"
if (Test-Path -LiteralPath $javaHomeJcmd) {
$jcmdCommand = Get-Command $javaHomeJcmd
}
}
if ($null -eq $jcmdCommand) {
throw "Could not find jcmd. Put a JDK bin directory on PATH or set JAVA_HOME."
}
$jcmdPath = $jcmdCommand.Source
function Invoke-Jcmd {
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$JcmdArgs
)
& $jcmdPath @JcmdArgs
if ($LASTEXITCODE -ne 0) {
throw "jcmd failed: jcmd $($JcmdArgs -join ' ')"
}
}
function Get-JcmdProcessList {
$lines = & $jcmdPath
if ($LASTEXITCODE -ne 0) {
throw "jcmd failed while listing JVMs"
}
foreach ($line in $lines) {
if ($line -match '^\s*(\d+)\s+(.+?)\s*$') {
[pscustomobject]@{
Id = [int]$matches[1]
Command = $matches[2]
Raw = $line
}
}
}
}
$processes = @(Get-JcmdProcessList)
if ($TargetPid -ne 0) {
$target = $processes | Where-Object { $_.Id -eq $TargetPid } | Select-Object -First 1
if ($null -eq $target) {
throw "Could not find JVM with PID $TargetPid."
}
} else {
$candidates = @(
$processes | Where-Object {
$_.Command -match 'net\.fabricmc\.devlaunchinjector\.Main|net\.minecraft\.client\.main\.Main|Minecraft' -and
$_.Command -notmatch 'Gradle|GradleDaemon|GradleWrapperMain|jdk\.jcmd|JCmd|JMC'
}
)
if ($candidates.Count -eq 0) {
Write-Host "No Minecraft JVM found. Current jcmd output:"
$processes | ForEach-Object { Write-Host " $($_.Raw)" }
throw "Start the Minecraft client first, or pass -TargetPid <pid>."
}
if ($candidates.Count -eq 1) {
$target = $candidates[0]
} else {
Write-Host "Multiple Minecraft-like JVMs found:"
for ($i = 0; $i -lt $candidates.Count; $i++) {
Write-Host (" [{0}] {1}" -f ($i + 1), $candidates[$i].Raw)
}
$selection = Read-Host "Select target [1]"
if ([string]::IsNullOrWhiteSpace($selection)) {
$selection = "1"
}
$index = [int]$selection - 1
if ($index -lt 0 -or $index -ge $candidates.Count) {
throw "Invalid selection: $selection"
}
$target = $candidates[$index]
}
}
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$outDir = Join-Path $PSScriptRoot "run/jfr"
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
$jfrPath = Join-Path $outDir "$RecordingName-$timestamp.jfr"
$targetPidText = [string]$target.Id
$started = $false
Write-Host "Target JVM: $($target.Raw)"
Write-Host "Recording: $jfrPath"
try {
Invoke-Jcmd $targetPidText "JFR.start" "name=$RecordingName" "settings=$Settings" "filename=$jfrPath"
$started = $true
Write-Host ""
Read-Host "Profiling started. Press Enter to stop"
} finally {
if ($started) {
Write-Host "Stopping JFR recording..."
try {
Invoke-Jcmd $targetPidText "JFR.stop" "name=$RecordingName" "filename=$jfrPath"
Write-Host "Saved JFR recording to $jfrPath"
} catch {
Write-Warning $_.Exception.Message
Write-Warning "The target JVM may have exited before the recording was stopped."
}
}
}