runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Implement GetLogicalCpuCountFromOS in Vm for Linux

Open sergiy-k opened this issue 11 years ago • 22 comments

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.

sergiy-k avatar Feb 11 '15 05:02 sergiy-k

sysctlbyname with machdep.cpu.core_count is probably what we want on osx.

kangaroo avatar Feb 11 '15 05:02 kangaroo

Part of dotnet/runtime#3926?

linquize avatar Feb 12 '15 11:02 linquize

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

kangaroo avatar Feb 15 '15 02:02 kangaroo

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?

janhenke avatar Mar 09 '15 22:03 janhenke

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?

kangaroo avatar Mar 10 '15 01:03 kangaroo

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?

janhenke avatar Mar 10 '15 09:03 janhenke

Sure -- I'll handle this for OSX.

kangaroo avatar Mar 10 '15 21:03 kangaroo

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 :)

kangaroo avatar Mar 11 '15 05:03 kangaroo

I also think it would be the better solution to move that into the PAL (the Windows code should be there as well).

janhenke avatar Mar 11 '15 07:03 janhenke

@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.

janvorli avatar Mar 11 '15 09:03 janvorli

PR https://github.com/dotnet/coreclr/pull/432

kangaroo avatar Mar 11 '15 17:03 kangaroo

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... :-)

janhenke avatar Mar 12 '15 14:03 janhenke

Would this be helpful? http://www.gnu.org/software/libc/manual/html_node/Processor-Resources.html

janvorli avatar Mar 12 '15 14:03 janvorli

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.

janhenke avatar Mar 12 '15 14:03 janhenke

Why don't you want to use the lib function? Is there anything wrong with it?

janvorli avatar Mar 12 '15 14:03 janvorli

@janhenke Hope this would be helpful. Programmatically find the number of cores on a machine

cnblogs-dudu avatar Mar 12 '15 15:03 cnblogs-dudu

@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?

janhenke avatar Mar 12 '15 16:03 janhenke


It is logical for both Linux and OSX.  I'll update and squash my PR.

kangaroo avatar Mar 12 '15 17:03 kangaroo

@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)

janvorli avatar Mar 12 '15 17:03 janvorli

Squashed and pushed. Once merged, we should be able to close this out.

kangaroo avatar Mar 12 '15 17:03 kangaroo