-
Notifications
You must be signed in to change notification settings - Fork 770
Fix heartbeat monitoring on Windows - replace deprecated wmic with PowerShell #13555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
428f185
47d30e6
e66a552
f1024e6
fcec27b
1ac9379
dd4c55a
14692f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -167,14 +167,15 @@ string GetCpuUsage(ref long prevIdle, ref long prevTotal, ref TimeSpan prevCpu, | |||||||||
| } | ||||||||||
| else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||||||||||
| { | ||||||||||
| // Use wmic for Windows | ||||||||||
| var (success, output) = RunCommand("wmic", "cpu get loadpercentage /value"); | ||||||||||
| // Use PowerShell for Windows (wmic is deprecated) | ||||||||||
| // Get average CPU load across all processors | ||||||||||
| var (success, output) = RunCommand("powershell", "-NoProfile -NonInteractive -Command \"(Get-CimInstance Win32_Processor | Measure-Object -Property LoadPercentage -Average).Average\""); | ||||||||||
| if (success) | ||||||||||
| { | ||||||||||
| var match = System.Text.RegularExpressions.Regex.Match(output, @"LoadPercentage=(\d+)"); | ||||||||||
| if (match.Success) | ||||||||||
| var trimmed = output.Trim(); | ||||||||||
| if (double.TryParse(trimmed, out var loadPercentage)) | ||||||||||
| { | ||||||||||
| return $"{match.Groups[1].Value}%"; | ||||||||||
| return $"{loadPercentage:F1}%"; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
@@ -254,19 +255,16 @@ long GetPages(string key) => | |||||||||
| } | ||||||||||
| else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||||||||||
| { | ||||||||||
| // Use GC memory info as a baseline, plus wmic for system memory | ||||||||||
| var (success, output) = RunCommand("wmic", "OS get FreePhysicalMemory,TotalVisibleMemorySize /value"); | ||||||||||
| // Use PowerShell for Windows (wmic is deprecated) | ||||||||||
| var (success, output) = RunCommand("powershell", "-NoProfile -NonInteractive -Command \"$os = Get-CimInstance Win32_OperatingSystem; Write-Host \\\"$($os.FreePhysicalMemory),$($os.TotalVisibleMemorySize)\\\"\""); | ||||||||||
| if (success) | ||||||||||
| { | ||||||||||
| var freeMatch = System.Text.RegularExpressions.Regex.Match(output, @"FreePhysicalMemory=(\d+)"); | ||||||||||
| var totalMatch = System.Text.RegularExpressions.Regex.Match(output, @"TotalVisibleMemorySize=(\d+)"); | ||||||||||
|
|
||||||||||
| if (freeMatch.Success && totalMatch.Success) | ||||||||||
| var parts = output.Trim().Split(','); | ||||||||||
| if (parts.Length == 2 && | ||||||||||
| long.TryParse(parts[0].Trim(), out var freeKb) && | ||||||||||
| long.TryParse(parts[1].Trim(), out var totalKb)) | ||||||||||
|
Comment on lines
+264
to
+265
|
||||||||||
| long.TryParse(parts[0].Trim(), out var freeKb) && | |
| long.TryParse(parts[1].Trim(), out var totalKb)) | |
| long.TryParse(parts[0].Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out var freeKb) && | |
| long.TryParse(parts[1].Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out var totalKb)) |
Copilot
AI
Dec 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The int.TryParse and long.TryParse calls should specify NumberStyles and CultureInfo.InvariantCulture to ensure consistent parsing across different system locales. This prevents potential parsing failures on systems with non-English regional settings. Change to int.TryParse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out var pid) and long.TryParse(parts[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out var workingSet) for consistency with other parsing operations in this file.
| int.TryParse(parts[1], out var pid) && | |
| long.TryParse(parts[2], out var workingSet)) | |
| int.TryParse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out var pid) && | |
| long.TryParse(parts[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out var workingSet)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
double.TryParsecall should specifyCultureInfo.InvariantCultureto ensure consistent parsing across different system locales. PowerShell may output decimal numbers with culture-specific separators (e.g., comma vs. period), which could cause parsing to fail on systems with non-English locales. Change todouble.TryParse(trimmed, NumberStyles.Float, CultureInfo.InvariantCulture, out var loadPercentage)for consistency with other parsing operations in this file (see lines 130, 194, 222).