2008-01-15

Ubuntu rocks but has its drawbacks

2008-01-03 14:15:02

"Ubuntu rocks but has its drawbacks" -这段话源自lxpages.com blog April 24, 2007写的一篇文章,文中提到了如何根据自己的需要优化Ubuntu的性能(作者用的UbuntuFiesty Fawn (7.04)),它整理了一些关于这个话题的资料,我迫不及待的要看看都有些什么高招:

注意:有提示意义的原文用50% Gray,提高性能的步骤用100% Red

Hacking Ubuntu to Improve Performance

前言:Ubuntu is designed for the average computer user. As a result, there are some running processes that may not be needed and some resources that are not used optimally. Using a variety of commands, you can see what is running, what resources are available, and what resources are being used. The system can be tuned by adjusting kernel parameters, shell parameters, and settings for specific applications.

1.1 Introduction
- Different versions of Ubuntu (and Linux) use different startup scripts and run different support processes. Knowing how one version of Linux works does not mean that you know how all versions work.

1.2 Viewing Running Processes
- ps –ef shows every (-e) running process in a full (-f) detailed list

- Unix的两大分支:BSD(FreeBSD, OpenBSD, SunOS, and Mac OS X) and System V(it follows the POSIX standards, HP-UX, AIX, Solaris, and IRIX). 不同分支在进程管理、设备驱动的命名和系统目录的位置等方面会有一些不同。Linux呢?Linux supports both BSD and POSIX.

1.3 Killing Processes

- Note: Technically, PIDs are handles to processes and not actual processes. A multi-threaded process may have one handle for all threads, or one handle per thread. It all depends on how the process creates the threads.

- there are some processes that cannot be killed.

  • Zombies:—When a process dies, it returns an error code to its parent (PPID). A dead PD whose return code has not yet been received by its parent becomes a zombie. Zombies take up no CPU, but do take up a PID. For programmers, calling the wait() function retrieves return codes and kills zombies. In contrast, sending a kill signal to a zombie does nothing since the process is already dead.
  • I/O Bound—A process that is blocked on a kernel driver call may not process the kill signal until the kernel call returns. This is usually seen with network file system calls (NFS) when the network is down or on disk I/O when the drive is bad. For example, if you are using an NFS mounted directory and run ls, the command may hang if the network mount is bad. Sending a kill signal to the ls process will not immediately kill it. I've also experienced these hangs when using dd to copy a disk that was in the middle of a head-crash.
  • Interception—Some kill signals can be intercepted by applications. For example, programs can intercept the default signal (kill, kill -15, or kill –TERM). This is usually done so the program can clean up before exiting. Unfortunately, some programs don't die immediately. Other kill signals, such as kill –KILL or kill -9, cannot be intercepted.

- Tip: (稳妥而且优雅的kill方式) If you really want to kill a process, first use kill PID (e.g., kill 1234). This sends a TERM signal and allows well-behaved processes to clean up resources. If that does not get the result you want, try kill -1 PID. This sends a hang-up signal, telling the process that the terminal died. This signal is usually only intercepted by well-behaved processes; other processes just die. If that does not kill it, then using kill -9 PID. This is a true kill signal and cannot be intercepted by the process.

- Signals are flags; they are not queued up. If you send a dozen TERM signals to a process before the process can handle them, then the process will only receive one TERM signal. Similarly, if a program spawns six children and all die at once, then the parent may only receive one CHLD signal. If the parent fails to check for other dead children, then the remaining children could become zombies.

- Stop processes—(如何kill掉那些能自动spawn的进程)Infinite spawning loops usually happen because one process detects the death of another process. Instead of killing the processes, use the stop signal: kill -STOP PID or killall -STOP Name. This will prevent further spawning and enable you to kill all the sleeping processes without them re-spawning

1.4 Identifying Resources

- Linux provides a virtual file system that is mounted in the /proc directory. This directory lists system resources and running processes.

