Is there a good cross-platform method for determining the number of processors a machine has in elisp? I'm trying to make my config auto detect some build options, and would like to have it automatically use the number of processors + 1. Grepping /proc/cpuinfo isn't a solution for me because it won't work on Windows.
Through Windows Device Manager: Open Device Manager (in the search box of the taskbar, type in "Device Manager", then select Open) Click on ">" to expand the Processors section. Count the number of entries to get the number of logical processors.
Press Ctrl + Shift + Esc to open Task Manager. Select the Performance tab to see how many cores and logical processors your PC has.
The way to tell how may cores you have is to look for "cpu cores" in your /proc/cpuinfo file. This line will show up for each virtual processor. If the number of cores shown is less than the number of virtual processors, your system is multi-threading.
Emacs 24.3 Lisp doesn't have access to that information. Your options would seem to include:
At least, that was true four hours ago, when I first started writing this answer. But then I got interested in the problem, and now you have a third option:
(system-cores :physical)
to get the number of physical processors, (system-cores :logical)
to get the number of logical cores, or just plain (system-cores)
to get an alist containing both.Caveats include:
(getenv "PATH")
doesn't contain /usr/sbin
, PROCESS-LINES will bomb out with "Searching for program: no such file or directory, system_profiler".)The systems currently known to be supported are GNU/Linux (anything with /proc/cpuinfo, more or less), Windows NT (and its derivatives, including 2000, XP, and all subsequent versions), and Darwin (OS X, at least 10.8, theoretically as far back as 10.2). Not coincidentally, these are also the systems to which I have access.
I've also included a delegate which should work properly on at least some flavors of BSD, but I don't have a BSD box on which to test it, so there's no telling whether or not it'll really work -- at the very least, you'll almost certainly need to modify the list of sysctls examined by the SYSTEM-CORES-SYSCTL delegate.
If you're using a modern variety of Linux, Windows, or OS X, great! You should be good to go, right out of the box. If not, and if your platform includes a command-line utility which provides the requisite information in its results, then it shouldn't be hard to write a delegate for your system. Or, if you don't want to write a delegate yourself, then email me all of:
M-: system-type
in EmacsM-: system-configuration
in Emacsand I'll be able to write a delegate myself and add it to the library.
Edit: The :cores
and :processors
keywords have been replaced with :physical
and :logical
, respectively; I couldn't keep them straight, and I don't see why I should expect anyone else to, either.
Edit: Per @lunaryorn, replaced (split-string (shell-command-to-string ...))
with (process-lines ...)
. This saves invoking a shell, which might make the library more reliable, and certainly makes its code easier to read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With