From ilepore@damnhippie.dyndns.org  Mon Oct 17 20:44:39 2011
Return-Path: <ilepore@damnhippie.dyndns.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 47BC21065675
	for <freebsd-gnats-submit@freebsd.org>; Mon, 17 Oct 2011 20:44:39 +0000 (UTC)
	(envelope-from ilepore@damnhippie.dyndns.org)
Received: from qmta09.emeryville.ca.mail.comcast.net (qmta09.emeryville.ca.mail.comcast.net [76.96.30.96])
	by mx1.freebsd.org (Postfix) with ESMTP id 2CF928FC23
	for <freebsd-gnats-submit@freebsd.org>; Mon, 17 Oct 2011 20:44:38 +0000 (UTC)
Received: from omta15.emeryville.ca.mail.comcast.net ([76.96.30.71])
	by qmta09.emeryville.ca.mail.comcast.net with comcast
	id lwhR1h0071Y3wxoA9wkXPz; Mon, 17 Oct 2011 20:44:31 +0000
Received: from damnhippie.dyndns.org ([24.8.232.202])
	by omta15.emeryville.ca.mail.comcast.net with comcast
	id lwsR1h01X4NgCEG8bwsR2r; Mon, 17 Oct 2011 20:52:26 +0000
Received: from revolution.hippie.lan (revolution.hippie.lan [172.22.42.240])
	by damnhippie.dyndns.org (8.14.3/8.14.3) with ESMTP id p9HKiauX023552
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 17 Oct 2011 14:44:36 -0600 (MDT)
	(envelope-from ilepore@damnhippie.dyndns.org)
Received: (from ilepore@localhost)
	by revolution.hippie.lan (8.14.5/8.14.4/Submit) id p9HKiaxe088680;
	Mon, 17 Oct 2011 14:44:36 -0600 (MDT)
	(envelope-from ilepore)
Message-Id: <201110172044.p9HKiaxe088680@revolution.hippie.lan>
Date: Mon, 17 Oct 2011 14:44:36 -0600 (MDT)
From: Ian Lepore <freebsd@damnhippie.dyndns.org>
Reply-To: Ian Lepore <freebsd@damnhippie.dyndns.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [patch] /bin/sh: read files in 1024-byte chunks rather than 1023
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         161756
>Category:       bin
>Synopsis:       [patch] sh(1) /bin/sh: read files in 1024-byte chunks rather than 1023
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    jilles
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Oct 17 20:50:10 UTC 2011
>Closed-Date:    Sun Apr 20 19:20:03 UTC 2014
>Last-Modified:  Sun Apr 20 19:20:03 UTC 2014
>Originator:     Ian Lepore <freebsd@damnhippie.dyndns.org>
>Release:        FreeBSD 8.2-STABLE arm
>Organization:
Symmetricom, Inc.
>Environment:
FreeBSD tflex 8.2-STABLE FreeBSD 8.2-STABLE #29: Tue Oct 11 13:32:35 UTC 2011     root@revolution.hippie.lan:/usr/obj/arm/usr/src/sys/TFLEX  arm

>Description:
Script files are read by /bin/sh in 1023-byte gulps using unbuffered IO
(read(2) calls); not very efficient for direct reads from disk.

The current code uses BUFSIZ (from stdio.h, current value is 1024) to 
allocate buffers, then reads BUFSIZ-1 bytes at a time.  The attached
patch allocates buffers using BUFSIZ+1 and reads BUFSIZ bytes at a time.  

The performance increase from reading 1024 bytes at a time isn't going to
knock your socks off, but on a slow embedded platform it can be noticible.

>How-To-Repeat:
N/A

>Fix:
This patch should apply cleanly to -current and -stable (with fuzz).

--- temp.diff begins here ---
--- bin/sh/input.c.orig	2011-10-11 10:56:15.000000000 -0600
+++ bin/sh/input.c	2011-10-17 14:24:49.000000000 -0600
@@ -97,7 +97,7 @@ int parsenleft;			/* copy of parsefile->
 MKINIT int parselleft;		/* copy of parsefile->lleft */
 char *parsenextc;		/* copy of parsefile->nextc */
 MKINIT struct parsefile basepf;	/* top level input file */
-char basebuf[BUFSIZ];		/* buffer for top level input file */
+char basebuf[BUFSIZ+1];		/* buffer for top level input file */
 static struct parsefile *parsefile = &basepf;	/* current input file */
 int init_editline = 0;		/* editline library initialized? */
 int whichprompt;		/* 1 == PS1, 2 == PS2 */
@@ -188,8 +188,8 @@ retry:
 			nr = 0;
 		else {
 			nr = el_len;
-			if (nr > BUFSIZ - 1)
-				nr = BUFSIZ - 1;
+			if (nr > BUFSIZ)
+				nr = BUFSIZ;
 			memcpy(parsenextc, rl_cp, nr);
 			if (nr != el_len) {
 				el_len -= nr;
@@ -199,7 +199,7 @@ retry:
 		}
 	} else
 #endif
