QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4249|回复: 9

又一系统加速方法

[复制链接]
发表于 2006-11-20 18:40:23 | 显示全部楼层 |阅读模式
原帖:http://forums.gentoo.org/viewtop ... 989057cd9d91ca23873
这个和readahead-list的原理有点象,分别则是readahead-list的预读文件表是静态的,预设的,而本贴的方法则是实时探测运行中的系统所需加载的文件。

首先安装lsof:
[code:1]# emerge -av lsof[/code:1]
然后用文字编辑器创建以下三个文件:
/etc/init.d/my-readahead
[code:1]#!/sbin/runscript
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

depend() {
        before logger
}

start() {
        ebegin "Starting readahead ROYALE"
        cat /etc/conf.d/* > /dev/null 2>&1 &
        cat `cat /etc/conf.d/sofile-list.load` > /dev/null 2>&1 &
        if [ -f /forcesampler ];then
                /usr/sbin/sample-init-process /etc/conf.d/sofile-list.load &
                rm -f /forcesampler
        fi
        eend 0
}

stop() {
        eend 0
} [/code:1]
/usr/sbin/sample-init-process
[code:1]
#!/bin/bash
final_db="$1"

# total number of lists to keep in /etc/conf.d/
total_to_keep=4

# total number of samples to make in /forcesampler boot.
# It will take twice this amount of seconds to finish sampling.
total_samples_per_boot=200

# touch empty file if not there
[ ! -f "$final_db" ] && touch "$final_db"

let i=0
while [ "$i" -ne "$total_to_keep" ] ; do
        if [ ! -f "$final_db$i" ] ; then
                break;
        else
                j=$((i+1))
                if [ "$j" = "$total_to_keep" ] ;then
                        j=0
                fi
                if [ -f "$final_db$j" -a "$final_db$i" -nt "$final_db$j" ];then
                        i=$((j))
                        break;
                fi
        fi
        i=$((i+1))
done
if [ "$i" = "$total_to_keep" ] ;then
        i=0
fi

slot="$i"
cp "$final_db" "$final_db$i"

function collect_sample_old()
{
        lsof |awk '{print $9}'|grep \.so|sort|uniq > $1
        lsof |awk '{print $9}'|grep \.conf|sort|uniq >> $1
        lsof |awk '{print $9}'|grep /bin/|sort|uniq >> $1
        lsof |awk '{print $9}'|grep /sbin/|sort|uniq >> $1
        lsof |awk '{print $9}'|grep ttf|sort|uniq >> $1
        lsof |awk '{print $9}'|grep /libexec|sort|uniq >> $1
}

function collect_sample()
{
        /usr/sbin/lsof -F n -f |grep "^n"|sed -e "s:^n::g" | grep -v "^$" | grep -v "^/dev"| grep -v "^/proc"| grep -v "^/tmp"| grep -v "^/var"| grep -v "/.mozilla"| grep "^/[a-z]" > $1
}

collect_sample /tmp/init_sample1

i=0
while [ "$i" -ne "$total_samples_per_boot" ] ; do
        collect_sample /tmp/init_sample2
        cat /tmp/init_sample1 /tmp/init_sample2 | /usr/bin/uniquer > /tmp/init_sample3
        mv /tmp/init_sample3 /tmp/init_sample1
        sleep 0.5
        i=$((i+1))
done

cat /tmp/init_sample1 "$final_db$slot" | /usr/bin/uniquer > "$final_db"

rm -f /tmp/init_sample[123] [/code:1]
/usr/bin/uniquer
[code:1]#!/bin/sh
/usr/bin/awk '{
if ($0 in stored_lines)
        x=1
else
        print
        stored_lines[$0]=1
}' $1 [/code:1]
接着执行以下命令:
[code:1]# chmod +x /usr/sbin/sample-init-process /usr/bin/uniquer /etc/init.d/my-readahead
# rc-update add my-readahead default
# touch /forcesampler
[/code:1]
重启,如果开机时停在
Starting readahead ROYALE...
不动时,按Enter继续。my-readahead是用来创建预读文件表的,所以此时系统可能会有点慢。这时你可以将常用的,启动较慢的程序(如Firefox,Openoffice等)一一打开,以便让my-readahead进程正确读取所需的文件。此进程持续的长短可修改,在/usr/sbin/sample-init-process里那段
[code:1]# It will take twice this amount of seconds to finish sampling.
total_samples_per_boot=200[/code:1]按需要改成100,300之类的。查看此进程是否仍在运行可用以下命令:
[code:1]$ ps -aef | grep -v grep | grep sample-init-process[/code:1]
当这个进程结束后,再次重启。这次你应该会感到系统得到加速了,启动Firefox,Openoffice等软件应该比以前快了点。不过用这方法你得有足够的内存,我这里只有512M,已经可以感觉到有分别了。

创建的预读文件表可在/etc/conf.d里找到,即sofile-list.load那些。如果需要重建预读文件表,只需
[code:1]# touch /forcesampler [/code:1]然后重启一次便可以了。

如果不想用了,则可以:
[code:1]# rc-update del my-readahead && rm etc/init.d/my-readahead /usr/sbin/sample-init-process
\ /etc/conf.d/sofile-list.load* /usr/bin/uniquer[/code:1]
发表于 2006-11-21 14:22:28 | 显示全部楼层
试了下,确实感觉快了点。看来Gentoo论坛有不少好东西没发掘出来啊!
回复

使用道具 举报

发表于 2006-11-21 14:50:19 | 显示全部楼层
还有个runlevel排序问题,before logger得到的预加载列表长度很有限。在桌面开足常用软件再执行sudo /usr/sbin/sample-init-process /etc/conf.d/sofile-list.load才真正能体会这个readahead的好处。
回复

使用道具 举报

发表于 2006-11-21 14:54:41 | 显示全部楼层
看贴时还以为每次开机都会重建那个文件表,后来发现sample-init-process完成后,那个/forcesampler文件不见了。
回复

使用道具 举报

 楼主| 发表于 2006-11-21 15:06:12 | 显示全部楼层
[quote:b7f40a86fa="midx"]还有个runlevel排序问题,before logger得到的预加载列表长度很有限。在桌面开足常用软件再执行sudo /usr/sbin/sample-init-process /etc/conf.d/sofile-list.load才真正能体会这个readahead的好处。[/quote]
这个不是问题,那个脚本会不停地搜集sample,并持续更新,只需在sample-init-process完成之前(此进程持续时间的设置贴子里写了)将各个应用程序打开,便可以得到正确的预加载列表。
回复

使用道具 举报

 楼主| 发表于 2006-11-21 15:08:00 | 显示全部楼层
[quote:82aedc7417="phaedo"]看贴时还以为每次开机都会重建那个文件表,后来发现sample-init-process完成后,那个/forcesampler文件不见了。 [/quote]
是啊,创建预读文件表只是一次性的。
回复

使用道具 举报

发表于 2006-11-21 15:27:49 | 显示全部楼层
[quote:e2a2e89df6="Hauser"][quote:e2a2e89df6="midx"]还有个runlevel排序问题,before logger得到的预加载列表长度很有限。在桌面开足常用软件再执行sudo /usr/sbin/sample-init-process /etc/conf.d/sofile-list.load才真正能体会这个readahead的好处。[/quote]
这个不是问题,那个脚本会不停地搜集sample,并持续更新,只需在sample-init-process完成之前(此进程持续时间的设置贴子里写了)将各个应用程序打开,便可以得到正确的预加载列表。[/quote]
可是我这里sample-init-process这个进程在进入桌面后就不见了
只能touch /forcesampler后eselect rc restart my-readahead
100~300是个什么概念?再加大些看看……
回复

使用道具 举报

 楼主| 发表于 2006-11-21 15:45:01 | 显示全部楼层
[quote:a6e721b8e1="midx"]...
100~300是个什么概念?再加大些看看……[/quote]
是sample的数量,乘2则是进程持续的秒数。
回复

使用道具 举报

发表于 2006-11-21 15:53:33 | 显示全部楼层
我觉得这个列表不应太长,象adobe reader这样占大内存的东西也应避免,最好是能在X启动之前预载完毕,要不然进X时可能会令系统慢下来。
回复

使用道具 举报

 楼主| 发表于 2006-11-22 19:18:11 | 显示全部楼层
[quote:503dd1fa2a="phaedo"]我觉得这个列表不应太长,象adobe reader这样占大内存的东西也应避免,最好是能在X启动之前预载完毕,要不然进X时可能会令系统慢下来。[/quote]
有道理,固然可以自行修改列表,也可以考虑在建立列表后将my-readahead换至boot runlevel。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-3-28 18:50 , Processed in 0.061701 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表