服务器安全不容忽视,虽然可以通过种种手段,禁止root直接登录,但是如果不使用ssh密匙,还是会给黑客们有尝试不同用户登录的可能性,所以我们通过密匙文件,来绑定登录,这样没有密匙文件的人就无法登录服务器了,就像现在的网银需要一个文件认证一样。有了文件才能登录,是不是倍感安全呢?本文章由ArthurXF倾情奉献,谢谢大家的支持。

  下面来看看操作过程。
  1.登录服务器,不需要转换到root下。
  2.进入当前用户的.ssh目录,如果没有就mkdir一个,例如:/home/aaa/.ssh/
  3.执行ssh-keygen -t dsa 生成公匙id_dsa.pub和密匙id_dsa两个文件,中间需要输入密码,不输入则认为是不需要密码,只要有密匙文件即可登录的。
  4.将公匙转换一个文件名,这个文件是登录验证的文件,执行命令mv id_dsa.pub authorized_keys
  5.将密匙文件id_dsa的内容保存到本地的一个文件中,例如:aaa.txt
  6.用puttygen.exe载入已经存在的密匙文件aaa.txt,载入成功,后保存新的密匙文件aaa.ppk。
  7.打开putty,选择ssh->认证->认证私钥文件,选择转换的私钥文件路径
  8.打开后输入用户名,会提示输入密码,这个密码是生成key输入的密码,输入密码后,登录成功

  让ssh支持密匙登录还需要修改以下设置:
引用
打开/etc/ssh/sshd_config修改以下内容,使SSH仅支持密钥登陆:

Port XXXXX     #设定端口
Protocol 2    

LoginGraceTime 2m
PermitRootLogin no
MaxAuthTries 3

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile  .ssh/authorized_keys

PasswordAuthentication no
PermitEmptyPasswords no

ChallengeResponseAuthentication no

UseDNS no

保存修改并重启:

/etc/rc.d/sshd restart


现在ArthurXF本人正在搞PHP等技术培训,如果想学习的人可以跟我联系。另外培训的招生简章在这个网址,想了解的可以去看看。加我QQ:29011218交流也可。
PHP培训招生简章
3:哪些活动是占用I/O资源的大户?
4:利用iostat分析I/O的利用率
5:利用SAR命令分析磁盘活动
6:利用SAR命令分析缓冲区的活动
7:利用SAR命令分析交换区的活动
8:利用GlancePlus分析系统的I/O活动情况
9:对I/O需求密集型系统的性能调试


衡量I/O闲忙程度的指标


下面是一些衡量I/O闲忙程度的经用指标:

磁盘利用率(disk utilization)
磁盘队列长度(disk queue length)
磁头/逻辑卷的读/写速率(read/write rates per spindle/logical volume)
原始I/O(raw I/O):主要用于数据库应用
交换队列的长度(swap queue length)
缓存命中率(buffer cache hit ratio)
网络文件系统和无盘工作站速率(NFS and diskless rates(server))

I/O资源成为系统性能的瓶颈的征兆


当I/O成为瓶颈时,会出现下面这些典型的症状:

过高的磁盘利用率(high disk utilization)
太长的磁盘等待队列(large disk queue length)
等待磁盘I/O的时间所占的百分率太高(large percentage of time waiting for disk I/O)
太高的物理I/O速率:large physical I/O rate(not sufficient in itself)
过低的缓存命中率(low buffer cache hit ratio(not sufficient in itself))
太长的运行进程队列,但CPU却空闲(large run queue with idle CPU)

哪些活动是占用I/O资源的大户?


下面是一些占用大量I/O资源的活动:

换页(paging):paging不仅会引起内存问题,还可能引起磁盘问题;
open,creat,and stat system calls:系统调用会引起大量的磁盘I/O;
multiuser I/O and random I/O
relational database
core dumps


利用iostat分析I/O的利用率


iostat - report I/O statistics

iostat iteratively reports I/O statistics for each active disk on the system.

If two or more disks are present, data is presented on successive lines for each disk.

With the advent of new disk technologies, such as data striping, where a single data transfer is spread across several disks, the number of milliseconds per average seek becomes impossible to compute accurately. At best it is only an approximation, varying greatly, based on several dynamic system conditions. For this reason and to maintain backward compatibility, the milliseconds per average seek ( msps ) field is set to the value 1.0.

它的语法为:

iostat [-t] [interval [count]]

其选项的含义为:

-t:Report terminal statistics as well as disk statistics.
interval: Display successive lines which are summaries of the last interval seconds. The first line reported is for the time since a reboot and each subsequent line is for the last interval only.
count: Repeat the statistics count times.
对结果的分析:

通过查看bps列和sps列的值我们可以知道哪些磁盘比较忙,哪些磁盘比较闲。


利用SAR命令分析磁盘活动


通过命令sar -d,我们可以分析系统中的每个磁盘和磁带的活动情况。

Report activity for each block device, e.g., disk or tape drive. One line is printed for each device that had activity during the last interval. If no devices were active, a blank line is printed.Each line contains the following data:

device:设备名;
%busy: Portion of time device was busy servicing a request; statistics.
avque: Average number of requests outstanding for the device;
r+w/s: Number of data transfers per second (read and writes) from and to the device;
blks/s: Number of bytes transferred (in 512-byte units) from and to the device;
avwait: Average time (in milliseconds) that transfer requests waited idly on queue for the device;
avserv: Average time (in milliseconds) to service each transfer request (includes seek, rotational latency, and data transfer times) for the device.
对结果的分析:

如果某个磁盘的%busy列的值大于50%,则说明该磁盘可能存在瓶颈;

如果某个磁盘的avwait珍的值大于avserv列的值,也说明该磁盘可能存在瓶颈;


利用SAR命令分析缓冲区的活动


通过命令sar -b,我们可以分析系统中的缓冲区的活动情况。

