[pypy-commit] pypy default: test + impl cpu_count in rposix module
plan_rich
pypy.commits at gmail.com
Fri Oct 14 10:03:46 EDT 2016
Author: Richard Plangger <planrichi at gmail.com>
Branch:
Changeset: r87789:d485def1cc89
Date: 2016-10-14 16:00 +0200
http://bitbucket.org/pypy/pypy/changeset/d485def1cc89/
Log: test + impl cpu_count in rposix module
diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -2305,3 +2305,47 @@
self.cached_nosys = -1
_pipe2_syscall = ENoSysCache()
+
+# cpu count for linux, windows and mac (+ bsds)
+# note that the code is copied from cpython and split up here
+if sys.platform.startswith('linux'):
+ cpucount_eci = ExternalCompilationInfo(includes=["unistd.h"],
+ separate_module_sources=["""
+ RPY_EXTERN int _cpu_count(void) {
+ return sysconf(_SC_NPROCESSORS_ONLN);
+ }
+ """])
+elif sys.platform == "win32":
+ cpucount_eci = ExternalCompilationInfo(includes=["Windows.h"],
+ separate_module_sources=["""
+ RPY_EXTERN int _cpu_count(void) {
+ int ncpu = 0;
+ SYSTEM_INFO sysinfo;
+ GetSystemInfo(&sysinfo);
+ return sysinfo.dwNumberOfProcessors;
+ }
+ """])
+else:
+ cpucount_eci = ExternalCompilationInfo(includes=["sys/types.h", "sys/sysctl.h"],
+ separate_module_sources=["""
+ RPY_EXTERN int _cpu_count(void) {
+ int ncpu = 0;
+ #if defined(__DragonFly__) || \
+ defined(__OpenBSD__) || \
+ defined(__FreeBSD__) || \
+ defined(__NetBSD__) || \
+ defined(__APPLE__)
+ int mib[2];
+ size_t len = sizeof(ncpu);
+ mib[0] = CTL_HW;
+ mib[1] = HW_NCPU;
+ if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
+ ncpu = 0;
+ #endif
+ return ncpu;
+ }
+ """])
+
+cpu_count = rffi.llexternal('_cpu_count', [], rffi.INT_real,
+ compilation_info=cpucount_eci)
+
diff --git a/rpython/rlib/test/test_rposix.py b/rpython/rlib/test/test_rposix.py
--- a/rpython/rlib/test/test_rposix.py
+++ b/rpython/rlib/test/test_rposix.py
@@ -608,3 +608,7 @@
def test_sync():
if sys.platform != 'win32':
rposix.sync()
+
+def test_cpu_count():
+ cc = rposix.cpu_count()
+ assert cc >= 1
More information about the pypy-commit
mailing list