Pinning java threads to cores

Viewed 1489

The java process I'm launching is Elasticsearch and it has numerous threads that are created. I check that using ps HuH p <pid> | wc -l. this is how I get the pid of elasticsearch:

ES=`jps | egrep 'Elasticsearch' | awk '{print $1}'`

I pinning all the threads from the given pid to a set of cores using the following python script.

#!/usr/bin/env python
import os
import argparse

def task_set(pid, core):
    print "pinning all threads of pid: ", pid, " to cores: ", core
    os.system('taskset -apc '+str(core)+' ' +str(pid)+' >/dev/null')

def main(args):
    experiments = ["1B", "2B", "1B3S", "2B2S", "1S", "2S", "3S", "4S"]
    which = args.id[0]
    idx = experiments.index(which)
    PC = ["0", "0,1", "0,2,3,4", "0,1,2,3", "2", "2,3","2,3,4", "2,3,4,5"]
    task_set(args.pid[0], PC[idx])
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--pid', nargs=1, help='appid')
    parser.add_argument('--id', nargs=1, help='core')
    args = parser.parse_args()
    main(args)

However, when I look at top -H -p <pid> and monitor the core allocation. It does not obey most of the time. Is there something I'm missing here? Does JVM have to do something here?

Is there anyother way I can pin the threads to the cores?

1 Answers

taskset will be working correctly. However, if a process is in Sleeping state, it's current CPU will not switch to the new one.

cat /proc/<PID>/status | grep State:

To workaround this you can do

kill -SIGSTOP <PID>
kill -SIGCONT <PID>

This forces the process out of sleep state and ensures the CPU switch. top or htop will then show the value correctly.

Related