长拳一段单练套路:rTorrent Optimisation

来源:百度文库 编辑:九乡新闻网 时间:2024/05/02 00:19:31
rTorrent Optimisation 一.优化指南
(1)Linux 系统网络优化
查资料已核实与下载有关的部分网络参数,以下几项可修改。
#ip_forward,ip包转发(默认0),做路由器需设为1启用,MSS只是客户端关闭设0即可
echo 0 > /proc/sys/net/ipv4/ip_forward
#ip_default_ttl,ip包生存时间(默认64),默认64即可
echo 64 > /proc/sys/net/ipv4/ip_default_ttl
#tcp_retries2,已存在tcp连接重试次数(默认15次),普遍认为数值可调小故设7
echo 7 > /proc/sys/net/ipv4/tcp_retries2
#tcp_fin_timeout,tcp会话wait2超时时间(默认180秒),普遍认为数值可调小故设60
echo 60 > /proc/sys/net/ipv4/tcp_fin_timeout
#ip_local_port_range,设定本地对外连接的端口范围,默认1024-4999,适当扩大到6999.
echo "1024 6999" > /proc/sys/net/ipv4/ip_local_port_range

所有设定重启后失效,建议全部写入开机脚本/opt/etc/init.d/S99local中,本来用sysctl -w应该可以修改并保存的(但测试MSS不可)

(2)rTorrent普通设定优化
使用vi /opt/root/.rtorrent.rc编辑 附:vi使用方法
首先要确定你是否可以被外网连接,下面简成外网YES用户、外网NO用户。
(可能需要网络设定打开相应端口转发等步骤,网上介绍文章很多)
1.功能设定
#此项一定要开(如PT种子该功能自动失效)
peer_exchange = yes
外网YES用户,可以使用下面两个设定。
#使用udp trancker
use_udp_trackers = yes
#打开dht(必须设定session)
session = /shares/mss-hdd/Public/rtorrent/.session
dht = on
dht_port = 6881
外网NO用户,因为实测完全无效,最好关闭。(如果XP下BT软件该功能是有效的)
use_udp_trackers = no
dht = off

2.连接设定
因为连接用户数(peers)与网络速率(up&down rate)两部分,与rTorrent的资源占用率成正比。
与它们有关设定为以下五项,理论上来说越大越好,但实际上要综合设定以提高效率。
如果你的机器足够好,你可以尽管把它设大,所以我说的优化仅针对128M内存、300Mips的MSS1代。
外网YES用户有incoming端口时,可以和任何peer进行连接,所以上传速率只需结合情况设定即可(建议30以上)
download_rate = 0
upload_rate = 30
外网NO用户,你所有的连接必须是你主动去连接外网YES用户,而BT下载的一条机制是“上传决定下载”
所以你必须也要有足够的上传(建议设为0,不限速)
download_rate = 0
upload_rate = 0

每个任务最小连接数、最大连接数、最大上传数。
正常情况下:连接数越大,下载速率越快。而外网NO用户也要依赖最大上传数来一定程度提高下载速度。
如果按照理论上来,可以设为下面的,但具体参数应根据实际情况修改。
min_peers = 40
max_peers = 200
max_uploads = 15
因为考虑到MSS的性能,min_peers的设定很难取舍。
因为如果是下载中(leecher),现有连接数
3.细节设定
设定服务类型Tos(Type of Service),分别是低延迟lowdelay,高速率throughput,高可靠度reliability,低消耗mincost。
或者也可以自定义一个值,这里目前把它设为高速率
#tos = default|lowdelay|throughput|reliability|mincost, tos = hex
tos = throughput
设定http open最大值,此项是非常重要的效率优化值。
目前具体数值仍在试验中(外网上有老外探讨这个问题,专门把它设低,主要针对低性能多任务情况。)
因为rTorrent在进行HTTP访问时,对CPU占用率极高,所以才有了MSS上经常遇到的假死现象(screen操作没有任何反应),
这时如果你用top看一下,rtorrent的CPU占用不是90%以上就是不正常的显示为0%,但Load average值会很高。
rtorrent对HTTP访问(也就是访问tracker服务器取得返回连接信息)的默认设置是32个.
当你有很多任务(假如下载10个,做种50个)同时进行,一旦开始这个过程,CPU立刻就从<10%飙升到100%了。
这种情况,轻微的会导致rTorrent的screen下无法操作,
严重的会导致MSS失去响应,所有的tcp连接失效,等恢复后看到下载上传速度均为0.
而造成这种情况的关键原因是rTorrent完全遵守标准的BT下载原理所致,比如软件启动后的过程是。
载入任务(如50个种子),按次序所有的tracker开始排队进行访问(此时CPU已经飙升),依次访问成功后,进入等待状态(CPU降到10%以下),
但每个tracker在各自等待1800秒(30分钟)会再次重复同样的过程,所以就造成rTorrent总是在大约30分钟进入严重的假死状态。
所以如果将HTTP最大值减少,可以避免http集中访问,可以减少或避免rTorrent假死状态提高工作效率。
我的测试
设2时基本可避免假死状况,但数字太小会影响访问tracker取得连接的效率。
设8,经过观察即便CPU不处于100%,依然有假死问题,仍在测试。
#设定http最大值(用于http tracker访问),默认32,建议MSS暂时设为8。
set_max_open_http = 8


