Debian Codegraph @

tests/shm-getraw.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 /* Global Configuration */
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 /* Read a page from device and write to shm segment */
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 }