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; + mib[1] = HW_NCPU; + len = sizeof(nproc); + sysctl(mib, 2, &nproc, &len, nullptr, 0); +#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)); +} + void syms_of_emacs (void) { @@ -3176,6 +3208,7 @@ syms_of_emacs (void) defsubr (&Sinvocation_directory); defsubr (&Sdaemonp); defsubr (&Sdaemon_initialized); + defsubr (&Sprocessor_count); DEFVAR_LISP ("command-line-args", Vcommand_line_args, doc: /* Args passed by shell to Emacs, as a list of strings.