Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
The challenges of working out how many CPUs your program can use on Linux (utcc.utoronto.ca)
10 points by telotortium on July 23, 2024 | hide | past | favorite | 4 comments


See also my comment for how to determine this: https://news.ycombinator.com/item?id=38185277


That's good, but i'd prefer not to assume perl is there. Perhaps something like this untested POSIX shell might be better?

  if [ -e /sys/fs/cgroup/cpu/cpu.cfs_quota_us ] && [ -e /sys/fs/cgroup/cpu/cpu.cfs_period_us ]; then
      read -r quota < /sys/fs/cgroup/cpu/cpu.cfs_quota_us
      read -r period < /sys/fs/cgroup/cpu/cpu.cfs_period_us
      # Perform division and rounding
      GOMAXPROCS=$(( (quota + period - 1) / period ))
  else
      GOMAXPROCS=$(nproc)
  fi
  export GOMAXPROCS


If their complaint is that there's no lightweight API or cli app that provides this info, why not make one?


The author is not complaining, but pointing out how complicated it is to determine programmatically the number of processes to use, and that most software doesn't take all the different possibilities into account.

Even if it were a complaint, the solution isn't only 'make one yourself' but includes 'write about it and see if someone knows of an existing solution'.

Which is what happened here. A commenter wrote: "I think that hwloc understands as many CPU limits on Linux as feasible, including the cgroups, but it's really overkill for something as small as a build tool. The end result is that a lot of limits must be checked in order to find out how many processes can be used."

That hwloc links to https://www.open-mpi.org/projects/hwloc/ which among other things 'may also help many applications ... find out how many cores and/or hardware threads are available'.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: