diff --git a/src/interfaces/queuemanagerinterface.h b/src/interfaces/queuemanagerinterface.h --- a/src/interfaces/queuemanagerinterface.h +++ b/src/interfaces/queuemanagerinterface.h @@ -22,6 +22,7 @@ #define BTQUEUEMANAGERINTERFACE_H #include +#include namespace bt { @@ -52,12 +53,22 @@ * @param trk First tier of trackers */ virtual void mergeAnnounceList(const SHA1Hash & ih,const TrackerTier* trk) = 0; - + /** * Disable or enable the QM * @param on */ static void setQueueManagerEnabled(bool on); + + /** + * Requested by each TorrentControl during its update to + * get permission on saving Stats file to disk. May be + * overriden to balance I/O operations. + * @param tc Pointer to TorrentControl instance + * @return true if file save is permitted, false otherwise + */ + + virtual bool permitStatsSync(bt::TorrentControl* tc); static bool enabled() {return qm_enabled;} }; diff --git a/src/interfaces/queuemanagerinterface.cpp b/src/interfaces/queuemanagerinterface.cpp --- a/src/interfaces/queuemanagerinterface.cpp +++ b/src/interfaces/queuemanagerinterface.cpp @@ -36,4 +36,9 @@ qm_enabled = on; } + bool QueueManagerInterface::permitStatsSync(TorrentControl *tc) + { + return tc->getStatsSyncElapsedTime() >= 5 * 60 * 1000; // 5 sec + } + } diff --git a/src/torrent/torrentcontrol.h b/src/torrent/torrentcontrol.h --- a/src/torrent/torrentcontrol.h +++ b/src/torrent/torrentcontrol.h @@ -180,7 +180,10 @@ /// Set a custom Cache factory void setCacheFactory(CacheFactory* cf); - + + /// Get time in msec since the last Stats file save on disk + TimeStamp getStatsSyncElapsedTime() { return stats_save_timer.getElapsedSinceUpdate(); } + public Q_SLOTS: /** * Update the object, should be called periodically. @@ -284,6 +287,7 @@ private: JobQueue* job_queue; + QueueManagerInterface* m_qman; Torrent* tor; PeerSourceManager* psman; ChunkManager* cman; diff --git a/src/torrent/torrentcontrol.cpp b/src/torrent/torrentcontrol.cpp --- a/src/torrent/torrentcontrol.cpp +++ b/src/torrent/torrentcontrol.cpp @@ -78,7 +78,7 @@ Uint32 TorrentControl::min_diskspace = 100; TorrentControl::TorrentControl() - : tor(0), psman(0), cman(0), pman(0), downloader(0), uploader(0), choke(0), tmon(0), prealloc(false) + : m_qman(0), tor(0), psman(0), cman(0), pman(0), downloader(0), uploader(0), choke(0), tmon(0), prealloc(false) { job_queue = new JobQueue(this); cache_factory = 0; @@ -241,12 +241,15 @@ cman->checkMemoryUsage(); } - // to satisfy people obsessed with their share ratio - if (stats_save_timer.getElapsedSinceUpdate() >= 5 * 60 * 1000) - { - saveStats(); - stats_save_timer.update(); - } + // to satisfy people obsessed with their share ratio + bool save_stats = m_qman ? m_qman->permitStatsSync(this) : + (stats_save_timer.getElapsedSinceUpdate() >= 5 * 60 * 1000); + + if (save_stats) + { + saveStats(); + stats_save_timer.update(); + } // Update DownloadCap updateStats(); @@ -528,6 +531,8 @@ void TorrentControl::init(QueueManagerInterface* qman, const QByteArray& data, const QString& tmpdir, const QString& ddir) { + m_qman = qman; + // first load the torrent file tor = new Torrent(); try