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