Report activity for each block device, e.g., disk or tape drive. One line is printed for each device that had activity during the last interval. If no devices were active, a blank line is printed.Each line contains the following data:

bread/s Number of physical reads per second from the disk (or other block devices) to the buffer cache;
bwrit/s: Number of physical writes per second from the buffer cache to the disk (or other block device);
lread/s: Number of reads per second from buffer cache;
lwrit/s: Number of writes per second to buffer cache;
%rcache: Buffer cache hit ratio for read requests e.g., 1 - bread/lread;
%wcache: Buffer cache hit ratio for write requests e.g., 1 - bwrit/lwrit;
pread/s: Number of reads per second from character device using the physio() (raw I/O) mechanism;
pwrit/s: Number of writes per second to character device using the physio() (i.e., raw I/O ) mechanism; mechanism.
对结果的分析:

如果%rcache列的值小于90%,并且%wcache列的值不在70-70%之间,我们必须观察系统中什么应用在做什么样的读/写操作,我们是否需要增加缓冲欧的大小。


利用SAR命令分析交换区的活动


通过命令sar -w,我们可以分析系统中的交换区的活动情况。

Report activity for each block device, e.g., disk or tape drive. One line is printed for each device that had activity during the last interval. If no devices were active, a blank line is printed.Each line contains the following data:

swpin/s: Number of process swapins per second;
swpot/s: Number of process swapouts per second;
bswin/s: Number of 512-byte units transferred for swapins per second;
bswot/s: Number of 512-byte units transferred for swapouts per second;
pswch/s: Number of process context switches per second.
对结果的分析:

如果swpin/s的值大于零,那么swpot的值必须引起注意;

同时必须注意pswch/s的值,如果很大,说明进程切换频繁。



利用GlancePlus分析系统的I/O活动情况


1)对磁盘的整体使用情况的分析:

进入GlancePlus;
按?键进入联机帮助界面;
按d键进入磁盘的详细界面;
按b键表示向后翻页,按f键表示向前翻页;
通过Disk Detail Screen,我们可以知道所有的逻辑和物理I/O请求的分布情况。

2)对Disk I/O by File System

进入GlancePlus;
按?键进入联机帮助界面;
按i键Disk I/O by File System表界面;
按b键表示向后翻页,按f键表示向前翻页;
这个界面很有用,因为它按不同的文件显示I/O的繁忙程度。

3)对逻辑卷的分析

进入GlancePlus;
按?键进入联机帮助界面;
按v键I/O by logical volumes表界面;
按b键表示向后翻页,按f键表示向前翻页;
通过把对文件系统的分析和逻辑卷的分析结合起来,能够知道哪个磁盘的I/O最忙。

4)对磁盘的分析

5)对交换区的分析


GlancePlus


GlancePlus是HP公司的性能分析工具,它是一个联机性能分析和诊断工具,用于监控正在发生的系统活动情况。它的特点是:

联机性能分析和诊断;
监控系统资源的使用情况;
多屏幕输出;
带有联机帮助;
进程阀值识别和报警,这个阀值可以基于被监控的进程、资源的利用率、用户、进程名或者终端名;
和前面介绍的其他性能分析工具,如:SAR,iostat,vmstat等相比,GlancePlus有一最大好处,这就是:GlancePlus不仅能对系统的整体情况进行分析,而且还可以对某个进程进行深入分析。

GlancePlus所有监控的系统资源主要有以下这些,应该说,它们已经包括了主要的系统资源。

CPU、磁盘、内存和交换区的整体使用情况;
全局进程的活动情况;
CPU的详细使用情况;
内存的详细使用情况;
文件系统、设备和逻辑卷的磁盘输入/输出;
磁盘队列和长度;
交换区的详细使用情况;
系统表的使用情况;
单个进程的活动情况;
GlancePlus的运行形式有两种:

图形方式:#/opt/perf/bin/gpm,或以后台方式运行#gpm&;不过,我们必须先设置环境变量DISPLAY,因为它是图形输出的目的地,比如:export DISPLAY=主机名(IP地址):0.0;如果是工作站telnet别的主机上,则本工作站还要运行#xhost +,表示别的机器上输出可以到本工作站;
终端方式:#/opt/perf/bin/glance
GlancePlus的界面主要有两部分,上面部分是系统资源的整体使用情况,下面部分则按不同的资源的详细使用情况。

GlancePlus的界面的上面部分的详细说明,其中字母表示谁用了资源,而字母之间的长度资源占用率。

CPU的利用率:S-系统CPU利用率,U-用户CPU利用率,R-实时CPU利用率,N-nice CPU利用率,A-negative-nice CPU利用率;
磁盘的利用率:F-文件系统I/O,V-虚拟内存I/O
内存的利用率:S-用于系统代码和数据的内存,U-用于用户代码和数据的内存,B-用作缓存的内存
交换区的利用率:U-正在使用的交换区,R-预留的交换区
前面讲到,GlancePlus是一个联机分析诊断工具,其实,GlancePlus还可以与MeasureWare Agent一起使用,MeasureWare Agent将可以 在一段时间内帮助GlancePlus收集基本数据,然后由GlancePlus进行分析。

Glance可以运行在HP-UX,Solaris和AIX上。

与其他的工具相比,Glance占用的系统资源会更多一些。

通常情况下,启动工具的命令的语法如下:

#glance [-j interval][-p dest][-f dest][maxpagers n][-command][-nice n][-nosort][lock][]

其中,各选项的含义为:

-j :指定数据刷新的时间间隔;
-p :使得结果送到打印机队列;
-f :enbles printing to a file;
-maxpages :limits the number of pages to be printed;
-command:selects the inital metric screen;
-nice :nice value at which glance runs;
-nosort:don't sort processes on the screen;
-lock:locks glance into memory


