当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 4 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 让你的Gentoo起飞 -- 又名: xhy是怎样炼成的 :)
帖子发表于 : 2007-09-02 11:02 
头像

注册: 2007-06-02 14:56
帖子: 547
送出感谢: 0 次
接收感谢: 0 次
非常精彩的一篇Gentoo优化指南,原帖链接:http://forums.gentoo.org/viewtopic.php?t=231170
版本: asph on Mon Jul 31, 2006 2:29 am

HOW TO OPTIMIZE AND ACCELERATE YOUR SYSTEM
also known as how to fly with gentoo

This is guide is a sum of a lot of posts here in the forum and some investigations i have done in my boxes by trying out several programs and options. Some of them are completely safe, but some other could damage your system. Please make backups of your important data, try this guide in testing boxes, and then move the optimizations to the production boxes.
This improvements will obviously have a greater effect on a old computer, if you have a very good computer this will have a minor impact, as your system should already be pretty fast ;)

INDEX

0. How to make tests without being in danger
1. Optimization of the init scripts
2. Using rc-update
3. Cflags and ldflags
4. Using hdparm
5. Should you.. prelink?
6. Managing the Swap
7. Ccache
8. Distcc
9. USE's
10. Modifying ebuilds and injecting packages
11. Halt vs Suspend
12. Xdelta - Deltup
13. NPTL
14. GCC
15. Filesystems [data safety vs speed]
16. i/o and tasks schedulers
17. Useful Scripts


18. New tips


0. How to make tests without being in danger

To test new ebuilds and try new configurations I did an instalation inside a chroot to be completely safe. There's is a guide HERE to make a similar thing, and be able to test this howto without damaging your system. You could also use vmware or uml, but this seems a fastest way.
Once I did the chroot install, I made a compressed image, so that everytime I broke the sub-system with my tests i can extract it and go on testing with a new installed system (and my system keeps clean).

1. Optimization of the init scripts

Some of the operations performed when you boot the system are not always needed. Let's modify the scripts so that we only do them if they are really needed.

/etc/init.d/modules

change:

代码:
ebegin "Calculating module dependencies"
    /sbin/modules-update &>/dev/null
eend $? "Failed to calculate dependencies"


for:
代码:
if [ /etc/modules.d -nt /etc/modules.conf ]
    then
        ebegin "Calculating module dependencies"
        /sbin/modules-update &>/dev/null
        eend $? "Failed to calculate dependencies"
    else
        einfo "Module dependencies are up-to-date"
fi

Doing this, modules-update will only run if it?s really needed because you made changes in the system.

/etc/init.d/localmount

change:
代码:
mount -at nocoda,nonfs,noproc,noncpfs,nosmbfs,noshm >/dev/null


for:
代码:
mount -aFt nocoda,nonfs,noproc,noncpfs,nosmbfs,noshm >/dev/null


The will start all the local mounts at the same moment, not one after the other.

/etc/init.d/bootmisc

change:
代码:
if [ -x /sbin/env-update.sh ]
then

    ebegin "Updating environment"
    /sbin/env-update.sh >/dev/null
    eend 0
fi


for:
代码:
if [ -x /sbin/env-update.sh ]
then
    if [ /etc/env.d -nt /etc/profile.env ]
    then
        ebegin "Updating environment"
        /sbin/env-update.sh >/dev/null
        eend 0
    else
        einfo "Environment up-to-date"
    fi
fi


Doing this, env-update will only run if it's really needed because you made changes in the system.

/etc/conf.d/rc

change:
代码:
RC_PARALLEL_STARTUP="no"


for:
代码:
RC_PARALLEL_STARTUP="yes"



This will start all the services at the same moment, not one after the other.

note: could some devel explain if this tweaks are dangeorus, and if not why they are not in portage by default?


2. Using rc-update

Managing the runlevels is very easy thanks to rc-update, that makes easy this job:

to see how we have currently configured the boot runlevels:
代码:
# rc-update show



to remove a service from the system start:
代码:
# rc-update del aplicacion runlevel


note: change runlevel for boot or default (you could also create more), if you omit the runlevel it will search in all of them and remove the service.

to add an application:
代码:
# rc-update add application runlevel



