Debian Codegraph @

libgomp/testsuite/libgomp.c/alloc-pinned-4.c

raw
1 /* { dg-do run } */
2
3 /* Test that pinned memory fails correctly, pool_size code path. */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #ifdef __linux__
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 #include <sys/mman.h>
13 #include <sys/resource.h>
14
15 #define PAGE_SIZE sysconf(_SC_PAGESIZE)
16
17 int
18 get_pinned_mem ()
19 {
20 int pid = getpid ();
21 char buf[100];
22 sprintf (buf, "/proc/%d/status", pid);
23
24 FILE *proc = fopen (buf, "r");
25 if (!proc)
26 abort ();
27 while (fgets (buf, 100, proc))
28 {
29 int val;
30 if (sscanf (buf, "VmLck: %d", &val))
31 {
32 fclose (proc);
33 return val;
34 }
35 }
36 abort ();
37 }
38
39 void
40 set_pin_limit (int size)
41 {
42 struct rlimit limit;
43 if (getrlimit (RLIMIT_MEMLOCK, &limit))
44 abort ();
45 limit.rlim_cur = (limit.rlim_max < size ? limit.rlim_max : size);
46 if (setrlimit (RLIMIT_MEMLOCK, &limit))
47 abort ();
48 }
49 #else
50 #define PAGE_SIZE 10000 * 1024 /* unknown */
51 #define EXPECT_OMP_NULL_ALLOCATOR
52
53 int
54 get_pinned_mem ()
55 {
56 return 0;
57 }
58
59 void
60 set_pin_limit (int size)
61 {
62 }
63 #endif
64
65 static void
66 verify0 (char *p, size_t s)
67 {
68 for (size_t i = 0; i < s; ++i)
69 if (p[i] != 0)
70 abort ();
71 }
72
73 #include <omp.h>
74
75 int
76 main ()
77 {
78 /* This needs to be large enough to cover multiple pages. */
79 const int SIZE = PAGE_SIZE * 4;
80
81 /* Pinned memory, no fallback. */
82 const omp_alloctrait_t traits1[] = {
83 { omp_atk_pinned, 1 },
84 { omp_atk_fallback, omp_atv_null_fb },
85 { omp_atk_pool_size, SIZE * 8 }
86 };
87 omp_allocator_handle_t allocator1 = omp_init_allocator (omp_default_mem_space,
88 3, traits1);
89
90 /* Pinned memory, plain memory fallback. */
91 const omp_alloctrait_t traits2[] = {
92 { omp_atk_pinned, 1 },
93 { omp_atk_fallback, omp_atv_default_mem_fb },
94 { omp_atk_pool_size, SIZE * 8 }
95 };
96 omp_allocator_handle_t allocator2 = omp_init_allocator (omp_default_mem_space,
97 3, traits2);
98
99 #ifdef EXPECT_OMP_NULL_ALLOCATOR
100 if (allocator1 == omp_null_allocator
101 && allocator2 == omp_null_allocator)
102 return 0;
103 #endif
104
105 /* Ensure that the limit is smaller than the allocation. */
106 set_pin_limit (SIZE / 2);
107
108 // Sanity check
109 if (get_pinned_mem () != 0)
110 abort ();
111
112 // Should fail
113 void *p = omp_alloc (SIZE, allocator1);
114 if (p)
115 abort ();
116
117 // Should fail
118 p = omp_calloc (1, SIZE, allocator1);
119 if (p)
120 abort ();
121
122 // Should fall back
123 p = omp_alloc (SIZE, allocator2);
124 if (!p)
125 abort ();
126
127 // Should fall back
128 p = omp_calloc (1, SIZE, allocator2);
129 if (!p)
130 abort ();
131 verify0 (p, SIZE);
132
133 // Should fail to realloc
134 void *notpinned = omp_alloc (SIZE, omp_default_mem_alloc);
135 p = omp_realloc (notpinned, SIZE, allocator1, omp_default_mem_alloc);
136 if (!notpinned || p)
137 abort ();
138
139 // Should fall back to no realloc needed
140 p = omp_realloc (notpinned, SIZE, allocator2, omp_default_mem_alloc);
141 if (p != notpinned)
142 abort ();
143
144 // No memory should have been pinned
145 int amount = get_pinned_mem ();
146 if (amount != 0)
147 abort ();
148
149 return 0;
150 }