Debian Codegraph @

tests/linkhuge.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
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25
26 #include "hugetests.h"
27
28 #define BLOCK_SIZE 16384
29 #define CONST 0xdeadbeef
30
31 #define BIG_INIT { \
32 [0] = CONST, [17] = CONST, [BLOCK_SIZE-1] = CONST, \
33 }
34 static int small_data = 1;
35 static int big_data[BLOCK_SIZE] = BIG_INIT;
36
37 static int small_bss;
38 static int big_bss[BLOCK_SIZE];
39
40 const int small_const = CONST;
41 const int big_const[BLOCK_SIZE] = BIG_INIT;
42
43 static int static_func(int x)
44 {
45 return x;
46 }
47
48 int global_func(int x)
49 {
50 return x;
51 }
52
53 static struct test_entry {
54 const char *name;
55 void *data;
56 int size;
57 char linkchar;
58 int writable, execable;
59 int is_huge;
60 } testtab[] = {
61 #define RWENT(name, linkchar) { #name, &name, sizeof(name), linkchar, 1, 0, }
62 #define ROENT(name, linkchar) { #name, (void *)&name, sizeof(name), linkchar, 0, 0, }
63 #define RXENT(name, linkchar) { #name, &name, sizeof(name), linkchar, 0, 1, }
64 RWENT(small_data, 'D'),
65 RWENT(big_data, 'D'),
66 RWENT(small_bss, 'B'),
67 RWENT(big_bss, 'B'),
68 ROENT(small_const, 'T'),
69 ROENT(big_const, 'T'),
70 RXENT(static_func, 'T'),
71 RXENT(global_func, 'T'),
72 };
73
74 #define NUM_TESTS (sizeof(testtab) / sizeof(testtab[0]))
75
76 static char link_string[32];
77
78 static void get_link_string(const char *argv0)
79 {
80 const char *p, *q;
81
82 /* Find program basename */
83 p = strrchr(argv0, '/');
84 if (p)
85 p++;
86 else
87 p = argv0;
88
89 if (*p != 'x')
90 return; /* just a plain ordinary link */
91
92 q = strchr(p, '.');
93 if (!q)
94 /* ERROR? */
95 return;
96
97 memcpy(link_string, p, q-p);
98 }
99
100 static void do_test(struct test_entry *te)
101 {
102 int i;
103 volatile int *p = te->data;
104
105 if (te->writable) {
106 for (i = 0; i < (te->size / sizeof(*p)); i++)
107 p[i] = CONST ^ i;
108
109 barrier();
110
111 for (i = 0; i < (te->size / sizeof(*p)); i++)
112 if (p[i] != (CONST ^ i))
113 FAIL("mismatch on %s", te->name);
114 } else if (te->execable) {
115 int (*pf)(int) = te->data;
116
117 if ((*pf)(CONST) != CONST)
118 FAIL("%s returns incorrect results", te->name);
119 } else {
120 /* Otherwise just read touch it */
121 for (i = 0; i < (te->size / sizeof(*p)); i++)
122 p[i];
123 }
124
125 te->is_huge = (test_addr_huge(te->data) == 1);
126 }
127
128 int main(int argc, char *argv[])
129 {
130 int i;
131 char *env;
132 int elfmap_inhibited;
133
134 test_init(argc, argv);
135
136 get_link_string(argv[0]);
137
138 env = getenv("HUGETLB_ELFMAP");
139
140 verbose_printf("Link string is [%s], HUGETLB_ELFMAP=%s\n",
141 link_string, env);
142
143 elfmap_inhibited = env && (strcasecmp(env, "no") == 0);
144
145 for (i = 0; i < NUM_TESTS; i++) {
146 do_test(testtab + i);
147 }
148
149 verbose_printf("Hugepages used for:");
150 for (i = 0; i < NUM_TESTS; i++)
151 if (testtab[i].is_huge)
152 verbose_printf(" %s", testtab[i].name);
153 verbose_printf("\n");
154
155 for (i = 0; i < NUM_TESTS; i++) {
156 char linkchar = testtab[i].linkchar;
157
158 if (elfmap_inhibited) {
159 if (testtab[i].is_huge)
160 FAIL("%s is hugepage despite HUGETLB_ELFMAP=%s\n",
161 testtab[i].name, env);
162 } else {
163 if (linkchar && strchr(link_string, linkchar)) {
164 if (! testtab[i].is_huge)
165 FAIL("%s is not hugepage\n",
166 testtab[i].name);
167 }
168 if (linkchar && !strchr(link_string, linkchar)) {
169 if (testtab[i].is_huge)
170 FAIL("%s is hugepage\n",
171 testtab[i].name);
172 }
173 }
174 }
175 PASS();
176 }