1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * hugepage-shm: 3 * hugepage-shm: 4 * 4 * 5 * Example of using huge page memory in a user 5 * Example of using huge page memory in a user application using Sys V shared 6 * memory system calls. In this example the a 6 * memory system calls. In this example the app is requesting 256MB of 7 * memory that is backed by huge pages. The a 7 * memory that is backed by huge pages. The application uses the flag 8 * SHM_HUGETLB in the shmget system call to in 8 * SHM_HUGETLB in the shmget system call to inform the kernel that it is 9 * requesting huge pages. 9 * requesting huge pages. 10 * 10 * 11 * Note: The default shared memory limit is qu 11 * Note: The default shared memory limit is quite low on many kernels, 12 * you may need to increase it via: 12 * you may need to increase it via: 13 * 13 * 14 * echo 268435456 > /proc/sys/kernel/shmmax 14 * echo 268435456 > /proc/sys/kernel/shmmax 15 * 15 * 16 * This will increase the maximum size per sha 16 * This will increase the maximum size per shared memory segment to 256MB. 17 * The other limit that you will hit eventuall 17 * The other limit that you will hit eventually is shmall which is the 18 * total amount of shared memory in pages. To 18 * total amount of shared memory in pages. To set it to 16GB on a system 19 * with a 4kB pagesize do: 19 * with a 4kB pagesize do: 20 * 20 * 21 * echo 4194304 > /proc/sys/kernel/shmall 21 * echo 4194304 > /proc/sys/kernel/shmall 22 */ 22 */ 23 23 24 #include <stdlib.h> 24 #include <stdlib.h> 25 #include <stdio.h> 25 #include <stdio.h> 26 #include <sys/types.h> 26 #include <sys/types.h> 27 #include <sys/ipc.h> 27 #include <sys/ipc.h> 28 #include <sys/shm.h> 28 #include <sys/shm.h> 29 #include <sys/mman.h> 29 #include <sys/mman.h> 30 30 31 #define LENGTH (256UL*1024*1024) 31 #define LENGTH (256UL*1024*1024) 32 32 33 #define dprintf(x) printf(x) 33 #define dprintf(x) printf(x) 34 34 35 int main(void) 35 int main(void) 36 { 36 { 37 int shmid; 37 int shmid; 38 unsigned long i; 38 unsigned long i; 39 char *shmaddr; 39 char *shmaddr; 40 40 41 shmid = shmget(2, LENGTH, SHM_HUGETLB 41 shmid = shmget(2, LENGTH, SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W); 42 if (shmid < 0) { 42 if (shmid < 0) { 43 perror("shmget"); 43 perror("shmget"); 44 exit(1); 44 exit(1); 45 } 45 } 46 printf("shmid: 0x%x\n", shmid); 46 printf("shmid: 0x%x\n", shmid); 47 47 48 shmaddr = shmat(shmid, NULL, 0); 48 shmaddr = shmat(shmid, NULL, 0); 49 if (shmaddr == (char *)-1) { 49 if (shmaddr == (char *)-1) { 50 perror("Shared memory attach f 50 perror("Shared memory attach failure"); 51 shmctl(shmid, IPC_RMID, NULL); 51 shmctl(shmid, IPC_RMID, NULL); 52 exit(2); 52 exit(2); 53 } 53 } 54 printf("shmaddr: %p\n", shmaddr); 54 printf("shmaddr: %p\n", shmaddr); 55 55 56 dprintf("Starting the writes:\n"); 56 dprintf("Starting the writes:\n"); 57 for (i = 0; i < LENGTH; i++) { 57 for (i = 0; i < LENGTH; i++) { 58 shmaddr[i] = (char)(i); 58 shmaddr[i] = (char)(i); 59 if (!(i % (1024 * 1024))) 59 if (!(i % (1024 * 1024))) 60 dprintf("."); 60 dprintf("."); 61 } 61 } 62 dprintf("\n"); 62 dprintf("\n"); 63 63 64 dprintf("Starting the Check..."); 64 dprintf("Starting the Check..."); 65 for (i = 0; i < LENGTH; i++) 65 for (i = 0; i < LENGTH; i++) 66 if (shmaddr[i] != (char)i) { 66 if (shmaddr[i] != (char)i) { 67 printf("\nIndex %lu mi 67 printf("\nIndex %lu mismatched\n", i); 68 exit(3); 68 exit(3); 69 } 69 } 70 dprintf("Done.\n"); 70 dprintf("Done.\n"); 71 71 72 if (shmdt((const void *)shmaddr) != 0) 72 if (shmdt((const void *)shmaddr) != 0) { 73 perror("Detach failure"); 73 perror("Detach failure"); 74 shmctl(shmid, IPC_RMID, NULL); 74 shmctl(shmid, IPC_RMID, NULL); 75 exit(4); 75 exit(4); 76 } 76 } 77 77 78 shmctl(shmid, IPC_RMID, NULL); 78 shmctl(shmid, IPC_RMID, NULL); 79 79 80 return 0; 80 return 0; 81 } 81 } 82 82
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.