-		nr = read(parsefile->fd, parsenextc, BUFSIZ - 1);
+		nr = read(parsefile->fd, parsenextc, BUFSIZ);
 
 	if (nr <= 0) {
                 if (nr < 0) {
@@ -427,13 +427,13 @@ setinputfd(int fd, int push)
 	(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
 	if (push) {
 		pushfile();
-		parsefile->buf = ckmalloc(BUFSIZ);
+		parsefile->buf = ckmalloc(BUFSIZ+1);
 	}
 	if (parsefile->fd > 0)
 		close(parsefile->fd);
 	parsefile->fd = fd;
 	if (parsefile->buf == NULL)
-		parsefile->buf = ckmalloc(BUFSIZ);
+		parsefile->buf = ckmalloc(BUFSIZ+1);
 	parselleft = parsenleft = 0;
 	plinno = 1;
 }
--- temp.diff ends here ---

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->freebsd-bugs 
Responsible-Changed-By: eadler 
Responsible-Changed-When: Tue Oct 18 03:22:41 UTC 2011 
Responsible-Changed-Why:  
fix synopsis  

http://www.freebsd.org/cgi/query-pr.cgi?pr=161756 
State-Changed-From-To: open->feedback 
State-Changed-By: jilles 
State-Changed-When: Wed Oct 19 22:33:38 UTC 2011 
State-Changed-Why:  
Although this change looks like an improvement, it does not seem 
fully satisfying. I would like to see performance numbers for the 
change on your slow embedded platform. Also, why use 1023 or 1024? 
Another buffer size may be better. 


Responsible-Changed-From-To: freebsd-bugs->jilles 
Responsible-Changed-By: jilles 
Responsible-Changed-When: Wed Oct 19 22:33:38 UTC 2011 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=161756 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/161756: commit references a PR
Date: Sat, 14 Jan 2012 22:46:33 +0000 (UTC)

 Author: jilles
 Date: Sat Jan 14 22:46:18 2012
 New Revision: 230118
 URL: http://svn.freebsd.org/changeset/base/230118
 
 Log:
   sh: Change input buffer size from 1023 to 1024.
   
   PR:		bin/161756
 
 Modified:
   head/bin/sh/input.c
 
 Modified: head/bin/sh/input.c
 ==============================================================================
 --- head/bin/sh/input.c	Sat Jan 14 21:54:12 2012	(r230117)
 +++ head/bin/sh/input.c	Sat Jan 14 22:46:18 2012	(r230118)
 @@ -97,7 +97,7 @@ int parsenleft;			/* copy of parsefile->
  MKINIT int parselleft;		/* copy of parsefile->lleft */
  char *parsenextc;		/* copy of parsefile->nextc */
  MKINIT struct parsefile basepf;	/* top level input file */
 -char basebuf[BUFSIZ];		/* buffer for top level input file */
 +char basebuf[BUFSIZ + 1];	/* buffer for top level input file */
  static struct parsefile *parsefile = &basepf;	/* current input file */
  int init_editline = 0;		/* editline library initialized? */
  int whichprompt;		/* 1 == PS1, 2 == PS2 */
 @@ -189,8 +189,8 @@ retry:
  			nr = 0;
  		else {
  			nr = el_len;
 -			if (nr > BUFSIZ - 1)
 -				nr = BUFSIZ - 1;
 +			if (nr > BUFSIZ)
 +				nr = BUFSIZ;
  			memcpy(parsenextc, rl_cp, nr);
  			if (nr != el_len) {
  				el_len -= nr;
 @@ -200,7 +200,7 @@ retry:
  		}
  	} else
  #endif
 -		nr = read(parsefile->fd, parsenextc, BUFSIZ - 1);
 +		nr = read(parsefile->fd, parsenextc, BUFSIZ);
  
  	if (nr <= 0) {
                  if (nr < 0) {
 @@ -428,13 +428,13 @@ setinputfd(int fd, int push)
  	(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
  	if (push) {
  		pushfile();
 -		parsefile->buf = ckmalloc(BUFSIZ);
 +		parsefile->buf = ckmalloc(BUFSIZ + 1);
  	}
  	if (parsefile->fd > 0)
  		close(parsefile->fd);
  	parsefile->fd = fd;
  	if (parsefile->buf == NULL)
 -		parsefile->buf = ckmalloc(BUFSIZ);
 +		parsefile->buf = ckmalloc(BUFSIZ + 1);
  	parselleft = parsenleft = 0;
  	plinno = 1;
  }
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: feedback->patched 
State-Changed-By: jilles 
State-Changed-When: Sat Jan 14 22:51:37 UTC 2012 
State-Changed-Why:  
Applied to 10-current, thanks. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=161756 
State-Changed-From-To: patched->closed 
State-Changed-By: jilles 
State-Changed-When: Sun Apr 20 19:19:28 UTC 2014 
State-Changed-Why:  
This seems not sufficiently important for MFC. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=161756 
>Unformatted:
