Date: Fri, 05 Oct 2001 09:12:47 -0700 (PDT) From: John Baldwin <jhb@FreeBSD.org> To: Terry Lambert <tlambert2@mindspring.com> Cc: smp@freebsd.org, Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> Subject: Re: How to distinguish the SMP kernel and the UP kernel Message-ID: <XFMail.011005091247.jhb@FreeBSD.org> In-Reply-To: <3BBD80B9.4191B288@mindspring.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On 05-Oct-01 Terry Lambert wrote: > Kazutaka YOKOTA wrote: >> >> Is there any way for the loadable module to detect if >> >> the kernel is configured for SMP or UP? >> > >> >There is a global variable, "ncpu". Its name may have changed >> >recently, so you will want to look at the SYSCTL() stuff in >> >/sys/i386/i386 to be sure. >> >> The sysctl variable hw.ncpu returns 1 in the UP kernel, >> and returns the number of active CPUs in the SMP kernel. >> >> When hw.ncpu == 1, it doesn't necessarily signifies the kernel >> is configured for UP. Because it is perfectly permissible >> to run the SMP kernel on the multi-CPU motherboard with only one >> CPU installed... >> >> Am I wrong? > > No. > > I assumed you wanted the information to determine how many > kernel threads, etc., to run in order to ensure that your > code would operate optimally to take advantage of the extra > hardware. As you note, however, !SMP does not equal UP, > even though !UP equals SMP. > > If the reason is for locking or structure sizes, then you > will have to create two modules: one for SMP, and one for > non-SMP. > > I hesistate to tell you how to tell the difference, since I > would prefer that you not be able to write code that cares, > yet is able to run, since it sets a bad example, and is a > really, really ugly hack. But... I definitely agree. > A really ugly way to check would be to use the non-recursive > locking functions, create a lock, lock it, and then try a > non-blocking lock against yourself. If it fails, then you > know you are SMP (the lock functions are defined), otherwise > you know you are compiled UP. Nope. The locking functions are defined in a UP kernel as well. > Of course, you could always add a sysctl oid for "kern.smp" > to indicate whether or not it's compiled SMP -- but as I > said earlier, this would encourage what I personally would > consider really bad practice. No need. If a userland program needs to know, they can check if the kern.smp.active sysctl is present. This is how top(1) works, for example. I can understand userland possibly needing to know, but kernel code really shouldn't care or have to know. -- John Baldwin <jhb@FreeBSD.org> -- http://www.FreeBSD.org/~jhb/ PGP Key: http://www.baldwin.cx/~john/pgpkey.asc "Power Users Use the Power to Serve!" - http://www.FreeBSD.org/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-smp" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?XFMail.011005091247.jhb>