理解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.

 

整理自网络

 

 

 

 

你可能感兴趣的:(linux,知识拾遗,Linux)