Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 20 Feb 2019 20:55:15 +0300
From:      Slawa Olhovchenkov <slw@zxy.spb.ru>
To:        Farhan Khan <farhan@farhan.codes>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Optimize execution of processes by CPU core
Message-ID:  <20190220175515.GA2178@zxy.spb.ru>
In-Reply-To: <2fcd9aee092730e11880c3ae88de4898@farhan.codes>
References:  <2fcd9aee092730e11880c3ae88de4898@farhan.codes>

next in thread | previous in thread | raw e-mail | index | archive | help
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);
  



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20190220175515.GA2178>