- Measuring Disk I/O If the system seems to be running slowly, you can use iostat (sudo apt-get install sysstat) to check the performance.

1.5 Measuring Memory Usage Network Throughput

- The command swapon –s will list the available swap space and show the usage. There is usually a little swap space used, but if it is very full then you either need to allocate more swap space, install more RAM, or find out what is consuming the available RAM.

- In addition, the pmap command can show you memory allocations for specific process IDs.(这难道不是我前段时间到处找的、用来察看每个module的内存分布的命令么?)

- There is no simple way to determine video memory. If you have a PCI memory card, then the command lspci –v will show you all PCI cards (including your video card) and all memory associated with the card.

- If the computer seems sluggish when accessing the network, then you can check the network performance using netstat -i inet.

1.6 Finding Process Startups

- Inspecting Boot Scripts: The directory /etc/init.d/ contains scripts for starting and stopping system services. These scripts are activated by links in the different rc directories (/etc/rc0.d, /etc/rc1.d, and so on). As init enters each run level, the appropriate S links are executed. For example, at run level 1, all S links in /etc/rc1.d/ are started. This includes S20single for single-user mode configuration. When the system changes levels, all K (for kill) scripts are executed before changing run levels. To find the current run level, you can either use who -r or the runlevel command.

- Inspecting Device Startups: Dynamic device configuration is managed by udev. The udev process watches and manages plug-and-play devices. Based on the configuration in /etc/udev/rules.d/, different applications may be launched.

- Inspecting Network Services: There are four directories of scripts for managing network interfaces:

  • /etc/network/if-pre-up.d/—This contains steps that must be completed before (pre) the interface is brought up. For example, the scripts may need to load wireless drivers.
  • /etc/network/if-up.d/—These scripts are used after bringing up the configured network interface. For example, this is where the system clock is set—every time any network is brought up, the computer's clock is set.
  • /etc/network/if-down.d/—These scripts are used to remove any running configuration. For example, if you have the Postfix mail server running, then there is a script that tells Postfix to reload its configuration when an interface is taken down.
  • /etc/network/if-post-down.d/—This directory contains any cleanup stages that are needed after (post) the interface is taken down. For example, unnecessary wireless drivers can be unloaded.

- Inspecting Shell Startup Scripts

Table 7-1: Shell Startup Scripts

Initialization Script Purpose

/etc/profile Used by every login shell, system-wide.

$HOME/.bash_profile Used on a per-user basis. Each user can have a personalized login script.

/etc/bash.bashrc Every interactive shell runs this system-wide configuration script.

$HOME/.bashrc Every interactive shell runs this user-specific configuration script.

/etc/bash.logout If you create it, this system-wide script is executed every time a user logs out.

$HOME/.bash_logout User-specific script that is used during logout.

1.7 Further Inspection

- 根据不同需求来设置X-Windows的启动配置

Everyone running X-Windows?
* Use /etc/X11/Xsession or place the startup script under /etc/X11/Xsession.d/.
Just you running X-Windows? * Use $HOME/.xsession.
Everyone running Gnome? * Use /etc/X11/gdm/PostSession/Default. This will work for Gnome users, but not KDE or other desktops. KDE, XDM, and other desktops have their own configuration directories and files.

? Just you running Gnome? * Use $HOME/.gnomerc or the graphical session editor

- 关掉CD-ROMAuto run一类的设置:System>Preferences>Removable Drives and Media.

- Inspecting Schedulers: at, cron, and anacron Programs that should run periodically are usually placed in a scheduler. The three common schedulers are at, cron, and anacron.

Scheduling with at The at command specifies that an application should run at a specific time. This is used for run-once commands.

Scheduling with cron While at is used for one-time applications, cron is used for repeated tasks

Scheduling with anacron While cron runs tasks repeatedly, it makes no distinction as to the system state. The anacron service is similar to cron, but allows tasks to be run based on a relative period rather than an absolute date.

1.8 Tuning Kernel Parameters

