diff --git a/libretroshare/src/pqi/pqihandler.cc b/libretroshare/src/pqi/pqihandler.cc index 72d7dbf9d..7074670a2 100644 --- a/libretroshare/src/pqi/pqihandler.cc +++ b/libretroshare/src/pqi/pqihandler.cc @@ -437,43 +437,45 @@ int pqihandler::UpdateRates() RsDbg() << "UPDATE_RATES pqihandler::UpdateRates mod_index " << mod_index << " in_max_bw " << in_max_bw << " remaining in bw " << in_remaining_bw << std::endl; #endif - /* Allocate only half the remaining in bw, if any, to make it smoother */ - in_max_bw = in_max_bw + in_remaining_bw / 2; + // allocate all remaining in bw + in_max_bw = in_max_bw + in_remaining_bw; - // store current total in and ou used bw + // store current total in and out used bw locked_StoreCurrentRates(used_bw_in, used_bw_out); #ifdef UPDATE_RATES_DEBUG RsDbg() << "UPDATE_RATES pqihandler::UpdateRates setting new out_max " << out_max_bw << " in_max " << in_max_bw << std::endl; #endif - // retrieve down (from peer point of view) bandwidth limits set by peers in their own settings + // retrieve the bandwidth limits provided by peers via BwCtrl std::map rateMap; rsConfig->getAllBandwidthRates(rateMap); std::map::iterator rateMap_it; #ifdef UPDATE_RATES_DEBUG - // Dump RsConfigurationDataRates + // dump RsConfigurationDataRates RsDbg() << "UPDATE_RATES pqihandler::UpdateRates RsConfigDataRates dump" << std::endl; for (rateMap_it = rateMap.begin(); rateMap_it != rateMap.end(); rateMap_it++) RsDbg () << "UPDATE_RATES pqihandler::UpdateRates PeerId " << rateMap_it->first.toStdString() << " mAllowedOut " << rateMap_it->second.mAllowedOut << std::endl; #endif - // update max rates taking into account the limits set by peers in their own settings + // update max rates for(it = mods.begin(); it != mods.end(); ++it) { SearchModule *mod = (it -> second); - // for our down bandwidth we set the max to the calculated value without taking into account the max set by peers: they will control their up bw on their side + // for our down bandwidth we use the calculated value without taking into account the max up provided by peers via BwCtrl + // this is harmless as they will control their up bw on their side mod -> pqi -> setMaxRate(true, in_max_bw); - // for our up bandwidth we limit to the maximum down bw provided by peers via BwCtrl because we don't want to clog our outqueues, the SSL buffers, and our friends inbound queues + // for our up bandwidth we take into account the max down provided by peers via BwCtrl + // because we don't want to clog our outqueues, the TCP buffers, and the peers inbound queues if ((rateMap_it = rateMap.find(mod->pqi->PeerId())) != rateMap.end()) { if (rateMap_it->second.mAllowedOut > 0) { - if (out_max_bw > rateMap_it->second.mAllowedOut) - mod -> pqi -> setMaxRate(false, rateMap_it->second.mAllowedOut); + if (out_max_bw > 0.95 * rateMap_it->second.mAllowedOut) + mod -> pqi -> setMaxRate(false, 0.95 * rateMap_it->second.mAllowedOut); else mod -> pqi -> setMaxRate(false, out_max_bw); } diff --git a/libretroshare/src/pqi/pqistreamer.cc b/libretroshare/src/pqi/pqistreamer.cc index 62b4d7f84..f5d61ab9f 100644 --- a/libretroshare/src/pqi/pqistreamer.cc +++ b/libretroshare/src/pqi/pqistreamer.cc @@ -550,14 +550,12 @@ int pqistreamer::handleoutgoing_locked() if ((!(mBio->cansend(0))) || (maxbytes < sentbytes)) { - -#ifdef DEBUG_PACKET_SLICING - if (maxbytes < sentbytes) - std::cerr << "pqistreamer::handleoutgoing_locked() Stopped sending: bio not ready. maxbytes=" << maxbytes << ", sentbytes=" << sentbytes << std::endl; - else - std::cerr << "pqistreamer::handleoutgoing_locked() Stopped sending: sentbytes=" << sentbytes << ", max=" << maxbytes << std::endl; +#ifdef DEBUG_PQISTREAMER + if (sentbytes > maxbytes) + RsDbg() << "PQISTREAMER pqistreamer::handleoutgoing_locked() stopped sending max reached, sentbytes " << std::dec << sentbytes << " maxbytes " << maxbytes; + else + RsDbg() << "PQISTREAMER pqistreamer::handleoutgoing_locked() stopped sending bio not ready, sentbytes " << std::dec << sentbytes << " maxbytes " << maxbytes; #endif - return 0; } // send a out_pkt., else send out_data. unless there is a pending packet. The strategy is to @@ -1019,12 +1017,11 @@ continue_packet: if(maxin > readbytes && mBio->moretoread(0)) goto start_packet_read ; -#ifdef DEBUG_TRANSFERS - if (readbytes >= maxin) - { - std::cerr << "pqistreamer::handleincoming() Stopped reading as readbytes >= maxin. Read " << readbytes << " bytes "; - std::cerr << std::endl; - } +#ifdef DEBUG_PQISTREAMER + if (readbytes > maxin) + RsDbg() << "PQISTREAMER pqistreamer::handleincoming() stopped reading max reached, readbytes " << std::dec << readbytes << " maxin " << maxin; + else + RsDbg() << "PQISTREAMER pqistreamer::handleincoming() stopped reading no more to read, readbytes " << std::dec << readbytes << " maxin " << maxin; #endif return 0; @@ -1157,18 +1154,19 @@ int pqistreamer::outAllowedBytes_locked() // this is used to take into account a possible excess of data sent during the previous round mCurrSent -= int(dt * maxout); + // we dont allow negative value, any quota not used during the previous round is therefore lost if (mCurrSent < 0) mCurrSent = 0; mCurrSentTS = t; - // now calculate the max amount of data allowed to be sent during the next round - // we limit this quota to what should be sent at most during mAvgDtOut, taking into account the excess of data possibly sent during the previous round + // now calculate the amount of data allowed to be sent during the next round + // we take into account the possible excess (but not deficit) of the previous round + // (this is handled differently when reading data, see below) double quota = mAvgDtOut * maxout - mCurrSent; #ifdef DEBUG_PQISTREAMER - uint64_t t_now = 1000 * getCurrentTS(); - std::cerr << std::dec << t_now << " DEBUG_PQISTREAMER pqistreamer::outAllowedBytes_locked PeerId " << this->PeerId().toStdString() << " dt " << (int)(1000 * dt) << "ms, mAvgDtOut " << (int)(1000 * mAvgDtOut) << "ms, maxout " << (int)(maxout) << " bytes/s, mCurrSent " << mCurrSent << " bytes, quota " << (int)(quota) << " bytes" << std::endl; + RsDbg() << "PQISTREAMER pqistreamer::outAllowedBytes_locked() dt " << std::dec << (int)(1000 * dt) << "ms, mAvgDtOut " << (int)(1000 * mAvgDtOut) << "ms, maxout " << (int)(maxout) << " bytes/s, mCurrSent " << mCurrSent << " bytes, quota " << (int)(quota) << " bytes"; #endif return quota; @@ -1198,27 +1196,27 @@ int pqistreamer::inAllowedBytes() double maxin = getMaxRate(true) * 1024.0; - // this is used to take into account a possible excess of data received during the previous round + // this is used to take into account a possible excess/deficit of data received during the previous round mCurrRead -= int(dt * maxin); - if (mCurrRead < 0) - mCurrRead = 0; + // we allow negative value up to the average amount of data received during one round + // in that case we will use this credit during the next around + if (mCurrRead < - mAvgDtIn * maxin) + mCurrRead = - mAvgDtIn * maxin; mCurrReadTS = t; - // now calculate the max amount of data allowed to be received during the next round - // we limit this quota to what should be received at most during mAvgDtOut, taking into account the excess of data possibly received during the previous round + // we now calculate the max amount of data allowed to be received during the next round + // we take into account the excess/deficit of the previous round double quota = mAvgDtIn * maxin - mCurrRead; #ifdef DEBUG_PQISTREAMER - uint64_t t_now = 1000 * getCurrentTS(); - std::cerr << std::dec << t_now << " DEBUG_PQISTREAMER pqistreamer::inAllowedBytes PeerId " << this->PeerId().toStdString() << " dt " << (int)(1000 * dt) << "ms, mAvgDtIn " << (int)(1000 * mAvgDtIn) << "ms, maxin " << (int)(maxin) << " bytes/s, mCurrRead " << mCurrRead << " bytes, quota " << (int)(quota) << " bytes" << std::endl; + RsDbg() << "PQISTREAMER pqistreamer::inAllowedBytes() dt " << std::dec << (int)(1000 * dt) << "ms, mAvgDtIn " << (int)(1000 * mAvgDtIn) << "ms, maxin " << (int)(maxin) << " bytes/s, mCurrRead " << mCurrRead << " bytes, quota " << (int)(quota) << " bytes"; #endif return quota; } - void pqistreamer::outSentBytes_locked(uint32_t outb) { #ifdef DEBUG_PQISTREAMER