现在ArthurXF本人正在搞PHP等技术培训,如果想学习的人可以跟我联系。另外培训的招生简章在这个网址,想了解的可以去看看。加我QQ:29011218交流也可。
PHP培训招生简章
4:什么地方/哪些进程是占用内存资源的大户?
5:利用vmstat命令分析内存的利用率
6:利用ipcs分析消息队列、共享内存和信号量
7:利用GlancePlus分析系统内存资源利用率
8:对内存需求密集型系统的性能调试


内存管理


1)内存管理的主要工作:

跟踪内存的使用和可用内存的情况;
为进程分配内存;
管理磁盘与物理内存之间的换页(paging);
2)什么是虚拟内存(virtual memory)?

virtual memory uses a disk as an extension of RAM so that the effective size of usable memory grows correspondingly. The kernel will write the contents of a currently unused block of memory to the hard disk so that the memory can be used for another purpose. When the original contents are needed again, they are read back into memory. This is all made completely transparent to the user; programs only see the larger amount of memory available and don't notice that parts of them reside on the disk from time to time. Of course, reading and writing the hard disk is slower (on the order of a thousand times slower) than using real memory, so the programs don't run as fast. The part of the hard disk that is used as virtual memory is called the swap space.

物理内存(physical memory)
swap space:
3)paging

A technique by which the contents of a virtual memory address(called pages) are moved from virtual memory(the disk)into and out of the main memory where it is accessed by the CPU.

这个机制是由一个叫vhand的进程来完成。

当可用内存的数量小于LOTSFREE时,例程pageout将被调用来选择什么内存可以释放。它采用two-handed clock algorithm,the reference hand turn off the reference bit of each memory page it references.If the refernce bit is still zero when the second hand gets to it,the page is freed.If the page is clean(unmodified),it is added to the freelist.If the page is dirty,it must be posted to the swap device before being put on the freelist.

lotsfree: based on physical memory (64 MB -> 863) upper bound where paging starts/stops
desfree: based on physical memory (64 MB -> 215)lower bound where paging starts/stops
minfree: based on physical memory (64 MB -> 53) threshold where deactivation starts
4)Page fault

An invalid address error(trap)that occurs when the CPU requests a page from memory that has not been paged in from the disk(virtual memory)to the main memory. The page may also have been paged out from the main memory prior the request.

5)Process Deactivaton

Deactivating a process so its memory pages will be flagged free or paged out rapidly by vhand.

6)Thrashing

A situation in which a process is spending more time paging than processing;a high number of page faults is occuring.

7)Buffer Cache

它是内存的一部分,用于加快文件存取时间;
缓存的大小可以随可用内存动态变化,但也可以通过修改内核参数而改成固定的大小;
缓存可以提高磁盘的读/写性能;
在缓存的内容可以通过sync进程来强制写入磁盘;
从缓存的读和写又称为逻辑读和逻辑写;
8)内存需求

按用途来分,内存可以分成两部分:预留内存和动态内存。

预留内存主要用于存放:

system table
data structures
buffer cache
其中系统表和数据结构占用的数量一般很小,但缓存则可能占到很大一部分。

动态内存主要用于存放:

process text
data stack
share memory segments
其中各进程锁定的内存会影响动态内存的大小。


衡量内存闲忙程度的指标


1)整体内存忙闲指标:

buffer cache size: 缓存区在内存开销中占很大比例;
page in/out rates;
swap in/out rates;
可用内存的大小,或用得到内存的大小(available memory size):
自由内存的大小(free memory size): what is currently available,it should not be confuzed with available memory,which does not change during normal sytem operation;
swap queue length;
2)单个进程的内存衡量指标:

一个进程占用物理内存的大小(resident set size)
一个进程占用虚拟内存的大小(virtual set size)
VM reads and writes: it can show how many physical memory management reads and writes were made to and from the disk during the chosen interval.

内存资源成为系统性能的瓶颈的征兆


当内存资源成为系统性能的瓶颈时,它有一些典型的症状:

很高的换页率(high pageout rate):HP-UX是一个按需调页的操作系统,通常情况下,它只执行调入页面进入内存的操作,以让进程能够运行。只有操作系统觉得系统需要释放一些内存空间时,才会执行从内存调出页面的操作,而过高的调出页面操作说明内存缺乏;
进程进入不活动状态(process deactivation activity):当自由的内存页面数量小于MINFREE时,很多进程将强制进入不活动状态,因为,any deactivation activity represents a condition in which normal paging is inadequate to handle the memory demands.
自由内存的数量很小,但活动的虚拟内存却很大(very small free memory and large active virtual memory)
交换区所有磁盘的活动次数可高(high disk activity on swap devices)
可高的全局系统CPU利用率(high global system CPU utilization):
很长的运行进程队列,但CPU的空闲时间却很多(large run queue with idle CPU)
内存不够出错(out of memory errors)
CPU用于vhand和swapper两中守护进程的时间(CPU time to vhand and swapper)
必须注意的是,有时候我们发现CPU很忙,这似乎是CPU资源成为系统性能的瓶颈,但如果进一步分析,发现vhand和swapper守护进程占用了大量的系统CPU时间,很显然,这时系统性能瓶颈真正所在可能是内存。


什么地方/哪些进程是占用内存资源的大户?


下面是一些典型的占用内存资源的大户:

buffer cache
plocked process:plocked processes are those that are locked in memory and are not elogible to be paged.通常,这些进程都是一些比较重要的进程,不便调出内存。
档案库(archive libraries):把库放入内存可以加快程序的执行,但它们将占用大量的内存;
共享内存(shared memory)
关系型数据库(relational databases)
X-终端和X-服务器进程(X-terminals and X-servers):通常,一个X-终端需要额外的2-4兆内存;一个X-服务器需要400KB以上的内存;

利用vmstat命令分析内存的利用率


vmstat-report virtual memory statistics

