Assign or restrict CPU cores to Neo4j process

Operating system run performance-critical applications on multi-core processors using something called “processor affinity” or “CPU pinning”. This feature “binds” a running process to particular CPU core(s), which can be beneficial for example in reducing CPU cache misses. Also, when multiple processes communicate via shared memory, scheduling both processes on the cores in the same NUMA domain may speed up their performance.

Upon initialisation, Neo4j will create an affinity mask (attachment list) to all available cpu cores, i.e. it will attach to all cores available. It may sometimes be desired to restrict or specify these for the Neo4j process, at initilisation or at runtime. Below steps were followed on a Linux Redhat 6.1 and 7.6 versions. Similar steps may be valid for other Linux flavours.

  1. Execute the lscpu command, which yields an output similar to below

List of available CPU cores

Amongst other info, we see the comma separated list of CPUs currently online, in this case 0,1. Note that this may be a binary, decimal, or hexadecimal number. In this case, it is decimal.

  1. Get the Neo4j process ID. The cpu core affinity of this process can now be viewed by executing:

taskset -p pid

CPU cores affinity list for the process

that this outputs a decimal, which can be translated into binary. Alternatively, the binary output can be directly obtained by executing taskset -cp pid. The above shows an affinity mask of 3, which in binary is 1,1, i.e. CPU cores 0 and 1. This also shows us that by default, the Neo4j process executes on all available CPU cores by default.

  1. We can now assign certain cpu cores to a running Neo4j process by executing:

taskset -cp <desired cpu(s) comma separated list> pid:

Updated CPU cores which the process is affiliated with

The above example restricts the Neo4j process to the CPU core with logical ID 0.

References:

  • Last Modified: 2020-10-22 22:13:22 UTC by Umar Muzammil.
  • Relevant for Neo4j Versions: 3.4, 3.5.
  • Relevant keywords cpu, core, pid, thread.