move Application#determine_n_threads -> Util.determine_n_threads

This commit is contained in:
Josh Holtrop 2019-04-14 15:49:46 -04:00
parent 0c07ea0437
commit 3115e55739
4 changed files with 100 additions and 100 deletions

View File

@ -21,7 +21,7 @@ module Rscons
# Create Application instance.
def initialize
@n_threads = determine_n_threads
@n_threads = Util.determine_n_threads
@vars = VarSet.new
end
@ -157,39 +157,6 @@ module Rscons
rv
end
# Determine the number of threads to use by default.
#
# @return [Integer]
# The number of threads to use by default.
def determine_n_threads
# If the user specifies the number of threads in the environment, then
# respect that.
if ENV["RSCONS_NTHREADS"] =~ /^(\d+)$/
return $1.to_i
end
# Otherwise try to figure out how many threads are available on the
# host hardware.
begin
case RbConfig::CONFIG["host_os"]
when /linux/
return File.read("/proc/cpuinfo").scan(/^processor\s*:/).size
when /mswin|mingw/
if `wmic cpu get NumberOfLogicalProcessors /value` =~ /NumberOfLogicalProcessors=(\d+)/
return $1.to_i
end
when /darwin/
if `sysctl -n hw.ncpu` =~ /(\d+)/
return $1.to_i
end
end
rescue
end
# If we can't figure it out, default to 1.
1
end
end
end

View File

@ -26,6 +26,39 @@ module Rscons
command.map { |c| c =~ /\s/ ? "'#{c}'" : c }.join(' ')
end
# Determine the number of threads to use by default.
#
# @return [Integer]
# The number of threads to use by default.
def determine_n_threads
# If the user specifies the number of threads in the environment, then
# respect that.
if ENV["RSCONS_NTHREADS"] =~ /^(\d+)$/
return $1.to_i
end
# Otherwise try to figure out how many threads are available on the
# host hardware.
begin
case RbConfig::CONFIG["host_os"]
when /linux/
return File.read("/proc/cpuinfo").scan(/^processor\s*:/).size
when /mswin|mingw/
if `wmic cpu get NumberOfLogicalProcessors /value` =~ /NumberOfLogicalProcessors=(\d+)/
return $1.to_i
end
when /darwin/
if `sysctl -n hw.ncpu` =~ /(\d+)/
return $1.to_i
end
end
rescue
end
# If we can't figure it out, default to 1.
1
end
# Return a string showing the path specified, or if more than one, then
# the first path with a "(+D)" afterward, where D is the number of
# remaining paths.

View File

@ -26,71 +26,5 @@ module Rscons
end
end
describe ".determine_n_threads" do
context "when specified by environment variable" do
before(:each) do
expect(ENV).to receive(:[]).with("RSCONS_NTHREADS").and_return("3")
end
it "returns the user-specified number of threads to use" do
expect(Rscons.application.__send__(:determine_n_threads)).to eq(3)
end
end
context "when not specified by environment variable" do
before(:each) do
expect(ENV).to receive(:[]).with("RSCONS_NTHREADS").and_return(nil)
end
context "on Linux" do
before(:each) do
expect(RbConfig::CONFIG).to receive(:[]).with("host_os").and_return("linux")
end
it "returns the number of processors from /proc/cpuinfo" do
expect(File).to receive(:read).with("/proc/cpuinfo").and_return(<<EOF)
processor : 0
processor : 1
EOF
expect(Rscons.application.__send__(:determine_n_threads)).to eq(2)
end
end
context "on Windows" do
before(:each) do
expect(RbConfig::CONFIG).to receive(:[]).with("host_os").and_return("mingw")
end
it "returns the number of logical processors that wmic reports" do
expect(Rscons.application).to receive(:`).with("wmic cpu get NumberOfLogicalProcessors /value").and_return("NumberOfLogicalProcessors=7")
expect(Rscons.application.__send__(:determine_n_threads)).to eq(7)
end
end
context "on Darwin" do
before(:each) do
expect(RbConfig::CONFIG).to receive(:[]).with("host_os").and_return("darwin")
end
it "returns the number of threads that sysctl reports" do
expect(Rscons.application).to receive(:`).with("sysctl -n hw.ncpu").and_return("6")
expect(Rscons.application.__send__(:determine_n_threads)).to eq(6)
end
end
context "on an unknown platform" do
before(:each) do
expect(RbConfig::CONFIG).to receive(:[]).with("host_os").and_return("other")
end
it "returns 1" do
expect(Rscons.application.__send__(:determine_n_threads)).to eq(1)
end
end
context "when an error occurs" do
it "returns 1" do
expect(RbConfig::CONFIG).to receive(:[]).with("host_os").and_raise("foo")
expect(Rscons.application.__send__(:determine_n_threads)).to eq(1)
end
end
end
end
end
end

View File

@ -29,6 +29,72 @@ module Rscons
end
end
describe ".determine_n_threads" do
context "when specified by environment variable" do
before(:each) do
expect(ENV).to receive(:[]).with("RSCONS_NTHREADS").and_return("3")
end
it "returns the user-specified number of threads to use" do
expect(Util.determine_n_threads).to eq(3)
end
end
context "when not specified by environment variable" do
before(:each) do
expect(ENV).to receive(:[]).with("RSCONS_NTHREADS").and_return(nil)
end
context "on Linux" do
before(:each) do
expect(RbConfig::CONFIG).to receive(:[]).with("host_os").and_return("linux")
end
it "returns the number of processors from /proc/cpuinfo" do
expect(File).to receive(:read).with("/proc/cpuinfo").and_return(<<EOF)
processor : 0
processor : 1
EOF
expect(Util.determine_n_threads).to eq(2)
end
end
context "on Windows" do
before(:each) do
expect(RbConfig::CONFIG).to receive(:[]).with("host_os").and_return("mingw")
end
it "returns the number of logical processors that wmic reports" do
expect(Util).to receive(:`).with("wmic cpu get NumberOfLogicalProcessors /value").and_return("NumberOfLogicalProcessors=7")
expect(Util.determine_n_threads).to eq(7)
end
end
context "on Darwin" do
before(:each) do
expect(RbConfig::CONFIG).to receive(:[]).with("host_os").and_return("darwin")
end
it "returns the number of threads that sysctl reports" do
expect(Util).to receive(:`).with("sysctl -n hw.ncpu").and_return("6")
expect(Util.determine_n_threads).to eq(6)
end
end
context "on an unknown platform" do
before(:each) do
expect(RbConfig::CONFIG).to receive(:[]).with("host_os").and_return("other")
end
it "returns 1" do
expect(Util.determine_n_threads).to eq(1)
end
end
context "when an error occurs" do
it "returns 1" do
expect(RbConfig::CONFIG).to receive(:[]).with("host_os").and_raise("foo")
expect(Util.determine_n_threads).to eq(1)
end
end
end
end
describe ".find_executable" do
context "when given a path with directory components" do
it "returns the path if it is executable" do