From fde088ce65b977bc504e09f56119ffa20dba7596 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 21 May 2020 11:31:40 +0200 Subject: [PATCH] Improved bandwidth monitoring Now measures over an entire update, which should hopefully give us more stable values. They are still small values for fast networks though so increase precision in the values we keep. --- common/rdr/FdInStream.cxx | 53 ++------------------------------------- common/rdr/FdInStream.h | 9 ------- 2 files changed, 2 insertions(+), 60 deletions(-) diff --git a/common/rdr/FdInStream.cxx b/common/rdr/FdInStream.cxx index d275338..27de92b 100644 --- a/common/rdr/FdInStream.cxx +++ b/common/rdr/FdInStream.cxx @@ -51,14 +51,12 @@ enum { DEFAULT_BUF_SIZE = 8192 }; FdInStream::FdInStream(int fd_, int timeoutms_, bool closeWhenDone_) : fd(fd_), closeWhenDone(closeWhenDone_), - timeoutms(timeoutms_), blockCallback(0), - timing(false), timeWaitedIn100us(5), timedKbits(0) + timeoutms(timeoutms_), blockCallback(0) { } FdInStream::FdInStream(int fd_, FdInStreamBlockCallback* blockCallback_) - : fd(fd_), timeoutms(0), blockCallback(blockCallback_), - timing(false), timeWaitedIn100us(5), timedKbits(0) + : fd(fd_), timeoutms(0), blockCallback(blockCallback_) { } @@ -104,10 +102,6 @@ bool FdInStream::fillBuffer(size_t maxSize, bool wait) size_t FdInStream::readWithTimeoutOrCallback(void* buf, size_t len, bool wait) { - struct timeval before, after; - if (timing) - gettimeofday(&before, 0); - int n; while (true) { do { @@ -144,48 +138,5 @@ size_t FdInStream::readWithTimeoutOrCallback(void* buf, size_t len, bool wait) if (n < 0) throw SystemException("read",errno); if (n == 0) throw EndOfStream(); - if (timing) { - gettimeofday(&after, 0); - int newTimeWaited = ((after.tv_sec - before.tv_sec) * 10000 + - (after.tv_usec - before.tv_usec) / 100); - int newKbits = n * 8 / 1000; - - // limit rate to between 10kbit/s and 40Mbit/s - - if (newTimeWaited > newKbits*1000) newTimeWaited = newKbits*1000; - if (newTimeWaited < newKbits/4) newTimeWaited = newKbits/4; - - timeWaitedIn100us += newTimeWaited; - timedKbits += newKbits; - } - return n; } - -void FdInStream::startTiming() -{ - timing = true; - - // Carry over up to 1s worth of previous rate for smoothing. - - if (timeWaitedIn100us > 10000) { - timedKbits = timedKbits * 10000 / timeWaitedIn100us; - timeWaitedIn100us = 10000; - } -} - -void FdInStream::stopTiming() -{ - timing = false; - if (timeWaitedIn100us < timedKbits/2) - timeWaitedIn100us = timedKbits/2; // upper limit 20Mbit/s -} - -unsigned int FdInStream::kbitsPerSecond() -{ - // The following calculation will overflow 32-bit arithmetic if we have - // received more than about 50Mbytes (400Mbits) since we started timing, so - // it should be OK for a single RFB update. - - return timedKbits * 10000 / timeWaitedIn100us; -} diff --git a/common/rdr/FdInStream.h b/common/rdr/FdInStream.h index 82280f9..0203389 100644 --- a/common/rdr/FdInStream.h +++ b/common/rdr/FdInStream.h @@ -45,11 +45,6 @@ namespace rdr { void setBlockCallback(FdInStreamBlockCallback* blockCallback); int getFd() { return fd; } - void startTiming(); - void stopTiming(); - unsigned int kbitsPerSecond(); - unsigned int timeWaited() { return timeWaitedIn100us; } - private: virtual bool fillBuffer(size_t maxSize, bool wait); @@ -60,10 +55,6 @@ namespace rdr { int timeoutms; FdInStreamBlockCallback* blockCallback; - bool timing; - unsigned int timeWaitedIn100us; - unsigned int timedKbits; - size_t offset; U8* start; };