二.分组管理与最大任务数
目前设计的分组管理功能如下(已经全部实现完美管理并正常运行)
1.以queue,watch,downloads为主目录,下设若干相同的分组子目录。
2.自动监测下载最大任务数脚本(即incomplete中运行的任务,不计算做种的seeding数),
3.在下载完成后进行一系列动作,包括4G以上文件合并、watch目录下的种子文件自动删除。
4.可以设定各自的分享率ratio,比如区分PT、BT种子(强力推荐此设置)
**注意事项:必须使用queue下分组目录保存种子(排队任务)或者watch下分组目录保存种子(立刻开始任务),
其他加入种子的方式(比如nTorrent加入)均可能引起一些功能失效。
**小提示:如果须暂停某任务,只需把它的种子文件从watch下移动回queue下。
实际工作流程简易介绍:(懒得画流程图,简单写一下)
1.RTqueue.sh每三分钟运行一次,过程为:
检查watch目录下.torrent文件数量,即当前下载任务数
如果小于最大任务数(max_active_dl,默认10个可自设),
就从queue的分组中寻找.torrent文件转移到对应的watch分组(比如game)下。
非.torrent文件会被放到unkownfile下。
2.rTorrent的schdule监测watch每个分组(间隔10秒,可自设)
当有新的.torrent加入时,就启动下载任务(默认立即开始)。
3.下载100%完成后执行的命令(必须是所有文件,不可以有把优先级off的文件)
on_finished = move_complete:转移对应的下载文件到设定的目录中去(如\pub\Downloads\!Game)
on_finished = remove_tied,任务标志T被去掉,删掉watch下关联的.torrent文件(这步非常重要,否则RTqueue.sh无法添加新任务)
on_finished = 4g 判断是否有分割的2G以上文件要合并。
4.判断分享率ratio,达到条件的结束任务,未达到的转为作种继续上传。

文件夹分组管理
要将文件夹全面重新规划,按照自己喜欢的方式分类。
(已经下载的所有文件也可以利用相应的命令全部转移,但比较麻烦,建议还是就全部重新设定,感兴趣的可以看下面迁移旧文件部分)
rtorrent直接迁移到/pub下,分为.session,watch,queue(下设若干分类文件夹,可任意追加)
注意事项inux的文件访问区分大小写,创建目录尽量不要使用符号(尤其是空格)。
结构图如下:
A.rTorrent程序文件夹
/pub
/rtorrent
/.session
/watch
/game
/video
/seeding
/queue
/game
/video
/seeding

B.下载文件夹,working为下载中任务目录,其他为和A中对应的下载后保存位置。
/pub
/downloads
/game
/video
/seeding
/working

以上的目录结构时需修改配置文件.rtorrent.rc中的设定有:
#下载时临时存放目录以及session存放目录
directory = /shares/mss-hdd/Public/downloads/working
session = /shares/mss-hdd/Public/rtorrent/.session

增加相应的不同watch如下:
注:已实现区分PT、BT种子的强化方法,强烈推荐混PT站的作此设定,详见下面红色标注的强化方法。

