| 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 <sys/stat.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <fcntl.h> |
| 30 |
#include <hugetlbfs.h> |
| 31 |
#include "hugetests.h" |
| 32 |
|
| 33 |
extern int errno; |
| 34 |
|
| 35 |
|
| 36 |
#define P "shm-getraw" |
| 37 |
#define DESC \ |
| 38 |
"* This test exercizes the code path which performs raw device IO *\n"\ |
| 39 |
"* into a large page backed shared memory segment. The specified *\n"\ |
| 40 |
"* device will be read into a shared memory segment. *" |
| 41 |
|
| 42 |
static int nr_hugepages; |
| 43 |
static int shmid = -1; |
| 44 |
|
| 45 |
void cleanup(void) |
| 46 |
{ |
| 47 |
remove_shmid(shmid); |
| 48 |
} |
| 49 |
|
| 50 |
int main(int argc, char ** argv) |
| 51 |
{ |
| 52 |
size_t size; |
| 53 |
size_t i; |
| 54 |
long hpage_size = check_hugepagesize(); |
| 55 |
volatile char *shmaddr; |
| 56 |
char *buffer; |
| 57 |
int raw_fd; |
| 58 |
|
| 59 |
test_init(argc, argv); |
| 60 |
|
| 61 |
check_hugetlb_shm_group(); |
| 62 |
|
| 63 |
if (argc < 3) |
| 64 |
CONFIG("Usage: %s <# pages> <device>", argv[0]); |
| 65 |
|
| 66 |
nr_hugepages = atoi(argv[1]); |
| 67 |
|
| 68 |
verbose_printf("hpage_size is: %ld\n", hpage_size); |
| 69 |
|
| 70 |
buffer = malloc(hpage_size*sizeof(char)); |
| 71 |
if (!buffer) |
| 72 |
FAIL("malloc(%li)", hpage_size*sizeof(char)); |
| 73 |
|
| 74 |
raw_fd = open(argv[2], O_RDONLY); |
| 75 |
if (!raw_fd) |
| 76 |
CONFIG("Cannot open raw device: %s", strerror(errno)); |
| 77 |
|
| 78 |
size = hpage_size * nr_hugepages; |
| 79 |
|
| 80 |
verbose_printf("Requesting %zu bytes\n", size); |
| 81 |
|
| 82 |
if ((shmid = shmget(2, size, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W )) < 0) |
| 83 |
FAIL("shmget(): %s", strerror(errno)); |
| 84 |
|
| 85 |
verbose_printf("shmid: 0x%x\n", shmid); |
| 86 |
shmaddr = shmat(shmid, 0, SHM_RND) ; |
| 87 |
if (shmaddr == MAP_FAILED) |
| 88 |
FAIL("shmat() failed: %s", strerror(errno)); |
| 89 |
|
| 90 |
verbose_printf("shmaddr: %p\n", shmaddr); |
| 91 |
|
| 92 |
|
| 93 |
for (i = 0; i < size; i+=hpage_size) { |
| 94 |
if (!read(raw_fd, buffer, hpage_size)) |
| 95 |
FAIL("Can't read from raw device: %s", |
| 96 |
strerror(errno)); |
| 97 |
memcpy((char*)(shmaddr + i), buffer, hpage_size); |
| 98 |
} |
| 99 |
|
| 100 |
verbose_printf("Done.\n"); |
| 101 |
if (shmdt((const void *)shmaddr) != 0) |
| 102 |
FAIL("shmdt() failed: %s", strerror(errno)); |
| 103 |
|
| 104 |
free(buffer); |
| 105 |
PASS(); |
| 106 |
} |