~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/vm/hugepage-shm.c

Version: ~ [ linux-5.5-rc6 ] ~ [ linux-5.4.11 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.95 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.164 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.209 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.209 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * hugepage-shm:
  3  *
  4  * Example of using huge page memory in a user application using Sys V shared
  5  * memory system calls.  In this example the app is requesting 256MB of
  6  * memory that is backed by huge pages.  The application uses the flag
  7  * SHM_HUGETLB in the shmget system call to inform the kernel that it is
  8  * requesting huge pages.
  9  *
 10  * For the ia64 architecture, the Linux kernel reserves Region number 4 for
 11  * huge pages.  That means that if one requires a fixed address, a huge page
 12  * aligned address starting with 0x800000... will be required.  If a fixed
 13  * address is not required, the kernel will select an address in the proper
 14  * range.
 15  * Other architectures, such as ppc64, i386 or x86_64 are not so constrained.
 16  *
 17  * Note: The default shared memory limit is quite low on many kernels,
 18  * you may need to increase it via:
 19  *
 20  * echo 268435456 > /proc/sys/kernel/shmmax
 21  *
 22  * This will increase the maximum size per shared memory segment to 256MB.
 23  * The other limit that you will hit eventually is shmall which is the
 24  * total amount of shared memory in pages. To set it to 16GB on a system
 25  * with a 4kB pagesize do:
 26  *
 27  * echo 4194304 > /proc/sys/kernel/shmall
 28  */
 29 
 30 #include <stdlib.h>
 31 #include <stdio.h>
 32 #include <sys/types.h>
 33 #include <sys/ipc.h>
 34 #include <sys/shm.h>
 35 #include <sys/mman.h>
 36 
 37 #ifndef SHM_HUGETLB
 38 #define SHM_HUGETLB 04000
 39 #endif
 40 
 41 #define LENGTH (256UL*1024*1024)
 42 
 43 #define dprintf(x)  printf(x)
 44 
 45 /* Only ia64 requires this */
 46 #ifdef __ia64__
 47 #define ADDR (void *)(0x8000000000000000UL)
 48 #define SHMAT_FLAGS (SHM_RND)
 49 #else
 50 #define ADDR (void *)(0x0UL)
 51 #define SHMAT_FLAGS (0)
 52 #endif
 53 
 54 int main(void)
 55 {
 56         int shmid;
 57         unsigned long i;
 58         char *shmaddr;
 59 
 60         shmid = shmget(2, LENGTH, SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
 61         if (shmid < 0) {
 62                 perror("shmget");
 63                 exit(1);
 64         }
 65         printf("shmid: 0x%x\n", shmid);
 66 
 67         shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS);
 68         if (shmaddr == (char *)-1) {
 69                 perror("Shared memory attach failure");
 70                 shmctl(shmid, IPC_RMID, NULL);
 71                 exit(2);
 72         }
 73         printf("shmaddr: %p\n", shmaddr);
 74 
 75         dprintf("Starting the writes:\n");
 76         for (i = 0; i < LENGTH; i++) {
 77                 shmaddr[i] = (char)(i);
 78                 if (!(i % (1024 * 1024)))
 79                         dprintf(".");
 80         }
 81         dprintf("\n");
 82 
 83         dprintf("Starting the Check...");
 84         for (i = 0; i < LENGTH; i++)
 85                 if (shmaddr[i] != (char)i) {
 86                         printf("\nIndex %lu mismatched\n", i);
 87                         exit(3);
 88                 }
 89         dprintf("Done.\n");
 90 
 91         if (shmdt((const void *)shmaddr) != 0) {
 92                 perror("Detach failure");
 93                 shmctl(shmid, IPC_RMID, NULL);
 94                 exit(4);
 95         }
 96 
 97         shmctl(shmid, IPC_RMID, NULL);
 98 
 99         return 0;
100 }
101 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp