[轉帖]Solaris記憶體管理以及判定記憶體是否夠用的方法

msdnchina發表於2011-08-24

Solaris記憶體管理以及判定記憶體是否夠用的方法
http://blog.sina.com.cn/s/blog_592060b50100dmwa.html

Solaris記憶體管理以及判定記憶體是否夠用的方法


Solaris記憶體管理的一些基本原理
<wbr> <wbr> <wbr> 首先需要對Solairs的記憶體管理有一個基本的瞭解。Solaris記憶體管理簡單來說就是“虛擬記憶體管理”模型。(注:現代OS差不多都是虛擬記憶體管理,包括Windows,Linux)虛擬記憶體的定義是:
<wbr> <wbr> <wbr>虛擬記憶體的定義是基於對的重定義的,即把地址空間定義為連續的虛擬記憶體地址,以藉此欺騙程式,使它們以為自己正在使用一大塊的連續地址。(參見:
<wbr> <wbr> <wbr> 虛擬記憶體也就是為了程式設計的方便,而為應用程式假象一個地址空間。就是對於64bit的Solaris來說,虛擬地址空間就是2^64Byte,基本就看成無窮大吧。;對於32位的作業系統就是4G(如果核心空間獨立的話,可能就是2G或者3G,如WindowsXP)。虛擬記憶體和實際實體記憶體的對映關係透過CPU的MMU(記憶體管理單元)來完成。

首先,透過在root許可權下執行
# echo ::memstat |mdb -k
Page Summary <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Pages <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> MB <wbr> %Tot
------------ <wbr> <wbr> <wbr> <wbr> ---------------- <wbr> ---------------- <wbr> ----
Kernel <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>49709 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 388 <wbr> <wbr> <wbr> <wbr> <wbr> 2%
Anon <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>927367 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 7245 <wbr> <wbr> <wbr> <wbr>46%
Exec and libs <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 5186 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 40 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 0%
Page cache <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 212025 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 1656 <wbr> <wbr> 11%
Free (cachelist) <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 787615 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 6153 <wbr> <wbr> 39%
Free (freelist) <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 31964 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 249 <wbr> <wbr> <wbr> <wbr> <wbr> 2%

Total <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 2013866 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 15733

# echo ::memstat |mdb -k
Page Summary <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Pages <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> MB <wbr> %Tot
------------ <wbr> <wbr> <wbr> <wbr> ---------------- <wbr> ---------------- <wbr> ----
Kernel <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 70542 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 551 <wbr> <wbr> <wbr> 4%
Anon <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 950567 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 7426 <wbr> <wbr> 47%
Exec and libs <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 5965 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 46 <wbr> <wbr> <wbr> 0%
Page cache <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 6219 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 48 <wbr> <wbr> <wbr> 0%
Free (cachelist) <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 978159 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 7641 <wbr> <wbr> 49%
Free (freelist) <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 2414 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 18 <wbr> <wbr> <wbr> 0%

Total <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 2013866 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 15733
這裡給出了“實體記憶體”的分配細節
  • Kernel:核心佔用的記憶體。這部分核心佔用的記憶體一般為了保證效能是不允許被換頁的。
  • Anon(Anonymous匿名記憶體):簡單的理解就是使用者程式分配給自己專有的記憶體。準確的說應該是包括使用者程式堆空間、棧空間和copy-on-write頁面、共享記憶體對映和小的核心輔助記憶體,諸如代表使用者程式的lwp執行緒棧空間。這部分記憶體常常需要需要進行寫操作的,導致換頁時候,也必須完成回寫操作,使得這部分記憶體的換頁成本非常高。----這又牽涉到一個的概念。
  • Exec and libs:為庫函式所分配的記憶體。差不多算是隻讀記憶體,庫函式顯然沒有必要被修改,這部分記憶體在需要的時候可以被直接扔掉。
  • Page Cache:不在快取列表上的頁面快取。可以這樣理解,系統剛開始執行時,保留一部分實體記憶體作為檔案系統快取,但初始時並沒有被任何檔案系統內容使用。一般來說系統執行一段時間後,這部分就差不多變成0了。
  • Free(cachelist): 在空閒列表中的頁面快取大小。差不多可以認為是已經分配的檔案系統快取。(Solaris 8不認為這屬於vmstat中的空閒記憶體,因此常常看到空閒記憶體為0,但是系統一樣跑的很好;在Solaris9中把這部分記憶體也作為vmstat中的空閒記憶體對待)
  • Free(freelist):真正空閒的記憶體大小。這是與任何檔案或者程式沒有聯絡的正真的空閒記憶體。
這裡的檔案系統快取差不多就是AIX裡面的“非計算記憶體”(參見:在AIX的NCOMP和COMP記憶體設定和安裝Oracle的聯絡
<wbr> <wbr> <wbr> 上面各種記憶體的順序差不多是按照“換頁”成本進行排序的,越往上,換頁成本越高。

$ pmap -x 15565
15565: <wbr> oracleora10g (LOCAL=NO)
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Address <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>Kbytes <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> RSS <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Anon <wbr> <wbr> <wbr> <wbr> Locked <wbr> <wbr> <wbr>Mode <wbr> <wbr> Mapped File
0000000100000000 <wbr> <wbr> <wbr> <wbr> 100128 <wbr> <wbr> <wbr> <wbr> <wbr> 99208 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> - <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>- r-x-- <wbr> <wbr> <wbr> <wbr> oracle
00000001062C6000 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 808 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 688 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 176 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>- rwx-- <wbr> <wbr> <wbr> <wbr>oracle
0000000106390000 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 936 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 920 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 920 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>- rwx-- <wbr> <wbr> <wbr> <wbr> <wbr>[ heap ]
......
FFFFFFFF7DB06000 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 56 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 40 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 40 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>- rwx-- <wbr> <wbr> <wbr> <wbr>libclsra10.so
FFFFFFFF7DB14000 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 8 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> - <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> - <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>- rwx-- <wbr> <wbr> <wbr> <wbr>libclsra10.so
FFFFFFFF7DB70000 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 64 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 48 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 48 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> - rw--- <wbr> <wbr> <wbr> <wbr> <wbr>[ anon ]
FFFFFFFF7DB80000 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 64 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 48 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 48 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> - rw--- <wbr> <wbr> <wbr> <wbr> <wbr> [ anon ]
......
FFFFFFFF7DC00000 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 9256 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 8200 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> - <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>- r-x-- <wbr> <wbr> <wbr> <wbr> <wbr> libjox10.so
......
FFFFFFFF7EB00000 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 8 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 8 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 8 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>- rwx-- <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>[ anon ]
......
FFFFFFFF7FFF0000 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 64 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 64 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 64 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>- rw--- <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>[ stack ]
---------------- ---------- ---------- ---------- ----------
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> total Kb <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 20243504 <wbr> <wbr> 20234792 <wbr> <wbr> <wbr> 3616 <wbr> <wbr> 20119600

<wbr> <wbr> <wbr>再來看看,對於一個應用程式來說,它的記憶體分配情況。舉例說明,比如,為Oracle啟動分配20G(也可以是200G)的記憶體。
  • 虛擬記憶體:在分配記憶體時候可以不必理會實體記憶體有多少,只要滿足兩個條件:1、交換分割槽(swap)空間+實體記憶體大於20G;2、不能超過作業系統的地址空間,如64位系統就是2^64位元組;從上面的例子看,為這個Oracle直接分配的就是就是分配的20243504Kbytes虛擬記憶體
  • 實體記憶體:由於這20G的記憶體,有可能一次也不會被訪問到(假設這Oracle是你自己開發的,你分配了一個20G的大陣列,卻忘了使用它----雖然愚蠢,但也不是不可以)。因此OS比較聰明的做法是沒必要一次性地把這20G都裝入實體記憶體,而是在確實需要訪問的時候再分次分批的裝入。裝入實體記憶體的部分就是“駐留記憶體”。從上面的例子看,這個Oracle所佔用的實體記憶體就是分配的RSS20234792。
  • 匿名記憶體:一般你的系統上有好幾十個Oracle程式,顯然不可能每一個Oracle都獨立佔用20G記憶體,事實上從上面例子看只有Anon所佔用的3616K記憶體是這個Oracle所獨有的,20243504-3616K都是共享的部分。
  • 缺頁掃描階段:前提是實體記憶體不足20G,這時候就利用上了swap分割槽。如果發生需要的資料不在記憶體,就會進行“缺頁掃描”,看看能否把啥記憶體換出到磁碟上(swap out)。Solaris有“慢掃”和“快掃”兩種,在缺頁情況不嚴重的時候,一秒鐘掃“頁表”幾十次;如果缺頁情況嚴重,一秒鐘就掃描“頁表”幾千次。當然在“快速掃描”的情況下,系統效能基本上就不能保證了。
  • 換頁階段:這個大家都理解了,這裡就不說了。

判定Solaris系統記憶體是否夠用的方法
1、prstat -mL檢視程式等待記憶體的時間百分比(DFL值)
$ prstat -mL
<wbr> <wbr> PID USERNAME USR SYS TRP TFL DFL LCK SLP LAT VCX ICX SCL SIG PROCESS/LWPID
<wbr>15931 oracle <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 12 4.8 <wbr> 0.0 <wbr> 0.0 0.0 0.0 <wbr> 83 0.0 237 <wbr> <wbr> 0 13K <wbr> <wbr> 0 oracle/1
<wbr>15929 oracle <wbr> <wbr> <wbr> <wbr> <wbr> 5.9 7.8 <wbr> 0.0 <wbr> 0.0 0.0 0.0 <wbr> 88 0.0 205 <wbr> 19 11K <wbr> <wbr> 0 oracle/1
<wbr>15927 oracle <wbr> <wbr> <wbr> <wbr> <wbr> 8.3 5.0 <wbr> 0.0 <wbr> 0.0 0.0 0.0 <wbr> 88 0.0 158 <wbr> <wbr> 0 <wbr> 9K <wbr> <wbr> 0 oracle/1
......DFL表示花費在等待記憶體的時間百分比。上述DFL=0表示沒有發生換頁情況,記憶體夠用。

2、vmstat檢視空閒記憶體(free值)和缺頁掃描頻率(sr值)
bash-2.05$ vmstat 1
<wbr>kthr <wbr> <wbr> <wbr> <wbr> <wbr> memory <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> page <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> disk <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> faults <wbr> <wbr> <wbr> <wbr> <wbr> cpu
<wbr>r b w <wbr> <wbr> swap <wbr> free <wbr> re <wbr> mf pi po fr de sr m1 m2 m3 m4 <wbr> <wbr> in <wbr> <wbr> sy <wbr> <wbr> cs us sy id
<wbr>0 0 0 26057344 7601120 219 2127 31 7 7 0 0 0 <wbr> 0 <wbr> 0 <wbr> 0 4560 6583 2744 <wbr> 7 <wbr> 3 90
<wbr>0 0 0 25980920 6367384 3 9 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 749 <wbr> 452 <wbr> 841 <wbr> 0 <wbr> 0 100
<wbr>0 0 0 25980920 6367384 0 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 0 <wbr> 581 <wbr> 297 <wbr> 710 <wbr> 0 <wbr> 0 100
......
free表示空閒記憶體為6367384,關鍵的是sr=0,表示缺頁掃描頻率為0,也就是不發生缺頁掃描。

3
# echo hardswap/D|mdb -k
hardswap:
hardswap: <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 0 <wbr>
硬交換為0,當然是記憶體夠用。
[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/161195/viewspace-1054315/,如需轉載,請註明出處,否則將追究法律責任。

相關文章