Implement GetLogicalCpuCountFromOS in Vm for Linux
Currently we have an empty stub in coreclr/src/vm/util.cpp that just return 0. We need to provide proper implementation for Linux and Mac.
sysctlbyname with machdep.cpu.core_count is probably what we want on osx.
Part of dotnet/runtime#3926?
Minor correction. We likely want thread_count, instead of core_count, as this is Logical processors:
basalt:src plasma$ sysctl machdep.cpu.brand_string machdep.cpu.core_count machdep.cpu.thread_count
machdep.cpu.brand_string: Intel(R) Core(TM) i7-4578U CPU @ 3.00GHz
machdep.cpu.core_count: 2
machdep.cpu.thread_count: 4
In C++11 there is a standard library function for this: std::thread::hardware_concurrency() http://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency
Would that be sufficient?
My concern is its documented as: "If the value is not well defined or not computable, returns 0." Do we want to rely on every C++ implementation on every arch doing the right thing over a few extra lines of os specific code?
While trying it out, I realized that part of code is compiled with -nostdinc, so no standard library function available. I am in favour now of Plan B, implement it in the platform abstraction library.
@kangaroo you seem to have an idea already how to do that for Mac OS X, would you mind implement it, so I can add the Linux code?
Sure -- I'll handle this for OSX.
So the implementation is simple enough, but it does touch on a placement problem.
@sergiy-k Should we introduce a PAL_GetLogicalCpuCountFromOS so we can have the platform specific #if's there, or are we ok pulling those deps and headers into the VM?
Basically we need
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>
int main (void) {
size_t size;
int ncores = 0;
int ret = sysctlbyname("machdep.cpu.thread_count", &ncores, &size, NULL, 0);
if (ret != 0)
return -1;
printf ("%d cores\n", ncores);
return 0;
}
Likely optimized by caching the result in a static :)
I also think it would be the better solution to move that into the PAL (the Windows code should be there as well).
@kangaroo The OSX / Linux code needs to be in PAL. We cannot include any platform specific headers in any of the coreclr itself, including the VM. @janhenke The windows code cannot be in PAL since there is no PAL on windows.
PR https://github.com/dotnet/coreclr/pull/432
For Linux I am still looking for the correct system call to get the number of logical cores/hardware threads. If anybody knows more in that regard... :-)
Would this be helpful? http://www.gnu.org/software/libc/manual/html_node/Processor-Resources.html
Thanks, but I am trying to find some documentation to skip libc and make a direct syscall to the kernel to get the number of cores, similar to the OS X above.
Why don't you want to use the lib function? Is there anything wrong with it?
@janhenke Hope this would be helpful. Programmatically find the number of cores on a machine
@janvorli I am not sure how much of platforms libraries are available. At first I thought std::thread::hardware_concurrency() would work, but then I noticed coreclr is compiled with no standard includes. Thus I assumed it would be best to avoid standard libraries and implement it ourselves. If that is not the case and we can call any glibc function on Linux, indeed it makes sense to use the documented feature.
@cnblogs-dudu Thanks, that question on SO I also found during my research.
@kangaroo If we are to believe the answer given in that SO question, numCPU = sysconf( _SC_NPROCESSORS_ONLN ); is supposed to work on OS X too. We could then simplify the implementation by using this one line for both Linux and OS X. Your thoughts?
It is logical for both Linux and OSX. I'll update and squash my PR.
@janhenke The rule is simple. In coreclr, you cannot use any platform specific stuff and everything needs to be built on top of PAL (on Windows, it is different since there is no PAL - the PAL emulates the minimal needes subset of windows APIs and adds other things that we need on non-Windows systems). In the PAL, you can use anything available on the platform, but you should stay away from STL, boost and similar high level libraries. Ideal level is e.g. the libc and other libs like that. And they should ideally be present on most systems without the need to install them if possible (the libunwind is an exception that we had to accept)
Squashed and pushed. Once merged, we should be able to close this out.