From philip@gandalf.eilio.com  Wed Mar  5 17:05:01 2003
Return-Path: <philip@gandalf.eilio.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 8221037B401
	for <FreeBSD-gnats-submit@freebsd.org>; Wed,  5 Mar 2003 17:05:01 -0800 (PST)
Received: from gandalf.eilio.com (gandalf.eilio.com [216.211.130.9])
	by mx1.FreeBSD.org (Postfix) with ESMTP id E478C43F85
	for <FreeBSD-gnats-submit@freebsd.org>; Wed,  5 Mar 2003 17:05:00 -0800 (PST)
	(envelope-from philip@gandalf.eilio.com)
Received: from gandalf.eilio.com (gandalf.eilio.com [216.211.130.9])
	by gandalf.eilio.com (8.12.6/8.12.6) with ESMTP id h26150qs097325
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 5 Mar 2003 17:05:00 -0800 (PST)
	(envelope-from philip@gandalf.eilio.com)
Received: (from philip@localhost)
	by gandalf.eilio.com (8.12.6/8.12.6/Submit) id h261508I097324;
	Wed, 5 Mar 2003 17:05:00 -0800 (PST)
Message-Id: <200303060105.h261508I097324@gandalf.eilio.com>
Date: Wed, 5 Mar 2003 17:05:00 -0800 (PST)
From: Philip Hallstrom <philip@gandalf.eilio.com>
Reply-To: Philip Hallstrom <philip@gandalf.eilio.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [PATCH] modify /usr/bin/fetch to allow bandwidth limiting
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         48962
>Category:       bin
>Synopsis:       [patch] modify fetch(1) to allow bandwidth limiting
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    des
>State:          suspended
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Wed Mar 05 17:10:06 PST 2003
>Closed-Date:    
>Last-Modified:  Sat May 24 17:20:36 UTC 2008
>Originator:     Philip Hallstrom <philip@eilio.com>
>Release:        FreeBSD 4.7-20030128-STABLE i386
>Organization:
>Environment:
System: FreeBSD gandalf.eilio.com 4.7-20030128-STABLE FreeBSD 4.7-20030128-STABLE #0: Fri Feb 7 10:56:11 PST 2003 root@warden.adhesivemedia.com:/usr/obj/usr/src/sys/WARDEN i386


	
>Description:
	This is a patch to /usr/bin/fetch to allow limiting the amount of bandwidth
    used during the transfer.  The following command would restrict fetch to only
    using 20000 bytes/sec (or very close to that).

    fetch -L 20000 http://domain/file

    I've been transfering a lot of files over DSL and without this I can't do much
    else while fetch is running since it takes all the bandwidth (naturally).

    I've never done this before so if you need anything else let me know.
>How-To-Repeat:
	
>Fix:

	The following are patches to the following files:

/usr/src/usr.bin/fetch/fetch.c v 1.10.2.20 2002/12/04 10:28:00 des Exp
/usr/src/usr.bin/fetch/fetch.1 v 1.33.2.11 2002/06/20 23:45:50 charnier Exp

--- fetch.c.diff begins here ---
*** fetch.c-orig	Wed Mar  5 14:40:30 2003
--- fetch.c	Wed Mar  5 16:43:04 2003
***************
*** 80,85 ****
--- 80,86 ----
  pid_t	 pgrp;		/*        our process group */
  long	 w_secs;	/*    -w: retry delay */
  int	 family = PF_UNSPEC;	/* -[46]: address family to use */
+ long	 limit_rate = 0;	/* -L: limit download to N bytes/sec */
  
  int	 sigalrm;	/* SIGALRM received */
  int	 siginfo;	/* SIGINFO received */
***************
*** 282,287 ****
--- 283,291 ----
  	u_int timeout;
  	u_char *ptr;
  
+ 	struct timeval limit_tv_now;
+ 	float limit_elapsed;
+ 
  	f = of = NULL;
  	tmppath = NULL;
  
***************
*** 548,554 ****
--- 552,569 ----
  			else
  				break;
  		}
+ 
  		stat_update(&xs, count += size);
