Debian Codegraph @

tests/shm-gettest.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 <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 /* Global Configuration */
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 }