Hauser 发表于 2006-11-20 18:40:23

又一系统加速方法

原帖:http://forums.gentoo.org/viewtopic-t-478491-postdays-0-postorder-asc-start-0.html?sid=231b69c8a6f78989057cd9d91ca23873
这个和readahead-list的原理有点象,分别则是readahead-list的预读文件表是静态的,预设的,而本贴的方法则是实时探测运行中的系统所需加载的文件。

首先安装lsof:
# emerge -av lsof
然后用文字编辑器创建以下三个文件:
/etc/init.d/my-readahead
#!/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
}
/usr/sbin/sample-init-process

#!/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]
/usr/bin/uniquer
#!/bin/sh
/usr/bin/awk '{
if ($0 in stored_lines)
      x=1
else
      print
      stored_lines[$0]=1
}' $1
接着执行以下命令:
# chmod +x /usr/sbin/sample-init-process /usr/bin/uniquer /etc/init.d/my-readahead
# rc-update add my-readahead default
# touch /forcesampler

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

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

如果不想用了,则可以:
# 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

muraji 发表于 2006-11-21 14:22:28

试了下,确实感觉快了点。看来Gentoo论坛有不少好东西没发掘出来啊!

midx 发表于 2006-11-21 14:50:19

还有个runlevel排序问题,before logger得到的预加载列表长度很有限。在桌面开足常用软件再执行sudo /usr/sbin/sample-init-process /etc/conf.d/sofile-list.load才真正能体会这个readahead的好处。

phaedo 发表于 2006-11-21 14:54:41

看贴时还以为每次开机都会重建那个文件表,后来发现sample-init-process完成后,那个/forcesampler文件不见了。 :)

Hauser 发表于 2006-11-21 15:06:12

还有个runlevel排序问题,before logger得到的预加载列表长度很有限。在桌面开足常用软件再执行sudo /usr/sbin/sample-init-process /etc/conf.d/sofile-list.load才真正能体会这个readahead的好处。
这个不是问题,那个脚本会不停地搜集sample,并持续更新,只需在sample-init-process完成之前(此进程持续时间的设置贴子里写了)将各个应用程序打开,便可以得到正确的预加载列表。

Hauser 发表于 2006-11-21 15:08:00

看贴时还以为每次开机都会重建那个文件表,后来发现sample-init-process完成后,那个/forcesampler文件不见了。 :)
是啊,创建预读文件表只是一次性的。

midx 发表于 2006-11-21 15:27:49

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

Hauser 发表于 2006-11-21 15:45:01

...
100~300是个什么概念?再加大些看看……
是sample的数量,乘2则是进程持续的秒数。

phaedo 发表于 2006-11-21 15:53:33

我觉得这个列表不应太长,象adobe reader这样占大内存的东西也应避免,最好是能在X启动之前预载完毕,要不然进X时可能会令系统慢下来。

Hauser 发表于 2006-11-22 19:18:11

我觉得这个列表不应太长,象adobe reader这样占大内存的东西也应避免,最好是能在X启动之前预载完毕,要不然进X时可能会令系统慢下来。
有道理,固然可以自行修改列表,也可以考虑在建立列表后将my-readahead换至boot runlevel。
页: [1]
查看完整版本: 又一系统加速方法