+ 
+ 		/* If desired limit the amount of bandwidth used. */
+ 		if( limit_rate > 0 ) {
+ 			gettimeofday(&limit_tv_now, NULL);
+ 			limit_elapsed = ((limit_tv_now.tv_sec - xs.start.tv_sec) + (limit_tv_now.tv_usec - xs.start.tv_usec) / 1000000.0) ;
+ 			if( xs.rcvd / limit_elapsed > limit_rate ) {
+ 				usleep(1000000 * (xs.rcvd / (float) limit_rate - limit_elapsed));
+ 			}
+ 		}
+ 
  		for (ptr = buf; size > 0; ptr += wr, size -= wr)
  			if ((wr = fwrite(ptr, 1, size, of)) < size) {
  				if (ferror(of) && errno == EINTR && !sigint)
***************
*** 655,661 ****
  {
  	fprintf(stderr, "%s\n%s\n%s\n",
  	    "usage: fetch [-146AFMPRUadlmnpqrsv] [-o outputfile] [-S bytes]",
! 	    "             [-B bytes] [-T seconds] [-w seconds]",
  	    "             [-h host -f file [-c dir] | URL ...]");
  }
  
--- 670,676 ----
  {
  	fprintf(stderr, "%s\n%s\n%s\n",
  	    "usage: fetch [-146AFMPRUadlmnpqrsv] [-o outputfile] [-S bytes]",
! 	    "             [-B bytes] [-T seconds] [-w seconds] [-L bytes/sec]",
  	    "             [-h host -f file [-c dir] | URL ...]");
  }
  
***************
*** 673,679 ****
  	int c, e, r;
  
  	while ((c = getopt(argc, argv,
! 	    "146AaB:bc:dFf:Hh:lMmnPpo:qRrS:sT:tUvw:")) != -1)
  		switch (c) {
  		case '1':
  			once_flag = 1;
--- 688,694 ----
  	int c, e, r;
  
  	while ((c = getopt(argc, argv,
! 	    "146AaB:bc:dFf:Hh:L:lMmnPpo:qRrS:sT:tUvw:")) != -1)
  		switch (c) {
  		case '1':
  			once_flag = 1;
***************
*** 717,722 ****
--- 732,742 ----
  			break;
  		case 'h':
  			h_hostname = optarg;
+ 			break;
+ 		case 'L':
+ 			limit_rate = strtol(optarg, &end, 10);
+ 			if (*optarg == '\0' || *end != '\0')
+ 				errx(1, "invalid bytes/sec limit (%s)", optarg);
  			break;
  		case 'l':
  			l_flag = 1;
--- fetch.c.diff ends here ---

--- fetch.1.diff begins here ---
*** fetch.1-orig	Wed Mar  5 16:43:34 2003
--- fetch.1	Wed Mar  5 16:45:04 2003
***************
*** 107,112 ****
--- 107,114 ----
  .Ar host .
  This option is deprecated and is provided for backward compatibility
  only.
+ .It Fl L Ar bytes/sec
+ Limit the transfer rate to bytes/sec.  Default (0) is not to limit rate.
  .It Fl l
  If the target is a file-scheme URL, make a symbolic link to the target
  rather than trying to copy it.
--- fetch.1.diff ends here ---


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->des 
Responsible-Changed-By: roam 
Responsible-Changed-When: Thu Mar 6 06:29:01 PST 2003 
Responsible-Changed-Why:  
Over to the fetch/libfetch author/maintainer. 

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

From: Philip Hallstrom <philip@eilio.com>
To: FreeBSD-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/48962: [PATCH] modify /usr/bin/fetch to allow bandwidth
 limiting
Date: Thu, 6 Mar 2003 08:42:22 -0800 (PST)

 I just noticed a problem when computing the elapsed time.  Line 561 is
 currently:
 
 limit_elapsed = ((limit_tv_now.tv_sec - xs.start.tv_sec) +
 (limit_tv_now.tv_usec - xs.start.tv_usec) / 1000000.0) ;
 
 and should be (notice the addition of "abs"):
 
 limit_elapsed = ((limit_tv_now.tv_sec - xs.start.tv_sec) +
 abs(limit_tv_now.tv_usec - xs.start.tv_usec) / 1000000.0) ;
 
 -philip
 
 On Wed, 5 Mar 2003 FreeBSD-gnats-submit@FreeBSD.org wrote:
 
 > Thank you very much for your problem report.
 > It has the internal identification `bin/48962'.
 > The individual assigned to look at your
 > report is: freebsd-bugs.
 >
 > You can access the state of your problem report at any time
 > via this link:
 >
 > http://www.freebsd.org/cgi/query-pr.cgi?pr=48962
 >
 > >Category:       bin
 > >Responsible:    freebsd-bugs
 > >Synopsis:       [PATCH] modify /usr/bin/fetch to allow bandwidth limiting
 > >Arrival-Date:   Wed Mar 05 17:10:06 PST 2003
 >

From: Dag-Erling Smorgrav <des@ofug.org>
To: Philip Hallstrom <philip@gandalf.eilio.com>
Cc: FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/48962: [PATCH] modify /usr/bin/fetch to allow bandwidth
 limiting
Date: Fri, 07 Mar 2003 13:54:07 +0100

 Philip Hallstrom <philip@gandalf.eilio.com> writes:
 >     I've been transfering a lot of files over DSL and without this I can't do much
 >     else while fetch is running since it takes all the bandwidth (naturally).
 
 The patch won't protect you against multiple instances of fetch(1)
 running concurrently.  The correct solution is to set up ipfw with
 dummynet, and limit non-SSH traffic to 80% or 90% of your available
 bandwidth.
 
 DES
 -- 
 Dag-Erling Smorgrav - des@ofug.org

From: Philip Hallstrom <philip@eilio.com>
To: Dag-Erling Smorgrav <des@ofug.org>
Cc: FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/48962: [PATCH] modify /usr/bin/fetch to allow bandwidth
 limiting
Date: Fri, 7 Mar 2003 08:04:09 -0800 (PST)

 Yeah, I know, but as the sole sysadmin moving a lot of data around
 "one-time" so to speak it works really well...
 
 Also, when building a new machine's ports I can set "FETCH_CMD" to "fetch
 -L 30000" and leave me 20kb/s for the rest of the office provided I only
 build one at a time which I usually do using porteasy...
 
 -philip
 
 On Fri, 7 Mar 2003, Dag-Erling Smorgrav wrote:
 
 > Philip Hallstrom <philip@gandalf.eilio.com> writes:
 > >     I've been transfering a lot of files over DSL and without this I can't do much
 > >     else while fetch is running since it takes all the bandwidth (naturally).
 >
 > The patch won't protect you against multiple instances of fetch(1)
 > running concurrently.  The correct solution is to set up ipfw with
 > dummynet, and limit non-SSH traffic to 80% or 90% of your available
 > bandwidth.
 >
 > DES
 > --
 > Dag-Erling Smorgrav - des@ofug.org
 >
State-Changed-From-To: open->suspended 
State-Changed-By: des 
State-Changed-When: Tue Aug 19 03:36:30 PDT 2003 
State-Changed-Why:  
Interesting patch, but not likely to be committed in the near future 

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