-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use sysctl to return the number of CPUs on BSD systems See: http://man.openbsd.org/sysctl.3 https://www.freebsd.org/cgi/man.cgi?query=sysctl&sektion=3
- Loading branch information
Wesley Moxam
authored and
Martin Verzilli
committed
Jun 15, 2017
1 parent
74b1bb4
commit 11e76e4
Showing
10 changed files
with
91 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Crystal | ||
# :nodoc | ||
module System | ||
# Returns the hostname | ||
# def self.hostname | ||
|
||
# Returns the number of logical processors available to the system. | ||
# | ||
# def self.cpu_count | ||
end | ||
end | ||
|
||
require "./system/unix/hostname" | ||
|
||
{% if flag?(:freebsd) || flag?(:openbsd) %} | ||
require "./system/unix/sysctl_cpucount" | ||
{% else %} | ||
# TODO: restrict on flag?(:unix) after crystal > 0.22.0 is released | ||
require "./system/unix/sysconf_cpucount" | ||
{% end %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "c/unistd" | ||
|
||
module Crystal::System | ||
def self.hostname | ||
String.new(255) do |buffer| | ||
unless LibC.gethostname(buffer, LibC::SizeT.new(255)) == 0 | ||
raise Errno.new("Could not get hostname") | ||
end | ||
len = LibC.strlen(buffer) | ||
{len, len} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% skip_file if flag?(:openbsd) || flag?(:freebsd) %} | ||
|
||
require "c/unistd" | ||
|
||
module Crystal::System | ||
def self.cpu_count | ||
LibC.sysconf(LibC::SC_NPROCESSORS_ONLN) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{% skip_file unless flag?(:openbsd) || flag?(:freebsd) %} | ||
|
||
require "c/sysctl" | ||
|
||
module Crystal::System | ||
def self.cpu_count | ||
mib = Int32[LibC::CTL_HW, LibC::HW_NCPU] | ||
ncpus = 0 | ||
size = sizeof(Int32).to_u64 | ||
|
||
if LibC.sysctl(mib, 2, pointerof(ncpus), pointerof(size), nil, 0) == 0 | ||
ncpus | ||
else | ||
-1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
lib LibC | ||
CTL_HW = 6 | ||
HW_NCPU = 3 | ||
|
||
fun sysctl(name : Int*, namelen : UInt, oldp : Void*, oldlenp : SizeT*, newp : Void*, newlen : SizeT) : Int | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
lib LibC | ||
CTL_HW = 6 | ||
CTL_KERN = 1 | ||
HW_NCPU = 3 | ||
KERN_PROC = 14 | ||
KERN_PROC_PATHNAME = 12 | ||
PATH_MAX = 1024 | ||
|
||
fun sysctl(name : Int*, namelen : UInt, oldp : Void*, oldlenp : SizeT*, newp : Void*, newlen : SizeT) : Int | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters