tmpfs

在建立rootfs時,有去設定/dev/shm
了解一下用意


更新記錄

item note
20161018 第一版

目錄


tmpfs

tmpfs 說明

  • 所有在tmpfs上儲存的資料在理論上都是暫時借放的(存在RAM裡面),那也表示說,檔案不會建立在硬碟上面
    沒有指定大小,預設為系統的一半

  • 对Linux系统中/dev/shm的一点认识

    • /dev/shm/是linux下一个非常有用的目录,因为这个目录不在硬盘上,而是在内存里
      因此在linux下,就不需要大费周折去建ramdisk,直接使用/dev/shm/就可达到很好的优化效果
    • tmpfs 可以使用您的 RAM,但它也可以使用您的交换分区来存储。
    • 而且传统的虚拟磁盘(ramdisk)是个块设备,并需要一个 mkfs 之类的命令才能真正地使用它,
    • tmpfs 是一个文件系统,而不是块设备;您只是安装它,它就可以使用了
  • POSIX Shared Memory

    • This uses the function shm_open from sys/mman.h.2 POSIX interprocess communication includes the shared-memory functions
    • Linux distributions based on the 2.6 kernel and later offer /dev/shm as shared memory in the form of a RAM disk
  • 使用方式

    1
    mount -o mode=0777 -t tmpfs tmpfs /dev/shm
  • df -h

    1
    2
    3
    4
    5
    6
    7
    8
    ~ # df -h
    Filesystem Size Used Available Use% Mounted on
    192.168.0.54:/xx/1.0.0/rootfs
    632.4G 201.2G 399.0G 34% /
    devtmpfs 120.1M 4.0K 120.0M 0% /dev
    tmpfs 120.1M 0 120.1M 0% /tmp
    tmpfs 16.0M 4.0K 16.0M 0% /var
    tmpfs 120.1M 0 120.1M 0% /dev/shm
  • cmdline

    1
    2
    3
    ~ # cat /proc/cmdline 
    mem=250M console=ttyAMA0,115200 root=/dev/nfs rw nfsroot=192.168.0.54:/xx/1.0.0/rootfs ip=192.168.0.130:192.168.0.54:255.2558
    ~ #

為何需要建立/dev/shm

  • tmpfs to be mounted at /dev/shm for POSIX shared memory (shm_open, shm_unlink).
  • 目前程式有用到shm_open

kernel 說明文件

  • filesystems/tmpfs.txt
    Tmpfs is a file system which keeps all files in virtual memory.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    tmpfs has 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.

    This mount 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

    Remember to create the directory that you intend to mount tmpfs on
    if necessary.

    This mount 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. And now
    loop mounts of tmpfs files do work, so mkinitrd shipped by most
    distributions should succeed with a tmpfs /tmp.

其它說明

  • 註:/dev/shm 不完全是 RamDisk,若它使用超過電腦一半以上的 RAM,就會開始吃 SWAP。
    另外它沒用到的部份,會自動釋放出來給系統使用

其它參考