1. Trang chủ
  2. » Công Nghệ Thông Tin

Managing the System

24 365 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Managing the System
Trường học Unknown University
Chuyên ngành Computer Systems Management
Thể loại Chapter
Năm xuất bản 2007
Định dạng
Số trang 24
Dung lượng 275,12 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Here are some other options for using vmstat: $ vmstat -S m Display output in 1000k megabytes $ vmstat -S M Display output in 1024k megabytes $ vmstat -S k Display output in 1000-byte ki

Trang 1

Managing the System

Without careful management, the demands onyour Linux system can sometimes exceed theresources you have available Being able to moni-tor your system’s activities (memory, CPU, anddevice usage) over time can help you make surethat your machine has enough resources to dowhat you need it to Likewise, managing otheraspects of your system, such as the device drivers

it uses and how the boot process works, can helpavoid performance problems and system failures

This chapter is divided into several sections thatrelate to ways of managing your Ubuntu or otherLinux system The first section can help you mon-itor the resources (processing power, devices, andmemory) on your Linux system The next sectiondescribes how to check and set your system clock

Descriptions of the boot process and subsequentrun levels follow The last sections describe how

to work with the kernel and related device ers, as well as how to view information aboutyour computer’s hardware components

driv-Monitoring ResourcesUbuntu, Debian, and other Linux systems do awonderful job of keeping track of what they do

If you care to look, you can find lots of tion about how your CPU, hard disks, virtualmemory, and other computer resources arebeing used

informa-You can go to where the Linux kernel stores time information about your system by directlyviewing the contents of files in the /procfile sys-tem (see Appendix C) An alternative, however, is

real-to use commands real-to view information about how

IN THIS CHAPTERChecking memory usewith free, top, vmstat,and slabtop

Viewing CPU use withiostat, dstat, and topMonitoring storagedevices with iostat,vmstat, and lsofWorking with dates/time using date,hwclock, cal, and NTPChanging GRUB bootloader behaviorRebuilding the initialramdisk

Dealing with run levelswith runlevel and initAdding, removing, andlisting services withchkconfig and serviceShutting down the system with reboot,halt, and shutdownChecking and chang-ing kernel driver settings with lsmod,modinfo, and modprobeWatching hardwaresettings with lspci,dmidecode, andhdparm

Trang 2

your computer’s virtual memory, processor, storage devices, and network interfacesare being used on your system.

There are commands that can monitor several different aspects of your system’sresources Because this book is not just a man page, however, we have divided thefollowing sections by topic (monitoring memory, CPU, and storage devices) ratherthan by the commands that do them (top, vmstat, and iostat)

NOTE Some of the applications described in this section are installed by default in Ubuntu, in packages such as the procps package To use iostator sar, however, you need to install the sysstat package Install the sysstat package with the follow- ing command:

$ sudo apt-get install sysstat

Monitoring Memory Use

Few things will kill system performance faster than running out of memory Commandssuch as freeand toplet you see basic information about how your RAM and swap arebeing used The vmstatcommand gives detailed information about memory use andcan run continuously The slabtopcommand can show how much memory the kernel(slab cache) is consuming

The freecommand provides the quickest way to see how much memory is being used onyour system It shows the total amount of RAM (Mem:) and swap space (Swap:), alongwith the amount currently being used Here are examples of the freecommand:

$ free List memory usage in kilobytes (-k default)

total used free shared buffers cached Mem: 742476 725108 17368 0 153388 342544

-/+ buffers/cache: 229176 513300

Swap: 1020116 72 1020044

$ free -m List memory usage in megabytes

total used free shared buffers cached Mem: 725 706 18 0 148 333

-/+ buffers/cache: 223 501

Swap: 996 0 996

$ free -b List memory usage in blocks

total used free shared buffers cached Mem: 760295424 742510592 17784832 0 157114368 350765056

-/+ buffers/cache: 234631168 525664256

