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

Fix BSD cpu count #4466

Merged
merged 15 commits into from Jun 15, 2017
Merged

Fix BSD cpu count #4466

merged 15 commits into from Jun 15, 2017

Conversation

wmoxam
Copy link
Contributor

@wmoxam wmoxam commented May 26, 2017

System.cpu_count always returns -1 on BSD since fetching the number of CPUs via sysconf is not supported.

Instead use sysctl to fetch cpu count.

@bew
Copy link
Contributor

bew commented May 26, 2017

Maybe this should leave under Crystal::System as it's a function with plateform dependent implementation?

@wmoxam
Copy link
Contributor Author

wmoxam commented May 26, 2017

@bew There is precedence for conditional platform dependant code elsewhere, ex: src/process/executable_path.cr

@bcardiff
Copy link
Member

@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
Error: formatting 'src/lib_c/x86_64-portbld-freebsd/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.

@wmoxam
Copy link
Contributor Author

wmoxam commented May 26, 2017

Okay good to know. Thanks @bcardiff

@wmoxam
Copy link
Contributor Author

wmoxam commented May 26, 2017

I'll refactor to match #4450

@bcardiff
Copy link
Member

@wmoxam that PR is now merged. You can base the refactor with that final structure.
Having if flag to select which file to include and also use a skip_file macro in each file.

@wmoxam
Copy link
Contributor Author

wmoxam commented May 30, 2017

Thanks @bcardiff

I'll have it ready later this week

@wmoxam wmoxam changed the base branch from master to collections_type_restrictions June 5, 2017 00:51
@wmoxam wmoxam changed the base branch from collections_type_restrictions to master June 5, 2017 00:51
@@ -0,0 +1,24 @@
# :nodoc
Copy link
Contributor

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.

# :nodoc
module System
# :nodoc
module System
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

@RX14 RX14 Jun 5, 2017

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.

Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Contributor

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.

Copy link
Member

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,

  1. src/crystal/system.cr with the if flags to include src/crystal/{platform}/system.cr/etc.
  2. src/crystal/{platform}/system.cr/etc. with the actual calls to lib C.
  3. Leave src/system.cr with require "crystal/system" and delagation to Crystal::System.hostname.

::System is the public api with documentation.
::Crystal::System is :nodoc:. We can refactor later if many method end up there.

Wesley Moxam and others added 2 commits June 5, 2017 15:58
Otherwise it will affect documentation elsewhere
@wmoxam
Copy link
Contributor Author

wmoxam commented Jun 13, 2017

@RX14 @bcardiff How does this look now?

# :nodoc
module System
# Returns the hostname
# def.self.hostname
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: . instead of

require "./system/unix/sysctl_cpucount"
{% else %}
# TODO: restrict on flag?(:unix) after crystal > 0.22.0 is released
require "./system/unix/sysconf_cpucount"
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

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}
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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)
Copy link
Contributor

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.

Copy link
Contributor Author

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

@ysbaddaden
Copy link
Contributor

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.

@RX14
Copy link
Contributor

RX14 commented Jun 13, 2017

I forgot that this was just a straight copy of the existing behaviour... Returning -1 would be something to change in the future though.

@Sija
Copy link
Contributor

Sija commented Jun 13, 2017

Build #4466 errored on osx with xcode8.2, again... Would it be a good time to merge #4528?

Copy link
Member

@bcardiff bcardiff left a 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.

@mverzilli mverzilli merged commit 11e76e4 into crystal-lang:master Jun 15, 2017
@mverzilli mverzilli added this to the Next milestone Jun 15, 2017
@wmoxam wmoxam deleted the fix-bsd-cpu-count branch June 16, 2017 04:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants