Skip to content
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

Implement System for Win32 #6972

Merged
merged 14 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion spec/std/system_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ describe System do

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 =
{% if flag?(:win32) %}
ENV["NUMBER_OF_PROCESSORS"].to_i
{% elsif flag?(:unix) %}
`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
17 changes: 12 additions & 5 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) %}
require "./system/unix/sysctl_cpucount"
{% elsif flag?(:unix) %}
require "./system/unix/sysconf_cpucount"
{% if flag?(:freebsd) || flag?(:openbsd) %}
require "./system/unix/sysctl_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 = LibC::DWORD.new(buffer.size)
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
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
ComputerNameDnsDomain
ComputerNameDnsFullyQualified
ComputerNamePhysicalNetBIOS
ComputerNamePhysicalDnsHostname
ComputerNamePhysicalDomain
ComputerNamePhysicalDnsFullyQualified
end
end