Swap: 1044598784 73728 1044525056

$ free -mt List memory usage with totals displayed (Swap + Mem)

total used free shared buffers cached Mem: 725 708 16 0 149 334

-/+ buffers/cache: 223 501

Swap: 996 0 996

Total: 1721 708 1013

$ free -g List memory usage in gigabytes

$ free -s 5 Continuously display memory usage every 5 seconds

Trang 3

To avoid wasting RAM and speed up applications, Linux uses as much otherwiseunused RAM as possible for the disc cache For that reason, the first line of outputfrom freethat often shows little free RAM can be misleading We recommend youpay closer attention to the second line of output, which shows the amount of RAMactually available for applications That amount is 501MB in this example:

The topcommand provides a means of watching the currently running processes, withthose processes sorted by CPU usage or memory (see Chapter 9 for a description oftopfor watching running processes) However, you can also use topto watch your mem-ory usage in a screen-oriented way Here is an example:

$ top

top - 14:14:59 up 3 days, 18:26, 1 user, load average: 0.11, 0.04, 0.01

Tasks: 114 total, 3 running, 111 sleeping, 0 stopped, 0 zombie

Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 742476k total, 727232k used, 15244k free, 153708k buffers

Swap: 1020116k total, 72k used, 1020044k free, 343924k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

For a more detailed view of your virtual memory statistics, use the vmstatcommand.With vmstatyou can view memory use over a given time period, such as since the previousreboot or using a sample period The following example shows vmstatredisplayingstatistics every three seconds:

$ vmstat 3

procs -memory - swap io system

-cpu -r b swpd f -cpu -ree buff cache si so bi bo in cs us sy id wa st

1 0 97740 32488 3196 148360 0 0 0 1 26 3876 85 15 0 0 0

1 1 98388 7428 3204 151472 0 216 0 333 30 3200 82 18 0 0 0

1 0 113316 8148 2980 146968 0 4980 4 5121 79 3846 77 23 0 0 0

Trang 4

to the swap area (see the 216 under the so column) Because the swap area resides onthe hard disk, you can see that the block written to disk device (bo) increases as theswap out increases You can see the amount of swap space being used increasingunder the swpdcolumn.

The CPU is also straining in the example, with no idle time showing (id 0) Noticealso that when some of the applications need to be swapped back in (see the lastthree lines of output), the processor has to wait on two occasions for input/output

to complete (wa 1)

Here are some other options for using vmstat:

$ vmstat -S m Display output in 1000k megabytes

$ vmstat -S M Display output in 1024k megabytes

$ vmstat -S k Display output in 1000-byte kilobytes

$ vmstat -S K Display output in 1024-byte kilobytes

$ vmstat -n 2 10 Output every two seconds, repeat 10 times

$ vmstat -s | less Display event counters and memory statistics

$ vmstat -S M -s | less Display statistics in megabytes

With commands such as psand top, you can see how much memory each application

is consuming on your system The kernel itself, however, has its own memory cache to

Trang 5

keep track of its resources, called the kernel slab You can use the vmstatcommand todisplay kernel slab memory cache statistics(from /proc/slabinfo) as follows:

$ vmstat -m | less Page through kernel slab memory cache

Cache Num Total Size Pages

$ slabtop

Active / Total Objects (% used) : 49127 / 70942 (69.2%)

Active / Total Slabs (% used) : 3094 / 3094 (100.0%)

Active / Total Caches (% used) : 101 / 145 (69.7%)

Active / Total Size (% used) : 8830.29K / 12013.73K (73.5%)

Minimum / Average / Maximum Object : 0.01K / 0.17K / 128.00K

OBJS ACTIVE USE OBJ SIZE SLABS OBJ/SLAB CACHE SIZE NAME

The slabtopoutput updates every three seconds By default, slab caches are sorted

by the number of objects (first column) in each cache By pressing c you can sort bycache size instead (as shown in the previous example)

Monitoring CPU Usage

