benjist
benjist

Reputation: 2881

Docker Java number of available CPU cores

What's a safe way to get the number of CPU cores that a docker instance is able to use?

I know it's this in plain Java (which ill include the logical cores also):

int cores = Runtime.getRuntime().availableProcessors();

Is this also ok when the Java app is run inside a docker image, or should I make additional considerations?

Upvotes: 6

Views: 4226

Answers (2)

Shayeeb Ahmed
Shayeeb Ahmed

Reputation: 74

On linux hosts you can read the pseudo files present in the path /sys/fs/cgroup/cpu,cpuacct/docker pseudo file system which keeps track of the cpu usage. In above path look for the directory named as the full container ID of the container you want to check and move into that directory. Then read the file named cpuacct.usage_percpu and you can see all the cpu time the container is consuming per each core. Absolute path for the above file will be like this /sys/fs/cgroup/cpu,cpuacct/docker/f24b61ce690fe77f9eb0f029215e9491cc54642d686770d71218b4a0ada27ce3 In java you can just read these pseudo files and do the computations for per cpu core usage.

Upvotes: 1

mdarwin
mdarwin

Reputation: 2384

Depends on your Java version. Older versions of Java would give you the number of cores on the docker server, whereas what you probably want is the number available to the container. From 8u131 and Java 9, this is fixed.

Upvotes: 4

Related Questions