2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2011-01-19 13:48:47 +00:00
|
|
|
* Copyright (c) Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-11-12 22:26:10 +00:00
|
|
|
#include <string.h> /* memset */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "platform.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "ratecontrol.h"
|
|
|
|
#include "utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-07 04:13:13 +00:00
|
|
|
/* return the xfer rate over the last `interval' seconds in KiB/sec */
|
2010-07-03 00:25:22 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
rateForInterval( const tr_ratecontrol * r,
|
2009-01-05 04:27:54 +00:00
|
|
|
int interval_msec,
|
|
|
|
uint64_t now )
|
2007-07-07 04:13:13 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint64_t bytes = 0;
|
2010-07-11 20:49:19 +00:00
|
|
|
const uint64_t cutoff = (now?now:tr_time_msec()) - interval_msec;
|
2008-09-23 19:11:04 +00:00
|
|
|
int i = r->newest;
|
|
|
|
|
|
|
|
for( ; ; )
|
2007-07-07 04:13:13 +00:00
|
|
|
{
|
2008-06-27 02:42:44 +00:00
|
|
|
if( r->transfers[i].date <= cutoff )
|
2007-07-07 04:13:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
bytes += r->transfers[i].size;
|
2007-01-22 22:58:51 +00:00
|
|
|
|
2009-01-02 20:42:35 +00:00
|
|
|
if( --i == -1 ) i = TR_RC_HISTORY_SIZE - 1; /* circular history */
|
2007-07-07 04:13:13 +00:00
|
|
|
if( i == r->newest ) break; /* we've come all the way around */
|
|
|
|
}
|
2007-01-22 22:58:51 +00:00
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
return bytes * ( 1000.0 / interval_msec );
|
2007-07-07 04:13:13 +00:00
|
|
|
}
|
2007-01-22 22:58:51 +00:00
|
|
|
|
2007-07-07 04:13:13 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
int
|
|
|
|
tr_rcRate_Bps( const tr_ratecontrol * r, uint64_t now )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2010-07-03 00:25:22 +00:00
|
|
|
int ret = 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-03-23 20:54:54 +00:00
|
|
|
if( r )
|
2009-01-05 04:27:54 +00:00
|
|
|
ret = rateForInterval( r, TR_RC_HISTORY_MSEC, now );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-07 04:13:13 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-07 04:13:13 +00:00
|
|
|
void
|
2011-01-06 01:00:21 +00:00
|
|
|
tr_rcTransferred( tr_ratecontrol * r, size_t size )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2010-07-11 20:49:19 +00:00
|
|
|
const uint64_t now = tr_time_msec ( );
|
2007-07-07 04:13:13 +00:00
|
|
|
|
2009-01-02 20:42:35 +00:00
|
|
|
if( r->transfers[r->newest].date + TR_RC_GRANULARITY_MSEC >= now )
|
2007-07-07 04:13:13 +00:00
|
|
|
r->transfers[r->newest].size += size;
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2009-01-02 20:42:35 +00:00
|
|
|
if( ++r->newest == TR_RC_HISTORY_SIZE ) r->newest = 0;
|
2007-07-07 04:13:13 +00:00
|
|
|
r->transfers[r->newest].date = now;
|
|
|
|
r->transfers[r->newest].size = size;
|
|
|
|
}
|
|
|
|
}
|