The vmstat command reports certain statistics kept about process, virtual memory, trap, and CPU activity. It also can clear the accumulators in the kernel sum structure.

它的语法:

vmstat [-dnS] [interval [count]]

vmstat -f | -s | -z

它的选项的说明:

-d: Report disk transfer information as a separate section, in the form of transfers per second.
-n: Provide an output format that is more easily viewed on an 80-column display device. This format separates the default output into two groups: virtual memory information and CPU data. Each group is displayed as a separate line of output. On multiprocessor systems, this display format also provides CPU utilization on a per CPU basis.
-S: Report the number of processes swapped in and out (si and so) instead of page reclaims and address translation faults (re and at)
interval: Display successive lines which are summaries over the last interval seconds. If interval is zero, the output is displayed once only. If the -d option is specified, the column headers are repeated. If -d is omitted, the column headers are not repeated.
count: Repeat the summary statistics count times. If count is omitted or zero, the output is repeated until an interrupt or quit signal is received.
-f: Report on the number of forks and the number of pages of virtual memory involved since boot-up.
-s: Print the total number of several kinds of paging- related events from the kernel sum structure that have occurred since boot-up or since vmstat was last executed with the -z option.
-z: Clear all accumulators in the kernel sum structure. This option is restricted to the super user.
对结果的说明:

在不带参数的vmstat的命令时,我们首先要关注的是avm(active virtual memory)列和free(free list zise)列的值。如果avm的值很大,而free的值却很小,这时,系统可能有内存瓶颈,我们 必须用带-S选项的vmstat命令查看系统是否有deactivation/reactivation活动正在发生。


利用ipcs分析消息队列、共享内存和信号量


ipcs - report status of interprocess communication facilities

ipcs displays certain information about active interprocess communication facilities. With no options, ipcs displays information in short format for the message queues, shared memory segments, and semaphores that are currently active in the system.

它的语法:

ipcs [-mqs] [-abcopt] [-C core] [-N namelist]


利用GlancePlus分析系统内存资源利用率


利用HP的GlancePlus可以对内存使用情况进行分析:

进入GlancePlus;
按?键进入联机帮助界面;
按m键进入内存的详细界面;
通过memory Detail Screen,我们可以知道内存的分布情况,物理内存多少、缓存多少、用户用了多少,系统用了多少、以及换页情况等。


对内存需求密集型系统的性能调试


1)基于硬件的方法:

增加物理内存
使用无盘工作站替代X-terminal
2)基于软件的方法:

减小内核参数maxdsiz的值;
减少内存锁定的使用;
杀死不必要的进程;
识别出需要大量内存的进程;
重新设计应用;
减小内核的大小;
减小系统表的大小;
减小缓存区的大小;
3)利用serialize命令来调度大进程的系统资源需求

/usr/bin/serialize applicaiton
/usr/bin/serialize -p pid

现在ArthurXF本人正在搞PHP等技术培训,如果想学习的人可以跟我联系。另外培训的招生简章在这个网址,想了解的可以去看看。加我QQ:29011218交流也可。
PHP培训招生简章
5:哪些进程是占用CPU资源的大户?
6:利用SAR工具分析CPU的利用率
7:利用SAR工具分析运行进程队列长度
8:利用SAR工具分析系统调用
9:利用time命令测试某个命令和程序的执行效率
10:利用top命令查看最耗CPU资源的进程
11:利用uptime命令查看系统整体情况
12:利用GlancePlus分析系统CPU资源利用率
13:对CPU需求密集型系统的性能调试


CPU的体系结构和工作原理


我们所说的CPU一般是指微处理器,即Microprocessor,一般地,一个CPU的主要组成部分为:

CPU(central processing unit)
cache:cache就是高速内存,它的存取时间一般是10-20微秒(ns),这样,CPU可以在一个时钟周期内访问一次cache;而一般的内存的存取时间为80-90微秒(ns),它的大小对CPU的性能有很大的影响。
TLB(translation lookaside boffer):TLB是高速cache,它用于存放最近访问的虚拟地址和与其对应的物理地址对,这样TLB将可以把虚拟地址转换为物理地址。TLB是内存中系统转换表的一个子集;TLB通常是指向一个内存页面,而不是一个内存地址;它的大小对CPU的性能有很大的影响。
coprocessor
不同的CPU,一般有不同的时钟频率和高速缓存容量。

CPU在一次时钟周期内一般可以从高速缓存内取到一个指令并执行它。因此,从理论上说,只要CPU的主频越快,单位时间内所能执行的指令则越多。目前,有些CPU可以在一个时钟周期内执行多条指令,如PA8500可以执行4条指令。
高速缓存的大小会制约CPU的执行效率,虽然CPU主频很快,但它取不到数据,则只有空运行。因此,高速缓存的大小很重要;高速缓存又分数据高速缓存和指令高速缓存,分别存放从内存预先取来的即将执行的数据和指令单元。
虚拟寻址

一般,系统中的虚拟地址空间要比物理地址空间大得多,例如,如果系统是64位的,则理论上,它的寻址空间可以达到2的64次幂(2**64=18,447PB),但由于受费用的因素的影响,实际上的物理内存最大只有十几GB的内存。

每个进程都有自己的唯一虚拟地址空间,然而,进程的运行必须把虚拟地址映射到物理地址,这需要TLB、高速缓存和内存三者的配合。如果需要的信息不在内存,则导致一个页面错。

流水线(Pipelining)

TLB和高速缓存试图在一个时钟周期内给CPU提供它所需的信息,然而,这个过程是100%的利用率,对CPU来说,它必须先用一个时钟周期去取下一个指令,再一个时钟周期去执行这条指令,这样,CPU的利用率也只有50%。为了让CPU更忙,通常的做法是,采用流水线的方法。如PA8500是采用7个步骤的流水线。

操作系统和进程


