Principles and applications of TEMPFS and/DEV/SHM under Linux

Source: Internet
Author: User
Tags php session

Introduction:

Using the DF command in Linux or UNIX to view the file system will find information similar to the following (ubuntu12.04)

Part of it is the Tmpfs file system, hanging under/rum, and a none file system hangs under/RUN/SHM.

What is TMPFS?

TMPFS is a memory-based file system on the Linux/unix system. TMPFS can use your memory or swap partition to store files. Thus, Temfs primarily stores staged files.

The VM subsystem in the Linux kernel is responsible for managing virtual memory resources in the background, namely RAM and swap resources, transparently moving RAM pages to swap partitions or from swap partitions to RAM pages, and the Tmpfs file system requires a page of the VM subsystem to store the files. TMPFS does not know whether these pages are in swap or in RAM; making this decision is the work of the VM subsystem. What the Tmpfs file system knows is that it is using some form of virtual memory.

TMPFS is based on memory, so the speed is equivalent, in addition TMPFS uses the VM resources are dynamic, when deleting TMPFS files, Tmpfs file system driver will dynamically reduce the file system and free the VM resources, of course, when the file is created in the dynamic allocation of VM resources. In addition, the TMPFS does not have persistence, and the data is not retained after rebooting, for obvious reasons, it is memory-based.

When compiling the kernel, enable Virtual memory file system support to use Tmpfs,linux kernel to start supporting TMPFS from 2.4 onwards. The current mainstream Linux system is enabled by default TMPFS, such as Redhat.

What is/DEV/SHM?

See dev Everyone makes known it's a device file. Using the Tmpfs file system, the default size is half of physical memory in Linux distributions such as Redhat/centos. If my virtual machine is loaded with CentOS6.0, allocating memory is 1G, so/dev/shm is about 500M.

[Email protected] tmp]# df-h

Filesystem Size used Avail use% mounted on

/dev/sda3 8.8G 4.0G 4.5G 48%/

Tmpfs 499M 88K 499M 1%/DEV/SHM

/DEV/SDA1 97M 43M 50M 47%/boot

TMPFS is a memory-based file system that does not need to be initialized with MKFS when it is created. If I want to change the size of/dev/shm Tmpfs to 512M.

PS: And in my system

None->/dev/shm
That is, the virtual memory of the system. Called Cache in Windows.


#vi/etc/fstab

TMPFS/DEV/SHM TMPFS Defaults 0 0

Change to

Tmpfs/dev/shm Tmpfs defaults,size=512m 0 0

[Email protected] tmp]# Mount-o REMOUNT/DEV/SHM

[Email protected] tmp]# df-h

Filesystem Size used Avail use% mounted on

/dev/sda3 8.8G 4.0G 4.5G 48%/

Tmpfs 512M 88K 512M 1%/DEV/SHM

/DEV/SDA1 97M 43M 50M 47%/boot

How, become 512M, use very convenient, haha.

Of course in the production environment you can increase the memory, anyway, now the memory is very cheap, in order to improve performance also need to increase the/DEV/SHM. Such as:

Mount-o size=1500m-o nr_inodes=1000000-o Noatime,nodiratime-o REMOUNT/DEV/SHM

NR_INNODES Specifies the number of index nodes, as explained in Nodiratime man:

Nodiratime does not update the directory inode access times on the this filesystem.

Do not update the log information in the inode when the directory is accessed, noatime the same, the benefits you understand.


TMPFS applications

TMPFS is memory-based, speed is needless to say, hard disk and it can not compare.

The automatic Memory management feature in Oracle uses/DEV/SHM.

In addition, if the site dimension good use of TMPFS, there will be unexpected harvest.

We will first build a TMP at/DEV/SHM, and bind with/tmp.

[Email protected] shm]# mkdir/dev/shm/tmp

[Email protected] shm]# chmod 1777 /dev/shm/tmp//Note the permissions

[Email protected] shm]# mount–bind/dev/shm/tmp/tmp

[Email protected] tmp]# ls-ld/tmp

DRWXRWXRWT 2 root root 23:58/tmp

Of course you can also not bind, directly mount on the existing installation point using TMPFS, such as:

#umount/tmp

#mount tmpfs/tmp-t Tmpfs-o size=512m

Also very convenient, do not need to use MKFS and other commands to create.

The following/TMP uses the Tmpfs file system.

(1) Put squid cache directory Cache_dir to/tmp

Cache_dir ufs/tmp 256 16 256

Restart the Squid service so that the cache directory is placed in the Tmpfs file, the speed needless to say.

(2) Place the session file of PHP in/tmp

Through the Phpinfo test file to see your PHP session storage location, if not in/tmp, modify the php.ini file, modify the following:

Session.save_path = "/tmp"

Of course, if you have a large number of site visits, you can set up a hierarchical directory store session in/TMP with the following syntax:

Session.save_path= "N;/save_path", N is a graded series, Save_path is the starting directory.

(3) Place the socket file of the service in/tmp

such as Nginx.socket and Mysql.sock

This kind of documents on the net is too few, I research also not enough in-depth, welcome everyone in the comments and I exchange your views. (http://blog.51osos.com/linux/linux-temfs-dev-shm/)




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.