2014-09-29 12:30:39 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Markus Goetz <markus@woboq.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "owncloudpropagator.h"
|
2014-11-11 14:16:14 +03:00
|
|
|
#include "propagatedownload.h"
|
|
|
|
#include "propagateupload.h"
|
2014-09-29 12:30:39 +04:00
|
|
|
#include "propagatorjobs.h"
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
2014-09-29 12:30:39 +04:00
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <windef.h>
|
|
|
|
#include <winbase.h>
|
|
|
|
#endif
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
#include <QLoggingCategory>
|
2014-09-29 12:30:39 +04:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QObject>
|
|
|
|
|
2014-12-02 16:20:13 +03:00
|
|
|
namespace OCC {
|
2014-09-29 12:30:39 +04:00
|
|
|
|
2017-12-28 22:33:10 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcBandwidthManager, "nextcloud.sync.bandwidthmanager", QtInfoMsg)
|
2017-05-09 15:24:11 +03:00
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
// Because of the many layers of buffering inside Qt (and probably the OS and the network)
|
|
|
|
// we cannot lower this value much more. If we do, the estimated bw will be very high
|
|
|
|
// because the buffers fill fast while the actual network algorithms are not relevant yet.
|
|
|
|
static qint64 relativeLimitMeasuringTimerIntervalMsec = 1000 * 2;
|
|
|
|
// See also WritingState in http://code.woboq.org/qt5/qtbase/src/network/access/qhttpprotocolhandler.cpp.html#_ZN20QHttpProtocolHandler11sendRequestEv
|
|
|
|
|
|
|
|
// FIXME At some point:
|
|
|
|
// * Register device only after the QNR received its metaDataChanged() signal
|
|
|
|
// * Incorporate Qt buffer fill state (it's a negative absolute delta).
|
|
|
|
// * Incorporate SSL overhead (percentage)
|
|
|
|
// * For relative limiting, do less measuring and more delaying+giving quota
|
|
|
|
// * For relative limiting, smoothen measurements
|
|
|
|
|
|
|
|
BandwidthManager::BandwidthManager(OwncloudPropagator *p)
|
|
|
|
: QObject()
|
2014-09-30 08:05:23 +04:00
|
|
|
, _propagator(p)
|
2018-11-12 20:46:39 +03:00
|
|
|
, _relativeLimitCurrentMeasuredDevice(nullptr)
|
2014-09-29 12:30:39 +04:00
|
|
|
, _relativeUploadLimitProgressAtMeasuringRestart(0)
|
|
|
|
, _currentUploadLimit(0)
|
2018-11-12 20:46:39 +03:00
|
|
|
, _relativeLimitCurrentMeasuredJob(nullptr)
|
2014-09-30 08:05:23 +04:00
|
|
|
, _currentDownloadLimit(0)
|
2014-09-29 12:30:39 +04:00
|
|
|
{
|
|
|
|
_currentUploadLimit = _propagator->_uploadLimit.fetchAndAddAcquire(0);
|
|
|
|
_currentDownloadLimit = _propagator->_downloadLimit.fetchAndAddAcquire(0);
|
|
|
|
|
2017-09-20 11:14:48 +03:00
|
|
|
QObject::connect(&_switchingTimer, &QTimer::timeout, this, &BandwidthManager::switchingTimerExpired);
|
2014-09-29 12:30:39 +04:00
|
|
|
_switchingTimer.setInterval(10 * 1000);
|
|
|
|
_switchingTimer.start();
|
|
|
|
QMetaObject::invokeMethod(this, "switchingTimerExpired", Qt::QueuedConnection);
|
|
|
|
|
2014-09-30 08:05:23 +04:00
|
|
|
// absolute uploads/downloads
|
2017-09-20 11:14:48 +03:00
|
|
|
QObject::connect(&_absoluteLimitTimer, &QTimer::timeout, this, &BandwidthManager::absoluteLimitTimerExpired);
|
2014-09-30 08:05:23 +04:00
|
|
|
_absoluteLimitTimer.setInterval(1000);
|
|
|
|
_absoluteLimitTimer.start();
|
|
|
|
|
|
|
|
// Relative uploads
|
2017-09-20 11:14:48 +03:00
|
|
|
QObject::connect(&_relativeUploadMeasuringTimer, &QTimer::timeout,
|
|
|
|
this, &BandwidthManager::relativeUploadMeasuringTimerExpired);
|
2014-09-29 12:30:39 +04:00
|
|
|
_relativeUploadMeasuringTimer.setInterval(relativeLimitMeasuringTimerIntervalMsec);
|
|
|
|
_relativeUploadMeasuringTimer.start();
|
|
|
|
_relativeUploadMeasuringTimer.setSingleShot(true); // will be restarted from the delay timer
|
2017-09-20 11:14:48 +03:00
|
|
|
QObject::connect(&_relativeUploadDelayTimer, &QTimer::timeout,
|
|
|
|
this, &BandwidthManager::relativeUploadDelayTimerExpired);
|
2014-09-29 12:30:39 +04:00
|
|
|
_relativeUploadDelayTimer.setSingleShot(true); // will be restarted from the measuring timer
|
2014-09-30 08:05:23 +04:00
|
|
|
|
|
|
|
// Relative downloads
|
2017-09-20 11:14:48 +03:00
|
|
|
QObject::connect(&_relativeDownloadMeasuringTimer, &QTimer::timeout,
|
|
|
|
this, &BandwidthManager::relativeDownloadMeasuringTimerExpired);
|
2014-09-30 08:05:23 +04:00
|
|
|
_relativeDownloadMeasuringTimer.setInterval(relativeLimitMeasuringTimerIntervalMsec);
|
|
|
|
_relativeDownloadMeasuringTimer.start();
|
|
|
|
_relativeDownloadMeasuringTimer.setSingleShot(true); // will be restarted from the delay timer
|
2017-09-20 11:14:48 +03:00
|
|
|
QObject::connect(&_relativeDownloadDelayTimer, &QTimer::timeout,
|
|
|
|
this, &BandwidthManager::relativeDownloadDelayTimerExpired);
|
2014-09-30 08:05:23 +04:00
|
|
|
_relativeDownloadDelayTimer.setSingleShot(true); // will be restarted from the measuring timer
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
|
|
|
|
2020-05-25 22:33:24 +03:00
|
|
|
BandwidthManager::~BandwidthManager() = default;
|
2015-02-06 13:19:41 +03:00
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
void BandwidthManager::registerUploadDevice(UploadDevice *p)
|
|
|
|
{
|
|
|
|
_absoluteUploadDeviceList.append(p);
|
|
|
|
_relativeUploadDeviceList.append(p);
|
2017-09-25 12:55:18 +03:00
|
|
|
QObject::connect(p, &QObject::destroyed, this, &BandwidthManager::unregisterUploadDevice);
|
2014-09-29 12:30:39 +04:00
|
|
|
|
|
|
|
if (usingAbsoluteUploadLimit()) {
|
|
|
|
p->setBandwidthLimited(true);
|
|
|
|
p->setChoked(false);
|
|
|
|
} else if (usingRelativeUploadLimit()) {
|
|
|
|
p->setBandwidthLimited(true);
|
|
|
|
p->setChoked(true);
|
|
|
|
} else {
|
|
|
|
p->setBandwidthLimited(false);
|
|
|
|
p->setChoked(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BandwidthManager::unregisterUploadDevice(QObject *o)
|
|
|
|
{
|
2017-09-25 12:55:18 +03:00
|
|
|
auto p = reinterpret_cast<UploadDevice *>(o); // note, we might already be in the ~QObject
|
2014-09-29 12:30:39 +04:00
|
|
|
_absoluteUploadDeviceList.removeAll(p);
|
|
|
|
_relativeUploadDeviceList.removeAll(p);
|
|
|
|
if (p == _relativeLimitCurrentMeasuredDevice) {
|
2018-11-12 20:46:39 +03:00
|
|
|
_relativeLimitCurrentMeasuredDevice = nullptr;
|
2014-12-02 11:37:48 +03:00
|
|
|
_relativeUploadLimitProgressAtMeasuringRestart = 0;
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BandwidthManager::registerDownloadJob(GETFileJob *j)
|
|
|
|
{
|
|
|
|
_downloadJobList.append(j);
|
2017-09-25 12:55:18 +03:00
|
|
|
QObject::connect(j, &QObject::destroyed, this, &BandwidthManager::unregisterDownloadJob);
|
2014-09-29 12:30:39 +04:00
|
|
|
|
|
|
|
if (usingAbsoluteDownloadLimit()) {
|
|
|
|
j->setBandwidthLimited(true);
|
|
|
|
j->setChoked(false);
|
|
|
|
} else if (usingRelativeDownloadLimit()) {
|
|
|
|
j->setBandwidthLimited(true);
|
|
|
|
j->setChoked(true);
|
|
|
|
} else {
|
|
|
|
j->setBandwidthLimited(false);
|
|
|
|
j->setChoked(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 12:55:18 +03:00
|
|
|
void BandwidthManager::unregisterDownloadJob(QObject *o)
|
2014-09-29 12:30:39 +04:00
|
|
|
{
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *j = reinterpret_cast<GETFileJob *>(o); // note, we might already be in the ~QObject
|
2014-09-29 12:30:39 +04:00
|
|
|
_downloadJobList.removeAll(j);
|
2014-12-02 11:37:48 +03:00
|
|
|
if (_relativeLimitCurrentMeasuredJob == j) {
|
2018-11-12 20:46:39 +03:00
|
|
|
_relativeLimitCurrentMeasuredJob = nullptr;
|
2014-12-02 11:37:48 +03:00
|
|
|
_relativeDownloadLimitProgressAtMeasuringRestart = 0;
|
|
|
|
}
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BandwidthManager::relativeUploadMeasuringTimerExpired()
|
|
|
|
{
|
2014-10-07 19:42:50 +04:00
|
|
|
if (!usingRelativeUploadLimit() || _relativeUploadDeviceList.count() == 0) {
|
2014-09-29 12:30:39 +04:00
|
|
|
// Not in this limiting mode, just wait 1 sec to continue the cycle
|
|
|
|
_relativeUploadDelayTimer.setInterval(1000);
|
|
|
|
_relativeUploadDelayTimer.start();
|
|
|
|
return;
|
|
|
|
}
|
2020-06-10 04:47:49 +03:00
|
|
|
if (!_relativeLimitCurrentMeasuredDevice) {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << "No device set, just waiting 1 sec";
|
2014-09-29 12:30:39 +04:00
|
|
|
_relativeUploadDelayTimer.setInterval(1000);
|
|
|
|
_relativeUploadDelayTimer.start();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << _relativeUploadDeviceList.count() << "Starting Delay";
|
2014-09-29 12:30:39 +04:00
|
|
|
|
|
|
|
qint64 relativeLimitProgressMeasured = (_relativeLimitCurrentMeasuredDevice->_readWithProgress
|
|
|
|
+ _relativeLimitCurrentMeasuredDevice->_read)
|
|
|
|
/ 2;
|
|
|
|
qint64 relativeLimitProgressDifference = relativeLimitProgressMeasured - _relativeUploadLimitProgressAtMeasuringRestart;
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << _relativeUploadLimitProgressAtMeasuringRestart
|
|
|
|
<< relativeLimitProgressMeasured << relativeLimitProgressDifference;
|
2014-09-29 12:30:39 +04:00
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
qint64 speedkBPerSec = (relativeLimitProgressDifference / relativeLimitMeasuringTimerIntervalMsec * 1000.0) / 1024.0;
|
|
|
|
qCDebug(lcBandwidthManager) << relativeLimitProgressDifference / 1024 << "kB =>" << speedkBPerSec << "kB/sec on full speed ("
|
|
|
|
<< _relativeLimitCurrentMeasuredDevice->_readWithProgress << _relativeLimitCurrentMeasuredDevice->_read
|
|
|
|
<< qAbs(_relativeLimitCurrentMeasuredDevice->_readWithProgress
|
|
|
|
- _relativeLimitCurrentMeasuredDevice->_read)
|
|
|
|
<< ")";
|
2014-09-29 12:30:39 +04:00
|
|
|
|
|
|
|
qint64 uploadLimitPercent = -_currentUploadLimit;
|
|
|
|
// don't use too extreme values
|
|
|
|
uploadLimitPercent = qMin(uploadLimitPercent, qint64(90));
|
|
|
|
uploadLimitPercent = qMax(qint64(10), uploadLimitPercent);
|
|
|
|
qint64 wholeTimeMsec = (100.0 / uploadLimitPercent) * relativeLimitMeasuringTimerIntervalMsec;
|
|
|
|
qint64 waitTimeMsec = wholeTimeMsec - relativeLimitMeasuringTimerIntervalMsec;
|
|
|
|
qint64 realWaitTimeMsec = waitTimeMsec + wholeTimeMsec;
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << waitTimeMsec << " - " << realWaitTimeMsec << " msec for " << uploadLimitPercent << "%";
|
2014-09-29 12:30:39 +04:00
|
|
|
|
|
|
|
// We want to wait twice as long since we want to give all
|
|
|
|
// devices the same quota we used now since we don't want
|
|
|
|
// any upload to timeout
|
|
|
|
_relativeUploadDelayTimer.setInterval(realWaitTimeMsec);
|
|
|
|
_relativeUploadDelayTimer.start();
|
|
|
|
|
|
|
|
int deviceCount = _relativeUploadDeviceList.count();
|
|
|
|
qint64 quotaPerDevice = relativeLimitProgressDifference * (uploadLimitPercent / 100.0) / deviceCount + 1.0;
|
|
|
|
Q_FOREACH (UploadDevice *ud, _relativeUploadDeviceList) {
|
|
|
|
ud->setBandwidthLimited(true);
|
|
|
|
ud->setChoked(false);
|
|
|
|
ud->giveBandwidthQuota(quotaPerDevice);
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << "Gave" << quotaPerDevice / 1024.0 << "kB to" << ud;
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
2018-11-12 20:46:39 +03:00
|
|
|
_relativeLimitCurrentMeasuredDevice = nullptr;
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BandwidthManager::relativeUploadDelayTimerExpired()
|
|
|
|
{
|
|
|
|
// Switch to measuring state
|
|
|
|
_relativeUploadMeasuringTimer.start(); // always start to continue the cycle
|
|
|
|
|
|
|
|
if (!usingRelativeUploadLimit()) {
|
|
|
|
return; // oh, not actually needed
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_relativeUploadDeviceList.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << _relativeUploadDeviceList.count() << "Starting measuring";
|
2014-09-29 12:30:39 +04:00
|
|
|
|
|
|
|
// Take first device and then append it again (= we round robin all devices)
|
|
|
|
_relativeLimitCurrentMeasuredDevice = _relativeUploadDeviceList.takeFirst();
|
|
|
|
_relativeUploadDeviceList.append(_relativeLimitCurrentMeasuredDevice);
|
|
|
|
|
|
|
|
_relativeUploadLimitProgressAtMeasuringRestart = (_relativeLimitCurrentMeasuredDevice->_readWithProgress
|
|
|
|
+ _relativeLimitCurrentMeasuredDevice->_read)
|
|
|
|
/ 2;
|
|
|
|
_relativeLimitCurrentMeasuredDevice->setBandwidthLimited(false);
|
|
|
|
_relativeLimitCurrentMeasuredDevice->setChoked(false);
|
|
|
|
|
|
|
|
// choke all other UploadDevices
|
|
|
|
Q_FOREACH (UploadDevice *ud, _relativeUploadDeviceList) {
|
|
|
|
if (ud != _relativeLimitCurrentMeasuredDevice) {
|
|
|
|
ud->setBandwidthLimited(true);
|
|
|
|
ud->setChoked(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we're in measuring state
|
|
|
|
}
|
|
|
|
|
2014-09-30 08:05:23 +04:00
|
|
|
// for downloads:
|
|
|
|
void BandwidthManager::relativeDownloadMeasuringTimerExpired()
|
|
|
|
{
|
2014-10-07 19:42:50 +04:00
|
|
|
if (!usingRelativeDownloadLimit() || _downloadJobList.count() == 0) {
|
2014-09-30 08:05:23 +04:00
|
|
|
// Not in this limiting mode, just wait 1 sec to continue the cycle
|
|
|
|
_relativeDownloadDelayTimer.setInterval(1000);
|
|
|
|
_relativeDownloadDelayTimer.start();
|
|
|
|
return;
|
|
|
|
}
|
2020-06-10 04:47:49 +03:00
|
|
|
if (!_relativeLimitCurrentMeasuredJob) {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << "No job set, just waiting 1 sec";
|
2014-09-30 08:05:23 +04:00
|
|
|
_relativeDownloadDelayTimer.setInterval(1000);
|
|
|
|
_relativeDownloadDelayTimer.start();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << _downloadJobList.count() << "Starting Delay";
|
2014-09-30 08:05:23 +04:00
|
|
|
|
|
|
|
qint64 relativeLimitProgressMeasured = _relativeLimitCurrentMeasuredJob->currentDownloadPosition();
|
|
|
|
qint64 relativeLimitProgressDifference = relativeLimitProgressMeasured - _relativeDownloadLimitProgressAtMeasuringRestart;
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << _relativeDownloadLimitProgressAtMeasuringRestart
|
2014-09-30 08:05:23 +04:00
|
|
|
<< relativeLimitProgressMeasured << relativeLimitProgressDifference;
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
qint64 speedkBPerSec = (relativeLimitProgressDifference / relativeLimitMeasuringTimerIntervalMsec * 1000.0) / 1024.0;
|
|
|
|
qCDebug(lcBandwidthManager) << relativeLimitProgressDifference / 1024 << "kB =>" << speedkBPerSec << "kB/sec on full speed ("
|
|
|
|
<< _relativeLimitCurrentMeasuredJob->currentDownloadPosition();
|
2014-09-30 08:05:23 +04:00
|
|
|
|
|
|
|
qint64 downloadLimitPercent = -_currentDownloadLimit;
|
|
|
|
// don't use too extreme values
|
|
|
|
downloadLimitPercent = qMin(downloadLimitPercent, qint64(90));
|
|
|
|
downloadLimitPercent = qMax(qint64(10), downloadLimitPercent);
|
|
|
|
qint64 wholeTimeMsec = (100.0 / downloadLimitPercent) * relativeLimitMeasuringTimerIntervalMsec;
|
|
|
|
qint64 waitTimeMsec = wholeTimeMsec - relativeLimitMeasuringTimerIntervalMsec;
|
|
|
|
qint64 realWaitTimeMsec = waitTimeMsec + wholeTimeMsec;
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << waitTimeMsec << " - " << realWaitTimeMsec << " msec for " << downloadLimitPercent << "%";
|
2014-09-30 08:05:23 +04:00
|
|
|
|
|
|
|
// We want to wait twice as long since we want to give all
|
|
|
|
// devices the same quota we used now since we don't want
|
2015-10-05 06:20:09 +03:00
|
|
|
// any download to timeout
|
2014-09-30 08:05:23 +04:00
|
|
|
_relativeDownloadDelayTimer.setInterval(realWaitTimeMsec);
|
|
|
|
_relativeDownloadDelayTimer.start();
|
|
|
|
|
|
|
|
int jobCount = _downloadJobList.count();
|
|
|
|
qint64 quota = relativeLimitProgressDifference * (downloadLimitPercent / 100.0);
|
2017-05-09 15:24:11 +03:00
|
|
|
if (quota > 20 * 1024) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcBandwidthManager) << "ADJUSTING QUOTA FROM " << quota << " TO " << quota - 20 * 1024;
|
2017-05-09 15:24:11 +03:00
|
|
|
quota -= 20 * 1024;
|
|
|
|
}
|
2014-09-30 08:05:23 +04:00
|
|
|
qint64 quotaPerJob = quota / jobCount + 1.0;
|
|
|
|
Q_FOREACH (GETFileJob *gfj, _downloadJobList) {
|
|
|
|
gfj->setBandwidthLimited(true);
|
|
|
|
gfj->setChoked(false);
|
|
|
|
gfj->giveBandwidthQuota(quotaPerJob);
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << "Gave" << quotaPerJob / 1024.0 << "kB to" << gfj;
|
2014-09-30 08:05:23 +04:00
|
|
|
}
|
2018-11-12 20:46:39 +03:00
|
|
|
_relativeLimitCurrentMeasuredDevice = nullptr;
|
2014-09-30 08:05:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BandwidthManager::relativeDownloadDelayTimerExpired()
|
|
|
|
{
|
|
|
|
// Switch to measuring state
|
|
|
|
_relativeDownloadMeasuringTimer.start(); // always start to continue the cycle
|
|
|
|
|
|
|
|
if (!usingRelativeDownloadLimit()) {
|
|
|
|
return; // oh, not actually needed
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_downloadJobList.isEmpty()) {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << _downloadJobList.count() << "No jobs?";
|
2014-09-30 08:05:23 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << _downloadJobList.count() << "Starting measuring";
|
2014-09-30 08:05:23 +04:00
|
|
|
|
|
|
|
// Take first device and then append it again (= we round robin all devices)
|
|
|
|
_relativeLimitCurrentMeasuredJob = _downloadJobList.takeFirst();
|
|
|
|
_downloadJobList.append(_relativeLimitCurrentMeasuredJob);
|
|
|
|
|
|
|
|
_relativeDownloadLimitProgressAtMeasuringRestart = _relativeLimitCurrentMeasuredJob->currentDownloadPosition();
|
|
|
|
_relativeLimitCurrentMeasuredJob->setBandwidthLimited(false);
|
|
|
|
_relativeLimitCurrentMeasuredJob->setChoked(false);
|
|
|
|
|
2015-10-05 06:20:09 +03:00
|
|
|
// choke all other download jobs
|
2014-09-30 08:05:23 +04:00
|
|
|
Q_FOREACH (GETFileJob *gfj, _downloadJobList) {
|
|
|
|
if (gfj != _relativeLimitCurrentMeasuredJob) {
|
|
|
|
gfj->setBandwidthLimited(true);
|
|
|
|
gfj->setChoked(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we're in measuring state
|
|
|
|
}
|
|
|
|
|
|
|
|
// end downloads
|
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
void BandwidthManager::switchingTimerExpired()
|
|
|
|
{
|
|
|
|
qint64 newUploadLimit = _propagator->_uploadLimit.fetchAndAddAcquire(0);
|
|
|
|
if (newUploadLimit != _currentUploadLimit) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcBandwidthManager) << "Upload Bandwidth limit changed" << _currentUploadLimit << newUploadLimit;
|
2014-09-29 12:30:39 +04:00
|
|
|
_currentUploadLimit = newUploadLimit;
|
|
|
|
Q_FOREACH (UploadDevice *ud, _relativeUploadDeviceList) {
|
|
|
|
if (newUploadLimit == 0) {
|
|
|
|
ud->setBandwidthLimited(false);
|
|
|
|
ud->setChoked(false);
|
|
|
|
} else if (newUploadLimit > 0) {
|
|
|
|
ud->setBandwidthLimited(true);
|
|
|
|
ud->setChoked(false);
|
|
|
|
} else if (newUploadLimit < 0) {
|
|
|
|
ud->setBandwidthLimited(true);
|
|
|
|
ud->setChoked(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qint64 newDownloadLimit = _propagator->_downloadLimit.fetchAndAddAcquire(0);
|
|
|
|
if (newDownloadLimit != _currentDownloadLimit) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcBandwidthManager) << "Download Bandwidth limit changed" << _currentDownloadLimit << newDownloadLimit;
|
2014-09-29 12:30:39 +04:00
|
|
|
_currentDownloadLimit = newDownloadLimit;
|
|
|
|
Q_FOREACH (GETFileJob *j, _downloadJobList) {
|
|
|
|
if (usingAbsoluteDownloadLimit()) {
|
|
|
|
j->setBandwidthLimited(true);
|
|
|
|
j->setChoked(false);
|
|
|
|
} else if (usingRelativeDownloadLimit()) {
|
|
|
|
j->setBandwidthLimited(true);
|
|
|
|
j->setChoked(true);
|
|
|
|
} else {
|
|
|
|
j->setBandwidthLimited(false);
|
|
|
|
j->setChoked(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BandwidthManager::absoluteLimitTimerExpired()
|
|
|
|
{
|
|
|
|
if (usingAbsoluteUploadLimit() && _absoluteUploadDeviceList.count() > 0) {
|
|
|
|
qint64 quotaPerDevice = _currentUploadLimit / qMax(1, _absoluteUploadDeviceList.count());
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << quotaPerDevice << _absoluteUploadDeviceList.count() << _currentUploadLimit;
|
2014-09-29 12:30:39 +04:00
|
|
|
Q_FOREACH (UploadDevice *device, _absoluteUploadDeviceList) {
|
|
|
|
device->giveBandwidthQuota(quotaPerDevice);
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << "Gave " << quotaPerDevice / 1024.0 << " kB to" << device;
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (usingAbsoluteDownloadLimit() && _downloadJobList.count() > 0) {
|
|
|
|
qint64 quotaPerJob = _currentDownloadLimit / qMax(1, _downloadJobList.count());
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << quotaPerJob << _downloadJobList.count() << _currentDownloadLimit;
|
2014-09-29 12:30:39 +04:00
|
|
|
Q_FOREACH (GETFileJob *j, _downloadJobList) {
|
|
|
|
j->giveBandwidthQuota(quotaPerJob);
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcBandwidthManager) << "Gave " << quotaPerJob / 1024.0 << " kB to" << j;
|
2014-09-29 12:30:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|