HP-UX一个多用户、多任务的UNIX操作系统。它的性能依赖于用户数的多少、用户任务的类型、硬/软件件的配置。

HP-UX有两种运行级别:

用户级:系统用户可以与操作系统进行交互操作,如运行应用和系统命令。用户级通过系统调用接口访问内核级。
内核级:操作系统自动运行一些功能,它们主要对硬件进行操作。
在操作系统中,用户程序是以进程方式运行。进程的状态有以下几种:

SRUN
SSLEEP
SZOMB
SIDL
SSTOP
CPU的调度

一旦进程所需的数据调入内存后,它将等待CPU调度者来分配CPU时间。一般,在HP-UX中,每个进程都可以得一个 固定的时间片来运行,这个时间片的长度为十分之一秒(1/10秒)。

由于HP-UX是一个多任务的操作系统,它需要一种手段来进程的执行次序,这就是中断。在系统中,时钟中断处理器是用来处理时钟中断的系统软件。具体地说, 它将收集系统和accounting statistics and does context switching.系统性能也与这种中断发生的频率有关。

进程何优先级

每个进程都有自己的优先级;
实时优先级:-32~127,一个进程如果想以实时优先级运行,则必须用命令#rtprio来设置;
分时系统优先级:128~177;
分时用户优先级:178~251;
优先级:252~255 are used by the system as virtual memory management priorities for process deactivation.
分时进程在初始优先级是由系统分配的,为一个定值。用户可以通过改变进程的nice值来改变分时进程的优先级。因为进程会随着它的执行,将以nice值来降低它的优先级,当 它在等待执行时,又将以nice值来增加其优先级。nice值的系统缺值为20。
在系统性能分析过程中,我关心不仅仅在完成一个进程耗时多少,还包括时间花在哪以及它的时间多少。


衡量CPU闲忙程度的指标


要分析系统的CPU资源是否够的前提谁占用了CPU资源,占用了多少,时间多长。 下面是一些衡量CPU闲忙程度的经用指标:

1)用户使用CPU的情况

CPU运行常规用户进程
CPU运行niced process
CPU运行实时进程
2)系统使用CPU的情况

用于系统调用
用于I/O管理:中断和驱动
用于内存管理:paging and swapping
用于进程管理:context switch and process start
3)WIO:由于进程等待I/O而使CPU处于空闲状态的比率,这些I/O主要指block I/O,raw I/O,VM paging/swapins;

4)CPU的空闲率,即除了上面的WIO以外的空闲情况;

5)CPU用于上下文交换的比率(Context Switch CPU utilization)

6)nice

7)real-time

8)运行进程队列的长度,即处于可运行状态的进程个数的大小,不过我们关心的是这些在等待CPU调度执行时所花的时间;

9)平均负载(load average)


CPU资源成为系统性能的瓶颈的征兆


CPU就像人的大脑,完成各种交给它的任务。如果任务太多,CPU就要忙不过来,它的运行效率就要下降。就像人生病会有一典型症状一样,当CPU资源 成为系统性能的瓶颈时,它也有一些典型的症状:

很慢的响应时间(slow response time)
CPU空闲时间为零(zero percent idle CPU)
过高的用户占用CPU时间(high percent user CPU)
过高的系统占用CPU时间(high percent system CPU)
长时间的有很长的运行进程队列(large run queue size sustained over time)
processes blocked on prority
必须注意的是,如果系统出现上面的这些症状并不能说一定是由于CPU资源不够,事实,有些症状 的出现很可能是由于其他资源的不足而引起,如内存不够时,CPU会忙内存管理的事,这时从表面上, CPU的利用是100%,甚至显得不够,如果据此就简单地认为增加CPU就可以解决问题是大错特错了。

因此,还是那句话,必须用不同的工具、从不同的方面对系统进行分析后,才能做出结论,即使这样,经验将 起到不可替代的作用。


哪些进程是占用CPU资源的大户?


在操作系统中,并不是所有的进程都以同样的方式使用CPU资源。通常情况下,有些进程需要比其他进程更多的CPU时间片才能顺利地完成任务。 下面是一些典型的占用CPU资源的大户:

