all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] support for accessing CPU/core count (processor-count)
@ 2021-10-10  0:02 Campbell Barton
  2021-10-10  7:54 ` Omar Polo
                   ` (3 more replies)
  0 siblings, 4 replies; 55+ messages in thread
From: Campbell Barton @ 2021-10-10  0:02 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 577 bytes --]

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]

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

[-- Attachment #2: 0001-processor-count.patch --]
[-- Type: text/x-patch, Size: 1664 bytes --]

commit 7be53f10f3df3c3183cc97d6bbadb78ebb61e8d2
Author: Campbell Barton <ideasman42@gmail.com>
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.

^ permalink raw reply related	[flat|nested] 55+ messages in thread

end of thread, other threads:[~2021-10-13  2:24 UTC | newest]

Thread overview: 55+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-10-10  0:02 [PATCH] support for accessing CPU/core count (processor-count) Campbell Barton
2021-10-10  7:54 ` Omar Polo
2021-10-10  9:38   ` Arthur Miller
2021-10-10  9:43     ` Omar Polo
2021-10-10 10:52       ` Arthur Miller
2021-10-10 12:07         ` Omar Polo
2021-10-10 16:48           ` Arthur Miller
2021-10-10 18:17             ` Omar Polo
2021-10-10 19:45               ` Arthur Miller
2021-10-10 21:04                 ` Omar Polo
2021-10-11  8:15                   ` Arthur Miller
2021-10-11  8:20                   ` Arthur Miller
2021-10-11  8:23                     ` Omar Polo
2021-10-11 15:55                       ` Arthur Miller
2021-10-10 21:32                 ` Andreas Schwab
2021-10-11  8:03                   ` Arthur Miller
2021-10-11  8:14                     ` Andreas Schwab
2021-10-11 15:53                       ` Arthur Miller
2021-10-11 16:49                         ` Andreas Schwab
2021-10-11 17:14                           ` Arthur Miller
2021-10-10 10:13     ` Campbell Barton
2021-10-10 10:38     ` Andreas Schwab
2021-10-10 11:21       ` Arthur Miller
2021-10-10 11:57         ` Andreas Schwab
2021-10-10 16:35           ` Arthur Miller
2021-10-10 17:27             ` Andreas Schwab
2021-10-10 18:13               ` Arthur Miller
2021-10-10 19:16                 ` Stefan Monnier
2021-10-10 19:50                   ` Arthur Miller
2021-10-10 22:58                     ` Campbell Barton
2021-10-11  8:01                       ` Arthur Miller
2021-10-10 12:21   ` Stefan Kangas
2021-10-10 16:03     ` Omar Polo
2021-10-10 21:11     ` Paul Eggert
2021-10-10 21:16       ` Omar Polo
2021-10-11 17:17       ` Arthur Miller
2021-10-10 10:50 ` Andy Moreton
2021-10-10 11:21   ` Arthur Miller
2021-10-10 12:09 ` Stefan Kangas
2021-10-10 22:43   ` Campbell Barton
2021-10-11  1:34 ` Po Lu
2021-10-11  1:51   ` Campbell Barton
2021-10-11  3:04     ` Po Lu
2021-10-11  4:01       ` Campbell Barton
2021-10-11  8:20         ` Lars Ingebrigtsen
2021-10-11 13:00           ` Eli Zaretskii
2021-10-11 15:12           ` Stefan Monnier
2021-10-11 16:07             ` Eli Zaretskii
2021-10-11 21:14               ` Andy Moreton
2021-10-11 22:13                 ` Ken Brown
2021-10-12  2:27                 ` Eli Zaretskii
2021-10-12 10:39               ` Lars Ingebrigtsen
2021-10-12 14:09                 ` Eli Zaretskii
2021-10-12 19:58                   ` Paul Eggert
2021-10-13  2:24                     ` Eli Zaretskii

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.