Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions spec/std/system_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ require "system"
describe System do
describe "hostname" do
it "returns current hostname" do
shell_hostname = `hostname`.strip
$?.success?.should be_true # The hostname command has to be available
shell_hostname = `hostname`.strip # Workaround for inability to execute shell commands on Windows.
$?.success?.should be_true # The hostname command has to be available
hostname = System.hostname
hostname.should eq(shell_hostname)
end
end

describe "cpu_count" do
it "returns current CPU count" do
shell_cpus = `getconf _NPROCESSORS_ONLN 2>/dev/null || nproc --all 2>/dev/null || grep -sc '^processor' /proc/cpuinfo || sysctl -n hw.ncpu 2>/dev/null`.to_i
shell_cpus = 0
{% if flag?(:win32) %}
shell_cpus = ENV["NUMBER_OF_PROCESSORS"].to_i
{% elsif flag?(:unix) %}
shell_cpus = `getconf _NPROCESSORS_ONLN 2>/dev/null || nproc --all 2>/dev/null || grep -sc '^processor' /proc/cpuinfo || sysctl -n hw.ncpu 2>/dev/null`.to_i
{% end %}
cpu_count = System.cpu_count
cpu_count.should eq(shell_cpus)
end
Expand Down
15 changes: 11 additions & 4 deletions src/crystal/system.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ module Crystal::System
# def self.cpu_count
end

require "./system/unix/hostname"
{% if flag?(:unix) %}
require "./system/unix/hostname"

{% if flag?(:freebsd) || flag?(:openbsd) %}
{% if flag?(:freebsd) || flag?(:openbsd) %}
require "./system/unix/sysctl_cpucount"
{% elsif flag?(:unix) %}
require "./system/unix/sysconf_cpucount"
{% else %}
require "./system/unix/sysconf_cpucount"
{% end %}
{% elsif flag?(:win32) %}
require "./system/win32/hostname"
require "./system/win32/cpucount"
{% else %}
{% raise "No Crystal::System implementation available" %}
{% end %}
8 changes: 8 additions & 0 deletions src/crystal/system/win32/cpucount.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "c/sysinfoapi"

module Crystal::System
def self.cpu_count
LibC.GetNativeSystemInfo(out system_info)
system_info.dwNumberOfProcessors
end
end
16 changes: 16 additions & 0 deletions src/crystal/system/win32/hostname.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "c/sysinfoapi"

module Crystal::System
def self.hostname
retry_wstr_buffer do |buffer, small_buf|
name_size = buffer.size.to_u
if LibC.GetComputerNameExW(LibC::COMPUTER_NAME_FORMAT::ComputerNameDnsHostname, buffer, pointerof(name_size)) != 0
break String.from_utf16(buffer[0, name_size])
elsif small_buf && name_size > 0
next name_size
else
raise WinError.new("GetComputerNameExW")
end
end
end
end
45 changes: 45 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/sysinfoapi.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "c/winnt"
require "c/win_def"
require "c/int_safe"

lib LibC
fun GetNativeSystemInfo(system_info : SYSTEM_INFO*)

struct PROCESSOR_INFO
wProcessorArchitecture : WORD
wReserved : WORD
end

union OEM_PROCESSOR_INFO
dwOemId : DWORD
processorInfo : PROCESSOR_INFO
end

struct SYSTEM_INFO
oemProcessorInfo : OEM_PROCESSOR_INFO
dwPageSize : DWORD
lpMinimumApplicationAddress : Void*
lpMaximumApplicationAddress : Void*
dwActiveProcessorMask : DWORD*
dwNumberOfProcessors : DWORD
dwProcessorType : DWORD
dwAllocationGranularity : DWORD
wProcessorLevel : WORD
wProcessorRevision : WORD
end

fun GetComputerNameExW(computer_name_format : COMPUTER_NAME_FORMAT,
machine_name : LPWSTR,
machine_name_size : DWORD*) : BOOLEAN

enum COMPUTER_NAME_FORMAT
ComputerNameNetBIOS
ComputerNameDnsHostname
ComputerNameDnsDomain
ComputerNameDnsFullyQualified
ComputerNamePhysicalNetBIOS
ComputerNamePhysicalDnsHostname
ComputerNamePhysicalDomain
ComputerNamePhysicalDnsFullyQualified
end
end