- Many of the tunable performance items can be configured directly by the kernel. The command sysctl is used to view current kernel settings and adjust them. For example, to display all available parameters (in a sorted list), use: sudo sysctl -a | sort | more

- There are two ways to adjust the kernel parameters. First, you can do it on the command line. For example, sudo sysctl -w kernel.threads-max=16000. This change takes effect immediately but is not permanent; if you reboot, this change will be lost. The other way to make a kernel change is to add the parameter to the /etc/sysctl.conf file. Adding the line kernel.threads-max=16000 will make the change take effect on the next reboot. Usually when tuning, you first use sysctl –w. If you like the change, then you can add it to /etc/sysctl.conf. Using sysctl –w first allows you to test modifications. In the event that everything breaks, you can always reboot to recover before committing the changes to /etc/sysctl.conf.

- The biggest improvement you can make to any computer running Ubuntu is to add RAM.

1.9 Speeding Up Boot Time

- Use sysv-rc-conf (sudo apt-get install sysv-rc-conf) to change the enable/disable settings.

- 可以考虑disable下列服务:

  • anacron—As mentioned earlier, this subsystem periodically runs processes. You may want to disable it and move any critical services to cron.
  • atd and cron—By default, there are not at or cron jobs scheduled. If you do not need these services, then they can be disabled. Personally, I would always leave them enabled since they take relatively few resources.
  • apmd—This service handles power management and is intended for older systems that do not support the ACPI interface. It only monitors the battery. If you have a newer laptop (or are not using a laptop), then you probably do not need this service enabled.
  • acpid—The acpid service monitors battery levels and special laptop buttons such as screen brightness, volume control, and wireless on/off. Although intended for laptops, it can also support some desktop computers that use special keys on the keyboard (for example, a www button to start the browser). If you are not using a laptop and do not have special buttons on your keyboard, then you probably do not need this service.
  • bluez-utiles—This provides support for BlueTooth devices. If you don't have any, then this can be disabled.
  • dns-clean, ppp, and pppd-dns—These services are used for dynamic, dial-up connections. If you do not use dialup, then these can be disabled.
  • hdparm—This system is used to tune disk drive performance. It is not essential and, unless configured, does not do anything. The configuration file is /etc/hdparm.conf and it is not enabled by default.
  • hplip—This provides Linux support for the HP Linux Image and Printing system. If you do not need it, then it can be disabled. Without this, you can still print using the lpr and CUPS systems.
  • mdadm, mdadm-raid, and lvm—file system support for RAID (mdadm and mdadm-raid) and Logical Volume groups (lvm). If you do not use either, then these can be disabled.
  • nfs-common, nfs-kernel-server, and portmap—re used by NFS—present if you installed NFS support. If you do not need NFS all the time, then you can disable these and only start the services when you need them:

sudo /etc/init.d/portmap start
sudo /etc/init.d/nfs-common start
sudo /etc/init.d/nfs-kernel-server start

  • pcmcia and pcmciautils—rovide support for PCMCIA devices on laptops. If you do not have any PCMCIA slots on your computer, then you do not need these services.
  • powernowd and powernowd.early—ervices are used to control variable-speed CPUs. Newer computers and laptops should have these enabled, but older systems (for example, my dual-processor 200 MHz PC) do not need it.
  • readahead and readahead-desktop—ervices are used to preload libraries so some applications will initially start faster. In a tradeoff for speed, these services slow down the initial boot time of the system and consume virtual memory with preloaded libraries. If you have limited RAM, then you should consider disabling these services.
  • rsync— a replacement for the remote copy (rcp) command. Few people need this—sed to synchronize files between computers.
  • vbesave—rvices monitors the Video BIOS real-time configuration. This is an ACPI function and is usually used on laptops when switching between the laptop display and an external display. If your computer does not support APCI or does not switch between displays, then you do not need this service.

