| 1 |
/* { dg-do run } */ |
| 2 |
|
| 3 |
/* Test that ompx_gnu_pinned_mem_alloc 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 |
|
| 52 |
int |
| 53 |
get_pinned_mem () |
| 54 |
{ |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
void |
| 59 |
set_pin_limit (int size) |
| 60 |
{ |
| 61 |
} |
| 62 |
#endif |
| 63 |
|
| 64 |
#include <omp.h> |
| 65 |
|
| 66 |
int |
| 67 |
main () |
| 68 |
{ |
| 69 |
/* Allocate at least a page each time, but stay within the ulimit. */ |
| 70 |
const int SIZE = PAGE_SIZE * 4; |
| 71 |
|
| 72 |
/* Ensure that the limit is smaller than the allocation. */ |
| 73 |
set_pin_limit (SIZE / 2); |
| 74 |
|
| 75 |
// Sanity check |
| 76 |
if (get_pinned_mem () != 0) |
| 77 |
abort (); |
| 78 |
|
| 79 |
// Should fail |
| 80 |
void *p = omp_alloc (SIZE, ompx_gnu_pinned_mem_alloc); |
| 81 |
if (p) |
| 82 |
abort (); |
| 83 |
|
| 84 |
// Should fail |
| 85 |
p = omp_calloc (1, SIZE, ompx_gnu_pinned_mem_alloc); |
| 86 |
if (p) |
| 87 |
abort (); |
| 88 |
|
| 89 |
// Should fail to realloc |
| 90 |
void *notpinned = omp_alloc (SIZE, omp_default_mem_alloc); |
| 91 |
p = omp_realloc (notpinned, SIZE, ompx_gnu_pinned_mem_alloc, |
| 92 |
omp_default_mem_alloc); |
| 93 |
if (!notpinned || p) |
| 94 |
abort (); |
| 95 |
|
| 96 |
// No memory should have been pinned |
| 97 |
int amount = get_pinned_mem (); |
| 98 |
if (amount != 0) |
| 99 |
abort (); |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |