Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 17 Jan 2001 18:28:43 +1100 (EST)
From:      Bruce Evans <bde@zeta.org.au>
To:        Kirk McKusick <mckusick@mckusick.com>
Cc:        Alfred Perlstein <bright@wintelcom.net>, arch@FreeBSD.ORG
Subject:   Re: dynamic vs static sysctls? 
Message-ID:  <Pine.BSF.4.21.0101171749290.4271-100000@besplex.bde.org>
In-Reply-To: <200101152345.PAA22257@beastie.mckusick.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, 15 Jan 2001, Kirk McKusick wrote:

> I have an example where sysctl is still useful. In my work on a
> background version of fsck, I have used sysctl to allow me to
> pass information into the kernel that I want to have updated in
> the filesystem. In the case of lost blocks, there may be hundreds
> or even thousands of blocks that need to be put back into the
> bit maps. Each non-contiguous block is passed in separately
> which results in hundreds or thousands of sysctl calls. I want
> to do one call to sysctlnametomib (a new, but obviously trivial
> function) to return the numeric mib, and then use that mib on all
> the subsequent sysctl calls. That dramatically cuts down on the
> time it takes to return the blocks since I do not have to translate
> the same name repeatedly (which takes far longer than the block
> release itself). I would think that this might be an issue for
> any application that uses sysctl to get/set the same information
> repeatedly.

There is no reason why numeric mibs should be significantly faster
than string mibs.  Syscall overhead would dominate string lookup
time if the implementation was even moderately efficient.  However,
out current implementation is inefficient for numeric mibs and worse
for string mibs.

The enclosed test program gives the following times on a Celeron522
for related lookups via getppid(), sysctl(), sysctlbyname() and
open()/read()/close().

GETPPID:        1.3 usec/call
SYSCTL:         8.6 usec/call
SYSCTLBYNAME:  21.9 usec/call
FS:            22.0 usec/call    (reading /COPYRIGHT)
FS:            34.5 usec/call    (reading /proc/curproc/regs)

getppid() isn't closely related, but it performs the same amount of
useful work as the other operations -- it copies a single integer from
the kernel to the application.

---
#include <sys/types.h>
#include <sys/sysctl.h>
#include <fcntl.h>
#include <unistd.h>

main()
{
	int fd;
	int hw_float;
	int i;
	size_t len;
	int mib[2];

	for (i = 0; i < 1000000; ++i) {
		len = sizeof(hw_float);
#ifdef GETPPID
		(void)getppid();
#endif
#ifdef SYSCTL
		mib[0] = CTL_HW;
		mib[1] = HW_FLOATINGPT;
		if (sysctl(mib, 2, &hw_float, &len, (void *)0, 0) == -1)
			err(1, "sysctl");
#endif
#ifdef SYSCTLBYNAME
		if (sysctlbyname("hw.floatingpoint", &hw_float, &len,
		    (void *)0, 0) == -1)
			err(1, "sysctl");
#endif
#ifdef FS
		if ((fd = open("/COPYRIGHT", 0)) == -1)
			err(1, "open");
		if (read(fd, &hw_float, sizeof(hw_float)) != sizeof(hw_float))
			err(1, "read");
		if (close(fd) != 0)
			err(1, "close");
#endif
	}
}
---

Bruce



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.21.0101171749290.4271-100000>