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

OS Info #4226

Closed
sdogruyol opened this issue Apr 1, 2017 · 3 comments
Closed

OS Info #4226

sdogruyol opened this issue Apr 1, 2017 · 3 comments

Comments

@sdogruyol
Copy link
Member

Not sure if this issue is opened before.
It'd be great to have a way of accessing OS specific info from Crystal.
For example accessing how many number of CPU cores e.g.

Here's a Stack Overflow question asking that http://stackoverflow.com/questions/43147366/how-to-detect-number-of-cpu-cores-in-crystal

@miketheman
Copy link
Contributor

#codetriage
This is quite an interesting question- there are a couple of approaches one might have here.

  • Do we have a reliable method to detect what OS/Kernel we're running on? I see calls to flag? that we might switch on for darwin, linux, and others.
  • Would this live in the System namespace? Currently only houses #hostname. I think so.
  • Is it acceptable to have a stdlib method shell out to get the values?

One simple C approach that works on my simple tests on Linux & Darwin:

#include "stdio.h"
#include "unistd.h"

int main(void) {
  int ncpu = sysconf(_SC_NPROCESSORS_ONLN);

  printf("%d\n", ncpu);
  return 0;
}

I also read that the sysctl interface is not as portable, so I stopped pursuing that approach. http://man7.org/linux/man-pages/man2/sysctl.2.html#CONFORMING_TO

Looking at the existing LibC.sysconf, I'm not sure how I would pass in the name, the interface appears to only accept an Int, so I'm a bit lost there.

@TheLonelyGhost
Copy link
Contributor

TheLonelyGhost commented May 22, 2017

The problem with CPU checking is that the method of getting the number of CPUs is pretty fragmented across Mac and Linux. I'm not sure shelling out for this command alone would be the best or most reliable option.

I'm currently looking into Rust's source code to see how they do it. So far I've tested a reliable way of pulling the Int values (found from Rust source code) to put into LibC.sysconf for each platform. The mapping of Ints to some name depends on the platform, so some macros with flag?() are necessary, similar to what is shown in the Crystal book.

For easier testing, I've been working on this as a separate shard, which can later be integrated into the standard library. https://github.com/TheLonelyGhost/crystal-sysinfo

Besides number of CPUs, what information about the platform should we check for?

@asterite
Copy link
Member

Closed by #4449

If other specific OS info is needed, either send a PR or (preferably) open a new issue. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants