TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Use `nproc` and not grep /proc/cpuinfo

105 pointsby harporoederover 4 years ago

11 comments

cypharover 4 years ago
It should be noted that the reason this is wrong under Docker is that &#x2F;proc&#x2F;cpuinfo is not cgroup-aware. LXC works around this by creating a fake &#x2F;proc&#x2F;cpuinfo (and &#x2F;proc&#x2F;meminfo)[1] which matches the values set by cgroup limits.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;lxc&#x2F;lxcfs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lxc&#x2F;lxcfs</a>
评论 #25225470 未加载
评论 #25225193 未加载
cyclopticover 4 years ago
IIRC this utility is a wrapper around glibc get_nprocs(), which on Linux will ultimately read from &#x2F;sys&#x2F;devices&#x2F;system&#x2F;cpu&#x2F;online. If you need this information from a compiled language, you should probably use the glibc wrapper.
评论 #25226703 未加载
cbanekover 4 years ago
Even this I feel doesn&#x27;t work all the time. At least from memory, I remember some problems where on kubernetes if you look at nproc or cpuinfo, that will give you the number of processors on the host machine, and not necessarily the CPU limit which is set per container. Although maybe I&#x27;m mis-remembering. I guess nproc could look at something else as well to filter based on that, and I agree having a nice portable commandline is way better than looking at &#x2F;proc.
评论 #25224787 未加载
评论 #25226347 未加载
catlifeonmarsover 4 years ago
&gt; So, what this will end up doing is just increase the number of context switches, possibly also adding a performance degradation.<p>I think this is a jumping to conclusions a bit. You’d need to be reading &#x2F;proc&#x2F;cpuinfo in a fairly tight loop in order for the time spend doing any context switching to dominate the runtime cost of a program. Is this a common practice in some types of computation? Or am I misunderstanding this quote from the article?
评论 #25224811 未加载
评论 #25224828 未加载
评论 #25224794 未加载
评论 #25224795 未加载
swillsover 4 years ago
The portable way is:<p>getconf _NPROCESSORS_ONLN
评论 #25225490 未加载
nomorecodehere8over 4 years ago
It&#x27;s amazing how much knowledge is being lost with the advent of container revolutions and orchestrator Os&#x27;es. Cgroups is _still_ opt in not opt out...though those who never have worked in HPC on bare metal with parallel libraries and approaches for cpu like openmp are loudly complaining about control groups that don&#x27;t control or provide consistent view of userland like there isn&#x27;t a host system underneath. It&#x27;s like this (<a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;libc&#x2F;manual&#x2F;html_node&#x2F;CPU-Affinity.html" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;libc&#x2F;manual&#x2F;html_node&#x2F;CPU-Affin...</a>) wasn&#x27;t the way things were done (and still are to this day) in everything but the container world. I know it&#x27;s hard to grasp but not everyone is containerizing everything.
oso2kover 4 years ago
I see suggestions to use ‘make -j$(nproc)’. However, I was taught when learning MPI programming 20 years ago, you want think at least “one to load, one to run”. The idea being that processes and threads waiting on IO will sleep&#x2F;yield while another process or thread can continue executing. An especially useful strategy if you can afford the RAM (trading RAM for CPU). Consequently, I tend to run twice as many ‘make’ jobs as threads on my systems.
评论 #25224487 未加载
评论 #25225358 未加载
评论 #25224500 未加载
评论 #25225625 未加载
2bluescover 4 years ago
For those running `make`, add this to you shell init to set the concurrency level via env var:<p>&gt; expert MAKEFLAGS=-j$(nproc)
评论 #25224237 未加载
评论 #25224219 未加载
评论 #25226799 未加载
评论 #25226821 未加载
评论 #25224377 未加载
s_gourichonover 4 years ago
Interesting, but how does nproc get the right information ?<p>OMP_NUM_THREADS and OMP_THREAD_LIMIT are in principle extra limitations, not the main source of information, right ?
评论 #25224268 未加载
skizziepopover 4 years ago
nproc isn&#x27;t available on some of the BSDs
评论 #25224940 未加载
评论 #25224472 未加载
shmerlover 4 years ago
Yeah, I just use $(nproc) in scripts when I need to set up threads number.