Campbell Barton writes: > Hi, this patch adds support for accessing the number of CPU's / cores > on a system, matching CPython's multiprocessing.cpu_count() [0]. > > I've only tested this for Linux, this includes code that should work > on other platforms, although that would need to be double-checked of > course. > For reference I checked CPython / Blender & Stack-overflow [1] I can confirm it works on OpenBSD too, but needs tweaking. Some comments inline and attaching an updated patch > Accessing this information can be useful to automatically detect the > number of jobs to run. > > > [0]: > https://docs.python.org/3/library/multiprocessing.html#multiprocessing.cpu_count > [1]: https://stackoverflow.com/a/3006416/432509 > > commit 7be53f10f3df3c3183cc97d6bbadb78ebb61e8d2 > Author: Campbell Barton > Date: Sun Oct 10 10:16:47 2021 +1100 > > Support accessing the number of CPU's. > > Add (processor-count) for accessing the number of cores/CPU's. > > diff --git a/src/emacs.c b/src/emacs.c > index 866e43fda9..26e2f6b1f2 100644 > --- a/src/emacs.c > +++ b/src/emacs.c > @@ -3156,6 +3156,38 @@ DEFUN ("daemon-initialized", Fdaemon_initialized, Sdaemon_initialized, 0, 0, 0, > return Qt; > } > > +DEFUN ("processor-count", Fprocessor_count, Sprocessor_count, 0, 0, 0, > + doc: /* Return the number of CPUs in the system. > + > +The value will always be above zero, 1 for unsupported systems. */) > + (void) > +{ > + int nproc = -1; > +#ifdef WINDOWSNT > + SYSTEM_INFO info; > + GetSystemInfo(&info); > + nproc = (int)info.dwNumberOfProcessors; > +#elif defined (__APPLE__) || \ > + defined (__OpenBSD__) || \ > + defined (__FreeBSD__) || \ > + defined (__NetBSD__) || \ > + defined (__DragonFly__) > + int mib[2]; > + size_t len; > + > + mib[0] = CTL_HW; the #include is missing. > + mib[1] = HW_NCPU; at least on OpenBSD this should be HW_NCPUONLINE. OpenBSD disables hyperthreading by default so HW_NCPU is (almost) always misleading. For example, on my machine % uname -a OpenBSD venera 7.0 GENERIC.MP#221 amd64 % sysctl hw.ncpu hw.ncpu=8 % sysctl hw.ncpuonline hw.ncpuonline=4 and this has been the case for a while already (I mean, a couple of years if not more.) I don't have access to other BSDs other than OpenBSD, but judging from the manpages online. - NetBSD has hw.ncpuonline - DragonFly and FreeBSD have only hw.ncpu - apple I don't know how to check. man.apple.com doesn't seem to exists ^^" > + len = sizeof(nproc); > + sysctl(mib, 2, &nproc, &len, nullptr, 0); ^^^^^^^ shouldn't this be NULL? > +#elif defined (__hpux) > + nproc = mpctl(MPC_GETNUMSPUS, NULL, NULL); > +#elif defined (_SC_NPROCESSORS_ONLN) > + nproc = (int)sysconf(_SC_NPROCESSORS_ONLN); > +#endif > + > + return make_fixnum (MAX(nproc, 1)); emacs.c:3188:23: warning: implicit declaration of function 'MAX' is invalid in C99 [-Wimplicit-function-declaration] I'm attaching an updated patch. Note that I'm not sure if including sys/types.h and sys/sysctl.h breaks the build on some OS. if someone knows how to reduce the number of #ifdefs I'll be glad :) > +} > + Cheers, Omar Polo