-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix BSD cpu count #4466
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
Fix BSD cpu count #4466
Changes from all commits
675442d
5cab9c8
d77e265
6919126
6e19fbd
0f3e387
c526950
71bf419
f3f87ce
910e945
df3b5b3
cdba538
b334f68
1b80956
a67cf46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 %} |
| 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 | ||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, we should check the return value and raise.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be part of a separate PR as the return value isn't checked elsewhere. Ex: |
||
| end | ||
| end | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we want to raise here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly? I was just matching the behaviour of the existing Is that the behaviour of other standard Crystal libs, or do they just rely on return value? |
||
| end | ||
| end | ||
| end | ||
| 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 |
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure this string doesn't contain utf8?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't implement this, I only moved it 😅