-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix BSD cpu count #4466
Conversation
Maybe this should leave under |
@bew There is precedence for conditional platform dependant code elsewhere, ex: |
@wmoxam from CI there are two files where the formatter is not happy. Error: formatting 'src/lib_c/amd64-unknown-openbsd/c/sysctl.cr' produced changes To clarify @bew comment, #4450 is the first PR trying to move all the platform specifics in it's own namespace and offer a better abstraction. If you want to can take the stab here in this same PR. But if not a refactor will follow probably. |
Okay good to know. Thanks @bcardiff |
I'll refactor to match #4450 |
@wmoxam that PR is now merged. You can base the refactor with that final structure. |
Thanks @bcardiff I'll have it ready later this week |
src/crystal/system/system.cr
Outdated
@@ -0,0 +1,24 @@ | |||
# :nodoc |
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.
We don't want to :nodoc:
Crystal
as we loose documenting macros and Crystal::VERSION
. The :nodoc:
should be placed on Crystal::System
only.
src/crystal/system/system.cr
Outdated
# :nodoc | ||
module System | ||
# :nodoc | ||
module System |
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 think that the methods in this module could go in just Crysatal::System
itself.
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.
Like replace src/system.cr
with this file?
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.
no keep the files the same but place the methods in Crystal::System
not Crystal::System::System
. It seems redundant and these are "misc" methods which can just go in Crystal::System
itself.
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.
IMO these are not "misc" methods, they are "System" (aka local computer system) utility methods for querying a local system's properties.
My understanding of the structure is that Crystal::System
is simply a wrapper namespace for platform specific implementations.
Crystal::System::System
is meant to contain platform specific implementations of the "System" methods.
It's confusing for sure since we're using the word 'System" for two different things. Perhaps a there is a different name that is more suitable?
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.
What about Crystal::System::Inspect
?
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.
@bcardiff If we changed that name we'd have to change the name of System
to Inspect
which would make little sense. I'd rather have bit of duplication than a nonsensical name.
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.
It's true there is no much value for some inspection method to be in it's own module for now.
Let's go with @RX14 proposal. So,
src/crystal/system.cr
with the if flags to includesrc/crystal/{platform}/system.cr
/etc.src/crystal/{platform}/system.cr
/etc. with the actual calls to lib C.- Leave
src/system.cr
withrequire "crystal/system"
and delagation toCrystal::System.hostname
.
::System
is the public api with documentation.
::Crystal::System
is :nodoc:
. We can refactor later if many method end up there.
Otherwise it will affect documentation elsewhere
src/crystal/system.cr
Outdated
# :nodoc | ||
module System | ||
# Returns the hostname | ||
# def.self.hostname |
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.
Typo: .
instead of
src/crystal/system.cr
Outdated
require "./system/unix/sysctl_cpucount" | ||
{% else %} | ||
# TODO: restrict on flag?(:unix) after crystal > 0.22.0 is released | ||
require "./system/unix/sysconf_cpucount" |
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.
The indentation on these two lines is off.
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.
Weird that the formatter didn't pick up on this
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.
@wmoxam it's because the formatter doesn't format macro code
raise Errno.new("Could not get hostname") | ||
end | ||
len = LibC.strlen(buffer) | ||
{len, len} |
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 😅
if LibC.sysctl(mib, 2, pointerof(ncpus), pointerof(size), nil, 0) == 0 | ||
ncpus | ||
else | ||
-1 |
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.
Don't we want to raise here?
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.
Possibly? I was just matching the behaviour of the existing sysconf
version
Is that the behaviour of other standard Crystal libs, or do they just rely on return value?
|
||
module Crystal::System | ||
def self.cpu_count | ||
LibC.sysconf(LibC::SC_NPROCESSORS_ONLN) |
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.
Similarly, we should check the return value and raise.
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 think this could be part of a separate PR as the return value isn't checked elsewhere. Ex: Process.times
If changes to the API are required it may later in another pull request, but this shouldn't delay this patch which fixes issues on *BSD. |
I forgot that this was just a straight copy of the existing behaviour... Returning |
Build #4466 errored on osx with xcode8.2, again... Would it be a good time to merge #4528? |
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.
LGTM. Should be merged as a single commit.
System.cpu_count
always returns -1 on BSD since fetching the number of CPUs viasysconf
is not supported.Instead use
sysctl
to fetch cpu count.