NetBSD Problem Report #44864

From stix@stix.id.au  Wed Apr 13 10:01:45 2011
Return-Path: <stix@stix.id.au>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by www.NetBSD.org (Postfix) with ESMTP id 98AF663BA68
	for <gnats-bugs@gnats.NetBSD.org>; Wed, 13 Apr 2011 10:01:45 +0000 (UTC)
Message-Id: <20110413084712.6E81B614B@kitt.stix.org.au>
Date: Wed, 13 Apr 2011 18:47:11 +1000 (EST)
From: stix@stix.id.au
Reply-To: stixpjr@gmail.com
To: gnats-bugs@gnats.NetBSD.org
Subject: sysctl unnecessarily recompiling regex
X-Send-Pr-Version: 3.95

>Number:         44864
>Category:       bin
>Synopsis:       sysctl unnecessarily recompiling regex
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          closed
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Wed Apr 13 10:05:00 +0000 2011
>Closed-Date:    Wed Apr 27 18:54:28 +0000 2011
>Last-Modified:  Wed Apr 27 18:54:28 +0000 2011
>Originator:     Paul Ripke
>Release:        NetBSD 5.99.22 and up, at least
>Organization:
Paul Ripke
I love deadlines. I like the whooshing sound they make as they fly by.
-- Douglas Adams
>Environment:
System: NetBSD kitt.stix.org.au 5.99.22 NetBSD 5.99.22 (KITT) #0: Sun Dec 13 23:56:43 EST 2009 stix@hex.stix.org.au:/hex/netbsd/20091212T0002/obj.mac68k/hex/netbsd/20091212T0002/src/sys/arch/mac68k/compile/KITT mac68k

Architecture: m68k
Machine: mac68k
>Description:
sysctl -a (or with any pattern) is woefully slow.

On my trusty slow box, without patch:
kitt:ksh$ time /sbin/sysctl -a > /dev/null
   23.75s real    20.50s user     0.65s system

With patch:
kitt:ksh$ time ./sysctl -a > /dev/null
    3.06s real     2.18s user     0.51s system

>How-To-Repeat:
Use sysctl with any pattern matching or even -a.

>Fix:

Index: sbin/sysctl/sysctl.c
===================================================================
RCS file: /usr/netbsd/cvsroot/src/sbin/sysctl/sysctl.c,v
retrieving revision 1.133
diff -u -d -r1.133 sysctl.c
--- sbin/sysctl/sysctl.c	13 Dec 2010 17:47:40 -0000	1.133
+++ sbin/sysctl/sysctl.c	13 Apr 2011 05:33:24 -0000
@@ -380,32 +380,37 @@
 findhandler(const char *s, int w)
 {
 	const struct handlespec *p;
-	regex_t re;
+	static regex_t *re = NULL;
 	int i, j, l;
 	char eb[64];
 	regmatch_t match[1];

 	p = &handlers[0];
 	l = strlen(s);
-	for (i = 0; p[i].ps_re != NULL; i++) {
-		j = regcomp(&re, p[i].ps_re, REG_EXTENDED);
-		if (j != 0) {
-			regerror(j, &re, eb, sizeof(eb));
-			errx(1, "regcomp: %s: %s", p[i].ps_re, eb);
+	if (re == NULL) {
+		for (i = 0; p[i].ps_re != NULL; i++)
+			;
+		re = malloc(sizeof(*re) * i);
+		for (i = 0; p[i].ps_re != NULL; i++) {
+			j = regcomp(&re[i], p[i].ps_re, REG_EXTENDED);
+			if (j != 0) {
+				regerror(j, &re[i], eb, sizeof(eb));
+				errx(1, "regcomp: %s: %s", p[i].ps_re, eb);
+			}
 		}
-		j = regexec(&re, s, 1, match, 0);
+	}
+	for (i = 0; p[i].ps_re != NULL; i++) {
+		j = regexec(&re[i], s, 1, match, 0);
 		if (j == 0) {
 			if (match[0].rm_so == 0 && match[0].rm_eo == l &&
 			    (w ? p[i].ps_w : p[i].ps_p) != NULL) {
-				regfree(&re);
 				return (&p[i]);
 			}
 		}
 		else if (j != REG_NOMATCH) {
-			regerror(j, &re, eb, sizeof(eb));
+			regerror(j, &re[i], eb, sizeof(eb));
 			errx(1, "regexec: %s: %s", p[i].ps_re, eb);
 		}
-		regfree(&re);
 	}

 	return (NULL);

>Release-Note:

>Audit-Trail:
From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/44864 CVS commit: src/sbin/sysctl
Date: Fri, 15 Apr 2011 21:15:55 -0400

 Module Name:	src
 Committed By:	christos
 Date:		Sat Apr 16 01:15:55 UTC 2011

 Modified Files:
 	src/sbin/sysctl: sysctl.c

 Log Message:
 PR/44864: Paul Ripke: Compile regular expressions on demand and only once.


 To generate a diff of this commit:
 cvs rdiff -u -r1.133 -r1.134 src/sbin/sysctl/sysctl.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->feedback
State-Changed-By: wiz@NetBSD.org
State-Changed-When: Sat, 16 Apr 2011 07:07:48 +0000
State-Changed-Why:
Christos committed, fine now?


From: Paul Ripke <stixpjr@gmail.com>
To: gnats-bugs@netbsd.org
Cc: gnats-admin@netbsd.org, netbsd-bugs@netbsd.org, wiz@netbsd.org
Subject: Re: bin/44864 (sysctl unnecessarily recompiling regex)
Date: Wed, 27 Apr 2011 10:42:59 +1000

 --001636eee473f197e504a1dbb870
 Content-Type: text/plain; charset=ISO-8859-1

 Looks good. Thanks, Christos!

 -- 
 Paul Ripke
 Ubi dubium ibi libertas

 --001636eee473f197e504a1dbb870
 Content-Type: text/html; charset=ISO-8859-1

 Looks good. Thanks, Christos!<br><br>-- <br>Paul Ripke<br>Ubi dubium ibi libertas<br>

 --001636eee473f197e504a1dbb870--

State-Changed-From-To: feedback->closed
State-Changed-By: snj@NetBSD.org
State-Changed-When: Wed, 27 Apr 2011 18:54:28 +0000
State-Changed-Why:
Fixed.  Thanks!


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.