理解linux下/dev/shm tmpfs

  Oracle 11g引入了MEMORY_TARGET參數,用於控制oracle對於系統內存的使用,首次將SGA和PGA整合在一起實現自動管理,一旦設置了Memory_target 參數,oracle就會根據需要自動調整SGA和PGA以合理分配及使用內存。如果Memory_target設置不當,就容易引發ORA-00845錯誤。原因是Memory_target和/dev/shm (即tmpfs)有緊密聯繫。下面就來研究下/dev/shm究竟是什麼,他的作用是什麼,如何修改以及他的應用場景

 

一、/dev/shm是什麼

       /dev/shm是linux非常有用的一個目錄,它就是所謂的tmpfs,也可以稱之爲臨時文件系統(不是塊設備),類似oracle中的臨時表空間一樣,用於加速和優化系統。該目錄並沒有放在磁盤上,而上在內存當中。因此在linux下,不用大費周折的去建ramdisk,直接使用/dev/shm就可以達到很好的效果。

       Tmpfs和ramdisk(虛擬磁盤)。Tmpfs可以使用RAM,也可以使用交換分區來進行存儲。傳統的ramdisk(虛擬磁盤)是個塊設備,並且需要mkfs之類的命令之後纔可以真正的使用它。Tmpfs是一個文件系統,不是塊設備,系統默認啓動就會加載/dev/shm,只要安裝它就可以使用了。

       Tmpfs優勢。

1.    動態文件系統的大小

2.    讀寫速度快。典型的tmpfs文件系統會完全駐留在RAM中,讀寫幾乎是瞬間完成。

3.    Tmpfs中的數據在重新啓動之後不會保留,因爲虛擬內存本質上是易失的,所以有必要做些腳本做諸如加載、綁定的操作。

 

注意

在oracle數據庫啓動後,在/dev/shm目錄下會產生大量ORA文件,一定不要試圖去刪除這些文件,刪除之後,oracle數據庫會宕掉。

 

 

、/dev/shm如何修改大小

 

       關於/dev/shm容量的問題,在linux下,默認/dev/shm爲實際物理內存的1/2,使用df –h命令查看。實際上它不會真正的佔用這塊內存。如果/dev/shm下沒有任何文件,它實際佔用的內存就是0字節;如果它最大爲1G,裏面放有100M的文件,那麼剩餘的900M任然可以被其他應用程序所使用,但是已經佔用的這100M內存空間是不會被系統回收重新劃分的。

       臨時調整tmpfs大小,重啓後失效

默認的最大一半內存在某些場合可能不夠用,並且默認的額inode數量很低,一般都要調高些,可以用下面的命令來實現

#mount–o size=1500m –o nr_inodes=1000000 –o noatime,nodiratime –o remount /dev/shm

在2G的機器上,/dev/shm最大尺寸調整到1.5G,並且inode數量調整到1000000,這意味着大致可存入100萬個小文件

       永久修改tmpfs大小,修改/etc/fstab文件

tmpfs /dev/shm tmpfsdefaults,size=1500M 0 0

修改之後remount

#mount –o remount  /dev/shm

注意:

這裏一定要注意,修改的size一定要是整數,否則在remount時候會遇到如下問題

#mount –o remount /dev/shm

mount: wrong fs type,bad option, bad superblockon tmpfs,

 missingcodepage or other error

 In somecases useful info is found in syslog – try

 dmesg |tail  or so

# dmesg | tail

Bluetooth: L2CAP socket layerinitialized

Bluetooth: RFCOMM socketlayer initialized

Bluetooth: RFCOMM TTY layerinitialized

Bluetooth: RFCOMM ver 1.8

Bluetooth: HIDP (HumanInterface Emulation) ver 1.1

mtrr: your processor doesn'tsupport write-combining

mtrr: your processor doesn'tsupport write-combining

tmpfs: Bad value '1.5G' formount option 'size'

tmpfs: Bad value '1.5G' for mount option 'size'

tmpfs: Bad value '0.5G' for mount option 'size'

三、/dev/shm應用

首先在/dev/shm建個tmp文件夾,然後與實際/tmp綁定,把/dev/shm綁定到/tmp目錄上
#mkdir /dev/shm/tmp
  #chmod 777 /dev/shm/tmp
  #mount –bind /dev/shm/tmp/tmp(–bind )
  在使用mount –bind olderdirnewerdir命令來掛載一個目錄到另一個目錄後,newerdir的權限和所有者等所有信息會發生變化。掛載後的目錄繼承了被掛載目錄的所有屬性,除了名稱。Oracle 11g的amm內存管理模式就是使用/dev/shm,所以有時候修改MEMORY_TARGET或者MEMORY_MAX_TARGET會出現ORA-00845的錯誤

 

四、tmpfs文檔

 