进程创建(process creation)
终端字符进程(teminal character processes(MUX- and LAN-based)
计算密集型进程和实时进程
X-终端和X-服务器进程(X-terminals and X-servers)


利用SAR工具分析CPU的利用率


利用SAR进行CPU的利用率分析的命令形式:

#sar -u,这时数据是通过sa1在后台定时生成;
#sar -u 5 100,每隔5秒取样一次,共取100次;
SAR -u:Report CPU utilization (the default); portion of time running in one of several modes. On a multi-processor system, if the -M option is used together with the -u option, per-CPU utilization as well as the average CPU utilization of all the processors are reported. If the -M option is not used, only the average CPU utilization of all the processors is reported:

cpu: cpu number (only on a multi-processor system with the -M option);
%usr: user mode;
%sys: system mode;
%wio: idle with some process waiting for I/O (only block I/O, raw I/O, or VM pageins/swapins indicated);
%idle: otherwise idle;
对结果的分析

首先,我们看%idle列的值,如果为接近零,则再看对应%wio列的值,如果这列的大于7,则表明系统的磁盘或其他I/O可能有问题,需要进一步的分析:

用iostat命令分析各个磁盘的传输闲忙状况,如#iostat -t 5 2,每隔5秒取样一次,共取2次;
用sar -d命令分析各块设备(磁盘、磁带)活动情况;
用sar -b命令分析系统的缓存的活动情况;
用sar -w命令分析进程的deactivation/reactivation and switching activities of the system;
如果%idle列很小,而对应的%wio列的值也很小,这时,我们查看%usr列和%sys列的值。如果%usr列的值很大,说明有用户进程占用很多CPU时间;如果%sys列的值很大,则说明系统管理方面花了很多时间。需要进一步的分析:

用GlancePlus对占用CPU时间最大的进程进行单独分析,为什么它会占用如此多的CPU时间。
如果%sys列的值很大,可以用SAR -C命令对系统调用进行进一步分解,看这些系统调用主要是做些什么。同时,还必须分析是否有其他瓶颈,如paging也会引起%sys的值很大,这时,可以用sar -q查看系统的运行进程队列长度,也可以用GlancePlus和vmstat查看内存的使用情况;

利用SAR工具分析运行进程队列长度


利用SAR进行运行进程队列长度分析的命令形式:

#sar -q,这时数据是通过sa1在后台定时生成;
#sar -q 5 100,每隔5秒取样一次,共取100次;
SAR -q: Report average queue length while occupied, and percent of time occupied. On a multi-processor machine, if the -M option is used together with the -q option, the per-CPU run queue as well as the average run queue of all the processors are reported. If the -M option is not used, only the average run queue information of all the processors is reported:

cpu: cpu number (only on a multi-processor system with the -M option);
runq-sz: Average length of the run queue(s) of processes (in memory and runnable);
%runocc: The percentage of time the run queue(s) were occupied by processes (in memory and runnable);
swpq-sz: Average length of the swap queue of runnable processes (processes swapped out but ready to run);
%swpocc: The percentage of time the swap queue of runnable processes (processes swapped out but ready to run) was occupied.
对结果的分析:

这些数据越小越好。

如果runq-sz大于4,或者%swapocc大于5时,则表明系统的CPU或内存可能有问题,需要进一步的分析:

用sar -u命令分析CPU的使用情况;
用sar -w命令分析进程的deactivation/reactivation and switching activities of the system;
也可以用GlancePlus;

利用SAR工具分析系统调用


利用SAR进行系统调用分析的命令形式:

#sar -c,这时数据是通过sa1在后台定时生成;
#sar -c 5 100,每隔5秒取样一次,共取100次;
SAR -c: Report system calls:

scall/s: Number of system calls of all types per second;
sread/s: Number of read() and/or readv() system calls per second;
swrit/s: Number of write() and/or writev() system calls per second;
swpq-sz: Average length of the swap queue of runnable processes (processes swapped out but ready to run);
fork/s: Number of fork() and/or vfork() system calls per second;
exec/s: Number of exec() system calls per second;
rchar/s: Number of characters transferred by read system calls block devices only) per second;
wchar/s: Number of characters transferred by write system calls (block devices only) per second.
对结果的分析:

如果scall/s列的值很大,那么这么多的系统调用的原因就必须仔细分析了。

我们可以查看fork/s和exec/s列的值,看看系统是否在创建大量新的进程。



利用time命令测试某个命令和程序的执行效率


我们可以利用time命令来测试一个命令的执行效率,语法为:

time command

command is executed. Upon completion, time prints the elapsed time during the command, the time spent in the system, and the time spent executing the command. Times are reported in seconds.

Execution time can depend on the performance of the memory in which the program is running.

当我们觉得某个进程的性能不好时,最简单的方法就是利用time命令来查看一下进程执行时它的 时间分布情况,然后再用其他工具进一步分析。


利用top命令查看最耗CPU资源的进程


我们可以利用top命令来查看最耗CPU资源的进程。top命令还会根据进程占用CPU资源的多少而动态改变。

它的语法为:

top [-s time] [-d count] [-q] [-u] [-h] [-n number]

其中各选项的含义为:

-s time: 屏幕刷新的时间间隔time,缺省为5秒;
-d count: 屏幕刷新count次后,top命令自己也退出;
-q: This option runs the top program at the same priority as if it is executed via a nice -20 command so that it will execute faster (see nice(1)). This can be very useful in discovering any system problem when the system is very sluggish. This option is accessibly only to users who have appropriate privileges.
-u: User ID (uid) numbers are displayed instead of usernames. This improves execution speed by eliminating the additional time required to map uid numbers to user names.
-h: Hides the individual CPU state information for systems having multiple processors. Only the average CPU status will be displayed.
-n number: Show only number processes per screen. Note that this option is ignored if number is greater than the maximum number of processes that can be displayed per screen.
在top命令运行时,我们可用以下几个快捷键来翻屏:

j: 向前翻;
k: 向后翻;
t: 回到第一页;
对结果的分析:

通过top命令,我们可以快速了解到目前系统的CPU资源使用情况,尤其是占用CPU资源最多的进程是我们必须关注的对象。

我们通过RES(the current size of the process resident in memory)列可以知道每个进程占用内存的数量。

我们通过NICE列可以知道系统是否使用NICE值来调节该进程的工作负载平衡。

利用uptime命令查看系统整体情况


uptime prints the current time, the length of time the system has been up, the number of users logged on to the system, and the average number of jobs in the run queue over the last 1, 5, and 15 minutes.

w is linked to uptime and prints the same output as uptime -w, displaying a summary of the current activity on the system.

它的语法为:

uptime [-hlsuw] [user]

w [-hlsuw] [user]

其中各选项的含义为:

-h: Suppress the first line and the heading line. This option should not be used with the -u option. This option assumes the use of the -w option to uptime.
-l: Use long output. This option assumes the use of the -w option to uptime.
-s: Use the short form of output for displaying terminal information. The terminal name is abbreviated; the login time and CPU times are suppressed.
-u: Print only the first line describing the overall state of the system. This is the default for the uptime command.ormation for systems having multiple processors. Only the average CPU status will be displayed.
-w: Print a summary of the current activity on the system for each user. This is the default for the w command.

利用GlancePlus分析系统CPU资源利用率


利用HP的GlancePlus工具可以对进程的整体情况和单独的某个进程都详细分析。

1)对CPU的整体使用情况的分析:

进入GlancePlus;
按?键进入联机帮助界面;
按c键进入CPU的详细界面;
按b键表示向后翻页,按f键表示向前翻页;
通过CPU Detail Screen,我们可以知道CPU时间的分布情况,用户用了多少,系统用了多少等。

