| 1 |
/* { dg-do run } */ |
| 2 |
|
| 3 |
/* { dg-skip-if "Pinning not implemented on this host" { ! *-*-linux-gnu* } } */ |
| 4 |
|
| 5 |
/* Test that pinned memory 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); // This is intended to help diagnose failures |
| 70 |
|
| 71 |
const omp_alloctrait_t traits[] = { |
| 72 |
{ omp_atk_pinned, 1 } |
| 73 |
}; |
| 74 |
omp_allocator_handle_t allocator = omp_init_allocator (omp_default_mem_space, |
| 75 |
1, traits); |
| 76 |
|
| 77 |
#ifdef EXPECT_OMP_NULL_ALLOCATOR |
| 78 |
if (allocator == omp_null_allocator) |
| 79 |
return 0; |
| 80 |
#endif |
| 81 |
|
| 82 |
// Sanity check |
| 83 |
if (get_pinned_mem () != 0) |
| 84 |
abort (); |
| 85 |
|
| 86 |
void *p = omp_alloc (SIZE, allocator); |
| 87 |
if (!p) |
| 88 |
abort (); |
| 89 |
|
| 90 |
int amount = get_pinned_mem (); |
| 91 |
if (amount == 0) |
| 92 |
abort (); |
| 93 |
|
| 94 |
p = omp_realloc (p, SIZE * 2, allocator, allocator); |
| 95 |
|
| 96 |
int amount2 = get_pinned_mem (); |
| 97 |
if (amount2 <= amount) |
| 98 |
abort (); |
| 99 |
|
| 100 |
/* SIZE*2 ensures that it doesn't slot into the space possibly |
| 101 |
vacated by realloc. */ |
| 102 |
p = omp_calloc (1, SIZE * 2, allocator); |
| 103 |
|
| 104 |
if (get_pinned_mem () <= amount2) |
| 105 |
abort (); |
| 106 |
|
| 107 |
verify0 (p, SIZE * 2); |
| 108 |
|
| 109 |
return 0; |
| 110 |
} |