Tmpfsis a file system which keeps all files in virtual memory.

Everythingin tmpfs is temporary in the sense that no files will be
created on your hard drive. If you unmount a tmpfs instance,
everything stored therein is lost.

tmpfsputs everything into the kernel internal caches and grows and
shrinks to accommodate the files it contains and is able to swap
unneeded pages out to swap space. It has maximum size limits which can
be adjusted on the fly via ‘mount -o remount …’

Ifyou compare it to ramfs (which was the template to create tmpfs)
you gain swapping and limit checking. Another similar thing is the RAM
disk (/dev/ram*), which simulates a fixed size hard disk in physical
RAM, where you have to create an ordinary filesystem on top. Ramdisks
cannot swap and you do not have the possibility to resize them.

Sincetmpfs lives completely in the page cache and on swap, all tmpfs
pages currently in memory will show up as cached. It will not show up
as shared or something like that. Further on you can check the actual
RAM+swap use of a tmpfs instance with df(1) and du(1).

tmpfshas the following uses:

1)There is always a kernel internal mount which you will not see at
all. This is used for shared anonymous mappings and SYSV shared
memory.

Thismount does not depend on CONFIG_TMPFS. If CONFIG_TMPFS is not
set, the user visible part of tmpfs is not build. But the internal
mechanisms are always present.

2)glibc 2.2 and above expects tmpfs to be mounted at /dev/shm for
POSIX shared memory (shm_open, shm_unlink). Adding the following
line to /etc/fstab should take care of this:

tmpfs/dev/shm tmpfs defaults 0 0

Rememberto create the directory that you intend to mount tmpfs on
if necessary (/dev/shm is automagically created if you use devfs).

Thismount is _not_ needed for SYSV shared memory. The internal
mount is used for that. (In the 2.3 kernel versions it was
necessary to mount the predecessor of tmpfs (shm fs) to use SYSV
shared memory)

3)Some people (including me) find it very convenient to mount it
e.g. on /tmp and /var/tmp and have a big swap partition. But be
aware: loop mounts of tmpfs files do not work due to the internal
design. So mkinitrd shipped by most distributions will fail with a
tmpfs /tmp.

4)And probably a lot more I do not know about

tmpfshas a couple of mount options:

size:The limit of allocated bytes for this tmpfs instance. The
default is half of your physical RAM without swap. If you
oversize your tmpfs instances the machine will deadlock
since the OOM handler will not be able to free that memory.
nr_blocks: The same as size, but in blocks of PAGECACHE_SIZE.
nr_inodes: The maximum number of inodes for this instance. The default
is half of the number of your physical RAM pages.

Theseparameters accept a suffix k, m or g for kilo, mega and giga and
can be changed on remount.

Tospecify the initial root directory you can use the following mount
options:

mode:The permissions as an octal number
uid: The user id
gid: The group id

Theseoptions do not have any effect on remount. You can change these
parameters with chmod(1), chown(1) and chgrp(1) on a mounted filesystem.

So‘mount -t tmpfs -o size=10G,nr_inodes=10k,mode=700 tmpfs /mytmpfs’
will give you tmpfs instance on /mytmpfs which can allocate 10GB
RAM/SWAP in 10240 inodes and it is only accessible by root.

TODOs:

1)give the size option a percent semantic: If you give a mount option
size=50% the tmpfs instance should be able to grow to 50 percent of
RAM + swap. So the instance should adapt automatically if you add
or remove swap space.
2) loop mounts: This is difficult since loop.c relies on the readpage
operation. This operation gets a page from the caller to be filled
with the content of the file at that position. But tmpfs always has
the page and thus cannot copy the content to the given page. So it
cannot provide this operation. The VM had to be changed seriously
to achieve this.
3) Show the number of tmpfs RAM pages. (As shared?)

Author:
Christoph Rohland , 1.12.01

 

 五、Doc ID 1399209.1

Starting with Oracle Database 11g, the Automatic MemoryManagement feature requires more shared memory (/dev/shm) and file descriptors.The size of the shared memory must be at least the greater of theMEMORY_MAX_TARGET and MEMORY_TARGET parameters for each Oracle instance on thecomputer. If the MEMORY_MAX_TARGET parameter or the MEMORY_TARGET parameter isset to a nonzero value, and an incorrect size is assigned to the shared memory,it results in an ORA-00845 error at startup.
On Linux systems, if the operating system /dev/shm mount size is too small forthe Oracle system global area (SGA) and program global area (PGA), then youencounter the following error:

The cause of this error is an insufficient /dev/shm allocation.The total memory size of the SGA and PGA, which sets the initializationparameter MEMORY_TARGET or MEMORY_MAX_TARGET, cannot be greater than the sharedmemory file system (/dev/shm) on your operating system.

 

整理自網絡

 

 

 

 

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