2)对单个进程的CPU资源占用情况分析:

进入GlancePlus;
按?键进入联机帮助界面;
按g键进入进程列表界面;
按s键进入进程选择界面,通常最忙的进程会作为缺省进程;
输入想查看的进程号;
按b键表示向后翻页,按f键表示向前翻页;
在对单个进程的分析中,我们通常要关注以下几个值:

CPU Usage;
User CPU;
System CPU;
Priority;
Logical and Physical Reads and Writes;
Total RSS/VSS;
blocked on(通过按shift+>来得到);

对CPU需求密集型系统的性能调试


1)基于硬件的方法:

升级到更快的CPU;
升级到更大的高速缓存;
增加CPU个数;
把应用分布到多个系统中;
使用无盘结点;
增加浮点处理器;
2)基于软件的方法:

在不是高峰时间运行批处理;
Nice umimportant application;
使用rtpio命令来帮助重要的应用;
使用plock命令来帮助重要的应用;
Turn off system accounting;
Consider using Taskbroker or DCE;
优化应用;
考虑使用进程资源管理器(Process Resource Manager),不过PRM只有在HP-UX平台上有。

现在ArthurXF本人正在搞PHP等技术培训,如果想学习的人可以跟我联系。另外培训的招生简章在这个网址,想了解的可以去看看。加我QQ:29011218交流也可。
PHP培训招生简章
2:如何知道系统安装了哪些性能分析工具?
3:SAR
4:time和timex
5:iostat
6:vmstat
7:GlancePlus


性能分析工具综述


目前,对系统进行性能调试的工具有很多,这些可以两大类:一类是标准的分析工具,即所有的UNIX都会带的分析工具; 另一类是不同厂商的UNIX所特有的性能分析工具,比如HP-UX就有自己的增值性能分析工具。

标准的分析工具,即所有的UNIX都会带的分析工具:

sar
iostat
vmstat
time
ps
bdf
top
ipcs
uptime
HP-UX自己的增值性能分析工具:

glance/gpm
puma
xps
按性能分析工具的用途来说,它可以分为:

CPU的使用情况:sar,time,top,ps,puma,xps
内存的使用情况:vmstat,ipcs
文件系统状态:dbf,iostat,sar,swapinfo,nfsstat
I/O子系统状态:iostat
网络性能:netstat
按分析时间点来分,性能分析工具可以分为:

日志型分析工具,如puma,xps,它们将在对系统在某一段时间的运行情况进行取样,然后得出某此结果。这种分析工具非常适合于 针对那些时断时续发生的问题进行分析,因为通过取样,可以得出结论;
实时型分析工具,如glance,它们又称“快照型分析工具”,即它们将实时取出系统运行环境的数据,这种分析工具非常适合于 那些在分析工具正在运行时发生的问题,或一直发生的问题。

如何知道系统安装了哪些性能分析工具?


想知道系统中安装了何种性能分析工具,运行命令:

#/opt/perf/perfstat -v

SAR-System Activity Reporter


顾名思义,SAR工具将对系统的当前状态进行取样,然后通过计算数据和比例来表达系统的当前运行状态。它的特点是:

System V-based,在很多的UNIX版本中都存在;
可以连续对系统取样,获得大量的取样数据;
取样数据和分析的结果都可以入文件;
自己所需的负载很小
SAR也有一个致命的不足,这就是:SAR不能对某个进程进行深入分析,它仅是一对系统的整体情况进行分析。

SAR的语法为:

#sar [-ubdycwaqvmAMS] [-o file] t [n]:每隔t秒取样一次,共取样n次,其中-o file表示取样结果将以二进制形式存入文件file中;
#sar [-ubdycwaqvmAMS] [-s time] [-e time] [-i sec] [-f file]:表示从file文件中取出数据,如果没有指定-f file,则从标准数据文件/var/adm/sa/sadd取数据,其中dd表示当前日子。另外, -s time表示起始时间;-e time表示停止时间;-i sec表示取样的时间间隔,如果不指定则表示取文件中所有的数据。
对[-ubdycwaqvmAMS]的解释请参阅相关的系统分析领域,如-u表示对CPU的分析;-A表示汇总所有数据,即等于-ubdycwaqvm,-M表示对多个CPU环境中每个CPU都进行各自的分析, 一般它与-q和-u联合使用,以对每个CPU的使用情况进行分析
从前面的命令语法中可知,sar可以从一个文件中读取性能数据,这个文件可以通过sar来产生(如sar -o file 2 3),也可以通过其他的命令来产生,具体地说,可以通过以下几个命令来产生:

#/usr/lbin/sa/sadc [t n] [ofile]:以t秒为时间间隔,共取样n次,所有数据存入文件ofile。如果我们要对系统进行分析,则通常的做法是:第一步,mkdir /var/adm/sa;第二步,chown adm:adm /var/adm/sa; 第三步,/usr/lbin/sa/sadc /var/adm/sa/sa`date +%d`,它会在/var/adm/sa目录下创建一个sadd文件,其中dd表示当前日期,这个命令将对以前取样进行清零。
运行shell命令:#/usr/lbin/sa/sa1 [t n]:表示以t秒为时间间隔,共取样n次,所有数据存入文件/var/adm/sa目录下的sadd文件,其中dd表示当前日期;
运行shell命令:#/usr/lbin/sa/sa2 [-ubdycwaqvmA] [-s time] [-e time] [i sec]:
一个通过crontab命令来设置定时执行的方案:

0 * * * 0,6 su adm -c "/usr/lbin/sa/sa1":表示以adm用户执行sa1,每个星期的日、六两天是每小时执行一次sa1命令;

0 8-17 * * 1-5 su adm -c "/usr/lbin/sa/sa1 3 3"