Tip: There is a System>Admin>Services applet for enabling and disabling some services. However, this applet only knows of a few services; it does not list every available service. The sysv-rc-conf command recognizes far more services and offers more management options.

The sysv-rc-conf command shows most of the system services. However, it does not show all of them. If the service's name ends with .sh, contains .dpkg-, or is named rc or rcS, then it is treated as a non-modifiable system service. To change these services, you will need to manually modify the /etc/init.d/ and /etc/rc*.d/ directory contents.

原文:http://blog.lxpages.com/2007/04/24/ubuntu-performance-guides/

另外一篇文章的主要内容是:

- 重新编译内核并打一个Con Kolivas patches - these are patches designed to improve system responsiveness with specific emphasis on the desktop, but suitable to any workload.

- 配置一些内核参数

- Enable/Disable一些服务

基于我的硬件现状,这些设置没什么新意,倒是它列出的一些Ubuntu服务的说明值得一看:
To enable/disable services go to System -> Administration -> Services
1. acpi-support - leave it on.

2. acpid - The acpi daemon. These two are for power management, quite important for laptop and desktop computers, so leave them on.

3. alsa - If you use alsa sound subsystem, yes leave it on. But if you have the service below, its safe to be off. The default is off when alsa-utils is on.

4. alsa-utils - On my system, this service supercedes the alsa, so I turn off the alsa and turn this on at S level.

5. anacron - A cron subsystem that executes any cron jobs not being executed when the time is on. Most likely you’ve probably turned your computer off when a certain cron job time is ready. For example, updatedb is scheduled at 2am everyday, but at that moment, you computer is off, then if anacron service is on, it will try to catch up that updatedb cron.

6. apmd - If you computer is not that old which can’t even support acpi, then you may try to turn this off.

7. atd - like cron, a job scheduler. I turned it off.

8. binfmt-support - Kernel supports other format of binary files. I left it on.

9. bluez-utiles - I turned it off. I don’t have any bluetooth devices.

10. bootlogd - Leave it on.

11. cron - Leave it on.

12. cupsys - subsystem to manager your printer. I don’t have one so I turned it off, but if you do, just leave it on.

13. dbus - Message bus system. Very important, leave it on.

14. dns-clean - Mainly for cleaning up the dns info when using dial-up connection. I don’t use dial up, so I turn it off.

15. evms - Enterprise Volumn Management system. I turned it off.

16. fetchmail - A mail receving daemon. I turned it off.

17. gdm - The gnome desktop manager. I turned it off anyway since I get use to boot to console first. This is up to you if you want to boot directly to GUI.

18. gdomap - You can turn it off.

19. gpm - Mouse support for console. If you feel you’d better have a mouse on console, go turn it on.

20. halt - Don’t change it.

21. hdparm - tuning harddisk script, should be on.

22. hibernate - If your system support hibernate, leave it on. Otherwise, its useless for you.

23. hotkey-setup - This daemon setup some hotkey mappings for Laptop. Manufacturers supported are: HP, Acer, ASUS, Sony, Dell, and IBM. If you have a laptop in those brands, you can leave it on, otherwise, this might not have any benefits for you.

24. hotplug and hotplug-net - activating hotplug subsystems takes time. I’d consider to turn them off.

25. hplip - HP printing and Image subsystem. I turned it off.

26. ifrename - network interface rename script. Sounds pretty neat but I turned it off. Mainly for managing multiple network interfaces names. Since I have a wireless card and an ethernet card, they all assigned eth0 and ath0 from kernel, so its not really useful for me.

27. ifupdown and ifupdown-clean - Leave it on. They are network interfaces activation scripts for the boot time.

28. inetd or inetd.real - take a look your /etc/inetd.conf file and comment out any services that you don’t need.

29. klogd - Leave it on.

30. laptop-mode - A service to tweak the battery utilization when using laptops. You can leave it on.

31. linux-restricted-modules-common - You need to see if you really have any restricted modules loaded on your system. I’d leave it on.