An overburdened CPU is another obvious place to look for performance problems

on your system The vmstatcommand, shown earlier, can produce basic statisticsrelating to CPU usage (user activity, system activity, idle time, I/O wait time, andtime stolen from a virtual machine) The iostatcommand (from the sysstat pack-age), however, can generate more detailed reports of CPU utilization

Here are two examples of using iostatto display a CPU utilization report:

$ iostat -c 3 CPU stats every 3 seconds (starting apps)

Linux 2.6.21-1.3194.fc7 (davinci) 08/10/2007

Trang 6

avg-cpu: %user %nice %system %iowait %steal %idle

0.50 0.00 0.00 0.00 0.00 99.50 avg-cpu: %user %nice %system %iowait %steal %idle

28.71 0.00 5.45 18.32 0.00 47.52 avg-cpu: %user %nice %system %iowait %steal %idle

98.99 0.00 1.01 0.00 0.00 0.00 avg-cpu: %user %nice %system %iowait %steal %idle

0.50 0.00 24.88 74.63 0.00 0.00 avg-cpu: %user %nice %system %iowait %steal %idle

0.50 0.00 10.00 89.50 0.00 0.00 avg-cpu: %user %nice %system %iowait %steal %idle

0.50 0.00 17.41 82.09 0.00 0.00 avg-cpu: %user %nice %system %iowait %steal %idle

0.00 0.00 14.65 85.35 0.00 0.00

The first iostatexample above starts with a quiet system, then several applicationsstarted up You can see that most of the processing to start the applications is beingdone in user space The second iostatexample shows a case where several largefiles are copied from one hard disk to another The result is a high percentage of time

being spent at the system level, also known as kernel space (in this case, reading from

and writing to disk partitions) Note that the file copies also result in a higher amount

of time waiting for I/O requests to complete (%iowait)

Here are examples using iostatto print CPU utilization reports with timestamps:

$ iostat -c -t Print time stamp with CPU report

Linux 2.6.21-1.3194.fc7 (davinci) 08/10/2007

Time: 9:28:03 AM

avg-cpu: %user %nice %system %iowait %steal %idle

0.50 0.00 0.00 0.00 0.00 99.50

$ iostat -c -t 2 10 Repeat every 2 seconds for 10 times

The dstatcommand (dstat package) is available as an alternative to iostatfor viewinginformation about your CPU usage(as well as other performance-related items) One advan-tage of dstatover other tools is that it more precisely shows the units of measurement

it is displaying (such as kilobytes or megabytes) and also uses colors to differentiate thedata Here is an example of dstatfor displaying CPU information:

$ dstat -t -c 3 View CPU usage continuously with time stamps

-time -

total-cpu-usage epoch _|usr sys idl wai hiq siq

1189727284| 0 0 100 0 0 0

1189727287| 1 0 99 0 0 0

Trang 7

If you want to find out specifically which processes are consuming the most ing time, you can use the topcommand Type top, then press Shift+p to sort by CPUusage (this is the default sorting order):

process-$ top Display running processes and sort by CPU usage

Tasks: 120 total, 3 running, 116 sleeping, 0 stopped, 1 zombie

Cpu(s): 86.8% us, 6.0% sy, 0.0% ni, 3.3% id, 4.0% wa, 0.0% hi, 0.0% si

Mem: 482992k total, 476884k used, 6108k free, 1220k buffers

Swap: 5863716k total, 1166252k used, 4697464k free, 52984k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

9648 chris 16 0 309m 123m 16m R 72.6 26.1 287:55.22 firefox-bin

552 root 15 0 762m 65m 5732 S 15.6 14.0 4388:27 X

The full output would show many more processes, all sorted by current CPU usage(%CPUcolumn) In this example, Firefox web browser (72.6%) and the X display server(15.6%) are consuming most of the CPU If you decided you wanted to kill the Firefoxprocess, you could type k followed by the process ID of Firefox (9648) and the number

