From owner-freebsd-hackers@freebsd.org Wed Feb 20 17:55:26 2019 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 873C614FACE2 for ; Wed, 20 Feb 2019 17:55:26 +0000 (UTC) (envelope-from slw@zxy.spb.ru) Received: from zxy.spb.ru (zxy.spb.ru [195.70.199.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 3031A8B352 for ; Wed, 20 Feb 2019 17:55:25 +0000 (UTC) (envelope-from slw@zxy.spb.ru) Received: from slw by zxy.spb.ru with local (Exim 4.86 (FreeBSD)) (envelope-from ) id 1gwW5T-000DCM-F4; Wed, 20 Feb 2019 20:55:15 +0300 Date: Wed, 20 Feb 2019 20:55:15 +0300 From: Slawa Olhovchenkov To: Farhan Khan Cc: freebsd-hackers@freebsd.org Subject: Re: Optimize execution of processes by CPU core Message-ID: <20190220175515.GA2178@zxy.spb.ru> References: <2fcd9aee092730e11880c3ae88de4898@farhan.codes> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2fcd9aee092730e11880c3ae88de4898@farhan.codes> User-Agent: Mutt/1.5.24 (2015-08-30) X-SA-Exim-Connect-IP: X-SA-Exim-Mail-From: slw@zxy.spb.ru X-SA-Exim-Scanned: No (on zxy.spb.ru); SAEximRunCond expanded to false X-Rspamd-Queue-Id: 3031A8B352 X-Spamd-Bar: +++ Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [3.48 / 15.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; NEURAL_SPAM_SHORT(0.90)[0.895,0]; MIME_GOOD(-0.10)[text/plain]; RCVD_TLS_LAST(0.00)[]; DMARC_NA(0.00)[zxy.spb.ru]; AUTH_NA(1.00)[]; NEURAL_SPAM_MEDIUM(0.94)[0.942,0]; TO_MATCH_ENVRCPT_SOME(0.00)[]; MX_GOOD(-0.01)[zxy.spb.ru]; RCPT_COUNT_TWO(0.00)[2]; NEURAL_SPAM_LONG(0.75)[0.751,0]; R_SPF_NA(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:5495, ipnet:195.70.192.0/19, country:RU]; MID_RHS_MATCH_FROM(0.00)[]; IP_SCORE(0.00)[country: RU(0.00)]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Feb 2019 17:55:26 -0000 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);