Debian Codegraph @

psm_diags.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2013. Intel Corporation. All rights reserved.
3 * Copyright (c) 2006-2012. QLogic Corporation. All rights reserved.
4 * Copyright (c) 2003-2006, PathScale, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35 #include "psm_user.h"
36 #include "psm_mq_internal.h"
37
38 typedef void (*memcpy_fn_t)(void *dst, const void *src, size_t n);
39 static int psmi_test_memcpy(memcpy_fn_t, const char *name);
40 static int psmi_test_epid_table(int numelems);
41
42 int psmi_diags(void);
43
44 #define diags_assert(x) do { \
45 if (!(x)) { \
46 _IPATH_ERROR("Diags assertion failure: %s\n", \
47 #x); \
48 goto fail; \
49 } \
50 } while (0)
51
52 #define DIAGS_RETURN_PASS(str) \
53 do { _IPATH_INFO("%s: PASSED %s\n", __func__, str); return 0; } \
54 while (0)
55 #define DIAGS_RETURN_FAIL(str) \
56 do { _IPATH_INFO("%s: FAILED %s\n", __func__, str); return 1; } \
57 while (0)
58
59 int
60 psmi_diags(void)
61 {
62 int ret = 0;
63 ret |= psmi_test_epid_table(2048);
64 ret |= psmi_test_memcpy((memcpy_fn_t) psmi_memcpyo, "psmi_memcpyo");
65 //ret |= psmi_test_memcpy((memcpy_fn_t) psmi_mq_mtucpy, "psmi_mq_mtucpy");
66
67 if (ret)
68 DIAGS_RETURN_FAIL("");
69 else
70 DIAGS_RETURN_PASS("");
71 }
72
73 /*
74 * Hash table test
75 */
76 #define NALLOC 1024
77 static int
78 psmi_test_epid_table(int numelems)
79 {
80 psm_epaddr_t *ep_array, epaddr, ep_alloc;
81 psm_epid_t *epid_array, epid_tmp;
82 psm_ep_t ep = (psm_ep_t) (uintptr_t) 0xabcdef00;
83 struct psmi_epid_table *tab;
84 int i, j;
85
86 ep_alloc = (psm_epaddr_t) psmi_calloc(PSMI_EP_NONE, UNDEFINED, numelems, sizeof(struct psm_epaddr));
87 ep_array = (psm_epaddr_t *) psmi_calloc(PSMI_EP_NONE, UNDEFINED, numelems, sizeof(struct psm_epaddr *));
88 epid_array = (psm_epid_t *) psmi_calloc(PSMI_EP_NONE, UNDEFINED, numelems, sizeof(psm_epid_t));
89 diags_assert(ep_alloc != NULL);
90 diags_assert(ep_array != NULL);
91 diags_assert(epid_array != NULL);
92
93 srand(12345678);
94
95 psmi_epid_init();
96 tab = &psmi_epid_table;
97
98 for (i = 0; i < numelems; i++) {
99 epid_array[i] = i;
100 ep_alloc[i].ep = ep;
101 ep_alloc[i].epid = epid_array[i];
102 ep_array[i] = &ep_alloc[i];
103 }
104 for (i = 0 ; i < numelems; i++) {
105 psmi_epid_add(ep, epid_array[i], ep_array[i]);
106 }
107
108 /* Randomize epid_array */
109 for (i = 0; i < numelems; i++) {
110 j = rand() % numelems;
111 epid_tmp = epid_array[i];
112 epid_array[i] = epid_array[j];
113 epid_array[j] = epid_tmp;
114 }
115 /* Lookup. */
116 for (i = 0; i < numelems; i++) {
117 epaddr = psmi_epid_lookup(ep, epid_array[i]);
118 diags_assert(epaddr != NULL);
119 diags_assert(epaddr->epid == epid_array[i]);
120 diags_assert(epaddr->ep == ep);
121 }
122
123 /* Randomize epid_array again */
124 for (i = 0; i < numelems; i++) {
125 j = rand() % numelems;
126 epid_tmp = epid_array[i];
127 epid_array[i] = epid_array[j];
128 epid_array[j] = epid_tmp;
129 }
130 /* Delete half */
131 for (i = 0; i < numelems/2; i++) {
132 epaddr = psmi_epid_remove(ep, epid_array[i]);
133 diags_assert(epaddr != NULL);
134 diags_assert(epaddr->epid == epid_array[i]);
135 diags_assert(epaddr->ep == ep);
136 }
137 /* Lookup other half -- expect non-NULL, then delete */
138 for (i = numelems/2; i < numelems; i++) {
139 epaddr = psmi_epid_lookup(ep, epid_array[i]);
140 diags_assert(epaddr != NULL);
141 diags_assert(epaddr->epid == epid_array[i]);
142 diags_assert(epaddr->ep == ep);
143 epaddr = psmi_epid_remove(ep, epid_array[i]);
144 epaddr = psmi_epid_lookup(ep, epid_array[i]);
145 diags_assert(epaddr == NULL);
146 }
147 /* Lookup whole thing, expect done */
148 for (i = 0; i < numelems; i++) {
149 epaddr = psmi_epid_lookup(ep, epid_array[i]);
150 diags_assert(epaddr == NULL);
151 }
152 for (i = 0; i < tab->tabsize; i++) {
153 diags_assert(tab->table[i].entry == NULL ||
154 tab->table[i].entry == EPADDR_DELETED);
155 }
156
157 /* Make sure we're not leaking memory somewhere... */
158 diags_assert(tab->tabsize > tab->tabsize_used &&
159 tab->tabsize * PSMI_EPID_TABLOAD_FACTOR >
160 tab->tabsize_used);
161
162 /* Only free on success */
163 psmi_epid_fini();
164 psmi_free(epid_array);
165 psmi_free(ep_array);
166 psmi_free(ep_alloc);
167 DIAGS_RETURN_PASS("");
168
169 fail:
170 /* Klocwork scan report memory leak. */
171 psmi_epid_fini();
172 if (epid_array) psmi_free(epid_array);
173 if (ep_array) psmi_free(ep_array);
174 if (ep_alloc) psmi_free(ep_alloc);
175 DIAGS_RETURN_FAIL("");
176 }
177
178 /*
179 * Memcpy correctness test
180 */
181 static int memcpy_check_size (memcpy_fn_t fn, int *p, int *f, size_t n);
182 static void *memcpy_check_one (memcpy_fn_t fn, void *dst, void *src, size_t n);
183
184 static int
185 psmi_test_memcpy(memcpy_fn_t fn, const char *memcpy_name)
186 {
187 const int CORNERS = 0;
188 const long long lo = 1;
189 const long long hi = 16 * 1024 * 1024;
190 const long long below = 32;
191 const long long above = 32;
192 long long n, m;
193 char buf[128];
194 int ret = 0;
195 int memcpy_passed;
196 int memcpy_failed;
197
198 memcpy_passed = 0;
199 memcpy_failed = 0;
200
201 ret = memcpy_check_size(fn, &memcpy_passed, &memcpy_failed, 0);
202 if (ret < 0)
203 DIAGS_RETURN_FAIL("no heap space");
204
205 for (n = lo; n <= hi; n <<= 1) {
206 _IPATH_INFO("%s %d align=0..16\n", memcpy_name, (int) n);
207 for (m = n - below; m <= n + above; m++) {
208 if (m == n) {
209 ret = memcpy_check_size(fn, &memcpy_passed, &memcpy_failed, n);
210 if (ret < 0)
211 DIAGS_RETURN_FAIL("no heap space");
212 }
213 else if (CORNERS && m >= lo && m <= hi && m > (n >> 1) &&
214 m < max(n, ((n << 1) - below)))
215 {
216 ret = memcpy_check_size(fn, &memcpy_passed, &memcpy_failed, (size_t) m);
217 if (ret < 0)
218 DIAGS_RETURN_FAIL("no heap space");
219 }
220 }
221 }
222
223 int total = memcpy_passed + memcpy_failed;
224 if (total > 0) {
225 _IPATH_INFO("%d memcpy tests with %d passed (%.2f%%) "
226 "and %d failed (%.2f%%)\n",
227 total, memcpy_passed, (100.0 * memcpy_passed) / total,
228 memcpy_failed, (100.0 * memcpy_failed) / total);
229 }
230 if (memcpy_failed) {
231 snprintf(buf, sizeof buf, "%s %.2f%% of tests memcpy_failed",
232 memcpy_name, (100.0 * memcpy_failed) / total);
233 DIAGS_RETURN_FAIL(buf);
234 }
235 else {
236 DIAGS_RETURN_PASS(memcpy_name);
237 }
238 }
239
240 void *memcpy_check_one (memcpy_fn_t fn, void *dst, void *src, size_t n)
241 {
242 int ok = 1;
243 unsigned int seed = (unsigned int)
244 ((uintptr_t) dst ^ (uintptr_t) src ^ (uintptr_t) n);
245 unsigned int state;
246 size_t i;
247 psmi_assert_always(n > 0);
248 memset(src, 0x55, n);
249 memset(dst, 0xaa, n);
250 srand(seed);
251 state = seed;
252 for (i = 0; i < n; i++) {
253 ((uint8_t *) src)[i] = (rand_r(&state) >> 16) & 0xff;
254 }
255
256 fn(dst, src, n);
257 memset(src, 0, n);
258 srand(seed);
259 state = seed;
260 for (i = 0; i < n; i++) {
261 int value = (int) (uint8_t) (rand_r(&state) >> 16);
262 int v = (int) ((uint8_t *) dst)[i];
263 if (v != value) {
264 _IPATH_ERROR("Error on index %llu : got %d instead of %d\n",
265 (unsigned long long) i, v, value);
266 ok = 0;
267 }
268 }
269 return ok ? dst : NULL;
270 }
271
272 int
273 memcpy_check_size (memcpy_fn_t fn, int *p, int *f, size_t n)
274 {
275 #define num_aligns 16
276 #define USE_MALLOC 0
277 #define DEBUG 0
278 uint8_t *src;
279 uint8_t *dst;
280 size_t size = n * 2 + num_aligns;
281 if (USE_MALLOC) {
282 src = psmi_malloc(PSMI_EP_NONE, UNDEFINED, size);
283 dst = psmi_malloc(PSMI_EP_NONE, UNDEFINED, size);
284 if (src == NULL || dst == NULL) {
285 if (src) psmi_free(src);
286 if (dst) psmi_free(dst);
287 return -1;
288 }
289 }
290 else {
291 void *src_p = NULL, *dst_p = NULL;
292 if (posix_memalign(&src_p, 64, size) != 0 ||
293 posix_memalign(&dst_p, 64, size) != 0) {
294 if (src_p) psmi_free(src_p);
295 if (dst_p) psmi_free(dst_p);
296 return -1;
297 }
298 else {
299 src = (uint8_t *) src_p;
300 dst = (uint8_t *) dst_p;
301 }
302 }
303 int src_align, dst_align;
304 for (src_align = 0; src_align < num_aligns; src_align++) {
305 for (dst_align = 0; dst_align < num_aligns; dst_align++) {
306 uint8_t *d = ((uint8_t *) dst) + dst_align;
307 uint8_t *s = ((uint8_t *) src) + src_align;
308 int ok = (memcpy_check_one(fn, d, s, n) != NULL);
309 if (DEBUG || !ok) {
310 _IPATH_INFO("memcpy(%p, %p, %llu) : %s\n", d, s,
311 (unsigned long long) n,
312 ok ? "passed" : "failed");
313 }
314 if (ok) {
315 (*p)++;
316 }
317 else {
318 (*f)++;
319 }
320 }
321 }
322 psmi_free(src);
323 psmi_free(dst);
324 return 0;
325 }