Debian Codegraph @

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

raw
1 /* { dg-do run } */
2
3 /* { dg-skip-if "Pinning not implemented on this host" { ! *-*-linux-gnu* } } */
4
5 /* Test that ompx_gnu_pinned_mem_alloc works. */
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);
70
71 // Sanity check
72 if (get_pinned_mem () != 0)
73 abort ();
74
75 void *p = omp_alloc (SIZE, ompx_gnu_pinned_mem_alloc);
76 if (!p)
77 abort ();
78
79 int amount = get_pinned_mem ();
80 if (amount == 0)
81 abort ();
82
83 p = omp_realloc (p, SIZE * 2, ompx_gnu_pinned_mem_alloc,
84 ompx_gnu_pinned_mem_alloc);
85
86 int amount2 = get_pinned_mem ();
87 if (amount2 <= amount)
88 abort ();
89
90 /* SIZE*2 ensures that it doesn't slot into the space possibly
91 vacated by realloc. */
92 p = omp_calloc (1, SIZE * 2, ompx_gnu_pinned_mem_alloc);
93
94 if (get_pinned_mem () <= amount2)
95 abort ();
96
97 verify0 (p, SIZE * 2);
98
99 return 0;
100 }