标准方法1:分别建立三组监视目录,并在下载完成后转移文件到对应文件夹下。(推荐使用,可自动转移)
可以根据自己的情况任意追加和设定分组,只要修改d.set_custom1=后面的目录位置即可。
schedule = wd_1,10,10,"load_start=/pub/rtorrent/watch/seeding/*.torrent,d.set_custom1=/pub/downloads/seeding"
schedule = wd_2,20,10,"load_start=/pub/rtorrent/watch/game/*.torrent,d.set_custom1=/pub/downloads/game"
schedule = wd_3,30,10,"load_start=/pub/rtorrent/watch/video/*.torrent,d.set_custom1=/pub/downloads/video"
on_finished = move_complete,"d.set_directory=$d.get_custom1= ;execute=mv,-u,$d.get_base_path=,$d.get_custom1="

标准方法2:分别建立三组监视目录,直接将文件保存到对应文件夹下。(不推荐,无法分清未完成和已完成)
schedule = wd_1,10,10,"load_start=/pub/rtorrent/watch/seeding/*.torrent,d.set_directory=/pub/downloads/seeding"
schedule = wd_2,20,10,"load_start=/pub/rtorrent/watch/game/*.torrent,d.set_directory=/pub/downloads/game"
schedule = wd_3,30,10,"load_start=/pub/rtorrent/watch/video/*.torrent,d.set_directory=/pub/downloads/video"

强化方法3(混PT站必备)
强化方法3与标准方法1区分在于加入了branch判断语句,
*做此设定因为使用了官方规定的load_start_verbose,所以如果当前已有任务会重新载入,
造成与原来的.session下种子冲突,并提示"已有相同的infohash种子等信息,可不必理会。(其实不用verbose也可以,但还是按照官方来)
以下为标准语句,可以用于对应本功能时测试使用:
作用是在载入watch目录下的种子时,自动判断是否为private tracker(PT),BT种子就把任务的custom2设为BT,同时将分享率Group设为RatioBT,
PT种子,custom2为空,分享率执行默认Ratio(其实也可custom2设为PT,但置空,是为了以后更多的复杂判断条件预备。)
**注意事项,schedule语句很长,任何符号都不能写错,建议在XP中复制粘帖,编辑成自己的再逐一粘帖到配置文件。
**private=后面是两个逗号,第一个之后是判断为private的执行命令,第二个是判断为public的执行命令。
schedule =wd_test,10,10,"load_start_verbose=/pub/rtorrent/watch/test/*.torrent,d.set_custom1=/pub/Downloads/test,\"branch={d.is_private=,,{d.set_custom2=BT,view.set_visible=RatioBT}}\""
可分别在test中分别放一个小文件的PT和BT种子,用下面的命令确认效果。
print=$d.get_custom2=
d.multicall=RatioPT,print=$d.get_name=
如果是BT种子,两个命令的输出应该在屏幕上显示"BT“,”种子文件名“。


参考范例
以强化方法3为标准,包括了在.rtorrent.rc中与分组有关的全部设定,
整个分类管理只须再加上RTqueue.sh的脚本crontab即可。

##on执行语句部分
#下载完成后将文件转移至指定的custom1文件夹
on_finished = move_complete,"d.set_directory=$d.get_custom1= ;execute=mv,-u,$d.get_base_path=,$d.get_custom1="
#下载完成后删除watch下.torrent文件
on_finished = remove_tied,"d.delete_tied="
#下载完成后执行4g合并脚本
on_finished = 4g,"execute=RT4g.sh,$d.get_base_path=,$d.get_custom2="
#删除任务时也将相关文件一并删除
on_erase = rm_complete,"d.open= ;execute=rm,-rf,$d.get_base_path="
##schedule语句,用于监测各分类脚本
#范例:可修改部分用红色标注,分别是
schedule名称(wd_test),开始时间(30秒后),时间间隔(10秒),监视分类目录(/pub/rtorrent/watch/test/),完成存放目录(/pub/Downloads/test)
BT的Ratio分组(RatioBT)
schedule =wd_test,10,10,"load_start_verbose=/pub/rtorrent/watch/test/*.torrent,d.set_custom1=/pub/Downloads/test,\"branch={d.is_private=,,{d.set_custom2=BT,view.set_visible=RatioBT}}\""
#正式分组
schedule = wd_1,30,10,"load_start_verbose=/pub/rtorrent/watch/!seeding/*.torrent,d.set_custom1=/pub/Downloads/!seeding,\"branch={d.is_private=,,{d.set_custom2=BT,view.set_visible=RatioBT}}\""
schedule = wd_2,30,10,"load_start_verbose=/pub/rtorrent/watch/!Game/*.torrent,d.set_custom1=/pub/Downloads/!Game,\"branch={d.is_private=,,{d.set_custom2=BT,view.set_visible=RatioBT}}\""
schedule = wd_3,30,10,"load_start_verbose=/pub/rtorrent/watch/!Video/*.torrent,d.set_custom1=/pub/Downloads/!Video,\"branch={d.is_private=,,{d.set_custom2=BT,view.set_visible=RatioBT}}\""