0 8-17 * * 1-5 su adm -c "/usr/lbin/sa/sa1"


在通过sadc或sa1、sa2等后台命令收集数据时,我们必须注意如下两点:

/usr文件系统的空闲空间够不够
本月数据将覆盖上月所取样的数据,因此,在系统即将进入下一个月时,必须对本月数据进行备份;
sa1和sa2是两个shell命令,在目录/usr/lbin/sa下,我们可以修改这两shell文件,以改变缺省的取样方法。


time和timex


TIME

Time命令可以汇报出执行某条命令所花的时间。它的输出包括:实际时间,用户时间和系统时间。

它的语法:#time ls

TIMEX

和time一样,timex可以汇报出执行某条命令所花的时间,不过它可以给出更多的信息。

选项-s:prints sar data for the interval in which the program ran.

option -p: List process accounting records for command and all its children.This option works only if the process accounting software is installed and /usr/lib/acct/turnacct has been invoked to create /var/adm/pacct

iostat-I/O statistics Reporter


iostat(输入/输出统计)工具将对系统的磁盘操作活动进行监视,它的输出结果会比sar -d的输出结果更有用。它的特点是:

Berkeley-based,在很多的UNIX版本中都存在;
汇报磁盘活动统计情况,同时也会汇报出终端和CPU使用情况;
looks at information on a per-spindle basis
和SAR一样,它也有一个致命的不足,这就是:iostat不能对某个进程进行深入分析,它仅是一对系统的整体情况进行分析。

iostat的语法为:

iostat [-t] [interval [count]]
interval: Display successive lines which are summaries of the last interval seconds. The first line reported is for the time since a reboot and each subsequent line is for the last interval only.
count: Repeat the statistics count times.
-t: Report terminal statistics as well as disk statistics.


vmstat-Virtual Meomory Statistics


vmstat工具将对系统的虚拟内存、进程、CPU活动进行监视,同时它也对磁盘和forks和vforks操作的个数进行汇总。它的特点是:

Berkeley-based,在很多的UNIX版本中都存在;
汇报虚拟内存、进程、CPU活动统计情况,同时也会汇报出磁盘活动情况和forks和vforks操作的个数;
和iostat一样,它也有一个致命的不足,这就是:vmstat不能对某个进程进行深入分析,它仅是一对系统的整体情况进行分析。

vmstat的语法为:

vmstat [-dnS] [interval [count]]
vmstat -f | -s | -z

GlancePlus


GlancePlus是HP公司的性能分析工具,它是一个联机性能分析和诊断工具,用于监控正在发生的系统活动情况。它的特点是:

联机性能分析和诊断;
监控系统资源的使用情况;
多屏幕输出;
带有联机帮助;
进程阀值识别和报警,这个阀值可以基于被监控的进程、资源的利用率、用户、进程名或者终端名;
和前面介绍的其他性能分析工具,如:SAR,iostat,vmstat等相比,GlancePlus有一最大好处,这就是:GlancePlus不仅能对系统的整体情况进行分析,而且还可以对某个进程进行深入分析。

GlancePlus所有监控的系统资源主要有以下这些,应该说,它们已经包括了主要的系统资源。

CPU、磁盘、内存和交换区的整体使用情况;
全局进程的活动情况;
CPU的详细使用情况;
内存的详细使用情况;
文件系统、设备和逻辑卷的磁盘输入/输出;
磁盘队列和长度;
交换区的详细使用情况;
系统表的使用情况;
单个进程的活动情况;
GlancePlus的运行形式有两种:

图形方式:#/opt/perf/bin/gpm,或以后台方式运行#gpm&;不过,我们必须先设置环境变量DISPLAY,因为它是图形输出的目的地,比如:export DISPLAY=主机名(IP地址):0.0;如果是工作站telnet别的主机上,则本工作站还要运行#xhost +,表示别的机器上输出可以到本工作站;
终端方式:#/opt/perf/bin/glance
GlancePlus的界面主要有两部分,上面部分是系统资源的整体使用情况,下面部分则按不同的资源的详细使用情况。

GlancePlus的界面的上面部分的详细说明,其中字母表示谁用了资源,而字母之间的长度资源占用率。

CPU的利用率:S-系统CPU利用率,U-用户CPU利用率,R-实时CPU利用率,N-nice CPU利用率,A-negative-nice CPU利用率;
磁盘的利用率:F-文件系统I/O,V-虚拟内存I/O
内存的利用率:S-用于系统代码和数据的内存,U-用于用户代码和数据的内存,B-用作缓存的内存
交换区的利用率:U-正在使用的交换区,R-预留的交换区
前面讲到,GlancePlus是一个联机分析诊断工具,其实,GlancePlus还可以与MeasureWare Agent一起使用,MeasureWare Agent将可以 在一段时间内帮助GlancePlus收集基本数据,然后由GlancePlus进行分析。

Glance可以运行在HP-UX,Solaris和AIX上。

与其他的工具相比,Glance占用的系统资源会更多一些。

通常情况下,启动工具的命令的语法如下:

#glance [-j interval][-p dest][-f dest][maxpagers n][-command][-nice n][-nosort][lock][]

其中,各选项的含义为:

-j :指定数据刷新的时间间隔;
-p :使得结果送到打印机队列;
-f :enbles printing to a file;
-maxpages :limits the number of pages to be printed;
-command:selects the inital metric screen;
-nice :nice value at which glance runs;
-nosort:don't sort processes on the screen;
-lock:locks glance into memory

现在ArthurXF本人正在搞PHP等技术培训,如果想学习的人可以跟我联系。另外培训的招生简章在这个网址,想了解的可以去看看。加我QQ:29011218交流也可。
PHP培训招生简章
分页: 11/20 第一页 上页 6 7 8 9 10 11 12 13 14 15 下页 最后页 [ 显示模式: 摘要 | 列表 ]