hugetlbpage.txt 9.2 KB

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