News for hackers. Stuff that matters.
Re:Use `nproc` and not grep /proc/cpuinfo by catlifeonmars on Thursday November 26, 2020 @08:03PM (#25224724)
> So, what this will end up doing is just increase the number of context switches, possibly also adding a performance degradation.

I think this is a jumping to conclusions a bit. You’d need to be reading /proc/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?

Re:Use `nproc` and not grep /proc/cpuinfo by jasontedor on Thursday November 26, 2020 @08:20PM (#25224811)
I think that the full context of the sentence you quoted will help:

> In this case, if you base your number of threads off grepping lscpu you take another dependency (on the util-linux package), which isn’t needed. You also get the wrong answer, as you do by grepping /proc/cpuinfo. So, what this will end up doing is just increase the number of context switches, possibly also adding a performance degradation.

The idea is that a common way to size an application that exploits concurrency is to base the number of application threads on the hardware threads available. This portion of the article is saying that if you do so on the basis of `lscpu` or grepping `/proc/cpuinfo` you could mis-size relative to the actual number of cores available to schedule the threads of your application on. Doing so will lead to excessive context switching for your application, and that can hurt.

Re:Use `nproc` and not grep /proc/cpuinfo by stewartesmith on Thursday November 26, 2020 @08:26PM (#25224828)
I wasn't talking about the reading of the number of CPUs you could use, but rather how many threads you'd spawn based on that information.

There may be 10 CPUs in the machine, but if your in your container which is restricted to using only 1 of them, it's no use launching 10 threads in that container over just having one.

Re:Use `nproc` and not grep /proc/cpuinfo by catlifeonmars on Thursday November 26, 2020 @09:06PM (#25225002)
Oh I see. My mistake. This makes more sense.
Re:Use `nproc` and not grep /proc/cpuinfo by EE84M3i on Thursday November 26, 2020 @08:17PM (#25224794)
The article is saying if you make too many processes (more than the number of hardware threads available) the OS will have to context switch between them to keep them all running.
Re:Use `nproc` and not grep /proc/cpuinfo by kcbanner on Thursday November 26, 2020 @08:17PM (#25224795)
They mean that you read it once, then spawn more threads than you have actual cores. Those threads then need to context switch back and forth to do work.