From owner-freebsd-hackers@FreeBSD.ORG Sun Jun 28 20:44:51 2009 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id F1D641065677 for ; Sun, 28 Jun 2009 20:44:51 +0000 (UTC) (envelope-from dan@dan.emsphone.com) Received: from email1.allantgroup.com (email1.emsphone.com [199.67.51.115]) by mx1.freebsd.org (Postfix) with ESMTP id AA2058FC27 for ; Sun, 28 Jun 2009 20:44:51 +0000 (UTC) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by email1.allantgroup.com (8.14.0/8.14.0) with ESMTP id n5SKinuk057438 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sun, 28 Jun 2009 15:44:50 -0500 (CDT) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (smmsp@localhost [127.0.0.1]) by dan.emsphone.com (8.14.3/8.14.3) with ESMTP id n5SKinY1031863 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sun, 28 Jun 2009 15:44:49 -0500 (CDT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.14.3/8.14.3/Submit) id n5SKim7R031789; Sun, 28 Jun 2009 15:44:48 -0500 (CDT) (envelope-from dan) Date: Sun, 28 Jun 2009 15:44:47 -0500 From: Dan Nelson To: msnkipa@mail.ru Message-ID: <20090628204446.GI76275@dan.emsphone.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-OS: FreeBSD 7.2-STABLE User-Agent: Mutt/1.5.19 (2009-01-05) X-Virus-Scanned: ClamAV version 0.94.1, clamav-milter version 0.94.1 on email1.allantgroup.com X-Virus-Status: Clean X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.0.2 (email1.allantgroup.com [199.67.51.78]); Sun, 28 Jun 2009 15:44:50 -0500 (CDT) X-Scanned-By: MIMEDefang 2.45 Cc: freebsd-hackers@freebsd.org Subject: Re: Can I bind POSIX thread to cpu core? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 28 Jun 2009 20:44:52 -0000 In the last episode (Jun 28): > I have system with 4 core cpu. How can I bind POSIX thread to the one > core? I mean that this thread can be executed on the fixed core. See the cpuset(2) and cpuset_setaffinity(2) manpages. Something like this should work: #include #include #include #include int main(void) { int i; cpuset_t myset; /* Get CPU mask for the current thread */ if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(myset), &myset) == -1) err(1, "getaffinity failed"); /* Find first available CPU - don't assume CPU0 is always available */ for (i = 0; i < CPU_SETSIZE; i++) if (CPU_ISSET(i, &myset)) break; if (i == CPU_SETSIZE) err(1, "Not allowed to run on any CPUs? How did I print this, then?"); /* Set new CPU mask */ printf ("Setting affinity to CPU %d\n", i); CPU_ZERO(&myset); CPU_SET(i, &myset); if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(myset), &myset) == -1) warn("setaffinity failed"); /* Do CPU-intensive stuff here */ return 0; } -- Dan Nelson dnelson@allantgroup.com