Since the set hz is (almost) never achieved the denominator is now based on the average hz the last 5 seconds.master
@@ -124,6 +124,10 @@ btpd_init(void) | |||||
btpd.port = 6881; | btpd.port = 6881; | ||||
btpd.bw_hz = 8; | btpd.bw_hz = 8; | ||||
btpd.bwcalls = 0; | |||||
for (int i = 0; i < BWCALLHISTORY; i++) | |||||
btpd.bwrate[i] = 0; | |||||
btpd.obwlim = 0; | btpd.obwlim = 0; | ||||
btpd.ibwlim = 0; | btpd.ibwlim = 0; | ||||
btpd.obw_left = 0; | btpd.obw_left = 0; | ||||
@@ -190,10 +194,12 @@ heartbeat_cb(int sd, short type, void *arg) | |||||
btpd.seconds++; | btpd.seconds++; | ||||
net_bw_rate(); | |||||
BTPDQ_FOREACH(tp, &btpd.cm_list, entry) | BTPDQ_FOREACH(tp, &btpd.cm_list, entry) | ||||
cm_by_second(tp); | cm_by_second(tp); | ||||
evtimer_add(&btpd.heartbeat, (& (struct timeval) { 0, 1000000 })); | evtimer_add(&btpd.heartbeat, (& (struct timeval) { 1, 0 })); | ||||
} | } | ||||
static void | static void | ||||
@@ -25,6 +25,8 @@ | |||||
#define BTPD_VERSION (PACKAGE_NAME "/" PACKAGE_VERSION) | #define BTPD_VERSION (PACKAGE_NAME "/" PACKAGE_VERSION) | ||||
#define BWCALLHISTORY 5 | |||||
struct child { | struct child { | ||||
pid_t pid; | pid_t pid; | ||||
void *data; | void *data; | ||||
@@ -56,6 +58,9 @@ struct btpd { | |||||
int ipc_sd; | int ipc_sd; | ||||
unsigned bw_hz; | unsigned bw_hz; | ||||
double bw_hz_avg; | |||||
unsigned bwcalls; | |||||
unsigned bwrate[BWCALLHISTORY]; | |||||
unsigned long obwlim, ibwlim; | unsigned long obwlim, ibwlim; | ||||
unsigned long ibw_left, obw_left; | unsigned long ibw_left, obw_left; | ||||
struct event bwlim; | struct event bwlim; | ||||
@@ -846,13 +846,35 @@ net_by_second(void) | |||||
} | } | ||||
} | } | ||||
void | |||||
net_bw_rate(void) | |||||
{ | |||||
unsigned sum = 0; | |||||
for (int i = 0; i < BWCALLHISTORY - 1; i++) { | |||||
btpd.bwrate[i] = btpd.bwrate[i + 1]; | |||||
sum += btpd.bwrate[i]; | |||||
} | |||||
btpd.bwrate[BWCALLHISTORY - 1] = btpd.bwcalls; | |||||
sum += btpd.bwrate[BWCALLHISTORY - 1]; | |||||
btpd.bwcalls = 0; | |||||
btpd.bw_hz_avg = sum / 5.0; | |||||
} | |||||
void | void | ||||
net_bw_cb(int sd, short type, void *arg) | net_bw_cb(int sd, short type, void *arg) | ||||
{ | { | ||||
struct peer *p; | struct peer *p; | ||||
btpd.obw_left = btpd.obwlim / btpd.bw_hz; | btpd.bwcalls++; | ||||
btpd.ibw_left = btpd.ibwlim / btpd.bw_hz; | double avg_hz; | ||||
if (btpd.seconds < BWCALLHISTORY) | |||||
avg_hz = btpd.bw_hz; | |||||
else | |||||
avg_hz = btpd.bw_hz_avg; | |||||
btpd.obw_left = btpd.obwlim / avg_hz; | |||||
btpd.ibw_left = btpd.ibwlim / avg_hz; | |||||
if (btpd.ibwlim > 0) { | if (btpd.ibwlim > 0) { | ||||
while ((p = BTPDQ_FIRST(&btpd.readq)) != NULL && btpd.ibw_left > 0) { | while ((p = BTPDQ_FIRST(&btpd.readq)) != NULL && btpd.ibw_left > 0) { | ||||
@@ -74,6 +74,7 @@ struct piece_req { | |||||
BTPDQ_HEAD(piece_req_tq, piece_req); | BTPDQ_HEAD(piece_req_tq, piece_req); | ||||
void net_connection_cb(int sd, short type, void *arg); | void net_connection_cb(int sd, short type, void *arg); | ||||
void net_bw_rate(void); | |||||
void net_bw_cb(int sd, short type, void *arg); | void net_bw_cb(int sd, short type, void *arg); | ||||
struct peer; | struct peer; | ||||