3

在Emacs中获取CPU核的数量

 3 years ago
source link: https://www.lujun9972.win/blog/2017/02/28/%E5%9C%A8emacs%E4%B8%AD%E8%8E%B7%E5%8F%96cpu%E6%A0%B8%E7%9A%84%E6%95%B0%E9%87%8F/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在Emacs中获取CPU核的数量

Counting Processor Cores in Emacs 这里看到的,记录一下

在linux/cygwin下获取CPU核的数量

linux/cygwin提供了 /proc 虚拟文件系统可以获取系统信息,具体来说,在 /proc/cpuinfo 中会列出所有CPU核的信息

(when (file-exists-p "/proc/cpuinfo")
  (with-temp-buffer
    (insert-file-contents "/proc/cpuinfo")
    (how-many "^processor[[:space:]]+:")))
2

在Windows下获取CPU核的数量

Windows提供了一个名为 NUMBER_OF_PROCESSORS 的环境变量,里面存的就是核的数量

(let ((number-of-processors (getenv "NUMBER_OF_PROCESSORS")))
  (when number-of-processors
    (string-to-number number-of-processors)))

在BSD/OSX下获取CPU核的数量

需要调用 sysctl 来获取

(with-temp-buffer
  (ignore-errors
    (when (zerop (call-process "sysctl" nil t nil "-n" "hw.ncpu"))
      (string-to-number (buffer-string)))))

通过变量 system-type 可以获得操作系统的类型,不同操作系统调用不同的方法来获取CPU核的数量

(defun numcores ()
  "Return number of cores"
  (case system-type
    ((gnu/linux cygwin)
     (when (file-exists-p "/proc/cpuinfo")
       (with-temp-buffer
         (insert-file-contents "/proc/cpuinfo")
         (how-many "^processor[[:space:]]+:")))
     )
    ((gnu/kfreebsd darwin)
     (with-temp-buffer
       (ignore-errors
         (when (zerop (call-process "sysctl" nil t nil "-n" "hw.ncpu"))
           (string-to-number (buffer-string)))))
     )
    ((windows-nt)
     (let ((number-of-processors (getenv "NUMBER_OF_PROCESSORS")))
       (when number-of-processors
         (string-to-number number-of-processors)))
     )))

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK