From 08571358b19618268310c975448be5f49dd6c667 Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Tue, 9 Apr 2002 04:29:46 +0000 Subject: [PATCH] Factor out bwlimit sleep code from writefd_unbuffered into its own function. --- io.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/io.c b/io.c index 07e1feb5..63df2504 100644 --- a/io.c +++ b/io.c @@ -390,6 +390,33 @@ unsigned char read_byte(int f) } +/** + * Sleep after writing to limit I/O bandwidth usage. + * + * @todo Rather than sleeping after each write, it might be better to + * use some kind of averaging. The current algorithm seems to always + * use a bit less bandwidth than specified, because it doesn't make up + * for slow periods. But arguably this is a feature. In addition, we + * ought to take the time used to write the data into account. + **/ +static void sleep_for_bwlimit(int bytes_written) +{ + struct timeval tv; + + if (!bwlimit) + return; + + tv.tv_sec = 0; + tv.tv_usec = bytes_written * 1000 / bwlimit; + + while (tv.tv_usec > 1000000) { + tv.tv_sec++; + tv.tv_usec -= 1000000; + } + select(0, NULL, NULL, NULL, tv); +} + + /** * Write len bytes to the file descriptor @p fd. * @@ -470,18 +497,7 @@ static void writefd_unbuffered(int fd,char *buf,size_t len) exit_cleanup(RERR_STREAMIO); } - /* Sleep after writing to limit I/O bandwidth */ - if (bwlimit) - { - tv.tv_sec = 0; - tv.tv_usec = ret * 1000 / bwlimit; - while (tv.tv_usec > 1000000) - { - tv.tv_sec++; - tv.tv_usec -= 1000000; - } - select(0, NULL, NULL, NULL, &tv); - } + sleep_for_bwlimit(ret); total += ret; -- 2.34.1