How to query the number of actual CPUs available?

Hi there. I’m trying to limit the number of CPU’s for a heavy task processing job that I’m doing.

I’ve got a 1 CPU machine. However when I query the CPU count using nproc I get 8 cpus, which seems to be the number of CPU’s on the host machine.

I’ve grepped through the cgroups directory and I can’t find anything there. Looking at the environmental variables also yields nothing. Is there a setting I’m missing? If not, can engineering put this in? Seems like it would be a very simple thing to implement.

1 Like

Hey,

Are you running a Docker container or using a native runtime? What’s your reason for wanting to limit the number of CPUs used by your application? Also, what technology or programming language are you using?

Jérémy.
Render Support, UTC+3

This would be helpful for example to decide how many gunicorn workers to run etc. I get 16 back from nproc!

I’m using the Docker service. My stack is python. But ssh’ing into the instance shows no indication of the number of CPU’s available.

I want to limit the number of CPU’s because each worker was forking the process and blowing up the memory budget and the OS was sending my program a kill -9 to the parent process, orphaning the children which still consumed the memory and this was a painful issue to debug.

Hey,

A program like ‘nproc’ will count all CPUs available across all machines within the same node/machine. Since you’re on a cloud, your server is in a containerized environment alongside other services.

To get an accurate reading of the available CPUs on your machine, refer to your instance type specs or check cgroups files like ‘/sys/fs/cgroup/cpu.max’, depending on the native runtimes you’re using.

Jérémy.
Render Support, UTC+3

Okay that’s great. Can you add this to the documentation?

It would be best if this can be injected into the environmental variables so commoners like me can simply discover it. Something like RENDER_NUM_CPUS=2 would be amazingly awesome. This path you’ve listed seems highly esoteric and I have no idea it even existed and is not easily discoverable.

This is the output, for anyone that is curious and comes across this problem.

cat /sys/fs/cgroup/cpu.max
50000 100000

Hey,

Just so I understand your use case a bit more, why do you need to query the number of CPUs available dynamically? Our instances have static resources, so the number of CPUs, RAM, etc., always remains the same. Since you know the instance type you’re using, why can’t you set the number of CPUs via an environment variable or within the start commands? I understand you might want to configure Gunicorn or other services, but it’s unclear why you need to do this dynamically via code if the resource amounts never change?

Jérémy.
Render Support, UTC+3