From owner-freebsd-current@FreeBSD.ORG Tue Oct 25 20:45:50 2011 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id EB9BE1065674 for ; Tue, 25 Oct 2011 20:45:50 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from cyrus.watson.org (cyrus.watson.org [65.122.17.42]) by mx1.freebsd.org (Postfix) with ESMTP id C3D178FC1A for ; Tue, 25 Oct 2011 20:45:50 +0000 (UTC) Received: from bigwig.baldwin.cx (66.111.2.69.static.nyinternet.net [66.111.2.69]) by cyrus.watson.org (Postfix) with ESMTPSA id 5B6A946B0C; Tue, 25 Oct 2011 16:45:50 -0400 (EDT) Received: from jhbbsd.localnet (unknown [209.249.190.124]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id DB9568A02F; Tue, 25 Oct 2011 16:45:49 -0400 (EDT) From: John Baldwin To: "Poul-Henning Kamp" Date: Tue, 25 Oct 2011 14:54:00 -0400 User-Agent: KMail/1.13.5 (FreeBSD/8.2-CBSD-20110714-p8; KDE/4.5.5; amd64; ; ) References: <99483.1319566152@critter.freebsd.dk> In-Reply-To: <99483.1319566152@critter.freebsd.dk> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201110251454.00471.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.6 (bigwig.baldwin.cx); Tue, 25 Oct 2011 16:45:50 -0400 (EDT) Cc: freebsd-current@freebsd.org, Luigi Rizzo Subject: Re: getting the cpuid for a userspace process ? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 25 Oct 2011 20:45:51 -0000 On Tuesday, October 25, 2011 2:09:12 pm Poul-Henning Kamp wrote: > In message <201110251342.45194.jhb@freebsd.org>, John Baldwin writes: > >On Tuesday, October 25, 2011 11:06:22 am Luigi Rizzo wrote: > >> as the subject says... is there any way to get the current > >> CPU id for a userspace process (of course, > >> valid only at the time the function is called as the > >> process might be arbitrarily moved while it runs) > > > >Not from userland, no. On x86 you can use cpuid to fetch the APIC ID, but > >that does not map 1:1 to FreeBSD cpu IDs. > > How does JEmalloc do it ? I don't think it does on FreeBSD. The only thing malloc() knows on FreeBSD is the total number of CPUs. I believe Linux has a system call that returns this though (sched_getcpu() is the public interface which is a wrapper around a getcpu() system call). From the manpage it would seem that sched_getcpu() uses a per-thread shared page of some sort so that it doesn't actually have to enter the kernel to get the CPU ID. Alternatively, you could imagine it on x86 at least exporting a table mapping APIC IDs to CPU IDs and then using cpuid and that table to do the lookup purely in userland. That would only necessitate a global shared page and not one per-thread. You could still fall back to a system call for other architectures that simply returned curthread->td_oncpu. -- John Baldwin