hugetlbpage.txt 9.6 KB

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