性能調試---(四)內存性能分析

性能調試---()內存性能分析

 

作者:性能調試---()內存性能分析

1:內存管理

2:衡量內存閒忙程度的指標

3:內存資源成爲系統性能的瓶頸的徵兆

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用於vhandswapper兩中守護進程的時間(CPU time to vhand and swapper)

必須注意的是,有時候我們發現CPU很忙,這似乎是CPU資源成爲系統性能的瓶頸,但如果進一步分析,發現vhandswapper守護進程佔用了大量的系統CPU時間,很顯然,這時系統性能瓶頸真正所在可能是內存。

 

 

什麼地方/哪些進程是佔用內存資源的大戶?

 

 

下面是一些典型的佔用內存資源的大戶:

 

buffer cache

plocked processplocked 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分析系統內存資源利用率

 

 

利用HPGlancePlus可以對內存使用情況進行分析:

 

進入GlancePlus

?鍵進入聯機幫助界面;

m鍵進入內存的詳細界面;

通過memory Detail Screen,我們可以知道內存的分佈情況,物理內存多少、緩存多少、用戶用了多少,系統用了多少、以及換頁情況等。

 

 

對內存需求密集型系統的性能調試

 

 

1)基於硬件的方法:

 

增加物理內存

使用無盤工作站替代X-terminal

2)基於軟件的方法:

 

減小內核參數maxdsiz的值;

減少內存鎖定的使用;

殺死不必要的進程;

識別出需要大量內存的進程;

重新設計應用;

減小內核的大小;

減小系統表的大小;

減小緩存區的大小;

3)利用serialize命令來調度大進程的系統資源需求

 

/usr/bin/serialize applicaiton

/usr/bin/serialize -p pid

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章