9 signal (if for some reason you couldn’t just close the Firefox window normally)

If you want information about the processor itself, you can view information directly fromthe /proc/cpuinfofile Here is an example:

$ cat /proc/cpuinfo View CPU information from /proc

Trang 8

An interesting thing to note about your CPU are the flags that represent features that

it supports Some features in Ubuntu require that particular CPU extensions ated with those flags be on for the Ubuntu feature to work For example, to use theXen virtualization para-virtualized guests, the paeflag must be set To run fully vir-tualized guests, the CPU must have either the vmxflag (for Intel processors) or svmflag (for AMD processors) extension support

associ-Similar information about your processor(s) is collected by the system at the verybeginning of the boot process, and can be obtained by looking at the beginning ofthe output of the dmesgcommand

Monitoring Storage Devices

Basic information about storage space available to your Linux file systems can be seenusing commands such as duand df(as described in Chapter 7) If you want detailsabout how your storage devices are performing, however, commands such as vmstatand iostatcan be useful

Some of the same kind of output from the iostatcommand shown earlier can beused to tell if bottlenecks occur while doing disk reads and writes Here’s an example:

$ iostat 3 Check disk reads and writes per disk

Linux 2.6.21-1.3194.fc7 (davinci) 08/11/2007

avg-cpu: %user %nice %system %iowait %steal %idle

13.15 0.60 0.59 0.16 0.00 85.49 Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn

sda 1.09 32.08 58.94 16086324 29554312

sdb 0.29 5.27 11.23 2644482 5631348

avg-cpu: %user %nice %system %iowait %steal %idle

1.00 0.00 42.14 45.15 0.00 11.71 Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn

sda 411.37 66515.05 2.68 198880 8

sdb 68.23 2.68 14696.99 8 43944

avg-cpu: %user %nice %system %iowait %steal %idle

0.67 0.00 41.00 58.33 0.00 0.00 Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn

sda 239.67 52530.67 106.67 157592 320

sdb 236.00 0.00 55077.33 0 165232

Trang 9

The first part of the output of iostatshows averages of CPU usage since the lastreboot The next part reflects processing that occurs when a large amount of data iscopied from the first disk (sda) to the second disk (sdb) High iowaitvalues indicatethat disk input/output is the bottleneck on the system In other words, faster diskwriting would improve performance more than a faster CPU.

The vmstatcommand can also list statistics about your disks Here’s an example ofusing vmstatto list information about disk reads and writes:

$ vmstat -d Display disk read, write, and input/output statistics

disk- -reads - -writes - total merged sectors ms total merged sectors ms cur sec

IO -sda 332773 74844 19022380 2524211 245477 3473801 29758560 37140075 0 1372 sdb 79963 253716 2646922 2158000 76044 977122 8428140 12489809 0 506

The Linux system in this example has two hard disks (sdaand sdb) You can see thetotal number of sectors successfully read and written from those hard disks You canalso see how many seconds were spent on input/output (IO) for those disks Further -more, you can see if there any I/O operations in progress, and you can also list read/write information for selected disk partitions Here is an example:

$ vmstat -p sda1 Display read/write stats for a disk partition

sda1 reads read sectors writes requested writes

is preventing the unmount and decide if you want to kill the process holding that fileopen and force an unmount of the file system Here is an example of lsof:

$ lsof | less List processes holding files and directories open

COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME

init 1 root cwd DIR 8,5 4096 2 /

init 1 root rtd DIR 8,5 4096 2 /

init 1 root txt REG 8,5 38620 2049530 /sbin/init

bash 23857 chris cwd DIR 8,1 4096 2719746 /mnt/sda1/dx

The first files shown as being open are those held open by the initprocess (the firstrunning process on the system) Files held open by system processes (such as udevd)and daemons (such as sshdand syslogd) follow init Eventually, you will see filesheld open by individual users (which are probably the ones you are interested in ifyou are unable to unmount a disk partition)

