Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting number of processors in emacs

Tags:

emacs

elisp

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.

like image 418
Dan Albert Avatar asked Dec 18 '13 19:12

Dan Albert


People also ask

How many processors do I have?

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.

How do I know how many cores my processor has?

Press Ctrl + Shift + Esc to open Task Manager. Select the Performance tab to see how many cores and logical processors your PC has.

How many cores does my virtual machine have Linux?

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.


1 Answers

Emacs 24.3 Lisp doesn't have access to that information. Your options would seem to include:

  • writing an Elisp library which uses the value of SYSTEM-TYPE to choose a system-specific method for getting the processor count;
  • modifying the Emacs C source and rebuilding it so that it can, for each potentially multiprocessor platform on which Emacs builds, expose the processor count at the Lisp level.

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:

  • downloading my system-cores.el library, which implements the first of the two options above, and calling (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:

  • This library relies strongly on the PROCESS-LINES function. If that function can't do anything sensible in the context where you need to call SYSTEM-CORES, then SYSTEM-CORES can't either. (For example, if you're on Darwin and (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:

  • the proper invocation for the command-line utility in question
  • a sample of the output it produces on your system
  • the output of M-: system-type in Emacs
  • the output of M-: system-configuration in Emacs

and 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.

like image 190
Aaron Miller Avatar answered Oct 15 '22 13:10

Aaron Miller