]> git.proxmox.com Git - mirror_spl.git/blame - module/splat/splat-kmem.c
splat kmem:slab_reclaim: Test cleanup
[mirror_spl.git] / module / splat / splat-kmem.c
CommitLineData
716154c5
BB
1/*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
716154c5
BB
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
715f6251 15 *
716154c5 16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting LAyer Tests (SPLAT) Kmem Tests.
25\*****************************************************************************/
715f6251 26
df870a69
BB
27#include <sys/kmem.h>
28#include <sys/thread.h>
7c50328b 29#include "splat-internal.h"
f1ca4da6 30
7c50328b 31#define SPLAT_KMEM_NAME "kmem"
32#define SPLAT_KMEM_DESC "Kernel Malloc/Slab Tests"
f1ca4da6 33
7c50328b 34#define SPLAT_KMEM_TEST1_ID 0x0101
35#define SPLAT_KMEM_TEST1_NAME "kmem_alloc"
36#define SPLAT_KMEM_TEST1_DESC "Memory allocation test (kmem_alloc)"
f1ca4da6 37
7c50328b 38#define SPLAT_KMEM_TEST2_ID 0x0102
39#define SPLAT_KMEM_TEST2_NAME "kmem_zalloc"
40#define SPLAT_KMEM_TEST2_DESC "Memory allocation test (kmem_zalloc)"
f1ca4da6 41
7c50328b 42#define SPLAT_KMEM_TEST3_ID 0x0103
2fb9b26a 43#define SPLAT_KMEM_TEST3_NAME "vmem_alloc"
44#define SPLAT_KMEM_TEST3_DESC "Memory allocation test (vmem_alloc)"
f1ca4da6 45
7c50328b 46#define SPLAT_KMEM_TEST4_ID 0x0104
2fb9b26a 47#define SPLAT_KMEM_TEST4_NAME "vmem_zalloc"
48#define SPLAT_KMEM_TEST4_DESC "Memory allocation test (vmem_zalloc)"
f1ca4da6 49
79b31f36 50#define SPLAT_KMEM_TEST5_ID 0x0105
ea3e6ca9 51#define SPLAT_KMEM_TEST5_NAME "slab_small"
2fb9b26a 52#define SPLAT_KMEM_TEST5_DESC "Slab ctor/dtor test (small)"
53
54#define SPLAT_KMEM_TEST6_ID 0x0106
ea3e6ca9 55#define SPLAT_KMEM_TEST6_NAME "slab_large"
2fb9b26a 56#define SPLAT_KMEM_TEST6_DESC "Slab ctor/dtor test (large)"
57
58#define SPLAT_KMEM_TEST7_ID 0x0107
ea3e6ca9
BB
59#define SPLAT_KMEM_TEST7_NAME "slab_align"
60#define SPLAT_KMEM_TEST7_DESC "Slab alignment test"
79b31f36 61
44b8f176 62#define SPLAT_KMEM_TEST8_ID 0x0108
ea3e6ca9
BB
63#define SPLAT_KMEM_TEST8_NAME "slab_reap"
64#define SPLAT_KMEM_TEST8_DESC "Slab reaping test"
44b8f176 65
48e0606a 66#define SPLAT_KMEM_TEST9_ID 0x0109
ea3e6ca9
BB
67#define SPLAT_KMEM_TEST9_NAME "slab_age"
68#define SPLAT_KMEM_TEST9_DESC "Slab aging test"
69
70#define SPLAT_KMEM_TEST10_ID 0x010a
71#define SPLAT_KMEM_TEST10_NAME "slab_lock"
72#define SPLAT_KMEM_TEST10_DESC "Slab locking test"
73
11124863 74#if 0
ea3e6ca9
BB
75#define SPLAT_KMEM_TEST11_ID 0x010b
76#define SPLAT_KMEM_TEST11_NAME "slab_overcommit"
77#define SPLAT_KMEM_TEST11_DESC "Slab memory overcommit test"
11124863 78#endif
48e0606a 79
e11d6c5f
BB
80#define SPLAT_KMEM_TEST12_ID 0x010c
81#define SPLAT_KMEM_TEST12_NAME "vmem_size"
82#define SPLAT_KMEM_TEST12_DESC "Memory zone test"
83
a9a7a01c
PS
84#define SPLAT_KMEM_TEST13_ID 0x010d
85#define SPLAT_KMEM_TEST13_NAME "slab_reclaim"
86#define SPLAT_KMEM_TEST13_DESC "Slab direct memory reclaim test"
87
7c50328b 88#define SPLAT_KMEM_ALLOC_COUNT 10
79b31f36 89#define SPLAT_VMEM_ALLOC_COUNT 10
90
44b8f176 91
f1ca4da6 92static int
7c50328b 93splat_kmem_test1(struct file *file, void *arg)
f1ca4da6 94{
7c50328b 95 void *ptr[SPLAT_KMEM_ALLOC_COUNT];
f1ca4da6 96 int size = PAGE_SIZE;
97 int i, count, rc = 0;
98
79b31f36 99 while ((!rc) && (size <= (PAGE_SIZE * 32))) {
f1ca4da6 100 count = 0;
101
7c50328b 102 for (i = 0; i < SPLAT_KMEM_ALLOC_COUNT; i++) {
23d91792 103 ptr[i] = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
f1ca4da6 104 if (ptr[i])
105 count++;
106 }
107
7c50328b 108 for (i = 0; i < SPLAT_KMEM_ALLOC_COUNT; i++)
f1ca4da6 109 if (ptr[i])
110 kmem_free(ptr[i], size);
111
7c50328b 112 splat_vprint(file, SPLAT_KMEM_TEST1_NAME,
ea3e6ca9
BB
113 "%d byte allocations, %d/%d successful\n",
114 size, count, SPLAT_KMEM_ALLOC_COUNT);
7c50328b 115 if (count != SPLAT_KMEM_ALLOC_COUNT)
f1ca4da6 116 rc = -ENOMEM;
117
118 size *= 2;
119 }
120
121 return rc;
122}
123
124static int
7c50328b 125splat_kmem_test2(struct file *file, void *arg)
f1ca4da6 126{
7c50328b 127 void *ptr[SPLAT_KMEM_ALLOC_COUNT];
f1ca4da6 128 int size = PAGE_SIZE;
129 int i, j, count, rc = 0;
130
79b31f36 131 while ((!rc) && (size <= (PAGE_SIZE * 32))) {
f1ca4da6 132 count = 0;
133
7c50328b 134 for (i = 0; i < SPLAT_KMEM_ALLOC_COUNT; i++) {
23d91792 135 ptr[i] = kmem_zalloc(size, KM_SLEEP | KM_NODEBUG);
f1ca4da6 136 if (ptr[i])
137 count++;
138 }
139
140 /* Ensure buffer has been zero filled */
7c50328b 141 for (i = 0; i < SPLAT_KMEM_ALLOC_COUNT; i++) {
f1ca4da6 142 for (j = 0; j < size; j++) {
143 if (((char *)ptr[i])[j] != '\0') {
5198ea0e 144 splat_vprint(file,SPLAT_KMEM_TEST2_NAME,
ea3e6ca9
BB
145 "%d-byte allocation was "
146 "not zeroed\n", size);
f1ca4da6 147 rc = -EFAULT;
148 }
149 }
150 }
151
7c50328b 152 for (i = 0; i < SPLAT_KMEM_ALLOC_COUNT; i++)
f1ca4da6 153 if (ptr[i])
154 kmem_free(ptr[i], size);
155
7c50328b 156 splat_vprint(file, SPLAT_KMEM_TEST2_NAME,
ea3e6ca9
BB
157 "%d byte allocations, %d/%d successful\n",
158 size, count, SPLAT_KMEM_ALLOC_COUNT);
7c50328b 159 if (count != SPLAT_KMEM_ALLOC_COUNT)
f1ca4da6 160 rc = -ENOMEM;
161
162 size *= 2;
163 }
164
165 return rc;
166}
167
2fb9b26a 168static int
169splat_kmem_test3(struct file *file, void *arg)
170{
171 void *ptr[SPLAT_VMEM_ALLOC_COUNT];
172 int size = PAGE_SIZE;
173 int i, count, rc = 0;
174
175 while ((!rc) && (size <= (PAGE_SIZE * 1024))) {
176 count = 0;
177
178 for (i = 0; i < SPLAT_VMEM_ALLOC_COUNT; i++) {
179 ptr[i] = vmem_alloc(size, KM_SLEEP);
180 if (ptr[i])
181 count++;
182 }
183
184 for (i = 0; i < SPLAT_VMEM_ALLOC_COUNT; i++)
185 if (ptr[i])
186 vmem_free(ptr[i], size);
187
188 splat_vprint(file, SPLAT_KMEM_TEST3_NAME,
ea3e6ca9
BB
189 "%d byte allocations, %d/%d successful\n",
190 size, count, SPLAT_VMEM_ALLOC_COUNT);
2fb9b26a 191 if (count != SPLAT_VMEM_ALLOC_COUNT)
192 rc = -ENOMEM;
193
194 size *= 2;
195 }
196
197 return rc;
198}
199
200static int
201splat_kmem_test4(struct file *file, void *arg)
202{
203 void *ptr[SPLAT_VMEM_ALLOC_COUNT];
204 int size = PAGE_SIZE;
205 int i, j, count, rc = 0;
206
207 while ((!rc) && (size <= (PAGE_SIZE * 1024))) {
208 count = 0;
209
210 for (i = 0; i < SPLAT_VMEM_ALLOC_COUNT; i++) {
211 ptr[i] = vmem_zalloc(size, KM_SLEEP);
212 if (ptr[i])
213 count++;
214 }
215
216 /* Ensure buffer has been zero filled */
217 for (i = 0; i < SPLAT_VMEM_ALLOC_COUNT; i++) {
218 for (j = 0; j < size; j++) {
219 if (((char *)ptr[i])[j] != '\0') {
220 splat_vprint(file, SPLAT_KMEM_TEST4_NAME,
ea3e6ca9
BB
221 "%d-byte allocation was "
222 "not zeroed\n", size);
2fb9b26a 223 rc = -EFAULT;
224 }
225 }
226 }
227
228 for (i = 0; i < SPLAT_VMEM_ALLOC_COUNT; i++)
229 if (ptr[i])
230 vmem_free(ptr[i], size);
231
232 splat_vprint(file, SPLAT_KMEM_TEST4_NAME,
ea3e6ca9
BB
233 "%d byte allocations, %d/%d successful\n",
234 size, count, SPLAT_VMEM_ALLOC_COUNT);
2fb9b26a 235 if (count != SPLAT_VMEM_ALLOC_COUNT)
236 rc = -ENOMEM;
237
238 size *= 2;
239 }
240
241 return rc;
242}
243
7c50328b 244#define SPLAT_KMEM_TEST_MAGIC 0x004488CCUL
245#define SPLAT_KMEM_CACHE_NAME "kmem_test"
ea3e6ca9 246#define SPLAT_KMEM_OBJ_COUNT 1024
668d2a0d 247#define SPLAT_KMEM_OBJ_RECLAIM 32 /* objects */
ea3e6ca9
BB
248#define SPLAT_KMEM_THREADS 32
249
250#define KCP_FLAG_READY 0x01
f1ca4da6 251
252typedef struct kmem_cache_data {
f1ca4da6 253 unsigned long kcd_magic;
efcd0ca3 254 struct list_head kcd_node;
f1ca4da6 255 int kcd_flag;
2fb9b26a 256 char kcd_buf[0];
f1ca4da6 257} kmem_cache_data_t;
258
ea3e6ca9 259typedef struct kmem_cache_thread {
ea3e6ca9
BB
260 spinlock_t kct_lock;
261 int kct_id;
efcd0ca3 262 struct list_head kct_list;
ea3e6ca9
BB
263} kmem_cache_thread_t;
264
f1ca4da6 265typedef struct kmem_cache_priv {
266 unsigned long kcp_magic;
267 struct file *kcp_file;
268 kmem_cache_t *kcp_cache;
44b8f176 269 spinlock_t kcp_lock;
ea3e6ca9
BB
270 wait_queue_head_t kcp_ctl_waitq;
271 wait_queue_head_t kcp_thr_waitq;
272 int kcp_flags;
273 int kcp_kct_count;
274 kmem_cache_thread_t *kcp_kct[SPLAT_KMEM_THREADS];
2fb9b26a 275 int kcp_size;
48e0606a 276 int kcp_align;
f1ca4da6 277 int kcp_count;
44b8f176 278 int kcp_alloc;
f1ca4da6 279 int kcp_rc;
280} kmem_cache_priv_t;
281
ea3e6ca9
BB
282static kmem_cache_priv_t *
283splat_kmem_cache_test_kcp_alloc(struct file *file, char *name,
efcd0ca3 284 int size, int align, int alloc)
ea3e6ca9
BB
285{
286 kmem_cache_priv_t *kcp;
287
efcd0ca3 288 kcp = kmem_zalloc(sizeof(kmem_cache_priv_t), KM_SLEEP);
ea3e6ca9
BB
289 if (!kcp)
290 return NULL;
291
292 kcp->kcp_magic = SPLAT_KMEM_TEST_MAGIC;
293 kcp->kcp_file = file;
294 kcp->kcp_cache = NULL;
295 spin_lock_init(&kcp->kcp_lock);
296 init_waitqueue_head(&kcp->kcp_ctl_waitq);
297 init_waitqueue_head(&kcp->kcp_thr_waitq);
298 kcp->kcp_flags = 0;
299 kcp->kcp_kct_count = -1;
300 kcp->kcp_size = size;
301 kcp->kcp_align = align;
302 kcp->kcp_count = 0;
303 kcp->kcp_alloc = alloc;
304 kcp->kcp_rc = 0;
ea3e6ca9
BB
305
306 return kcp;
307}
308
309static void
310splat_kmem_cache_test_kcp_free(kmem_cache_priv_t *kcp)
311{
efcd0ca3 312 kmem_free(kcp, sizeof(kmem_cache_priv_t));
ea3e6ca9
BB
313}
314
315static kmem_cache_thread_t *
efcd0ca3 316splat_kmem_cache_test_kct_alloc(kmem_cache_priv_t *kcp, int id)
ea3e6ca9
BB
317{
318 kmem_cache_thread_t *kct;
319
320 ASSERTF(id < SPLAT_KMEM_THREADS, "id=%d\n", id);
efcd0ca3
BB
321 ASSERT(kcp->kcp_kct[id] == NULL);
322
323 kct = kmem_zalloc(sizeof(kmem_cache_thread_t), KM_SLEEP);
ea3e6ca9
BB
324 if (!kct)
325 return NULL;
326
327 spin_lock_init(&kct->kct_lock);
ea3e6ca9 328 kct->kct_id = id;
efcd0ca3
BB
329 INIT_LIST_HEAD(&kct->kct_list);
330
331 spin_lock(&kcp->kcp_lock);
332 kcp->kcp_kct[id] = kct;
333 spin_unlock(&kcp->kcp_lock);
ea3e6ca9
BB
334
335 return kct;
336}
337
338static void
efcd0ca3
BB
339splat_kmem_cache_test_kct_free(kmem_cache_priv_t *kcp,
340 kmem_cache_thread_t *kct)
341{
342 spin_lock(&kcp->kcp_lock);
343 kcp->kcp_kct[kct->kct_id] = NULL;
344 spin_unlock(&kcp->kcp_lock);
345
346 kmem_free(kct, sizeof(kmem_cache_thread_t));
347}
348
349static void
350splat_kmem_cache_test_kcd_free(kmem_cache_priv_t *kcp,
351 kmem_cache_thread_t *kct)
352{
353 kmem_cache_data_t *kcd;
354
355 spin_lock(&kct->kct_lock);
356 while (!list_empty(&kct->kct_list)) {
357 kcd = list_entry(kct->kct_list.next,
358 kmem_cache_data_t, kcd_node);
359 list_del(&kcd->kcd_node);
360 spin_unlock(&kct->kct_lock);
361
362 kmem_cache_free(kcp->kcp_cache, kcd);
363
364 spin_lock(&kct->kct_lock);
365 }
366 spin_unlock(&kct->kct_lock);
367}
368
369static int
370splat_kmem_cache_test_kcd_alloc(kmem_cache_priv_t *kcp,
371 kmem_cache_thread_t *kct, int count)
ea3e6ca9 372{
efcd0ca3
BB
373 kmem_cache_data_t *kcd;
374 int i;
375
376 for (i = 0; i < count; i++) {
377 kcd = kmem_cache_alloc(kcp->kcp_cache, KM_SLEEP);
378 if (kcd == NULL) {
379 splat_kmem_cache_test_kcd_free(kcp, kct);
380 return -ENOMEM;
381 }
382
383 spin_lock(&kct->kct_lock);
384 list_add_tail(&kcd->kcd_node, &kct->kct_list);
385 spin_unlock(&kct->kct_lock);
386 }
387
388 return 0;
ea3e6ca9
BB
389}
390
a9a7a01c
PS
391static void
392splat_kmem_cache_test_debug(struct file *file, char *name,
393 kmem_cache_priv_t *kcp)
394{
395 int j;
396
397 splat_vprint(file, name,
398 "%s cache objects %d, slabs %u/%u objs %u/%u mags ",
399 kcp->kcp_cache->skc_name, kcp->kcp_count,
400 (unsigned)kcp->kcp_cache->skc_slab_alloc,
401 (unsigned)kcp->kcp_cache->skc_slab_total,
402 (unsigned)kcp->kcp_cache->skc_obj_alloc,
403 (unsigned)kcp->kcp_cache->skc_obj_total);
404
405 for_each_online_cpu(j)
406 splat_print(file, "%u/%u ",
407 kcp->kcp_cache->skc_mag[j]->skm_avail,
408 kcp->kcp_cache->skc_mag[j]->skm_size);
409
410 splat_print(file, "%s\n", "");
411}
412
f1ca4da6 413static int
2fb9b26a 414splat_kmem_cache_test_constructor(void *ptr, void *priv, int flags)
f1ca4da6 415{
f1ca4da6 416 kmem_cache_priv_t *kcp = (kmem_cache_priv_t *)priv;
2fb9b26a 417 kmem_cache_data_t *kcd = (kmem_cache_data_t *)ptr;
f1ca4da6 418
0498e6c5 419 if (kcd && kcp) {
420 kcd->kcd_magic = kcp->kcp_magic;
efcd0ca3 421 INIT_LIST_HEAD(&kcd->kcd_node);
2fb9b26a 422 kcd->kcd_flag = 1;
0498e6c5 423 memset(kcd->kcd_buf, 0xaa, kcp->kcp_size - (sizeof *kcd));
424 kcp->kcp_count++;
f1ca4da6 425 }
426
427 return 0;
428}
429
430static void
2fb9b26a 431splat_kmem_cache_test_destructor(void *ptr, void *priv)
f1ca4da6 432{
f1ca4da6 433 kmem_cache_priv_t *kcp = (kmem_cache_priv_t *)priv;
2fb9b26a 434 kmem_cache_data_t *kcd = (kmem_cache_data_t *)ptr;
f1ca4da6 435
0498e6c5 436 if (kcd && kcp) {
437 kcd->kcd_magic = 0;
2fb9b26a 438 kcd->kcd_flag = 0;
0498e6c5 439 memset(kcd->kcd_buf, 0xbb, kcp->kcp_size - (sizeof *kcd));
440 kcp->kcp_count--;
f1ca4da6 441 }
442
443 return;
444}
445
ea3e6ca9
BB
446/*
447 * Generic reclaim function which assumes that all objects may
448 * be reclaimed at any time. We free a small percentage of the
449 * objects linked off the kcp or kct[] every time we are called.
450 */
451static void
452splat_kmem_cache_test_reclaim(void *priv)
453{
454 kmem_cache_priv_t *kcp = (kmem_cache_priv_t *)priv;
455 kmem_cache_thread_t *kct;
efcd0ca3
BB
456 kmem_cache_data_t *kcd;
457 LIST_HEAD(reclaim);
458 int i, count;
ea3e6ca9
BB
459
460 ASSERT(kcp->kcp_magic == SPLAT_KMEM_TEST_MAGIC);
ea3e6ca9 461
efcd0ca3 462 /* For each kct thread reclaim some objects */
ea3e6ca9 463 spin_lock(&kcp->kcp_lock);
efcd0ca3 464 for (i = 0; i < SPLAT_KMEM_THREADS; i++) {
ea3e6ca9 465 kct = kcp->kcp_kct[i];
efcd0ca3 466 if (!kct)
ea3e6ca9
BB
467 continue;
468
efcd0ca3 469 spin_unlock(&kcp->kcp_lock);
ea3e6ca9 470 spin_lock(&kct->kct_lock);
ea3e6ca9 471
efcd0ca3
BB
472 count = SPLAT_KMEM_OBJ_RECLAIM;
473 while (count > 0 && !list_empty(&kct->kct_list)) {
474 kcd = list_entry(kct->kct_list.next,
475 kmem_cache_data_t, kcd_node);
476 list_del(&kcd->kcd_node);
477 list_add(&kcd->kcd_node, &reclaim);
478 count--;
ea3e6ca9 479 }
efcd0ca3 480
ea3e6ca9 481 spin_unlock(&kct->kct_lock);
efcd0ca3
BB
482 spin_lock(&kcp->kcp_lock);
483 }
484 spin_unlock(&kcp->kcp_lock);
485
486 /* Freed outside the spin lock */
487 while (!list_empty(&reclaim)) {
488 kcd = list_entry(reclaim.next, kmem_cache_data_t, kcd_node);
489 list_del(&kcd->kcd_node);
490 kmem_cache_free(kcp->kcp_cache, kcd);
ea3e6ca9
BB
491 }
492
493 return;
494}
495
496static int
497splat_kmem_cache_test_threads(kmem_cache_priv_t *kcp, int threads)
498{
499 int rc;
500
501 spin_lock(&kcp->kcp_lock);
502 rc = (kcp->kcp_kct_count == threads);
503 spin_unlock(&kcp->kcp_lock);
504
505 return rc;
506}
507
508static int
509splat_kmem_cache_test_flags(kmem_cache_priv_t *kcp, int flags)
510{
511 int rc;
512
513 spin_lock(&kcp->kcp_lock);
514 rc = (kcp->kcp_flags & flags);
515 spin_unlock(&kcp->kcp_lock);
516
517 return rc;
518}
519
520static void
521splat_kmem_cache_test_thread(void *arg)
522{
523 kmem_cache_priv_t *kcp = (kmem_cache_priv_t *)arg;
524 kmem_cache_thread_t *kct;
efcd0ca3 525 int rc = 0, id;
ea3e6ca9
BB
526
527 ASSERT(kcp->kcp_magic == SPLAT_KMEM_TEST_MAGIC);
528
529 /* Assign thread ids */
530 spin_lock(&kcp->kcp_lock);
531 if (kcp->kcp_kct_count == -1)
532 kcp->kcp_kct_count = 0;
533
534 id = kcp->kcp_kct_count;
535 kcp->kcp_kct_count++;
536 spin_unlock(&kcp->kcp_lock);
537
efcd0ca3 538 kct = splat_kmem_cache_test_kct_alloc(kcp, id);
ea3e6ca9
BB
539 if (!kct) {
540 rc = -ENOMEM;
541 goto out;
542 }
543
ea3e6ca9
BB
544 /* Wait for all threads to have started and report they are ready */
545 if (kcp->kcp_kct_count == SPLAT_KMEM_THREADS)
546 wake_up(&kcp->kcp_ctl_waitq);
547
548 wait_event(kcp->kcp_thr_waitq,
549 splat_kmem_cache_test_flags(kcp, KCP_FLAG_READY));
550
efcd0ca3
BB
551 /* Create and destroy objects */
552 rc = splat_kmem_cache_test_kcd_alloc(kcp, kct, kcp->kcp_alloc);
553 splat_kmem_cache_test_kcd_free(kcp, kct);
ea3e6ca9 554out:
efcd0ca3
BB
555 if (kct)
556 splat_kmem_cache_test_kct_free(kcp, kct);
ea3e6ca9 557
efcd0ca3 558 spin_lock(&kcp->kcp_lock);
ea3e6ca9
BB
559 if (!kcp->kcp_rc)
560 kcp->kcp_rc = rc;
561
562 if ((--kcp->kcp_kct_count) == 0)
563 wake_up(&kcp->kcp_ctl_waitq);
564
565 spin_unlock(&kcp->kcp_lock);
566
567 thread_exit();
568}
569
f1ca4da6 570static int
48e0606a 571splat_kmem_cache_test(struct file *file, void *arg, char *name,
ea3e6ca9 572 int size, int align, int flags)
f1ca4da6 573{
ea3e6ca9 574 kmem_cache_priv_t *kcp;
efcd0ca3 575 kmem_cache_data_t *kcd = NULL;
f1ca4da6 576 int rc = 0, max;
577
efcd0ca3 578 kcp = splat_kmem_cache_test_kcp_alloc(file, name, size, align, 0);
ea3e6ca9
BB
579 if (!kcp) {
580 splat_vprint(file, name, "Unable to create '%s'\n", "kcp");
581 return -ENOMEM;
582 }
583
584 kcp->kcp_cache =
585 kmem_cache_create(SPLAT_KMEM_CACHE_NAME,
586 kcp->kcp_size, kcp->kcp_align,
587 splat_kmem_cache_test_constructor,
588 splat_kmem_cache_test_destructor,
589 NULL, kcp, NULL, flags);
590 if (!kcp->kcp_cache) {
2fb9b26a 591 splat_vprint(file, name,
ea3e6ca9 592 "Unable to create '%s'\n",
3f412673 593 SPLAT_KMEM_CACHE_NAME);
ea3e6ca9
BB
594 rc = -ENOMEM;
595 goto out_free;
f1ca4da6 596 }
597
ea3e6ca9 598 kcd = kmem_cache_alloc(kcp->kcp_cache, KM_SLEEP);
f1ca4da6 599 if (!kcd) {
2fb9b26a 600 splat_vprint(file, name,
ea3e6ca9
BB
601 "Unable to allocate from '%s'\n",
602 SPLAT_KMEM_CACHE_NAME);
f1ca4da6 603 rc = -EINVAL;
604 goto out_free;
605 }
606
efcd0ca3 607 if (!kcd->kcd_flag) {
2fb9b26a 608 splat_vprint(file, name,
ea3e6ca9
BB
609 "Failed to run contructor for '%s'\n",
610 SPLAT_KMEM_CACHE_NAME);
f1ca4da6 611 rc = -EINVAL;
612 goto out_free;
613 }
614
efcd0ca3 615 if (kcd->kcd_magic != kcp->kcp_magic) {
2fb9b26a 616 splat_vprint(file, name,
ea3e6ca9
BB
617 "Failed to pass private data to constructor "
618 "for '%s'\n", SPLAT_KMEM_CACHE_NAME);
f1ca4da6 619 rc = -EINVAL;
620 goto out_free;
621 }
622
ea3e6ca9 623 max = kcp->kcp_count;
efcd0ca3 624 kmem_cache_free(kcp->kcp_cache, kcd);
f1ca4da6 625
626 /* Destroy the entire cache which will force destructors to
627 * run and we can verify one was called for every object */
ea3e6ca9
BB
628 kmem_cache_destroy(kcp->kcp_cache);
629 if (kcp->kcp_count) {
2fb9b26a 630 splat_vprint(file, name,
ea3e6ca9
BB
631 "Failed to run destructor on all slab objects "
632 "for '%s'\n", SPLAT_KMEM_CACHE_NAME);
f1ca4da6 633 rc = -EINVAL;
634 }
635
f250d90b 636 splat_kmem_cache_test_kcp_free(kcp);
2fb9b26a 637 splat_vprint(file, name,
ea3e6ca9
BB
638 "Successfully ran ctors/dtors for %d elements in '%s'\n",
639 max, SPLAT_KMEM_CACHE_NAME);
f1ca4da6 640
641 return rc;
642
643out_free:
efcd0ca3
BB
644 if (kcd)
645 kmem_cache_free(kcp->kcp_cache, kcd);
ea3e6ca9
BB
646
647 if (kcp->kcp_cache)
648 kmem_cache_destroy(kcp->kcp_cache);
649
650 splat_kmem_cache_test_kcp_free(kcp);
651
652 return rc;
653}
654
655static int
656splat_kmem_cache_thread_test(struct file *file, void *arg, char *name,
10a4be0f 657 int size, int alloc, int max_time)
ea3e6ca9
BB
658{
659 kmem_cache_priv_t *kcp;
660 kthread_t *thr;
661 struct timespec start, stop, delta;
662 char cache_name[32];
663 int i, rc = 0;
664
efcd0ca3 665 kcp = splat_kmem_cache_test_kcp_alloc(file, name, size, 0, alloc);
ea3e6ca9
BB
666 if (!kcp) {
667 splat_vprint(file, name, "Unable to create '%s'\n", "kcp");
668 return -ENOMEM;
669 }
670
671 (void)snprintf(cache_name, 32, "%s-%d-%d",
672 SPLAT_KMEM_CACHE_NAME, size, alloc);
673 kcp->kcp_cache =
674 kmem_cache_create(cache_name, kcp->kcp_size, 0,
675 splat_kmem_cache_test_constructor,
676 splat_kmem_cache_test_destructor,
677 splat_kmem_cache_test_reclaim,
3c9ce2bf 678 kcp, NULL, 0);
ea3e6ca9
BB
679 if (!kcp->kcp_cache) {
680 splat_vprint(file, name, "Unable to create '%s'\n", cache_name);
681 rc = -ENOMEM;
682 goto out_kcp;
683 }
684
df2c0f18 685 getnstimeofday(&start);
ea3e6ca9
BB
686
687 for (i = 0; i < SPLAT_KMEM_THREADS; i++) {
688 thr = thread_create(NULL, 0,
689 splat_kmem_cache_test_thread,
690 kcp, 0, &p0, TS_RUN, minclsyspri);
691 if (thr == NULL) {
692 rc = -ESRCH;
693 goto out_cache;
694 }
695 }
696
697 /* Sleep until all threads have started, then set the ready
698 * flag and wake them all up for maximum concurrency. */
699 wait_event(kcp->kcp_ctl_waitq,
700 splat_kmem_cache_test_threads(kcp, SPLAT_KMEM_THREADS));
701
702 spin_lock(&kcp->kcp_lock);
703 kcp->kcp_flags |= KCP_FLAG_READY;
704 spin_unlock(&kcp->kcp_lock);
705 wake_up_all(&kcp->kcp_thr_waitq);
706
707 /* Sleep until all thread have finished */
708 wait_event(kcp->kcp_ctl_waitq, splat_kmem_cache_test_threads(kcp, 0));
709
df2c0f18 710 getnstimeofday(&stop);
ea3e6ca9 711 delta = timespec_sub(stop, start);
f1b59d26 712
ea3e6ca9
BB
713 splat_vprint(file, name,
714 "%-22s %2ld.%09ld\t"
715 "%lu/%lu/%lu\t%lu/%lu/%lu\n",
716 kcp->kcp_cache->skc_name,
717 delta.tv_sec, delta.tv_nsec,
718 (unsigned long)kcp->kcp_cache->skc_slab_total,
719 (unsigned long)kcp->kcp_cache->skc_slab_max,
720 (unsigned long)(kcp->kcp_alloc *
721 SPLAT_KMEM_THREADS /
722 SPL_KMEM_CACHE_OBJ_PER_SLAB),
723 (unsigned long)kcp->kcp_cache->skc_obj_total,
724 (unsigned long)kcp->kcp_cache->skc_obj_max,
725 (unsigned long)(kcp->kcp_alloc *
726 SPLAT_KMEM_THREADS));
727
10a4be0f 728 if (delta.tv_sec >= max_time)
ea3e6ca9
BB
729 rc = -ETIME;
730
731 if (!rc && kcp->kcp_rc)
732 rc = kcp->kcp_rc;
733
734out_cache:
735 kmem_cache_destroy(kcp->kcp_cache);
736out_kcp:
737 splat_kmem_cache_test_kcp_free(kcp);
f1ca4da6 738 return rc;
739}
740
a1502d76 741/* Validate small object cache behavior for dynamic/kmem/vmem caches */
2fb9b26a 742static int
743splat_kmem_test5(struct file *file, void *arg)
744{
a1502d76 745 char *name = SPLAT_KMEM_TEST5_NAME;
746 int rc;
747
ceb38728 748 /* On slab (default + kmem + vmem) */
48e0606a 749 rc = splat_kmem_cache_test(file, arg, name, 128, 0, 0);
a1502d76 750 if (rc)
751 return rc;
752
48e0606a 753 rc = splat_kmem_cache_test(file, arg, name, 128, 0, KMC_KMEM);
a1502d76 754 if (rc)
755 return rc;
756
ceb38728
BB
757 rc = splat_kmem_cache_test(file, arg, name, 128, 0, KMC_VMEM);
758 if (rc)
759 return rc;
760
761 /* Off slab (default + kmem + vmem) */
762 rc = splat_kmem_cache_test(file, arg, name, 128, 0, KMC_OFFSLAB);
763 if (rc)
764 return rc;
765
766 rc = splat_kmem_cache_test(file, arg, name, 128, 0,
767 KMC_KMEM | KMC_OFFSLAB);
768 if (rc)
769 return rc;
770
771 rc = splat_kmem_cache_test(file, arg, name, 128, 0,
772 KMC_VMEM | KMC_OFFSLAB);
773
774 return rc;
2fb9b26a 775}
776
efcd0ca3
BB
777/*
778 * Validate large object cache behavior for dynamic/kmem/vmem caches
779 */
2fb9b26a 780static int
781splat_kmem_test6(struct file *file, void *arg)
782{
a1502d76 783 char *name = SPLAT_KMEM_TEST6_NAME;
784 int rc;
785
ceb38728 786 /* On slab (default + kmem + vmem) */
e0dcb22e 787 rc = splat_kmem_cache_test(file, arg, name, 256*1024, 0, 0);
a1502d76 788 if (rc)
789 return rc;
790
e0dcb22e 791 rc = splat_kmem_cache_test(file, arg, name, 64*1024, 0, KMC_KMEM);
a1502d76 792 if (rc)
793 return rc;
794
ceb38728
BB
795 rc = splat_kmem_cache_test(file, arg, name, 1024*1024, 0, KMC_VMEM);
796 if (rc)
797 return rc;
798
799 /* Off slab (default + kmem + vmem) */
800 rc = splat_kmem_cache_test(file, arg, name, 256*1024, 0, KMC_OFFSLAB);
801 if (rc)
802 return rc;
803
804 rc = splat_kmem_cache_test(file, arg, name, 64*1024, 0,
805 KMC_KMEM | KMC_OFFSLAB);
806 if (rc)
807 return rc;
808
809 rc = splat_kmem_cache_test(file, arg, name, 1024*1024, 0,
810 KMC_VMEM | KMC_OFFSLAB);
811
812 return rc;
2fb9b26a 813}
814
efcd0ca3
BB
815/*
816 * Validate object alignment cache behavior for caches
817 */
ea3e6ca9
BB
818static int
819splat_kmem_test7(struct file *file, void *arg)
f1ca4da6 820{
ea3e6ca9
BB
821 char *name = SPLAT_KMEM_TEST7_NAME;
822 int i, rc;
2fb9b26a 823
8b45dda2 824 for (i = SPL_KMEM_CACHE_ALIGN; i <= PAGE_SIZE; i *= 2) {
ea3e6ca9
BB
825 rc = splat_kmem_cache_test(file, arg, name, 157, i, 0);
826 if (rc)
827 return rc;
ceb38728
BB
828
829 rc = splat_kmem_cache_test(file, arg, name, 157, i,
830 KMC_OFFSLAB);
831 if (rc)
832 return rc;
f1ca4da6 833 }
834
ea3e6ca9 835 return rc;
f1ca4da6 836}
837
efcd0ca3
BB
838/*
839 * Validate kmem_cache_reap() by requesting the slab cache free any objects
840 * it can. For a few reasons this may not immediately result in more free
841 * memory even if objects are freed. First off, due to fragmentation we
842 * may not be able to reclaim any slabs. Secondly, even if we do we fully
843 * clear some slabs we will not want to immediately reclaim all of them
844 * because we may contend with cache allocations and thrash. What we want
845 * to see is the slab size decrease more gradually as it becomes clear they
846 * will not be needed. This should be achievable in less than a minute.
847 * If it takes longer than this something has gone wrong.
848 */
f1ca4da6 849static int
ea3e6ca9 850splat_kmem_test8(struct file *file, void *arg)
f1ca4da6 851{
ea3e6ca9 852 kmem_cache_priv_t *kcp;
efcd0ca3 853 kmem_cache_thread_t *kct;
0936c344 854 unsigned int spl_kmem_cache_expire_old;
a9a7a01c 855 int i, rc = 0;
ea3e6ca9 856
0936c344
BB
857 /* Enable cache aging just for this test if it is disabled */
858 spl_kmem_cache_expire_old = spl_kmem_cache_expire;
859 spl_kmem_cache_expire = KMC_EXPIRE_AGE;
860
ea3e6ca9 861 kcp = splat_kmem_cache_test_kcp_alloc(file, SPLAT_KMEM_TEST8_NAME,
efcd0ca3 862 256, 0, 0);
ea3e6ca9
BB
863 if (!kcp) {
864 splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
865 "Unable to create '%s'\n", "kcp");
efcd0ca3
BB
866 rc = -ENOMEM;
867 goto out;
f1ca4da6 868 }
869
ea3e6ca9
BB
870 kcp->kcp_cache =
871 kmem_cache_create(SPLAT_KMEM_CACHE_NAME, kcp->kcp_size, 0,
872 splat_kmem_cache_test_constructor,
873 splat_kmem_cache_test_destructor,
874 splat_kmem_cache_test_reclaim,
875 kcp, NULL, 0);
876 if (!kcp->kcp_cache) {
ea3e6ca9
BB
877 splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
878 "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME);
efcd0ca3
BB
879 rc = -ENOMEM;
880 goto out_kcp;
ea3e6ca9 881 }
f1ca4da6 882
efcd0ca3
BB
883 kct = splat_kmem_cache_test_kct_alloc(kcp, 0);
884 if (!kct) {
885 splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
886 "Unable to create '%s'\n", "kct");
887 rc = -ENOMEM;
888 goto out_cache;
889 }
890
891 rc = splat_kmem_cache_test_kcd_alloc(kcp, kct, SPLAT_KMEM_OBJ_COUNT);
892 if (rc) {
893 splat_vprint(file, SPLAT_KMEM_TEST8_NAME, "Unable to "
894 "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME);
895 goto out_kct;
f1ca4da6 896 }
897
668d2a0d
BB
898 /* Force reclaim every 1/10 a second for 60 seconds. */
899 for (i = 0; i < 600; i++) {
ea3e6ca9 900 kmem_cache_reap_now(kcp->kcp_cache);
a9a7a01c 901 splat_kmem_cache_test_debug(file, SPLAT_KMEM_TEST8_NAME, kcp);
ea3e6ca9
BB
902
903 if (kcp->kcp_cache->skc_obj_total == 0)
2fb9b26a 904 break;
905
906 set_current_state(TASK_INTERRUPTIBLE);
668d2a0d 907 schedule_timeout(HZ / 10);
2fb9b26a 908 }
909
ea3e6ca9
BB
910 if (kcp->kcp_cache->skc_obj_total == 0) {
911 splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
2fb9b26a 912 "Successfully created %d objects "
913 "in cache %s and reclaimed them\n",
ea3e6ca9 914 SPLAT_KMEM_OBJ_COUNT, SPLAT_KMEM_CACHE_NAME);
2fb9b26a 915 } else {
ea3e6ca9 916 splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
2fb9b26a 917 "Failed to reclaim %u/%d objects from cache %s\n",
ea3e6ca9
BB
918 (unsigned)kcp->kcp_cache->skc_obj_total,
919 SPLAT_KMEM_OBJ_COUNT, SPLAT_KMEM_CACHE_NAME);
2fb9b26a 920 rc = -ENOMEM;
921 }
f1ca4da6 922
2fb9b26a 923 /* Cleanup our mess (for failure case of time expiring) */
efcd0ca3
BB
924 splat_kmem_cache_test_kcd_free(kcp, kct);
925out_kct:
926 splat_kmem_cache_test_kct_free(kcp, kct);
927out_cache:
ea3e6ca9 928 kmem_cache_destroy(kcp->kcp_cache);
efcd0ca3 929out_kcp:
ea3e6ca9 930 splat_kmem_cache_test_kcp_free(kcp);
efcd0ca3 931out:
0936c344
BB
932 spl_kmem_cache_expire = spl_kmem_cache_expire_old;
933
f1ca4da6 934 return rc;
935}
936
efcd0ca3
BB
937/* Test cache aging, we have allocated a large number of objects thus
938 * creating a large number of slabs and then free'd them all. However,
939 * since there should be little memory pressure at the moment those
940 * slabs have not been freed. What we want to see is the slab size
941 * decrease gradually as it becomes clear they will not be be needed.
942 * This should be achievable in less than minute. If it takes longer
943 * than this something has gone wrong.
944 */
ea3e6ca9
BB
945static int
946splat_kmem_test9(struct file *file, void *arg)
44b8f176 947{
ea3e6ca9 948 kmem_cache_priv_t *kcp;
efcd0ca3 949 kmem_cache_thread_t *kct;
0936c344 950 unsigned int spl_kmem_cache_expire_old;
a9a7a01c 951 int i, rc = 0, count = SPLAT_KMEM_OBJ_COUNT * 128;
ea3e6ca9 952
0936c344
BB
953 /* Enable cache aging just for this test if it is disabled */
954 spl_kmem_cache_expire_old = spl_kmem_cache_expire;
955 spl_kmem_cache_expire = KMC_EXPIRE_AGE;
956
ea3e6ca9 957 kcp = splat_kmem_cache_test_kcp_alloc(file, SPLAT_KMEM_TEST9_NAME,
efcd0ca3 958 256, 0, 0);
ea3e6ca9
BB
959 if (!kcp) {
960 splat_vprint(file, SPLAT_KMEM_TEST9_NAME,
961 "Unable to create '%s'\n", "kcp");
efcd0ca3
BB
962 rc = -ENOMEM;
963 goto out;
ea3e6ca9 964 }
44b8f176 965
ea3e6ca9
BB
966 kcp->kcp_cache =
967 kmem_cache_create(SPLAT_KMEM_CACHE_NAME, kcp->kcp_size, 0,
968 splat_kmem_cache_test_constructor,
969 splat_kmem_cache_test_destructor,
970 NULL, kcp, NULL, 0);
971 if (!kcp->kcp_cache) {
ea3e6ca9
BB
972 splat_vprint(file, SPLAT_KMEM_TEST9_NAME,
973 "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME);
efcd0ca3
BB
974 rc = -ENOMEM;
975 goto out_kcp;
44b8f176 976 }
977
efcd0ca3
BB
978 kct = splat_kmem_cache_test_kct_alloc(kcp, 0);
979 if (!kct) {
980 splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
981 "Unable to create '%s'\n", "kct");
982 rc = -ENOMEM;
983 goto out_cache;
44b8f176 984 }
985
efcd0ca3
BB
986 rc = splat_kmem_cache_test_kcd_alloc(kcp, kct, count);
987 if (rc) {
988 splat_vprint(file, SPLAT_KMEM_TEST9_NAME, "Unable to "
989 "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME);
990 goto out_kct;
991 }
992
993 splat_kmem_cache_test_kcd_free(kcp, kct);
e9d7a2be 994
ea3e6ca9 995 for (i = 0; i < 60; i++) {
a9a7a01c 996 splat_kmem_cache_test_debug(file, SPLAT_KMEM_TEST9_NAME, kcp);
ea3e6ca9
BB
997
998 if (kcp->kcp_cache->skc_obj_total == 0)
999 break;
44b8f176 1000
ea3e6ca9
BB
1001 set_current_state(TASK_INTERRUPTIBLE);
1002 schedule_timeout(HZ);
1003 }
44b8f176 1004
ea3e6ca9
BB
1005 if (kcp->kcp_cache->skc_obj_total == 0) {
1006 splat_vprint(file, SPLAT_KMEM_TEST9_NAME,
1007 "Successfully created %d objects "
1008 "in cache %s and reclaimed them\n",
1009 count, SPLAT_KMEM_CACHE_NAME);
1010 } else {
1011 splat_vprint(file, SPLAT_KMEM_TEST9_NAME,
1012 "Failed to reclaim %u/%d objects from cache %s\n",
1013 (unsigned)kcp->kcp_cache->skc_obj_total, count,
1014 SPLAT_KMEM_CACHE_NAME);
1015 rc = -ENOMEM;
1016 }
1017
efcd0ca3
BB
1018out_kct:
1019 splat_kmem_cache_test_kct_free(kcp, kct);
1020out_cache:
ea3e6ca9 1021 kmem_cache_destroy(kcp->kcp_cache);
efcd0ca3 1022out_kcp:
ea3e6ca9 1023 splat_kmem_cache_test_kcp_free(kcp);
efcd0ca3 1024out:
0936c344
BB
1025 spl_kmem_cache_expire = spl_kmem_cache_expire_old;
1026
ea3e6ca9 1027 return rc;
44b8f176 1028}
1029
ea3e6ca9
BB
1030/*
1031 * This test creates N threads with a shared kmem cache. They then all
1032 * concurrently allocate and free from the cache to stress the locking and
1033 * concurrent cache performance. If any one test takes longer than 5
1034 * seconds to complete it is treated as a failure and may indicate a
1035 * performance regression. On my test system no one test takes more
1036 * than 1 second to complete so a 5x slowdown likely a problem.
44b8f176 1037 */
1038static int
ea3e6ca9 1039splat_kmem_test10(struct file *file, void *arg)
44b8f176 1040{
e11d6c5f 1041 uint64_t size, alloc, rc = 0;
44b8f176 1042
efcd0ca3 1043 for (size = 32; size <= 1024*1024; size *= 2) {
44b8f176 1044
ea3e6ca9
BB
1045 splat_vprint(file, SPLAT_KMEM_TEST10_NAME, "%-22s %s", "name",
1046 "time (sec)\tslabs \tobjs \thash\n");
1047 splat_vprint(file, SPLAT_KMEM_TEST10_NAME, "%-22s %s", "",
1048 " \ttot/max/calc\ttot/max/calc\n");
44b8f176 1049
ea3e6ca9 1050 for (alloc = 1; alloc <= 1024; alloc *= 2) {
44b8f176 1051
e11d6c5f
BB
1052 /* Skip tests which exceed available memory. We
1053 * leverage availrmem here for some extra testing */
1054 if (size * alloc * SPLAT_KMEM_THREADS > availrmem / 2)
ea3e6ca9 1055 continue;
7ea1cbf5 1056
ea3e6ca9 1057 rc = splat_kmem_cache_thread_test(file, arg,
10a4be0f 1058 SPLAT_KMEM_TEST10_NAME, size, alloc, 5);
ea3e6ca9
BB
1059 if (rc)
1060 break;
1061 }
44b8f176 1062 }
1063
7ea1cbf5 1064 return rc;
44b8f176 1065}
1066
11124863 1067#if 0
ea3e6ca9
BB
1068/*
1069 * This test creates N threads with a shared kmem cache which overcommits
1070 * memory by 4x. This makes it impossible for the slab to satify the
1071 * thread requirements without having its reclaim hook run which will
1072 * free objects back for use. This behavior is triggered by the linum VM
1073 * detecting a low memory condition on the node and invoking the shrinkers.
1074 * This should allow all the threads to complete while avoiding deadlock
1075 * and for the most part out of memory events. This is very tough on the
4e5691fa
BB
1076 * system so it is possible the test app may get oom'ed. This particular
1077 * test has proven troublesome on 32-bit archs with limited virtual
1078 * address space so it only run on 64-bit systems.
ea3e6ca9 1079 */
fece7c99 1080static int
ea3e6ca9 1081splat_kmem_test11(struct file *file, void *arg)
fece7c99 1082{
ea3e6ca9 1083 uint64_t size, alloc, rc;
fece7c99 1084
efcd0ca3 1085 size = 8 * 1024;
e11d6c5f 1086 alloc = ((4 * physmem * PAGE_SIZE) / size) / SPLAT_KMEM_THREADS;
fece7c99 1087
e11d6c5f 1088 splat_vprint(file, SPLAT_KMEM_TEST11_NAME, "%-22s %s", "name",
ea3e6ca9 1089 "time (sec)\tslabs \tobjs \thash\n");
e11d6c5f 1090 splat_vprint(file, SPLAT_KMEM_TEST11_NAME, "%-22s %s", "",
ea3e6ca9 1091 " \ttot/max/calc\ttot/max/calc\n");
48e0606a 1092
ea3e6ca9 1093 rc = splat_kmem_cache_thread_test(file, arg,
10a4be0f 1094 SPLAT_KMEM_TEST11_NAME, size, alloc, 60);
48e0606a
BB
1095
1096 return rc;
1097}
11124863 1098#endif
48e0606a 1099
e11d6c5f
BB
1100/*
1101 * Check vmem_size() behavior by acquiring the alloc/free/total vmem
1102 * space, then allocate a known buffer size from vmem space. We can
1103 * then check that vmem_size() values were updated properly with in
1104 * a fairly small tolerence. The tolerance is important because we
1105 * are not the only vmem consumer on the system. Other unrelated
1106 * allocations might occur during the small test window. The vmem
1107 * allocation itself may also add in a little extra private space to
1108 * the buffer. Finally, verify total space always remains unchanged.
1109 */
1110static int
1111splat_kmem_test12(struct file *file, void *arg)
1112{
6ae7fef5
BB
1113 size_t alloc1, free1, total1;
1114 size_t alloc2, free2, total2;
e11d6c5f
BB
1115 int size = 8*1024*1024;
1116 void *ptr;
1117
1118 alloc1 = vmem_size(NULL, VMEM_ALLOC);
1119 free1 = vmem_size(NULL, VMEM_FREE);
1120 total1 = vmem_size(NULL, VMEM_ALLOC | VMEM_FREE);
6ae7fef5
BB
1121 splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Vmem alloc=%lu "
1122 "free=%lu total=%lu\n", (unsigned long)alloc1,
1123 (unsigned long)free1, (unsigned long)total1);
e11d6c5f
BB
1124
1125 splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Alloc %d bytes\n", size);
1126 ptr = vmem_alloc(size, KM_SLEEP);
1127 if (!ptr) {
1128 splat_vprint(file, SPLAT_KMEM_TEST12_NAME,
1129 "Failed to alloc %d bytes\n", size);
1130 return -ENOMEM;
1131 }
1132
1133 alloc2 = vmem_size(NULL, VMEM_ALLOC);
1134 free2 = vmem_size(NULL, VMEM_FREE);
1135 total2 = vmem_size(NULL, VMEM_ALLOC | VMEM_FREE);
6ae7fef5
BB
1136 splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Vmem alloc=%lu "
1137 "free=%lu total=%lu\n", (unsigned long)alloc2,
1138 (unsigned long)free2, (unsigned long)total2);
e11d6c5f
BB
1139
1140 splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Free %d bytes\n", size);
1141 vmem_free(ptr, size);
1142 if (alloc2 < (alloc1 + size - (size / 100)) ||
1143 alloc2 > (alloc1 + size + (size / 100))) {
6ae7fef5
BB
1144 splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Failed "
1145 "VMEM_ALLOC size: %lu != %lu+%d (+/- 1%%)\n",
1146 (unsigned long)alloc2,(unsigned long)alloc1,size);
e11d6c5f
BB
1147 return -ERANGE;
1148 }
1149
1150 if (free2 < (free1 - size - (size / 100)) ||
1151 free2 > (free1 - size + (size / 100))) {
6ae7fef5
BB
1152 splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Failed "
1153 "VMEM_FREE size: %lu != %lu-%d (+/- 1%%)\n",
1154 (unsigned long)free2, (unsigned long)free1, size);
e11d6c5f
BB
1155 return -ERANGE;
1156 }
1157
1158 if (total1 != total2) {
6ae7fef5
BB
1159 splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Failed "
1160 "VMEM_ALLOC | VMEM_FREE not constant: "
1161 "%lu != %lu\n", (unsigned long)total2,
1162 (unsigned long)total1);
e11d6c5f
BB
1163 return -ERANGE;
1164 }
1165
1166 splat_vprint(file, SPLAT_KMEM_TEST12_NAME,
6ae7fef5
BB
1167 "VMEM_ALLOC within tolerance: ~%ld%% (%ld/%d)\n",
1168 (long)abs(alloc1 + (long)size - alloc2) * 100 / (long)size,
1169 (long)abs(alloc1 + (long)size - alloc2), size);
e11d6c5f 1170 splat_vprint(file, SPLAT_KMEM_TEST12_NAME,
6ae7fef5
BB
1171 "VMEM_FREE within tolerance: ~%ld%% (%ld/%d)\n",
1172 (long)abs((free1 - (long)size) - free2) * 100 / (long)size,
1173 (long)abs((free1 - (long)size) - free2), size);
e11d6c5f
BB
1174
1175 return 0;
1176}
1177
a9a7a01c
PS
1178typedef struct dummy_page {
1179 struct list_head dp_list;
1180 char dp_pad[PAGE_SIZE - sizeof(struct list_head)];
1181} dummy_page_t;
1182
1183/*
1184 * This test is designed to verify that direct reclaim is functioning as
1185 * expected. We allocate a large number of objects thus creating a large
1186 * number of slabs. We then apply memory pressure and expect that the
1187 * direct reclaim path can easily recover those slabs. The registered
1188 * reclaim function will free the objects and the slab shrinker will call
1189 * it repeatedly until at least a single slab can be freed.
1190 *
1191 * Note it may not be possible to reclaim every last slab via direct reclaim
1192 * without a failure because the shrinker_rwsem may be contended. For this
1193 * reason, quickly reclaiming 3/4 of the slabs is considered a success.
1194 *
1195 * This should all be possible within 10 seconds. For reference, on a
1196 * system with 2G of memory this test takes roughly 0.2 seconds to run.
1197 * It may take longer on larger memory systems but should still easily
1198 * complete in the alloted 10 seconds.
1199 */
1200static int
1201splat_kmem_test13(struct file *file, void *arg)
1202{
1203 kmem_cache_priv_t *kcp;
efcd0ca3 1204 kmem_cache_thread_t *kct;
a9a7a01c
PS
1205 dummy_page_t *dp;
1206 struct list_head list;
df2c0f18 1207 struct timespec start, stop, delta = { 0, 0 };
a9a7a01c
PS
1208 int size, count, slabs, fails = 0;
1209 int i, rc = 0, max_time = 10;
1210
1211 size = 128 * 1024;
1212 count = ((physmem * PAGE_SIZE) / 4 / size);
1213
1214 kcp = splat_kmem_cache_test_kcp_alloc(file, SPLAT_KMEM_TEST13_NAME,
efcd0ca3 1215 size, 0, 0);
a9a7a01c
PS
1216 if (!kcp) {
1217 splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
1218 "Unable to create '%s'\n", "kcp");
efcd0ca3
BB
1219 rc = -ENOMEM;
1220 goto out;
a9a7a01c
PS
1221 }
1222
1223 kcp->kcp_cache =
1224 kmem_cache_create(SPLAT_KMEM_CACHE_NAME, kcp->kcp_size, 0,
1225 splat_kmem_cache_test_constructor,
1226 splat_kmem_cache_test_destructor,
1227 splat_kmem_cache_test_reclaim,
1228 kcp, NULL, 0);
1229 if (!kcp->kcp_cache) {
a9a7a01c
PS
1230 splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
1231 "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME);
efcd0ca3
BB
1232 rc = -ENOMEM;
1233 goto out_kcp;
a9a7a01c
PS
1234 }
1235
efcd0ca3
BB
1236 kct = splat_kmem_cache_test_kct_alloc(kcp, 0);
1237 if (!kct) {
1238 splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
1239 "Unable to create '%s'\n", "kct");
1240 rc = -ENOMEM;
1241 goto out_cache;
1242 }
1243
1244 rc = splat_kmem_cache_test_kcd_alloc(kcp, kct, count);
1245 if (rc) {
1246 splat_vprint(file, SPLAT_KMEM_TEST13_NAME, "Unable to "
1247 "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME);
1248 goto out_kct;
a9a7a01c
PS
1249 }
1250
1251 i = 0;
1252 slabs = kcp->kcp_cache->skc_slab_total;
1253 INIT_LIST_HEAD(&list);
df2c0f18 1254 getnstimeofday(&start);
a9a7a01c 1255
efcd0ca3 1256 /* Apply memory pressure */
a9a7a01c
PS
1257 while (kcp->kcp_cache->skc_slab_total > (slabs >> 2)) {
1258
1259 if ((i % 10000) == 0)
1260 splat_kmem_cache_test_debug(
1261 file, SPLAT_KMEM_TEST13_NAME, kcp);
1262
df2c0f18
RY
1263 getnstimeofday(&stop);
1264 delta = timespec_sub(stop, start);
a9a7a01c
PS
1265 if (delta.tv_sec >= max_time) {
1266 splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
1267 "Failed to reclaim 3/4 of cache in %ds, "
1268 "%u/%u slabs remain\n", max_time,
1269 (unsigned)kcp->kcp_cache->skc_slab_total,
1270 slabs);
1271 rc = -ETIME;
1272 break;
1273 }
1274
668d2a0d 1275 dp = (dummy_page_t *)__get_free_page(GFP_KERNEL);
a9a7a01c
PS
1276 if (!dp) {
1277 fails++;
1278 splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
1279 "Failed (%d) to allocate page with %u "
1280 "slabs still in the cache\n", fails,
1281 (unsigned)kcp->kcp_cache->skc_slab_total);
1282 continue;
1283 }
1284
1285 list_add(&dp->dp_list, &list);
1286 i++;
1287 }
1288
1289 if (rc == 0)
1290 splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
1291 "Successfully created %u slabs and with %d alloc "
1292 "failures reclaimed 3/4 of them in %d.%03ds\n",
1293 slabs, fails,
1294 (int)delta.tv_sec, (int)delta.tv_nsec / 1000000);
1295
1296 /* Release memory pressure pages */
1297 while (!list_empty(&list)) {
1298 dp = list_entry(list.next, dummy_page_t, dp_list);
1299 list_del_init(&dp->dp_list);
1300 free_page((unsigned long)dp);
1301 }
1302
1303 /* Release remaining kmem cache objects */
efcd0ca3
BB
1304 splat_kmem_cache_test_kcd_free(kcp, kct);
1305out_kct:
1306 splat_kmem_cache_test_kct_free(kcp, kct);
1307out_cache:
a9a7a01c 1308 kmem_cache_destroy(kcp->kcp_cache);
efcd0ca3 1309out_kcp:
a9a7a01c 1310 splat_kmem_cache_test_kcp_free(kcp);
efcd0ca3 1311out:
a9a7a01c
PS
1312 return rc;
1313}
1314
7c50328b 1315splat_subsystem_t *
1316splat_kmem_init(void)
f1ca4da6 1317{
ea3e6ca9 1318 splat_subsystem_t *sub;
f1ca4da6 1319
ea3e6ca9
BB
1320 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
1321 if (sub == NULL)
1322 return NULL;
f1ca4da6 1323
ea3e6ca9
BB
1324 memset(sub, 0, sizeof(*sub));
1325 strncpy(sub->desc.name, SPLAT_KMEM_NAME, SPLAT_NAME_SIZE);
7c50328b 1326 strncpy(sub->desc.desc, SPLAT_KMEM_DESC, SPLAT_DESC_SIZE);
ea3e6ca9 1327 INIT_LIST_HEAD(&sub->subsystem_list);
f1ca4da6 1328 INIT_LIST_HEAD(&sub->test_list);
ea3e6ca9
BB
1329 spin_lock_init(&sub->test_lock);
1330 sub->desc.id = SPLAT_SUBSYSTEM_KMEM;
1331
1332 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST1_NAME, SPLAT_KMEM_TEST1_DESC,
1333 SPLAT_KMEM_TEST1_ID, splat_kmem_test1);
1334 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST2_NAME, SPLAT_KMEM_TEST2_DESC,
1335 SPLAT_KMEM_TEST2_ID, splat_kmem_test2);
1336 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST3_NAME, SPLAT_KMEM_TEST3_DESC,
1337 SPLAT_KMEM_TEST3_ID, splat_kmem_test3);
1338 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST4_NAME, SPLAT_KMEM_TEST4_DESC,
1339 SPLAT_KMEM_TEST4_ID, splat_kmem_test4);
1340 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST5_NAME, SPLAT_KMEM_TEST5_DESC,
1341 SPLAT_KMEM_TEST5_ID, splat_kmem_test5);
1342 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST6_NAME, SPLAT_KMEM_TEST6_DESC,
1343 SPLAT_KMEM_TEST6_ID, splat_kmem_test6);
1344 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST7_NAME, SPLAT_KMEM_TEST7_DESC,
1345 SPLAT_KMEM_TEST7_ID, splat_kmem_test7);
1346 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST8_NAME, SPLAT_KMEM_TEST8_DESC,
1347 SPLAT_KMEM_TEST8_ID, splat_kmem_test8);
1348 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST9_NAME, SPLAT_KMEM_TEST9_DESC,
1349 SPLAT_KMEM_TEST9_ID, splat_kmem_test9);
1350 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST10_NAME, SPLAT_KMEM_TEST10_DESC,
1351 SPLAT_KMEM_TEST10_ID, splat_kmem_test10);
11124863 1352#if 0
ea3e6ca9
BB
1353 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST11_NAME, SPLAT_KMEM_TEST11_DESC,
1354 SPLAT_KMEM_TEST11_ID, splat_kmem_test11);
11124863 1355#endif
e11d6c5f
BB
1356 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST12_NAME, SPLAT_KMEM_TEST12_DESC,
1357 SPLAT_KMEM_TEST12_ID, splat_kmem_test12);
a9a7a01c
PS
1358 SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST13_NAME, SPLAT_KMEM_TEST13_DESC,
1359 SPLAT_KMEM_TEST13_ID, splat_kmem_test13);
ea3e6ca9
BB
1360
1361 return sub;
f1ca4da6 1362}
1363
1364void
7c50328b 1365splat_kmem_fini(splat_subsystem_t *sub)
f1ca4da6 1366{
ea3e6ca9 1367 ASSERT(sub);
a9a7a01c 1368 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST13_ID);
e11d6c5f 1369 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST12_ID);
11124863 1370#if 0
ea3e6ca9 1371 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST11_ID);
11124863 1372#endif
ea3e6ca9
BB
1373 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST10_ID);
1374 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST9_ID);
1375 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST8_ID);
1376 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST7_ID);
1377 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST6_ID);
1378 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST5_ID);
1379 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST4_ID);
1380 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST3_ID);
1381 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST2_ID);
1382 SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST1_ID);
1383
1384 kfree(sub);
f1ca4da6 1385}
1386
1387int
7c50328b 1388splat_kmem_id(void) {
ea3e6ca9 1389 return SPLAT_SUBSYSTEM_KMEM;
f1ca4da6 1390}