Trang 10

NOTE You may see permission restrictions unless you run the sudo

command first:

$ sudo lsof | less

When you are looking at the lsofoutput, you want to see the name of the file

or directory that is open (NAME), the command that has it open (COMMAND), and the process ID of that running command (PID) As is often the case when a file system you want to unmount is being held open, the /mnt/sda1file system isbeing held open by a bashshell in the preceding example (/mnt/sda1/dxis thebash shell’s current working directory) In fact, instead of piping lsofoutput tolessor grep, here are a few other ways you can find what you are looking for from lsofoutput:

$ lsof -c bash List files open by bash shells

$ lsof -d cwd List directories open as current working directory

$ lsof -u chris List files and directories open by user chris

$ lsof /mnt/sda1 List anything open on /mnt/sda1 file system

$ lsof +d /mnt/sda1/dx List anything open under /mnt/sda1/dx directory

As noted previously, you may need to use the sudocommand to acquire the root missions to view all the output of the lsofcommand

per-Mastering Time

Keeping correct time on your Linux system is critical to the system’s proper ing Your computer running Linux keeps time in two different ways: a system clock(which Linux uses to keep track of time) and a hardware clock (that sets the systemtime when Linux boots up)

function-The system time is what is used to set timestamps for file creation, process runtimes,and anything else where date and time are used System time can be viewed and setmanually (with the datecommand) or automatically (with the ntpdservice) The hardware clock is part of the motherboard’s CMOS and runs on a battery attached

to the motherboard when the system is powered off You set the hardware clock withthe hwclockcommand

There are many other tools that can be used to work with time in Linux systems Forexample, there are tools for checking time in different ways, such as using clockdiff(to measure clock difference between computers) and uptime(to see how long yoursystem has been up)

Trang 11

Changing Time/Date with Graphical Tools

Graphical tools in Ubuntu and and other Linux systems for changing the date, timeand time zone used on your system include the Date and Time Settings window(sudo time-admincommand) That window can also be used to enable the NetworkTime Protocol (NTP), to automatically synchronize your Linux system’s date and timewith a selected time server over the network You may need to install NTP support,available as a handy button-click on the Date and Time Settings window

The Date/Time Properties window saves the settings and choices you make DuringUbuntu startup, the system reads these settings to set your time zone and whetheryour system is using UTC time

Your Linux system’s time zone is set based on the contents of the /etc/localtimefile You can set a new time zone immediately by copying the file representing yourtime zone from a subdirectory of /usr/share/zoneinfo For example, to changethe current time zone to that of America/Chicago, you could do the following:

$ sudo cp /usr/share/zoneinfo/America/Chicago /etc/localtime

This can also be accomplished by creating a symlink:

$ sudo ln –s /usr/share/zoneinfo/America/Chicago /etc/localtime

To change the time zone permanently, set it in the Date and Time Settings window tothe time zone you want For example, America/Chicago

Displaying and Setting Your System Clock

The datecommand is the primary command-based interface for viewing and ing date and time settings, if you are not having that done automatically with NTP.Here are examples of datecommands for displaying dates and times in different ways:

chang-$ date Display current date, time and time zone

Sun Aug 12 01:26:50 CDT 2007

$ date ‘+%A %B %d %G’ Display day, month, day of month, year

Sunday August 12 2007

$ date ‘+The date today is %F.’ Add words to the date output

The date today is 2007-08-12

$ date date=’4 weeks’ Display date four weeks from today

Trang 12

Although our primary interest in this section is time, since we are on the subject

of dates as well, the calcommand is a quick way to display dates by month Here areexamples:

$ cal Show current month calendar (today is highlighted)

The datecommand can also be used to change the system date and time For example:

$ sudo date 081215212008 Set date/time to Aug 12, 2:21PM, 2008

Ngày đăng: 29/09/2013, 22:20

TỪ KHÓA LIÊN QUAN