##Ratio设定(只适用于0.8.4版本)
#注意:做种默认分享率(seeding)与group.ratio(RatioBT)同时有效,所以(seeding)必须是所有组别中最大分享率。
#如果需要对应已存在的任务,可以加上下面的一行命令(此句与分组管理无关,可不设置)
on_start = VerifyBTset ,"branch=d.is_private=,,{d.set_custom2=BT,view.set_visible=RatioBT}"
#新增system.method.set语句,必须设定,用于对应每个分享率组达到后后续处理(测试时可以在后面加上显示任务名的命令,print=$d.get_name=)。
#做种默认分享率(seeding),指当上传大于10G且分享率达到100%或最大分享率200%(如为0则是无限,直到满足10G上传和100%的要求)
ratio.enable=
ratio.min.set=100
ratio.max.set=200
ratio.upload.set=10000M
system.method.set=group.seeding.ratio.command,d.close=,d.stop=,d.set_ignore_commands=1


#BT分享率,设为最小分享率10%并上传10M,最大分享率20(下载完成后,判断分享率机制就启用,符合的就执行对应的group.group.ratio.command语句)
group.insert_persistent_view = RatioBT
group.RatioBT.ratio.enable=
group.RatioBT.ratio.min.set=10
group.RatioBT.ratio.max.set=20
group.RatioBT.ratio.upload.set=10M
system.method.set=group.RatioBT.ratio.command,d.close=,d.stop=,d.set_ignore_commands=1

#最大任务数监测脚本
自动监测下载最大任务数脚本(即incomplete中运行的任务,不计算做种的seeding数),默认设为10。
目前实现的是针对watch和queue两个目录的总数量进行管理,没有涉及分类
(实现无问题,我还有保留的程序,问题在于按什么规则?似乎没必要或仍想不到每一个分组的任务控制规则)
推荐在.rtorrent.rc配置文件加上下面两句话,功能是以tied文件是否在watch目录下开始和关闭任务:
schedule = tied_directory,180,30,start_tied=
schedule = untied_directory,180,30,close_untied=
加入此命令后,如果需要暂停任务,可以把该文件从watch转移回queue下。
但又需要注意该任务会被自动加上I标志,下次要执行任务时要到screen下按"I"键取消就可以开始下载了。

把附件里的脚本复制到/opt/bin后,要敲crontab -e加入一句*/3 * * * * /opt/bin/RTqueue.sh
这是我根据国外脚本(不好意思,地址忘了)修改为简易实用的
max_active_dl=设最大任务数(128M内存设12为宜)
xxxxx_dir,是几个相关目录的地址(unknown须建立,用于保存后缀名不是.torrent的文件)
logfile保存脚本的纪录(须建立,可任意指定位置)
#!/opt/bin/sh
#

# rTorrent queue manager
# RTqueue.sh
# 2009/01/20

#export LC_ALL=zh_CN.UTF-8
#export TZ=CST-8
max_active_dl=11

queue_dir="/shares/mss-hdd/Public/rtorrent/queue"
watch_dir="/shares/mss-hdd/Public/rtorrent/watch"
session_dir="/shares/mss-hdd/Public/rtorrent/.session"
unknown_dir="/shares/mss-hdd/Public/rtorrent/unknown_file"
logfile="/shares/mss-hdd/Public/log/rtqm.log"

