linux 获取内核线程数,如何从命令行获取Linux中的CPU /内核数量?
前言:
基于/proc/cpuinfo的答案的問題是,它們parsing了供人類使用的信息,因此缺乏為機器分析devise的穩定格式 :輸出格式可能因平臺和運行時間條件而有所不同; 在Linux上使用lscpu -p (和OSX上的sysctl )可以繞過這個問題 。
getconf _NPROCESSORS_ONLN / getconf NPROCESSORS_ONLN不區分邏輯 CPU和物理 CPU。
這里有一個bash代碼片斷,它可以在Linux和macOS上運行,以確定在線 – 邏輯或物理 CPU的數量 ; 請參閱評論的細節。
Linux使用lscpu ,macOS使用sysctl 。
術語說明 : CPU指操作系統所見的最小處理單元。 非超線程核心每個對應1個CPU,而超線程核心包含多于1個(通常是2個)邏輯CPU。
Linux使用以下分類法,從最小單元開始: CPU < 核心 < 套接字 < 書 < 節點 ,每個級別包括下一個較低級別的一個或多個實例。
我的知識有些不穩定 – 如果我錯了,請告訴我。 有沒有人知道在這方面“書”是什么?
# macOS: Use `sysctl -n hw.*cpu_max`, which returns the values of # interest directly. # CAVEAT: Using the "_max" key suffixes means that the *maximum* # available number of CPUs is reported, whereas the # current power-management mode could make *fewer* CPUs # available; dropping the "_max" suffix would report the # number of *currently* available ones; see [1] below. # # Linux: Parse output from `lscpu -p`, where each output line represents # a distinct (logical) CPU. # Note: Newer versions of `lscpu` support more flexible output # formats, but we stick with the parseable legacy format # generated by `-p` to support older distros, too. # `-p` reports *online* CPUs only - ie, on hot-pluggable # systems, currently disabled (offline) CPUs are NOT # reported. # Number of LOGICAL CPUs (includes those reported by hyper-threading cores) # Linux: Simply count the number of (non-comment) output lines from `lscpu -p`, # which tells us the number of *logical* CPUs. logicalCpuCount=$([[ $(uname) = 'Darwin' ]] && sysctl -n hw.logicalcpu_max || lscpu -p | egrep -v '^#' | wc -l) # Number of PHYSICAL CPUs (cores). # Linux: The 2nd column contains the core ID, with each core ID having 1 or # - in the case of hyperthreading - more logical CPUs. # Counting the *unique* cores across lines tells us the # number of *physical* CPUs (cores). physicalCpuCount=$([[ $(uname) = 'Darwin' ]] && sysctl -n hw.physicalcpu_max || lscpu -p | egrep -v '^#' | sort -u -t, -k 2,4 | wc -l)
[1] macOS sysctl (3)文檔
請注意,除macOS以外的BSD派生系統(例如FreeBSD)僅支持sysctl的hw.ncpu密鑰,這在macOS上已棄用; 我不清楚哪個新密鑰hw.npu對應于: hw.(logical|physical)cpu_[max] 。
幫助糾正物理CPU數量lscpu命令的帽子提示lscpu 。
注意 : lscpu -p輸出不包括“book”列( man頁提到“books”是分類層次結構中套接字和節點之間的實體)。 如果“books”在給定的Linux系統上運行( 是否有人知道何時以及如何? ),則可能會報告physical-CPU-count命令(這是基于lscpu報告非唯一跨 ID 更高級的實體 ;例如:來自2個不同套接字的2個不同的核心可以具有相同的ID)。
總結
以上是生活随笔為你收集整理的linux 获取内核线程数,如何从命令行获取Linux中的CPU /内核数量?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用who,w,ps和top等指令查看l
- 下一篇: linux虚拟机漏洞修复,linux幽灵