Test big page ing in Linux (MAP_HUGETLB)

Source: Internet
Author: User
Linux's introduction of large page memory has a good effect on reducing the failure of TLB, especially for memory-intensive programs, such as database usage. Innodb engine supports large page memory, specific use can see http://www.cyberciti.biz/tips/linux-hugetlbfs-and-mysq...

Linux's introduction of large page memory has a good effect on reducing the failure of TLB, especially for memory-intensive programs, such as database usage. The innodb engine supports large page memory. For more information, see http://www.cyberciti.biz/tips/linux-hugetlbfs-and-mysql-performance.html.

For more details about the large page, refer to: Documentation/vm/hugetlbpage.

In the past, we used huge page memory to mount the file system to a certain point through hugetlbfs, which is inconvenient to deploy. we just wanted to try Anonymous Pages. is it so troublesome?
The new 2.6.32 kernel uses the memory by supporting the MAP_HUGETLB mode, which avoids the cumbersome mount operation and is more user-friendly.

See man mmap:

MAP_HUGETLB (since Linux 2.6.32)
Allocate the mapping using "huge pages." See the kernel source file Documentation/vm/hugetlbpage.txt for further information.

This is obviously more convenient, but the operations reserved for large memory pages are still required. Let's demonstrate how to prepare the environment first:
# Uname-
Linux dr4000 2.6.32-131.17.1.el6.x86 _ 64 #1 SMP Wed Oct 5 17:19:54 CDT 2011 x86_64 x86_64 x86_64 GNU/Linux

# Cat/proc/meminfo | grep-I huge
AnonHugePages: 2048 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB

# Sysctl vm. nr_hugepages = 192
Vm. nr_hugepages = 192

# Cat/proc/meminfo | grep-I huge
AnonHugePages: 2048 kB
HugePages_Total: 192
HugePages_Free: 192
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
From the above output, we can see that our kernel is 2.6.32, I use the RHEL 6U2 release version, and the 192*2 M page is retained.

The demo code is as follows:

# Cat huge. c
# Include
# Include
# Include

Int main (int argc, char * argv []) {
Char * m;
Size_t s = (8UL * 1024*1024 );

M = mmap (NULL, s, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | 0x40000/* MAP_HUGETLB */,-1, 0 );
If (m = MAP_FAILED ){
Perror ("map mem ");
M = NULL;
Return 1;
}

Memset (m, 0, s );

Printf ("map_hugetlb OK, press ENTER to quit! \ N ");
Getchar ();

Munmap (m, s );
Return 0;
}
# Gcc huge. c
#./A. out
Map_hugetlb OK, press ENTER to quit!
We successfully applied 8 MB memory and 4 large pages with large pages, and successfully cleared them at the same time. before munmap, we need to confirm that the memory is actually used by us.

Okay. switch to another window to see the following:

# Cat/proc/meminfo | grep-I huge
AnonHugePages: 2048 kB
HugePages_Total: 192
HugePages_Free: 188
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Is it possible to succeed without any effort, which also shows that the kernel is improving every day.

Have a good time!

Post Footer automatically generated bywp-posturl pluginfor wordpress.



This article is from the IT blog

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.