no_queue_bt=`find $queue_dir/. -type f | wc -l | awk '{print $1}'`;
no_watch_bt=`find $watch_dir/. -type f | wc -l | awk '{print $1}'`;

# check if there is torrent for download
if [ $no_queue_bt -ge 1 ]; then
# make sure active download session is less than maximum download
if [ $no_watch_bt -le $max_active_dl ]; then
loop=$(($max_active_dl-$no_watch_bt))

if [ $no_queue_bt -lt $loop ]; then
loop=$no_queue_bt;
fi

echo "" >> $logfile
date >> $logfile

#insert
i=0;
until [ $i -eq $loop ]
do

src_bt=`find $queue_dir/. -type f |sort | head -1 | sed 's/\ /\\ /g'`;
file_type=${src_bt##*.}

if [ $file_type = 'torrent' ]; then
dest_bt="${watch_dir}${src_bt#*/queue/.}"
dest_dir=$(dirname "$dest_bt")

# get the base directory
cat=${dest_dir#*/watch/*}
cat=${cat%%/*}

# move files
echo "move $dest_bt" >> $logfile
if cp -fr "$src_bt" "$dest_bt" ; then
rm -fr "$src_bt"
else
echo "unstable to copy file $src_bt" >> $logfile
fi
i=$(( $i + 1 ));
else
echo "unsupported file $src_bt" >> $logfile
mv $src_bt $unknown_dir
fi
done

fi
fi

Tips
如果要增加一个分类,要做的就是下面的步骤,反之删掉即可。
.rtorrent.rc增加一行
schedule = wd_1,10,10,"load_start=/pub/rtorrent/watch/foo/*.torrent,d.set_custom1=/pub/Downloads/foo"
建立目录
/pub/rtorrent/watch/foo
/pub/rtorrent/queue/foo
迁移旧文件步骤(注意小心操作避免错误,也许可能造成rTorrent崩溃)
建议不熟练的话,最好把任务都close并且ctrl+q然后再启动rtorrent进行迁移)
1.如果session目录位置变更了
首先要ctrl+q关闭rtorrent,然后把旧目录下所有文件复制到新目录下
2.再启动rtorrent进行文件目录转移和设置。
比如说原有某游戏A放在/pub/downloads下,而且有关联.torrent种子文件放在旧watch目录下。
以下均要求screen -r窗口中,把光标对应到需要转移的A任务进行操作。
1.首先ctrl+k关闭任务,按U键去掉种子关联(有关联的任务状态为T)
2.笨办法:
把/pub/downloads下文件移动到/pub/downloads/game下
ctrl+x(下面显示command),输入d.set_custom1=/pub/downloads/game
按ctrl+o变更存放位置:删掉给出的目录字符串,把/pub/Downloads/game输入后回车
(或者直接ctrl+x,输入命令:d.set_directory=/pub/Downloads/game更方便)
3.新办法:
ctrl+x(下面显示command):d.set_custom1=/pub/downloads/game
ctrl+x:d.set_directory=$d.get_custom1= ;execute=mv,-u,$d.get_base_path=,$d.get_custom1=
4.ctrl+s开始这个任务确认是否转移成功。
三.自动化应用设定

这些是积累的设定总结,已有多处更新,部分已包含在分组管理中。
(1)关于共享率的设置如下即可实现(当任务下载完成后)
ratio.enable=
ratio.min.set=10
ratio.max.set=50
ratio.upload.set=20M
是指满足下面两个条件之一就关闭(close)任务,
1.当分享率达到10%并且上传大于20M时,
2.当分享率达到50%
默认设置就是close任务了(不需要下面这句),如果加上的话是关闭+删除,也可增加其他设定
注意不要用d.erase是删除任务且如果有其他设定将删掉全部文件。
#system.method.set = group.seeding.ratio.command, d.close=, d.erase=
(2)完成或者删除任务时可以设为自动转移或者执行其他命令
以下为旧版本命令,目前兼容可用:
1.删除时自动删除下载目录
on_erase = rm_complete,"d.open= ;execute=rm,-rf,$d.get_base_path="
2.完成时自动转移下载目录
on_finished = move_complete,"execute=mv,-u,$d.get_base_path=,/shares/mss-hdd/Public/Downloads ;d.set_directory=/shares/mss-hdd/Public/Downloads"
3.完成后删除T状态(tied到watch下种子)和watch下对应的.torrent文件
on_finished = remove_tied,"d.delete_tied="
4.完成后执行4g脚本判断是否需要合并(详见第3条4g脚本)
on_finished = 4g,"execute=RT4g.sh,$d.get_base_path=,$d.get_custom2="
以上命令实测可以支持任何文件名(包括中文、字符、空格等),如果中文字符出现问题
大概的解决方法是:
ipkg install utf8proc (安装了一个utf8相关的包)
export LC_ALL=zh_CN.UTF-8(设置了一下语言)
(3)大于4g的文件合并修改版(感谢原脚本作者,只是做了一些功能修正)
修改的脚本解决两处bug(不含目录的单个文件被RT强制分割后无法进行合并和必须依赖建立link文件才能运行),
增加判断是否为PT种子的功能,默认如果是PT种子的话,不删除分割文件(000,001),保证下载继续做种(但需要以后自己动手删除文件)。
内容详见附件
(4)活用schedule命令可以做很多事情。
其实它的标准格式是schedule = 任务名称,执行时间,生效时间,命令
如:自动变更下载速率
schedule = throttle_1,01:00:00,24:00:00,download_rate=0
schedule= throttle_2,05:00:00,24:00:00,download_rate=25
自动变更加密协议(因为我的宽带是在凌晨0点解开P2P封锁)
schedule = encryption_1a,00:00:00,24:00:00,encryption=enable_retry
schedule = encryption_1p,00:00:05,24:00:00,"print=\"encryption:enable_retry\""
schedule = encryption_2a,07:00:00,24:00:00,encryption=try_outgoing
schedule = encryption_2p,07:00:05,24:00:00,"print=\"encryption:try_outgoing\""

(5)利用screen的外部输入方法来修改rTorrent的任何设置。
比如设定upload为30的话,可是输入下面的命令
screen -s torrent -X eval 'stuff ^Xupload_rate=30^M'
比如在screen下用命令调整上传速率。
screen -s torrent -X eval 'stuff \"ccccssss\"'
以此来结合crontab,可以做到在任意日期时间组合下修改任何rTorrent的配置。

(6)实现rTorrent的邮件提醒功能
http://bbs.baoyinet.com/viewthread.php?tid=2422

其他辅助性设定:
(1).设定rTorrent时区以保证运行
schdule设定前提必须是时间正确,结果我发现出错误的原因是时区问题。
所以必须在rTorrent启动脚本中增加"export TZ=CST-8"语句,
也就是说现在的S99rtorrent脚本开头应该是下面几句。
#!/opt/bin/sh
export HOME=/opt/root
export LC_ALL=zh_CN.UTF-8
export TZ=CST-8
rm /shares/mss-hdd/Public/bt/rtorrent/session/rtorrent.lock

(2).增加执行记录
配置文件加入一行,可以记录rtorrent执行的命令,比如下载完成后mv。
才发现/var/log在mss下是随着系统启动临时的,所以把日志放到/pub/log下可以永久保存。
execute_log = /pub/log/rtorrent.log

(3).给rTorrent增加速度记录功能的简易脚本
1.
在/opt/bin下
vi rtlog.sh
#!/bin/sh
time=`date +%Y"-"%m"-"%d" "%H":"%M":"%S`
echo "$time Down: "$1" bps Up: "$2" bps">>/pub/log/speedRT.log
wq存盘后,一定要chmod 777 rtlog.sh

2.
vi .rtorrent.rc
增加一行配置(目前时间间隔设定为15分钟)
schedule = speedlog,600,600,"execute=/opt/bin/rtlog.sh,$get_down_rate=,$get_up_rate="
3.cat /pub/log/speedRT.log
就能看到rTorrent的速度记录了

(4)实现rTorrent单个资源多做种的超强小Tip(必须用到rT,但其他软件亦可借鉴)
http://bbs.baoyinet.com/viewthread.php?tid=2451

(5)解决screen下直接按ctrl+s,ctrl+q无效的方法
在登陆后输入命令取消这两个的绑定即可。
stty start undef
stty stop undef