From cee0d5d0bfe4dc491a3434a28b83ba96f9e2f039 Mon Sep 17 00:00:00 2001 From: ardhipoetra Date: Tue, 5 Apr 2016 20:17:25 +0200 Subject: [PATCH] credit mining awareness of main downloading activity -flexible priority- --- .../Core/Libtorrent/LibtorrentDownloadImpl.py | 6 ++- Tribler/Policies/BoostingManager.py | 45 ++++++++++++++++++- logger.conf | 2 +- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py b/Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py index 72fb33d7f47..ba0ae34544d 100644 --- a/Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py +++ b/Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py @@ -213,6 +213,10 @@ def create_engine_wrapper(self, lm_network_engine_wrapper_created_callback, psta if not self.ltmgr or not dht_ok or not session_ok: self._logger.info(u"LTMGR/DHT/session not ready, rescheduling create_engine_wrapper") + + if tunnels_ready < 1: + tunnel_community.build_tunnels(self.get_hops()) + self.session.lm.threadpool.call(5, self.create_engine_wrapper, lm_network_engine_wrapper_created_callback, pstate, initialdlstatus=initialdlstatus, share_mode=share_mode, @@ -332,7 +336,7 @@ def set_vod_mode(self, enable=True): self._logger.debug("LibtorrentDownloadImpl: going into VOD mode %s", filename) else: self.handle.set_sequential_download(False) - self.handle.set_priority(0) + self.handle.set_priority(30) if self.get_vod_fileindex() >= 0: self.set_byte_priority([(self.get_vod_fileindex(), 0, -1)], 1) diff --git a/Tribler/Policies/BoostingManager.py b/Tribler/Policies/BoostingManager.py index 3aa71aa17f4..72d46445e16 100644 --- a/Tribler/Policies/BoostingManager.py +++ b/Tribler/Policies/BoostingManager.py @@ -39,7 +39,7 @@ from Tribler.Main.vwxGUI.SearchGridManager import ChannelManager as cm logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) +logger.setLevel(logging.INFO) formatter = logging.Formatter( "%(asctime)s.%(msecs).03dZ-%(levelname)s-%(message)s", datefmt="%Y%m%dT%H%M%S") @@ -147,6 +147,8 @@ def __init__(self, session): class BoostingManager(TaskManager): __single = None + MULTIPLIER_DL = [0, 0.2, 0.6, 1, 1.5, 3] + DEFAULT_PRIORITY_TORRENT = 5 def __init__(self, session, utility=None, policy=SeederRatioPolicy, src_interval=20, sw_interval=20, max_per_source=100, max_active=2): @@ -395,7 +397,37 @@ def on_torrent_insert(self, source, infohash, torrent): # logger.info("Got new torrent %s from %s", infohash.encode('hex'), # source_str) + def _check_main_download(self, idx_multiplier): + + total_cm_prio = sum([tr['download'].handle.status().priority or 0 + for tr in self.torrents.values() if tr and tr.get('download', False) and + tr['download'].handle]) + + (hits_num, hits) = self.gui_util.library_manager.getHitsInCategory() + + total_main_prio = sum([chn_tor.ds.get_download().handle.status().priority or 0 + for chn_tor in hits if chn_tor and chn_tor.ds and chn_tor.ds.get_download().handle]) + + ret_cm_prio = total_cm_prio + + self._logger.debug("totm %d totc %d retc %d",total_main_prio, total_cm_prio, ret_cm_prio) + + # if main downloading not that much, we can continue as it is + # else, reduce priority + if total_main_prio >= total_cm_prio: + ret_cm_prio = total_cm_prio - self.MULTIPLIER_DL[idx_multiplier]*\ + (total_main_prio-total_cm_prio) + + return len([tr for tr in self.torrents.values() if tr and tr.get('download', False)]), ret_cm_prio + def scrape_trackers(self): + + num_dl, total_prio_tor = self._check_main_download(3) + + # 1 is the lowest priority we'd want to assign + new_prio = (total_prio_tor/float(num_dl) if num_dl else self.DEFAULT_PRIORITY_TORRENT) or 1 + + for infohash, torrent in self.torrents.iteritems(): tf = torrent['metainfo'] @@ -441,6 +473,13 @@ def _logtorrentpeer(status, lt_torrent): print "----------------------" # _logtorrentpeer(status, lt_torrent) + + # change priority (reduce load on main downloading) + if new_prio and int(new_prio) != status.get_priority(): + lt.set_priority(int(new_prio)) + self._logger.info("Change priority %s from %d to %d", status.info_hash, + status.get_priority(),new_prio) + peer_list = [] for i in lt_torrent.get_peer_info(): peer = LibtorrentDownloadImpl.create_peerlist_data(i) @@ -554,7 +593,7 @@ def do_start(): tobj['download'] = self.session.lm.add(tobj['metainfo'], dscfg, pstate=tobj.get('pstate', None), hidden=True, share_mode=not preload, checkpoint_disabled=True) - tobj['download'].set_priority(tobj.get('prio', 1)) + tobj['download'].set_priority(tobj.get('prio', self.DEFAULT_PRIORITY_TORRENT)) self.session.lm.threadpool.add_task_in_thread(do_start, 0) @@ -705,6 +744,7 @@ def update_torrent_stats(self, torrent_infohash_str, seeding_stats): else: self.torrents[torrent_infohash_str]['last_seeding_stats'] = seeding_stats + class BoostingSource(object): def __init__(self, session, tqueue, source, interval, max_torrents, callback): @@ -740,6 +780,7 @@ def _update(self): def getSource(self): return self.source + class ChannelSource(BoostingSource): def __init__(self, session, tqueue, dispersy_cid, interval, max_torrents, callback): diff --git a/logger.conf b/logger.conf index 4f80ca36e33..eb924e80616 100644 --- a/logger.conf +++ b/logger.conf @@ -12,7 +12,7 @@ level=ERROR handlers=default [logger_TftpRequester] -level=DEBUG +level=ERROR qualname=TftpRequester handlers=default propagate=0