| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <sys/types.h> |
| 20 |
#include <sys/shm.h> |
| 21 |
#include <sys/types.h> |
| 22 |
#include <sys/mman.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <hugetlbfs.h> |
| 29 |
#include "hugetests.h" |
| 30 |
|
| 31 |
extern int errno; |
| 32 |
|
| 33 |
|
| 34 |
#define P "shm-gettest" |
| 35 |
#define DESC \ |
| 36 |
"* A looping test to verify the functionality of large page backed *\n"\ |
| 37 |
"* shared memory segments. A segment is created, written, *\n"\ |
| 38 |
"* verified, and detached a specified number of times. *" |
| 39 |
|
| 40 |
static unsigned int iter; |
| 41 |
static int nr_hugepages; |
| 42 |
static int shmid = -1; |
| 43 |
static long hpage_size; |
| 44 |
|
| 45 |
void cleanup(void) |
| 46 |
{ |
| 47 |
remove_shmid(shmid); |
| 48 |
} |
| 49 |
|
| 50 |
static void do_one(size_t size) { |
| 51 |
size_t i,j; |
| 52 |
char pattern; |
| 53 |
char *shmaddr; |
| 54 |
|
| 55 |
verbose_printf("Requesting %zu bytes\n", size); |
| 56 |
|
| 57 |
if ((shmid = shmget(2, size, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W )) < 0) |
| 58 |
FAIL("shmget(): %s", strerror(errno)); |
| 59 |
|
| 60 |
verbose_printf("shmid: 0x%x\n", shmid); |
| 61 |
|
| 62 |
shmaddr = shmat(shmid, 0, SHM_RND) ; |
| 63 |
if (shmaddr == MAP_FAILED) |
| 64 |
FAIL("shmat(): %s", strerror(errno)); |
| 65 |
|
| 66 |
verbose_printf("shmaddr: %p\n", shmaddr); |
| 67 |
|
| 68 |
for (i = 0; i < nr_hugepages; i++) { |
| 69 |
pattern = 65+(i%26); |
| 70 |
verbose_printf("Touching %p with %c\n", shmaddr+(i*hpage_size),pattern); |
| 71 |
memset(shmaddr+(i*hpage_size), pattern, hpage_size); |
| 72 |
} |
| 73 |
|
| 74 |
for (i = 0; i < nr_hugepages; i++) { |
| 75 |
pattern = 65+(i%26); |
| 76 |
verbose_printf("Verifying %p\n", (shmaddr+(i*hpage_size))); |
| 77 |
for (j = 0; j < hpage_size; j++) |
| 78 |
if (*(shmaddr+(i*hpage_size)+j) != pattern) |
| 79 |
FAIL("Verifying the segment failed. " |
| 80 |
"Got %c, expected %c", |
| 81 |
*(shmaddr+(i*hpage_size)+j), pattern); |
| 82 |
} |
| 83 |
|
| 84 |
if (shmdt((const void *)shmaddr) != 0) |
| 85 |
FAIL("shmdt(): %s", strerror(errno)); |
| 86 |
} |
| 87 |
|
| 88 |
int main(int argc, char ** argv) |
| 89 |
{ |
| 90 |
size_t size; |
| 91 |
int i; |
| 92 |
|
| 93 |
test_init(argc, argv); |
| 94 |
|
| 95 |
if (argc < 3) |
| 96 |
CONFIG("Usage: shmgettest <# iterations> <# pages>\n"); |
| 97 |
|
| 98 |
check_hugetlb_shm_group(); |
| 99 |
|
| 100 |
iter = atoi(argv[1]); |
| 101 |
nr_hugepages = atoi(argv[2]); |
| 102 |
|
| 103 |
hpage_size = gethugepagesize(); |
| 104 |
size = nr_hugepages * hpage_size; |
| 105 |
|
| 106 |
for (i=0; i < iter; i++) |
| 107 |
do_one(size); |
| 108 |
|
| 109 |
PASS(); |
| 110 |
} |