hugetlbpage.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. The intent of this file is to give a brief summary of hugetlbpage support in
  2. the Linux kernel. This support is built on top of multiple page size support
  3. that is provided by most modern architectures. For example, i386
  4. architecture supports 4K and 4M (2M in PAE mode) page sizes, ia64
  5. architecture supports multiple page sizes 4K, 8K, 64K, 256K, 1M, 4M, 16M,
  6. 256M and ppc64 supports 4K and 16M. A TLB is a cache of virtual-to-physical
  7. translations. Typically this is a very scarce resource on processor.
  8. Operating systems try to make best use of limited number of TLB resources.
  9. This optimization is more critical now as bigger and bigger physical memories
  10. (several GBs) are more readily available.
  11. Users can use the huge page support in Linux kernel by either using the mmap
  12. system call or standard SYSv shared memory system calls (shmget, shmat).
  13. First the Linux kernel needs to be built with the CONFIG_HUGETLBFS
  14. (present under "File systems") and CONFIG_HUGETLB_PAGE (selected
  15. automatically when CONFIG_HUGETLBFS is selected) configuration
  16. options.
  17. The kernel built with hugepage support should show the number of configured
  18. hugepages in the system by running the "cat /proc/meminfo" command.
  19. /proc/meminfo also provides information about the total number of hugetlb
  20. pages configured in the kernel. It also displays information about the
  21. number of free hugetlb pages at any time. It also displays information about
  22. the configured hugepage size - this is needed for generating the proper
  23. alignment and size of the arguments to the above system calls.
  24. The output of "cat /proc/meminfo" will have lines like:
  25. .....
  26. HugePages_Total: vvv
  27. HugePages_Free: www
  28. HugePages_Rsvd: xxx
  29. HugePages_Surp: yyy
  30. Hugepagesize: zzz kB
  31. where:
  32. HugePages_Total is the size of the pool of hugepages.
  33. HugePages_Free is the number of hugepages in the pool that are not yet
  34. allocated.
  35. HugePages_Rsvd is short for "reserved," and is the number of hugepages
  36. for which a commitment to allocate from the pool has been made, but no
  37. allocation has yet been made. It's vaguely analogous to overcommit.
  38. HugePages_Surp is short for "surplus," and is the number of hugepages in
  39. the pool above the value in /proc/sys/vm/nr_hugepages. The maximum
  40. number of surplus hugepages is controlled by
  41. /proc/sys/vm/nr_overcommit_hugepages.
  42. /proc/filesystems should also show a filesystem of type "hugetlbfs" configured
  43. in the kernel.
  44. /proc/sys/vm/nr_hugepages indicates the current number of configured hugetlb
  45. pages in the kernel. Super user can dynamically request more (or free some
  46. pre-configured) hugepages.
  47. The allocation (or deallocation) of hugetlb pages is possible only if there are
  48. enough physically contiguous free pages in system (freeing of hugepages is
  49. possible only if there are enough hugetlb pages free that can be transferred
  50. back to regular memory pool).
  51. Pages that are used as hugetlb pages are reserved inside the kernel and cannot
  52. be used for other purposes.
  53. Once the kernel with Hugetlb page support is built and running, a user can
  54. use either the mmap system call or shared memory system calls to start using
  55. the huge pages. It is required that the system administrator preallocate
  56. enough memory for huge page purposes.
  57. Use the following command to dynamically allocate/deallocate hugepages:
  58. echo 20 > /proc/sys/vm/nr_hugepages
  59. This command will try to configure 20 hugepages in the system. The success
  60. or failure of allocation depends on the amount of physically contiguous
  61. memory that is preset in system at this time. System administrators may want
  62. to put this command in one of the local rc init files. This will enable the
  63. kernel to request huge pages early in the boot process (when the possibility
  64. of getting physical contiguous pages is still very high). In either
  65. case, adminstrators will want to verify the number of hugepages actually
  66. allocated by checking the sysctl or meminfo.
  67. /proc/sys/vm/nr_overcommit_hugepages indicates how large the pool of
  68. hugepages can grow, if more hugepages than /proc/sys/vm/nr_hugepages are
  69. requested by applications. echo'ing any non-zero value into this file
  70. indicates that the hugetlb subsystem is allowed to try to obtain
  71. hugepages from the buddy allocator, if the normal pool is exhausted. As
  72. these surplus hugepages go out of use, they are freed back to the buddy
  73. allocator.
  74. Caveat: Shrinking the pool via nr_hugepages while a surplus is in effect
  75. will allow the number of surplus huge pages to exceed the overcommit
  76. value, as the pool hugepages (which must have been in use for a surplus
  77. hugepages to be allocated) will become surplus hugepages. As long as
  78. this condition holds, however, no more surplus huge pages will be
  79. allowed on the system until one of the two sysctls are increased
  80. sufficiently, or the surplus huge pages go out of use and are freed.
  81. If the user applications are going to request hugepages using mmap system
  82. call, then it is required that system administrator mount a file system of
  83. type hugetlbfs:
  84. mount -t hugetlbfs \
  85. -o uid=<value>,gid=<value>,mode=<value>,size=<value>,nr_inodes=<value> \
  86. none /mnt/huge
  87. This command mounts a (pseudo) filesystem of type hugetlbfs on the directory
  88. /mnt/huge. Any files created on /mnt/huge uses hugepages. The uid and gid
  89. options sets the owner and group of the root of the file system. By default
  90. the uid and gid of the current process are taken. The mode option sets the
  91. mode of root of file system to value & 0777. This value is given in octal.
  92. By default the value 0755 is picked. The size option sets the maximum value of
  93. memory (huge pages) allowed for that filesystem (/mnt/huge). The size is
  94. rounded down to HPAGE_SIZE. The option nr_inodes sets the maximum number of
  95. inodes that /mnt/huge can use. If the size or nr_inodes option is not
  96. provided on command line then no limits are set. For size and nr_inodes
  97. options, you can use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. For
  98. example, size=2K has the same meaning as size=2048.
  99. While read system calls are supported on files that reside on hugetlb
  100. file systems, write system calls are not.
  101. Regular chown, chgrp, and chmod commands (with right permissions) could be
  102. used to change the file attributes on hugetlbfs.
  103. Also, it is important to note that no such mount command is required if the
  104. applications are going to use only shmat/shmget system calls. Users who
  105. wish to use hugetlb page via shared memory segment should be a member of
  106. a supplementary group and system admin needs to configure that gid into
  107. /proc/sys/vm/hugetlb_shm_group. It is possible for same or different
  108. applications to use any combination of mmaps and shm* calls, though the
  109. mount of filesystem will be required for using mmap calls.
  110. *******************************************************************
  111. /*
  112. * Example of using hugepage memory in a user application using Sys V shared
  113. * memory system calls. In this example the app is requesting 256MB of
  114. * memory that is backed by huge pages. The application uses the flag
  115. * SHM_HUGETLB in the shmget system call to inform the kernel that it is
  116. * requesting hugepages.
  117. *
  118. * For the ia64 architecture, the Linux kernel reserves Region number 4 for
  119. * hugepages. That means the addresses starting with 0x800000... will need
  120. * to be specified. Specifying a fixed address is not required on ppc64,
  121. * i386 or x86_64.
  122. *
  123. * Note: The default shared memory limit is quite low on many kernels,
  124. * you may need to increase it via:
  125. *
  126. * echo 268435456 > /proc/sys/kernel/shmmax
  127. *
  128. * This will increase the maximum size per shared memory segment to 256MB.
  129. * The other limit that you will hit eventually is shmall which is the
  130. * total amount of shared memory in pages. To set it to 16GB on a system
  131. * with a 4kB pagesize do:
  132. *
  133. * echo 4194304 > /proc/sys/kernel/shmall
  134. */
  135. #include <stdlib.h>
  136. #include <stdio.h>
  137. #include <sys/types.h>
  138. #include <sys/ipc.h>
  139. #include <sys/shm.h>
  140. #include <sys/mman.h>
  141. #ifndef SHM_HUGETLB
  142. #define SHM_HUGETLB 04000
  143. #endif
  144. #define LENGTH (256UL*1024*1024)
  145. #define dprintf(x) printf(x)
  146. /* Only ia64 requires this */
  147. #ifdef __ia64__
  148. #define ADDR (void *)(0x8000000000000000UL)
  149. #define SHMAT_FLAGS (SHM_RND)
  150. #else
  151. #define ADDR (void *)(0x0UL)
  152. #define SHMAT_FLAGS (0)
  153. #endif
  154. int main(void)
  155. {
  156. int shmid;
  157. unsigned long i;
  158. char *shmaddr;
  159. if ((shmid = shmget(2, LENGTH,
  160. SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W)) < 0) {
  161. perror("shmget");
  162. exit(1);
  163. }
  164. printf("shmid: 0x%x\n", shmid);
  165. shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS);
  166. if (shmaddr == (char *)-1) {
  167. perror("Shared memory attach failure");
  168. shmctl(shmid, IPC_RMID, NULL);
  169. exit(2);
  170. }
  171. printf("shmaddr: %p\n", shmaddr);
  172. dprintf("Starting the writes:\n");
  173. for (i = 0; i < LENGTH; i++) {
  174. shmaddr[i] = (char)(i);
  175. if (!(i % (1024 * 1024)))
  176. dprintf(".");
  177. }
  178. dprintf("\n");
  179. dprintf("Starting the Check...");
  180. for (i = 0; i < LENGTH; i++)
  181. if (shmaddr[i] != (char)i)
  182. printf("\nIndex %lu mismatched\n", i);
  183. dprintf("Done.\n");
  184. if (shmdt((const void *)shmaddr) != 0) {
  185. perror("Detach failure");
  186. shmctl(shmid, IPC_RMID, NULL);
  187. exit(3);
  188. }
  189. shmctl(shmid, IPC_RMID, NULL);
  190. return 0;
  191. }
  192. *******************************************************************
  193. /*
  194. * Example of using hugepage memory in a user application using the mmap
  195. * system call. Before running this application, make sure that the
  196. * administrator has mounted the hugetlbfs filesystem (on some directory
  197. * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this
  198. * example, the app is requesting memory of size 256MB that is backed by
  199. * huge pages.
  200. *
  201. * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages.
  202. * That means the addresses starting with 0x800000... will need to be
  203. * specified. Specifying a fixed address is not required on ppc64, i386
  204. * or x86_64.
  205. */
  206. #include <stdlib.h>
  207. #include <stdio.h>
  208. #include <unistd.h>
  209. #include <sys/mman.h>
  210. #include <fcntl.h>
  211. #define FILE_NAME "/mnt/hugepagefile"
  212. #define LENGTH (256UL*1024*1024)
  213. #define PROTECTION (PROT_READ | PROT_WRITE)
  214. /* Only ia64 requires this */
  215. #ifdef __ia64__
  216. #define ADDR (void *)(0x8000000000000000UL)
  217. #define FLAGS (MAP_SHARED | MAP_FIXED)
  218. #else
  219. #define ADDR (void *)(0x0UL)
  220. #define FLAGS (MAP_SHARED)
  221. #endif
  222. void check_bytes(char *addr)
  223. {
  224. printf("First hex is %x\n", *((unsigned int *)addr));
  225. }
  226. void write_bytes(char *addr)
  227. {
  228. unsigned long i;
  229. for (i = 0; i < LENGTH; i++)
  230. *(addr + i) = (char)i;
  231. }
  232. void read_bytes(char *addr)
  233. {
  234. unsigned long i;
  235. check_bytes(addr);
  236. for (i = 0; i < LENGTH; i++)
  237. if (*(addr + i) != (char)i) {
  238. printf("Mismatch at %lu\n", i);
  239. break;
  240. }
  241. }
  242. int main(void)
  243. {
  244. void *addr;
  245. int fd;
  246. fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
  247. if (fd < 0) {
  248. perror("Open failed");
  249. exit(1);
  250. }
  251. addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0);
  252. if (addr == MAP_FAILED) {
  253. perror("mmap");
  254. unlink(FILE_NAME);
  255. exit(1);
  256. }
  257. printf("Returned address is %p\n", addr);
  258. check_bytes(addr);
  259. write_bytes(addr);
  260. read_bytes(addr);
  261. munmap(addr, LENGTH);
  262. close(fd);
  263. unlink(FILE_NAME);
  264. return 0;
  265. }