hugetlbpage.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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, administrators 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 such that it becomes less
  75. than the number of hugepages in use will convert the balance to surplus
  76. huge pages even if it would exceed the overcommit value. As long as
  77. this condition holds, however, no more surplus huge pages will be
  78. allowed on the system until one of the two sysctls are increased
  79. sufficiently, or the surplus huge pages go out of use and are freed.
  80. With support for multiple hugepage pools at run-time available, much of
  81. the hugepage userspace interface has been duplicated in sysfs. The above
  82. information applies to the default hugepage size (which will be
  83. controlled by the proc interfaces for backwards compatibility). The root
  84. hugepage control directory is
  85. /sys/kernel/mm/hugepages
  86. For each hugepage size supported by the running kernel, a subdirectory
  87. will exist, of the form
  88. hugepages-${size}kB
  89. Inside each of these directories, the same set of files will exist:
  90. nr_hugepages
  91. nr_overcommit_hugepages
  92. free_hugepages
  93. resv_hugepages
  94. surplus_hugepages
  95. which function as described above for the default hugepage-sized case.
  96. If the user applications are going to request hugepages using mmap system
  97. call, then it is required that system administrator mount a file system of
  98. type hugetlbfs:
  99. mount -t hugetlbfs \
  100. -o uid=<value>,gid=<value>,mode=<value>,size=<value>,nr_inodes=<value> \
  101. none /mnt/huge
  102. This command mounts a (pseudo) filesystem of type hugetlbfs on the directory
  103. /mnt/huge. Any files created on /mnt/huge uses hugepages. The uid and gid
  104. options sets the owner and group of the root of the file system. By default
  105. the uid and gid of the current process are taken. The mode option sets the
  106. mode of root of file system to value & 0777. This value is given in octal.
  107. By default the value 0755 is picked. The size option sets the maximum value of
  108. memory (huge pages) allowed for that filesystem (/mnt/huge). The size is
  109. rounded down to HPAGE_SIZE. The option nr_inodes sets the maximum number of
  110. inodes that /mnt/huge can use. If the size or nr_inodes option is not
  111. provided on command line then no limits are set. For size and nr_inodes
  112. options, you can use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. For
  113. example, size=2K has the same meaning as size=2048.
  114. While read system calls are supported on files that reside on hugetlb
  115. file systems, write system calls are not.
  116. Regular chown, chgrp, and chmod commands (with right permissions) could be
  117. used to change the file attributes on hugetlbfs.
  118. Also, it is important to note that no such mount command is required if the
  119. applications are going to use only shmat/shmget system calls. Users who
  120. wish to use hugetlb page via shared memory segment should be a member of
  121. a supplementary group and system admin needs to configure that gid into
  122. /proc/sys/vm/hugetlb_shm_group. It is possible for same or different
  123. applications to use any combination of mmaps and shm* calls, though the
  124. mount of filesystem will be required for using mmap calls.
  125. *******************************************************************
  126. /*
  127. * Example of using hugepage memory in a user application using Sys V shared
  128. * memory system calls. In this example the app is requesting 256MB of
  129. * memory that is backed by huge pages. The application uses the flag
  130. * SHM_HUGETLB in the shmget system call to inform the kernel that it is
  131. * requesting hugepages.
  132. *
  133. * For the ia64 architecture, the Linux kernel reserves Region number 4 for
  134. * hugepages. That means the addresses starting with 0x800000... will need
  135. * to be specified. Specifying a fixed address is not required on ppc64,
  136. * i386 or x86_64.
  137. *
  138. * Note: The default shared memory limit is quite low on many kernels,
  139. * you may need to increase it via:
  140. *
  141. * echo 268435456 > /proc/sys/kernel/shmmax
  142. *
  143. * This will increase the maximum size per shared memory segment to 256MB.
  144. * The other limit that you will hit eventually is shmall which is the
  145. * total amount of shared memory in pages. To set it to 16GB on a system
  146. * with a 4kB pagesize do:
  147. *
  148. * echo 4194304 > /proc/sys/kernel/shmall
  149. */
  150. #include <stdlib.h>
  151. #include <stdio.h>
  152. #include <sys/types.h>
  153. #include <sys/ipc.h>
  154. #include <sys/shm.h>
  155. #include <sys/mman.h>
  156. #ifndef SHM_HUGETLB
  157. #define SHM_HUGETLB 04000
  158. #endif
  159. #define LENGTH (256UL*1024*1024)
  160. #define dprintf(x) printf(x)
  161. /* Only ia64 requires this */
  162. #ifdef __ia64__
  163. #define ADDR (void *)(0x8000000000000000UL)
  164. #define SHMAT_FLAGS (SHM_RND)
  165. #else
  166. #define ADDR (void *)(0x0UL)
  167. #define SHMAT_FLAGS (0)
  168. #endif
  169. int main(void)
  170. {
  171. int shmid;
  172. unsigned long i;
  173. char *shmaddr;
  174. if ((shmid = shmget(2, LENGTH,
  175. SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W)) < 0) {
  176. perror("shmget");
  177. exit(1);
  178. }
  179. printf("shmid: 0x%x\n", shmid);
  180. shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS);
  181. if (shmaddr == (char *)-1) {
  182. perror("Shared memory attach failure");
  183. shmctl(shmid, IPC_RMID, NULL);
  184. exit(2);
  185. }
  186. printf("shmaddr: %p\n", shmaddr);
  187. dprintf("Starting the writes:\n");
  188. for (i = 0; i < LENGTH; i++) {
  189. shmaddr[i] = (char)(i);
  190. if (!(i % (1024 * 1024)))
  191. dprintf(".");
  192. }
  193. dprintf("\n");
  194. dprintf("Starting the Check...");
  195. for (i = 0; i < LENGTH; i++)
  196. if (shmaddr[i] != (char)i)
  197. printf("\nIndex %lu mismatched\n", i);
  198. dprintf("Done.\n");
  199. if (shmdt((const void *)shmaddr) != 0) {
  200. perror("Detach failure");
  201. shmctl(shmid, IPC_RMID, NULL);
  202. exit(3);
  203. }
  204. shmctl(shmid, IPC_RMID, NULL);
  205. return 0;
  206. }
  207. *******************************************************************
  208. /*
  209. * Example of using hugepage memory in a user application using the mmap
  210. * system call. Before running this application, make sure that the
  211. * administrator has mounted the hugetlbfs filesystem (on some directory
  212. * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this
  213. * example, the app is requesting memory of size 256MB that is backed by
  214. * huge pages.
  215. *
  216. * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages.
  217. * That means the addresses starting with 0x800000... will need to be
  218. * specified. Specifying a fixed address is not required on ppc64, i386
  219. * or x86_64.
  220. */
  221. #include <stdlib.h>
  222. #include <stdio.h>
  223. #include <unistd.h>
  224. #include <sys/mman.h>
  225. #include <fcntl.h>
  226. #define FILE_NAME "/mnt/hugepagefile"
  227. #define LENGTH (256UL*1024*1024)
  228. #define PROTECTION (PROT_READ | PROT_WRITE)
  229. /* Only ia64 requires this */
  230. #ifdef __ia64__
  231. #define ADDR (void *)(0x8000000000000000UL)
  232. #define FLAGS (MAP_SHARED | MAP_FIXED)
  233. #else
  234. #define ADDR (void *)(0x0UL)
  235. #define FLAGS (MAP_SHARED)
  236. #endif
  237. void check_bytes(char *addr)
  238. {
  239. printf("First hex is %x\n", *((unsigned int *)addr));
  240. }
  241. void write_bytes(char *addr)
  242. {
  243. unsigned long i;
  244. for (i = 0; i < LENGTH; i++)
  245. *(addr + i) = (char)i;
  246. }
  247. void read_bytes(char *addr)
  248. {
  249. unsigned long i;
  250. check_bytes(addr);
  251. for (i = 0; i < LENGTH; i++)
  252. if (*(addr + i) != (char)i) {
  253. printf("Mismatch at %lu\n", i);
  254. break;
  255. }
  256. }
  257. int main(void)
  258. {
  259. void *addr;
  260. int fd;
  261. fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
  262. if (fd < 0) {
  263. perror("Open failed");
  264. exit(1);
  265. }
  266. addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0);
  267. if (addr == MAP_FAILED) {
  268. perror("mmap");
  269. unlink(FILE_NAME);
  270. exit(1);
  271. }
  272. printf("Returned address is %p\n", addr);
  273. check_bytes(addr);
  274. write_bytes(addr);
  275. read_bytes(addr);
  276. munmap(addr, LENGTH);
  277. close(fd);
  278. unlink(FILE_NAME);
  279. return 0;
  280. }