hugetlbpage.txt 9.2 KB

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