I have some service in the boot runlevel and some in the default runlevel, please note that some services need to be started after some other (this means that some services depends on some other.

You can check the depends editing the /etc/init.d/service file and checking the first lines, where the depends are declared. For example, if you want to start sshd, you will need the net services started before.

I have recently created a new runlevel (battery) where i added all i want to have running when i don't have AC. Then, with acpid's help I configured the runlevel so that when i unplug the AC, it changes to battery runlevel, and the when i plug it again it gets back to default runlevel. This way, when I switch to the battery level i use speedfreq, hdparm and iwconfig to reduce the power consumption of the hardisk, the wireless card and the processor.

You can check the actual runlevel using rc-status:
代码:
# rc-status
Runlevel: battery
acpid                                          started
alsasound                                      started
domainname                                     started
gpm                                            started
hdparm.battery                                 started
local                                          started
metalog                                        started
speedfreq.battery                              started
vixie-cron                                     started
wireless.baterry                               started



more information about the RC- HERE.

For those who boot directly into X (xdm, gdm, kdm,..) i have read that you can put xdm and it's dependencies in boot runlevel, so that it will load X while loading the rest of services in the background. If someone have this running please comment so that the rest can use it.

3. cflags and ldflags

CFLAGS (you can set them at /etc/make.conf) are parameters we pass to gcc when we compile a package when emerging it. You can be more or less risky here, in THIS webpage and THIS one there is a lot of info on recommended configurations for CFLAGS. My CFLAGS for my pentium4 box are the following:

代码:
CHOST="i686-pc-linux-gnu"
CFLAGS="-march=pentium4 -mcpu=pentium4 -O3 -pipe -fforce-addr -falign-functions=4 -fprefetch-loop-arrays -fomit-frame-pointer"



note: cflags changed a little bit in gcc 3.4.X and -mcpu is deprecated, you should use -mtune instead. Moreover, pentium-m is accepted for centrino laptops. The CFLAGS of my centrino laptop are the following:
代码:
CFLAGS="-O3 -march=pentium-m -mtune=pentium-m -pipe -ftracer -fomit-frame-pointer -ffast-math -momit-leaf-frame-pointers"



As for the CXXFLAGS, -fvisibility-inlines-hidden has been reported as a good flag to improve the C++ compiles (thx teutzz).
Frepo has also been reported as a good cxxflag, but it does not perform very well on my system.

The ldflags are also interesting, they are discussed HERE and HERE. Ldflags are optimizations for the dinamyc loader (ld), so it's more or less the same way prelink would go. I am using them already and didn't have any problem when emerging new packages:
代码:
LDFLAGS="-Wl,-O1 -Wl,--enable-new-dtags -Wl,--sort-common -s"


Something more "stable" would be
代码:
LDFLAGS="-Wl,-O1"


note: those in the ldflags are "L", not "1" (in the -W)
note: some users reported errors using ldflags, if you have problems compiling a package try to remove them (and post ldflags/package in this thread).

NOTE: Cflags and Ldflags are and will be always a subject in which everybody has his opinion. The best way is to test them yourself, do somebenchmarks, and keep the flags you find optimal for your system. Also check the differents posts in the forums talking about this so that you can get an idea. A useful program to determine what cflags you should use, is ACOVEA (already in portage), which is a benchmarking tool that will do several tests on your machine (the standard test will run for more than 15 hours) to help you pick the best cflags for your machine. See THIS thread for acovea scripts and results.

4. Using hdparm

Another important application is hdparm. It allows us to configure our hard disk parameters, so get the maximum performance:

代码:
# emerge hdparm
# rc-update add hdparm default


See /etc/conf.d/hdparm

see actual configuration:
代码:
# hdparm -i /dev/hda



testing the speed:
代码:
代码:
# hdparm -Tt /dev/hda



In my computer I modifified /etc/conf.d/hdparm to get the maximum of my hard disk:
代码:
hda_args="-d1 -X69 -c1"
cdrom0_args="-d1"



If you need more information the man page is pretty good
代码:
# man hdparm


Also check THIS for a little tutorial.

5. Should you.. prelink?

Prelink is a powerful application, it allows us to pre-link the libraries needed for a binary before using it. So instead of looking for which libraries the binary will need when we launch it, prelink will modify the binary addind a little descriptions of the libs it nees to run. This get rid of the search for shared libraries everytime we launch the binary, so it makes it faster.
Important: Everytime that you upgrade the libraries that are needed for the binaries, (for example glibc) you have to re-run prelink on the system.

This is a little optimization that we will thank when launching big applications like KDE (moreover, if you prelink your system KDE will not need to launch kdeinit, so it will also run faster). The little binaries are already pretty fast, so will not appreciate the difference.

Requirements: it is a must to have compiled the binaries with binutils-2.13.90.0.xx and gcc-3.2 or higher, and also have installed glibc-2.3.1-r2 or higher. The size of the binaries will be bigger with prelink, and to run the process you need enough free space on the hard disk.

Way to go:
代码:
# emerge prelink


Find config file at /etc/prelink.conf
代码:
# prelink -afmR


This is the common use of prelink, that will prelink ALL the binaries, and also will look if the binaries were already prelinked, and prelink them again if needed.

It is possible that you get some errors when running prelink, because some binaries can not be prelinked (the ones compressed with upx, for example).

More information HERE, and/or man prelink.

6. Managing the swap

In this section I just wanted to mention some things that can help us.
First of all, if you have two harddisks it is better to put the swap partition on the second disk (having the root partition in the first one) as this will improve the read/write times.

Also you should NOT use a file as swap. I tried it once in an old computer, deleted the swap partition and set a file as ./swap of 256 MB.? and modified fstab. This method is slower, as it has to find the file, open the file, find were it was writting to it, writte to it, save, and close the
file. With a partition in the disc this process is pretty much faster.

Another concept to know about is swappiness (kernel 2.6+). When an application needs memory and the RAM is full, there are two options: or the RAM gets empties a little bit cleaning out old used memory, or swap memory gets used (slower than ram). In the new kernels, you can set a variable to define if the kernel should empty some ram or use the swap partition.

/etc/sysctl.conf
代码:
vm.swappiness = 40


This value can be between 0 and 100. Close to 0 will mean that the kernel should empty some ram, and a higher value close to 100 will tell the kernel to use the swap memory more often.
The default value is 60. I set this to 25 in my laptop, so that i can reduce the disk access. You can use ?free -m? to see the stats of your memory useage.

7. Ccache

ccache is an application (included en portage and on by default when emerging (if you emerge ccache)) that acts as a cache for the compiler.
With this little prog we will be able to compile in a faster way when compiling packages, specially with the big make's (you could think that using a cache when compiling has no use, but it speeds up some instructions like the make clean)

Just emerge it and set the default cache size (read the einfo after the emerge), and it will be on by default. You can check it with:
代码:
# emerge info | grep ccache
ccache version 2.3 [enabled]


You can check the ccache stats by doing:
代码:
代码:
# ccache -s



8. Distcc

distcc is an application that will make our life easier when installing gentoo in several computers, or if our computer is very slow but have another fast computer. It can be combined with ccache, optimizing the compiling time. I won't explaint this
feature a lot, because it's not used by everyone. Just that you know about it, because you can use cross-compiling with different computers with different processors, they will just share the compiling tasks.

More information HERE.

[siez=16]9. USE's[/size]

The USE flags are a useful tool that gentoo provides us with to configure our packages as we want them.

For example, imagine that you want to compile apache:
代码:
# emerge -pv apache
[ebuild N ] net-www/apache-2.0.50 -debug -doc -ipv6 +ssl 6,197 kB


The (+/-) options that appear after the name of the program we are going to install (-v flag) are the USE vars we can use to configure the package we are installing. If, for example, we don't want our apache to use ssl, we can do this:
代码:
# USE="-ssl" emerge -pv apache
[ebuild N ] net-www/apache-2.0.50 -debug -doc -ipv6 -ssl 6,197 kB


We can see that ssl is now off, and apache won't be compiled with ssl support.

In this example we set the USE var directly on the propmt, but this is not the correct way to do it if you want the system to remember the USE flags everytime you compile that package. You can set your USE flags in the /etc/make.conf file, or also use /etc/portage/package.use to set them for
individual packages.

It is the same for the "x86" flag (to tell portage to use the latest version available of every package), it's not recommended to just set the flag on the prompt, you should set it on /etc/make.conf to use it for all the packages, or set it individually:
代码:
# echo "app-editors/nano ~x86" >> /etc/portage/package.keywords



It is important to take a look at the USE flags of the packages before installing them, so that we can turn off features we won't be needing saving space and sometimes dependencies.
For example, if we want to emerge the console irc client BitchX, portage want to install xmms, X, and some other dependencies that could be useless for some of us. Setting -xxms and -X for xmms in the /etc/portage/package.use will allow us to install BitchX without the xmms and X support, and we won't need to install all those packages.

You can see that this is a powerful tool to use when emerging, it gives us a lot of control of how the packages will be installed in our system.

You have a little description of the USE flags in /usr/portage/profiles/use.desc

10. Modifying ebuilds and injecting packages

Always keep a backup of your files before modifying them (also note that emerge sync will fix if you mess an ebuild).

?What's injecting a package and what's the use of it?
Remember that time when you wanted to install package X, and as a dependency portage asked to install package Y? For example, a lot of users manage the kernel manually instead of using portage to do it (gentoo-sources, gentoo-dev-sources, vanilla-sources, and so on). Then, when you emerge some package that needs the kernel-sources (like alsa, or ipw2X00) installed portage will try to install some kernel-sources. Why would you do that if you already have your kernel at /usr/src?

note: injecting has been deprecated, now you should use package.provided and virtuals as explained

- using package.provided:

package.provided tells portage that a package is installed (even if it's not)
You can use this file to put all the packages you want to tell portage you installed by hand.
代码:
echo "sys-kernel/ck-sources" >> /etc/portage/profile/package.provided



package.provided doesn't affect virtuals, virtuals are used to tell portage what packages should install for a given category:
For example, you could tell portage that you want to satisfy x11 with xorg, and not xfree anymore (which is not a valid example anymore, but it is useful to understand the use of it)
代码:
echo "virtual/x11 x11-base/xorg-x11" >> /etc/portage/profile/virtuals



more information on this and /etc/portage management -> man portage

11. Halt vs Suspend

Have you never asked yourself why you shutdown you computer, when you can suspend it to ram or disk?
If you suspend to ram, it has the handicap that it will still need AC input in order to keep the system alive. But suspending to disk is very useful (specially for laptops), because the laptop will "shutdown", but we will keep our sesion.

Doing it this way we avoid to boot all the system services everytime (it not an instant recuperation from the "sleep", but seems faster).
I have used swsusp2 in my laptop and works fine, but you have to patch the kernel sources. You can find info in THIS post on how to do this. Try it and you won't regret.

12. Xdelta - Deltup

In THIS post Deltup is discussed, because a new version is available. The point is that everytime portage needs to download a file, deltup will look for an older version of the file (/usr/portage/distfiles) and try to download only the
difference between the two files, and create the new file on-the-fly.

This can be a big improvement for all those 56k users who keep their /usr/portage/distfiles in order to only download patches when new "revisions" are released, because this can lower the mb to download everytime they want to emerge new versions of the installed packages.

13. Native POSIX Thread Library

This library, know as NPTL, can improve our system, because it's up to 4 times faster than the standard LinuxThreads when creating new threads. You *should* use 2.6 linux headers, and a new version of glibc and gcc (~x86). To use ntpl you just have to recompile glibc with the USE flag
"nptl" turned on.

note: in order to use nptl you need gcc 3.x or higher installed.

HERE you can get more information and benchmarkings.

14. GCC

GCC is very important, specially in a distro like gentoo in which the users look to optimize their system by compiling theirselves the packages they install. Using an actual version of gcc has some advantages, because all the code we create when compiling will be more optimized (from version 3.4.x you can already use the -march=pentium-m for the centrino
processors).

To install the latest gcc, just emerge gcc with ~x86 turned on (rmember to change your profile also):
代码:
# echo "sys-devel/gcc ~x86" >> /etc/portage/package.keywords
# emerge -u gcc



15. Filesystems [data safety vs speed]

After trying out some filesystems, i use reiser4 because it's the fastest filesystem available. I think that it is very useful to use a fast filesystem, but you have to keep backups of your data just in case you have corruption issues.
Also point that, after using ext2, ext3, reiserfs, xfs, reiserfs and som other encripted filesystems i have never had corruption issues with my data. Just keep backups of your data and you will be all set.

Note that you need a version of the kernel with the reiser4 patches, so it will have to be a development patched release.

To convert your / partition to reiser4 you will need a livecd with reiser4 support like THIS one, and install a kernel with that support also.

NOTA: Some people have problems because they can't find the Reiser4 submenu in the Filesystems tree of the kernel, make sure that you have deactivated the option "use 4kb for kernel stacks instead of 8kb) under the kernel hacking submenu, and then you will find the Reiser4 option.

16. i/o and tasks schedulers

coming soon

17. Useful Scripts and tips

There are some useful scripts in the forums and the portage tree to manage our system:

1- esearch

This one is included in the portage tree (you can just emerge esearch), and it's an utility to search trought the portage package database. The good thing is that it's very fast (faster than emerge -sS).
Note that after every emerge sync we have to update de esearch database by running eupdatedb, or you can just use esync to update the portage tree (esync will emerge sync, and then show a list with the changes (new packages) from the last sync).


2- eix

The same concept as esearch, but it's faster (and it is also faster to update the package database).
It's a young project, but works pretty fine.
More info HERE.

3- Cruft

This script from will generate a list with all the files in our system that could be deleted because they don't belong to any installed package. You have to be careful when using it, because it can generase false positives.

Usage:
代码:
# ./cruft > cruft.log


Then you can check the list, and remove from it the files you want to keep.
Then you can run this command to get rid of the files:
代码:
# cat cruft.log | xargs rm -rf



You could also just take a look at the list and delete them yourself.

More info HERE.

4- Stale

Interesting script that will help us to keep /usr/portage/distfiles with an optimal size. The script will search in that dir, and delete (when invoked with the --nopretend option) the old files, not the actual ones.
For example, if we have libtool-1.3.5.tar.gz and libtool-1.5.2.tar.gz, it would delete libtool-1.3.5.tar.gz


It gets messed with files like font-arial-iso-8859-1 and font-arial-iso-8859-2, in which the numeration doesn't not correspond to the versioning of the package (the same with gtk and glib in their versions 1.x and 2.x)
More info HERE.

5- Porthole

Portage frontend made in python (+gtk), that offers us a visual way to configure portage, and it makes easier to configure the packages and emerges. It's already in portage, so you just have to "emerge porthole".

6- kuroo

Another portage frontend, this one is made with Qt (for the kde-lovers). It's a young project, but works fine.

More info HERE.

You can also find more useful scripts and programs related to Portage in THIS thread.

7- portage cdb

This is not a script itself, but it's a nice tip to improve the portage speed. Try it, you wont be disappointed!

8- mounting / in ram

very nice article, worth a read :) http://forums.gentoo.org/viewtopic-t-296892.html

18. New tips (xdarma)

1- Kernel 2.6 performance tips (link)

2- KDE performance tips (link)



-under construction-


最后由 ofewiofewo 编辑于 2007-09-02 12:56,总共编辑了 1 次

页首
 用户资料  
 
2 楼 
 文章标题 :
帖子发表于 : 2007-09-02 11:46 

注册: 2006-09-12 14:09
帖子: 1897
送出感谢: 0 次
接收感谢: 0 次
代码:
CFLAGS="-march=pentium4 -mcpu=pentium4 -O3 -pipe -fforce-addr -falign-functions=4 -fprefetch-loop-arrays -fomit-frame-pointer"

用-O3似乎不见得可以带来什么好处,还会使编译出来的程序体积加大,一般
代码:
CFLAGS="-O2 -march=pentium-m -pipe -fomit-frame-pointer "

就够了,别外关于keyword的mask处理,如果不能自已确认依赖关系,可以考虑使用autounmask


_________________
Fair winds and following seas !


页首
 用户资料  
 
3 楼 
 文章标题 :
帖子发表于 : 2007-09-02 12:12 
头像

注册: 2005-12-28 1:16
帖子: 3916
地址: 火星
系统: Ubuntu 12.10 X64
送出感谢: 1
接收感谢: 0 次
O3优化实际效果并不好 在X86架构的机器上 寄存器少的可怜 跟动辄数千寄存器的平台没法相提并论
O2优化编译时间短很多 跟O3比 性能也不会差 至少X86架构的是这样的

这篇优化思路还是不错的 有些地方有点老了
modules-update已经被update-modules取代了
mount的修改也应当慎重 如果/mnt和/mnt/media都要被挂载 那么这个F是不能加的

env-update中本来就有判断 新版本的不用自己改
代码:
if [[ $1 == "-u" ]] ; then
    is_older_than "${svcdir}/envcache" /etc/env.d && exit 0
    shift
fi



prelink和LDFLAGS容易引起问题 prelink在我机器上目前工作比较正常
为了稳定 我没有用LDFLAGS 一旦出现问题 重新编译的将是整个系统。。。


_________________
目前负债150多万


页首
 用户资料  
 
4 楼 
 文章标题 :
帖子发表于 : 2007-09-02 12:32 
头像

注册: 2006-10-16 7:24
帖子: 624
地址: 安康/西安/广州
送出感谢: 0 次
接收感谢: 0 次
呵呵,我就看官方手册弄的~不过我就选了O3,原来没有必要啊~


_________________
读书取正,读易取变,读骚取幽,读庄取达,读汉文取坚,最有味卷中岁月。
与菊同野,与梅同疏,与莲同洁,与兰同芳,与海棠同韵,定自称花里神仙。


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 4 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 2 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译