Debian Codegraph @

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

raw
1 /* { dg-do run } */
2
3 /* { dg-skip-if "Pinning not implemented on this host" { ! *-*-linux-gnu* } } */
4
5 /* Test that pinned memory works (pool_size code path). */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #ifdef __linux__
11 #include <sys/types.h>
12 #include <unistd.h>
13
14 #include <sys/mman.h>
15 #include <sys/resource.h>
16
17 #define PAGE_SIZE sysconf(_SC_PAGESIZE)
18 #define CHECK_SIZE(SIZE) { \
19 struct rlimit limit; \
20 if (getrlimit (RLIMIT_MEMLOCK, &limit) \
21 || limit.rlim_cur <= SIZE) \
22 { \
23 fprintf (stderr, "insufficient lockable memory; please increase ulimit\n"); \
24 abort (); \
25 } \
26 }
27
28 int
29 get_pinned_mem ()
30 {
31 int pid = getpid ();
32 char buf[100];
33 sprintf (buf, "/proc/%d/status", pid);
34
35 FILE *proc = fopen (buf, "r");
36 if (!proc)
37 abort ();
38 while (fgets (buf, 100, proc))
39 {
40 int val;
41 if (sscanf (buf, "VmLck: %d", &val))
42 {
43 fclose (proc);
44 return val;
45 }
46 }
47 abort ();
48 }
49 #else
50 #error "OS unsupported"
51 #endif
52
53 static void
54 verify0 (char *p, size_t s)
55 {
56 for (size_t i = 0; i < s; ++i)
57 if (p[i] != 0)
58 abort ();
59 }
60
61 #include <omp.h>
62
63 int
64 main ()
65 {
66 /* Allocate at least a page each time, allowing space for overhead,
67 but stay within the ulimit. */
68 const int SIZE = PAGE_SIZE - 128;
69 CHECK_SIZE (SIZE * 5); // This is intended to help diagnose failures
70
71 const omp_alloctrait_t traits[] = {
72 { omp_atk_pinned, 1 },
73 { omp_atk_pool_size, SIZE * 8 }
74 };
75 omp_allocator_handle_t allocator = omp_init_allocator (omp_default_mem_space,
76 2, traits);
77
78 #ifdef EXPECT_OMP_NULL_ALLOCATOR
79 if (allocator == omp_null_allocator)
80 return 0;
81 #endif
82
83 // Sanity check
84 if (get_pinned_mem () != 0)
85 abort ();
86
87 void *p = omp_alloc (SIZE, allocator);
88 if (!p)
89 abort ();
90
91 int amount = get_pinned_mem ();
92 if (amount == 0)
93 abort ();
94
95 p = omp_realloc (p, SIZE * 2, allocator, allocator);
96 if (!p)
97 abort ();
98
99 int amount2 = get_pinned_mem ();
100 if (amount2 <= amount)
101 abort ();
102
103 /* SIZE*2 ensures that it doesn't slot into the space possibly
104 vacated by realloc. */
105 p = omp_calloc (1, SIZE * 2, allocator);
106 if (!p)
107 abort ();
108
109 if (get_pinned_mem () <= amount2)
110 abort ();
111
112 verify0 (p, SIZE * 2);
113
114 return 0;
115 }