Blame view

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