Optimize execution of processes by CPU core

Slawa Olhovchenkov slw at zxy.spb.ru
Wed Feb 20 17:55:26 UTC 2019


On Wed, Feb 20, 2019 at 05:35:21PM +0000, Farhan Khan via freebsd-hackers wrote:

> Hi all,
> 
> I am trying to optimize the execution of a CPU-intensive workload where I am running multiple instances of a program. The moment the program ends (expected behavior), the calling shell script verifies the results and if its good it reruns the program. The machines I am running this on have 8 cores, but ps reports that some of the processes frequently run on the same CPU, so I suspect I am not getting optimized performance. If possible, and if most efficient, I would like to run each process on its own CPU core.
> 
> Are there any best practices on how to run something like this? I understand cpuset can perform some functionality around this, but I do not understand the tooling (The man page speaks of a CPU set?) Would I do something like "cpuset -c -l 0 program arg1 arg2 arg3" in one script, and then "cpuset -c -l 1 program arg1 arg2 arg3" in the next up to 7?


just "cpuset -l 0 program arg1 arg2 arg3" and etc. (w/o "-c")


> Obviously it would be best to re-write the program to handle multiple threads in in an optimized way, but that would take more time than the optimization would likely save.

for every thread:

  char name[128];
  cpuset_t mask;
  pthread_attr_t attr;
  
  pthread_attr_init(&attr);
  CPU_ZERO(&mask);
  CPU_SET(cpu, &mask);
  pthread_attr_setaffinity_np(&attr, sizeof(mask), &mask);
  pthread_create(&tid[n], &attr, worker_thread, (void *)args);
  snprintf(name, 128, "worker CPU#%d", cpu);
  pthread_set_name_np(tid[n], name);
  


More information about the freebsd-hackers mailing list