Debian Codegraph @

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

raw
1 /* { dg-do run } */
2
3 /* Test that pinned memory fails correctly. */
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 };
86 omp_allocator_handle_t allocator1 = omp_init_allocator (omp_default_mem_space,
87 2, traits1);
88
89 /* Pinned memory, plain memory fallback. */
90 const omp_alloctrait_t traits2[] = {
91 { omp_atk_pinned, 1 },
92 { omp_atk_fallback, omp_atv_default_mem_fb }
93 };
94 omp_allocator_handle_t allocator2 = omp_init_allocator (omp_default_mem_space,
95 2, traits2);
96
97 #ifdef EXPECT_OMP_NULL_ALLOCATOR
98 if (allocator1 == omp_null_allocator
99 && allocator2 == omp_null_allocator)
100 return 0;
101 #endif
102
103 /* Ensure that the limit is smaller than the allocation. */
104 set_pin_limit (SIZE / 2);
105
106 // Sanity check
107 if (get_pinned_mem () != 0)
108 abort ();
109
110 // Should fail
111 void *p1 = omp_alloc (SIZE, allocator1);
112 if (p1)
113 abort ();
114
115 // Should fail
116 void *p2 = omp_calloc (1, SIZE, allocator1);
117 if (p2)
118 abort ();
119
120 // Should fall back
121 void *p3 = omp_alloc (SIZE, allocator2);
122 if (!p3)
123 abort ();
124
125 // Should fall back
126 void *p4 = omp_calloc (1, SIZE, allocator2);
127 if (!p4)
128 abort ();
129 verify0 (p4, SIZE);
130
131 // Should fail to realloc
132 void *notpinned = omp_alloc (SIZE, omp_default_mem_alloc);
133 void *p5 = omp_realloc (notpinned, SIZE, allocator1, omp_default_mem_alloc);
134 if (!notpinned || p5)
135 abort ();
136
137 // Should fall back to no realloc needed
138 void *p6 = omp_realloc (notpinned, SIZE, allocator2, omp_default_mem_alloc);
139 if (p6 != notpinned)
140 abort ();
141
142 // No memory should have been pinned
143 int amount = get_pinned_mem ();
144 if (amount != 0)
145 abort ();
146
147 // Ensure free works correctly
148 if (p1) omp_free (p1, allocator1);
149 if (p2) omp_free (p2, allocator1);
150 if (p3) omp_free (p3, allocator2);
151 if (p4) omp_free (p4, allocator2);
152 // p5 and notpinned have been reallocated
153 if (p6) omp_free (p6, omp_default_mem_alloc);
154
155 return 0;
156 }