Skip to content

Commit

Permalink
Remove dependency on win32ole
Browse files Browse the repository at this point in the history
This will become bundled in Ruby 3.5

Unfortunately there is no portable way of checking for this.
The wmic command is deprecated, though I don't observe this myself on W11 (yet?)
  • Loading branch information
Earlopain authored and eregon committed Jun 7, 2024
1 parent 22f337c commit 393ad72
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/concurrent-ruby/concurrent/utility/processor_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,20 @@ def compute_physical_processor_count
end
cores.count
when /mswin|mingw/
require 'win32ole'
result_set = WIN32OLE.connect("winmgmts://").ExecQuery(
"select NumberOfCores from Win32_Processor")
result_set.to_enum.collect(&:NumberOfCores).reduce(:+)
# Get-CimInstance introduced in PowerShell 3 or earlier: https://learn.microsoft.com/en-us/previous-versions/powershell/module/cimcmdlets/get-ciminstance?view=powershell-3.0
result = run('powershell -command "Get-CimInstance -ClassName Win32_Processor | Select-Object -Property NumberOfCores"')
if !result || $?.exitstatus != 0
# fallback to deprecated wmic for older systems
result = run("wmic cpu get NumberOfCores")
end
if !result || $?.exitstatus != 0
# Bail out if both commands returned something unexpected
processor_count
else
# powershell: "\nNumberOfCores\n-------------\n 4\n\n\n"
# wmic: "NumberOfCores \n\n4 \n\n\n\n"
result.scan(/\d+/).map(&:to_i).reduce(:+)
end
else
processor_count
end
Expand All @@ -81,6 +91,11 @@ def compute_physical_processor_count
return 1
end

def run(command)
IO.popen(command, &:read)
rescue Errno::ENOENT
end

def compute_cpu_quota
if RbConfig::CONFIG["target_os"].include?("linux")
if File.exist?("/sys/fs/cgroup/cpu.max")
Expand Down

0 comments on commit 393ad72

Please sign in to comment.