32. lvm - I don’t use it so I turned it off. Leave it on if you *DO* have lvm.

33. makedev - Leave it on.

34. mdamd - Raid management tool. I don’t use it so I turned it off.

35. mdamd-raid - Raid tool. If you don’t have Raid devices, turn it off.

36. module-init-tools - Load extra modules from /etc/modules file. You can investigate your /etc/modules file and see if there is any modules that you don’t need. Normally, this is turned on.

37. mountvirtfs - mount virtual filesystems. Leave it on.

38. networking - bring up network interfaces and config dns info during boot time by scaning /etc/network/interfaces file. Leave it on.

39. ntpdate - Sync time with the ubuntu time server. Leave it on if you want.

40. nvidia-kernel - I compiled the nvidia driver by myself, so its useless for me now. If you use the ubuntu nvidia driver from the restrict modules, just leave it on. 41. pcmcia - pcmcia device - useless if you are using desktop which doesn’t have pcmcia card. So in that case, turn it off please.

42. portmap - daemon for managing services like nis, nfs, etc. If your laptop or desktop is a pure client, then turn it off.

43. powernowd - client to manage cpufreq. Mainly for laptops that support CPU speed stepping technology. Normally, you should leave it on if you are configuring a laptop, but for desktop, it might be useless.

44. ppp and ppp-dns - Useless to me. I don’t have dial-up.

45. readahead - It seems readahead is a kind of “preloader”. It loads at startup some libs on memory, so that some programs will start faster. But it increases startup time for about 3-4 seconds. So, you can keep it… or not. I tested and I just didn’t feel difference loading programs. So I decided to turn it off. If you have a reason to keep it on, please do so.

46. reboot - Don’t change it.

47. resolvconf - Automatically configuring DNS info according to your network status. I left it on.

48. rmnologin - Remove nologin if it finds it. It wouldn’t happen on my laptop, so I got rid of it.

49. rsync - rsync daemon. I don’t use it on my laptop, so turned it off.

50. sendsigs - send signals during reboot or shutdown. Leave it as it is.

51. single - Active single user mode. Leave it as it is.

52. ssh - ssh daemon. I need this so I turned it on.

53. stop-bootlogd - stop bootlogd from 2,3,4,5 runlevel. Leave it as it is.

54. sudo - check sudo stauts. I don’t see any good to run it everytime on a laptop or desktop client, so I turned it off.

55. sysklogd - Leave it as it is.

56. udev and udev-mab - Userspace dev filesystem. Good stuff, I left them on.

57. umountfs - Leave it as it is.

58. urandom - Random number generator. Might not useful but I left it on.

59. usplash - Well, if you really want to see the nice boot up screen, leave it as it is.

60. vbesave - video card BIOS configuration tool. Its able to save your video card status. I left it on.

61. xorg-common - setup X server ICE socket. Leave it as it is.

62. adjtimex - This is a kernel hw clock time adjusting too. Normally, you shouldn’t see this on your boot up list. In very rare case if you do see its on your boot up process, then there might be a reason why it is on, so better leave it that way. In my case, it is off.

63. dirmngr - A certification lists management tool. Work with gnupg. You will have to see if you need it or not. In my case, I turned it off.

64. hwtools - A tool to optimize irqs. Not sure what’s the benefits of turning it on. In my case, I turned it off.

65. libpam-devperm - A daemon to fix device files permissions after a system crash. Sounds pretty good, so I left it on.

66. lm-sensors - If you matherboard has builtin some sensor chips, it might be helpful to see hw status via userspace. I ran it and it said “No sensors found”, so I turned it off.

67. screen-cleanup - A script to cleanup the boot up screen. Well, turn on or off is up to you. In my case, I left it on.

68. xinetd - A inetd super daemon to manage other damons. In my system, the xinetd is managing chargen, daytime, echo and time (find them from /etc/xinetd.d dir), I care none of them, so I turned it off. If you do have some important services configured under xinetd, then leave it on.

No comments: