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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting LAyer Tests (SPLAT) Kmem Tests.
25 \*****************************************************************************/
28 #include <sys/kmem_cache.h>
30 #include <sys/thread.h>
31 #include <sys/vmsystm.h>
32 #include "splat-internal.h"
34 #define SPLAT_KMEM_NAME "kmem"
35 #define SPLAT_KMEM_DESC "Kernel Malloc/Slab Tests"
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)"
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)"
45 #define SPLAT_KMEM_TEST3_ID 0x0103
46 #define SPLAT_KMEM_TEST3_NAME "vmem_alloc"
47 #define SPLAT_KMEM_TEST3_DESC "Memory allocation test (vmem_alloc)"
49 #define SPLAT_KMEM_TEST4_ID 0x0104
50 #define SPLAT_KMEM_TEST4_NAME "vmem_zalloc"
51 #define SPLAT_KMEM_TEST4_DESC "Memory allocation test (vmem_zalloc)"
53 #define SPLAT_KMEM_TEST5_ID 0x0105
54 #define SPLAT_KMEM_TEST5_NAME "slab_small"
55 #define SPLAT_KMEM_TEST5_DESC "Slab ctor/dtor test (small)"
57 #define SPLAT_KMEM_TEST6_ID 0x0106
58 #define SPLAT_KMEM_TEST6_NAME "slab_large"
59 #define SPLAT_KMEM_TEST6_DESC "Slab ctor/dtor test (large)"
61 #define SPLAT_KMEM_TEST7_ID 0x0107
62 #define SPLAT_KMEM_TEST7_NAME "slab_align"
63 #define SPLAT_KMEM_TEST7_DESC "Slab alignment test"
65 #define SPLAT_KMEM_TEST8_ID 0x0108
66 #define SPLAT_KMEM_TEST8_NAME "slab_reap"
67 #define SPLAT_KMEM_TEST8_DESC "Slab reaping test"
69 #define SPLAT_KMEM_TEST9_ID 0x0109
70 #define SPLAT_KMEM_TEST9_NAME "slab_age"
71 #define SPLAT_KMEM_TEST9_DESC "Slab aging test"
73 #define SPLAT_KMEM_TEST10_ID 0x010a
74 #define SPLAT_KMEM_TEST10_NAME "slab_lock"
75 #define SPLAT_KMEM_TEST10_DESC "Slab locking test"
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"
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"
87 #define SPLAT_KMEM_ALLOC_COUNT 10
88 #define SPLAT_VMEM_ALLOC_COUNT 10
92 splat_kmem_test1(struct file
*file
, void *arg
)
94 void *ptr
[SPLAT_KMEM_ALLOC_COUNT
];
98 while ((!rc
) && (size
<= (PAGE_SIZE
* 32))) {
101 for (i
= 0; i
< SPLAT_KMEM_ALLOC_COUNT
; i
++) {
102 ptr
[i
] = kmem_alloc(size
, KM_SLEEP
| KM_NODEBUG
);
107 for (i
= 0; i
< SPLAT_KMEM_ALLOC_COUNT
; i
++)
109 kmem_free(ptr
[i
], size
);
111 splat_vprint(file
, SPLAT_KMEM_TEST1_NAME
,
112 "%d byte allocations, %d/%d successful\n",
113 size
, count
, SPLAT_KMEM_ALLOC_COUNT
);
114 if (count
!= SPLAT_KMEM_ALLOC_COUNT
)
124 splat_kmem_test2(struct file
*file
, void *arg
)
126 void *ptr
[SPLAT_KMEM_ALLOC_COUNT
];
127 int size
= PAGE_SIZE
;
128 int i
, j
, count
, rc
= 0;
130 while ((!rc
) && (size
<= (PAGE_SIZE
* 32))) {
133 for (i
= 0; i
< SPLAT_KMEM_ALLOC_COUNT
; i
++) {
134 ptr
[i
] = kmem_zalloc(size
, KM_SLEEP
| KM_NODEBUG
);
139 /* Ensure buffer has been zero filled */
140 for (i
= 0; i
< SPLAT_KMEM_ALLOC_COUNT
; i
++) {
141 for (j
= 0; j
< size
; j
++) {
142 if (((char *)ptr
[i
])[j
] != '\0') {
143 splat_vprint(file
,SPLAT_KMEM_TEST2_NAME
,
144 "%d-byte allocation was "
145 "not zeroed\n", size
);
151 for (i
= 0; i
< SPLAT_KMEM_ALLOC_COUNT
; i
++)
153 kmem_free(ptr
[i
], size
);
155 splat_vprint(file
, SPLAT_KMEM_TEST2_NAME
,
156 "%d byte allocations, %d/%d successful\n",
157 size
, count
, SPLAT_KMEM_ALLOC_COUNT
);
158 if (count
!= SPLAT_KMEM_ALLOC_COUNT
)
168 splat_kmem_test3(struct file
*file
, void *arg
)
170 void *ptr
[SPLAT_VMEM_ALLOC_COUNT
];
171 int size
= PAGE_SIZE
;
172 int i
, count
, rc
= 0;
174 while ((!rc
) && (size
<= (PAGE_SIZE
* 1024))) {
177 for (i
= 0; i
< SPLAT_VMEM_ALLOC_COUNT
; i
++) {
178 ptr
[i
] = vmem_alloc(size
, KM_SLEEP
);
183 for (i
= 0; i
< SPLAT_VMEM_ALLOC_COUNT
; i
++)
185 vmem_free(ptr
[i
], size
);
187 splat_vprint(file
, SPLAT_KMEM_TEST3_NAME
,
188 "%d byte allocations, %d/%d successful\n",
189 size
, count
, SPLAT_VMEM_ALLOC_COUNT
);
190 if (count
!= SPLAT_VMEM_ALLOC_COUNT
)
200 splat_kmem_test4(struct file
*file
, void *arg
)
202 void *ptr
[SPLAT_VMEM_ALLOC_COUNT
];
203 int size
= PAGE_SIZE
;
204 int i
, j
, count
, rc
= 0;
206 while ((!rc
) && (size
<= (PAGE_SIZE
* 1024))) {
209 for (i
= 0; i
< SPLAT_VMEM_ALLOC_COUNT
; i
++) {
210 ptr
[i
] = vmem_zalloc(size
, KM_SLEEP
);
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
,
220 "%d-byte allocation was "
221 "not zeroed\n", size
);
227 for (i
= 0; i
< SPLAT_VMEM_ALLOC_COUNT
; i
++)
229 vmem_free(ptr
[i
], size
);
231 splat_vprint(file
, SPLAT_KMEM_TEST4_NAME
,
232 "%d byte allocations, %d/%d successful\n",
233 size
, count
, SPLAT_VMEM_ALLOC_COUNT
);
234 if (count
!= SPLAT_VMEM_ALLOC_COUNT
)
243 #define SPLAT_KMEM_TEST_MAGIC 0x004488CCUL
244 #define SPLAT_KMEM_CACHE_NAME "kmem_test"
245 #define SPLAT_KMEM_OBJ_COUNT 1024
246 #define SPLAT_KMEM_OBJ_RECLAIM 32 /* objects */
247 #define SPLAT_KMEM_THREADS 32
249 #define KCP_FLAG_READY 0x01
251 typedef struct kmem_cache_data
{
252 unsigned long kcd_magic
;
253 struct list_head kcd_node
;
258 typedef struct kmem_cache_thread
{
261 struct list_head kct_list
;
262 } kmem_cache_thread_t
;
264 typedef struct kmem_cache_priv
{
265 unsigned long kcp_magic
;
266 struct file
*kcp_file
;
267 kmem_cache_t
*kcp_cache
;
269 wait_queue_head_t kcp_ctl_waitq
;
270 wait_queue_head_t kcp_thr_waitq
;
273 kmem_cache_thread_t
*kcp_kct
[SPLAT_KMEM_THREADS
];
281 static kmem_cache_priv_t
*
282 splat_kmem_cache_test_kcp_alloc(struct file
*file
, char *name
,
283 int size
, int align
, int alloc
)
285 kmem_cache_priv_t
*kcp
;
287 kcp
= kmem_zalloc(sizeof(kmem_cache_priv_t
), KM_SLEEP
);
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
);
298 kcp
->kcp_kct_count
= -1;
299 kcp
->kcp_size
= size
;
300 kcp
->kcp_align
= align
;
302 kcp
->kcp_alloc
= alloc
;
309 splat_kmem_cache_test_kcp_free(kmem_cache_priv_t
*kcp
)
311 kmem_free(kcp
, sizeof(kmem_cache_priv_t
));
314 static kmem_cache_thread_t
*
315 splat_kmem_cache_test_kct_alloc(kmem_cache_priv_t
*kcp
, int id
)
317 kmem_cache_thread_t
*kct
;
319 ASSERT3S(id
, <, SPLAT_KMEM_THREADS
);
320 ASSERT(kcp
->kcp_kct
[id
] == NULL
);
322 kct
= kmem_zalloc(sizeof(kmem_cache_thread_t
), KM_SLEEP
);
326 spin_lock_init(&kct
->kct_lock
);
328 INIT_LIST_HEAD(&kct
->kct_list
);
330 spin_lock(&kcp
->kcp_lock
);
331 kcp
->kcp_kct
[id
] = kct
;
332 spin_unlock(&kcp
->kcp_lock
);
338 splat_kmem_cache_test_kct_free(kmem_cache_priv_t
*kcp
,
339 kmem_cache_thread_t
*kct
)
341 spin_lock(&kcp
->kcp_lock
);
342 kcp
->kcp_kct
[kct
->kct_id
] = NULL
;
343 spin_unlock(&kcp
->kcp_lock
);
345 kmem_free(kct
, sizeof(kmem_cache_thread_t
));
349 splat_kmem_cache_test_kcd_free(kmem_cache_priv_t
*kcp
,
350 kmem_cache_thread_t
*kct
)
352 kmem_cache_data_t
*kcd
;
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
);
361 kmem_cache_free(kcp
->kcp_cache
, kcd
);
363 spin_lock(&kct
->kct_lock
);
365 spin_unlock(&kct
->kct_lock
);
369 splat_kmem_cache_test_kcd_alloc(kmem_cache_priv_t
*kcp
,
370 kmem_cache_thread_t
*kct
, int count
)
372 kmem_cache_data_t
*kcd
;
375 for (i
= 0; i
< count
; i
++) {
376 kcd
= kmem_cache_alloc(kcp
->kcp_cache
, KM_SLEEP
);
378 splat_kmem_cache_test_kcd_free(kcp
, kct
);
382 spin_lock(&kct
->kct_lock
);
383 list_add_tail(&kcd
->kcd_node
, &kct
->kct_list
);
384 spin_unlock(&kct
->kct_lock
);
391 splat_kmem_cache_test_debug(struct file
*file
, char *name
,
392 kmem_cache_priv_t
*kcp
)
396 splat_vprint(file
, name
, "%s cache objects %d",
397 kcp
->kcp_cache
->skc_name
, kcp
->kcp_count
);
399 if (kcp
->kcp_cache
->skc_flags
& (KMC_KMEM
| KMC_VMEM
)) {
400 splat_vprint(file
, name
, ", slabs %u/%u objs %u/%u",
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
);
406 if (!(kcp
->kcp_cache
->skc_flags
& KMC_NOMAGAZINE
)) {
407 splat_vprint(file
, name
, "%s", "mags");
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
);
416 splat_print(file
, "%s\n", "");
420 splat_kmem_cache_test_constructor(void *ptr
, void *priv
, int flags
)
422 kmem_cache_priv_t
*kcp
= (kmem_cache_priv_t
*)priv
;
423 kmem_cache_data_t
*kcd
= (kmem_cache_data_t
*)ptr
;
426 kcd
->kcd_magic
= kcp
->kcp_magic
;
427 INIT_LIST_HEAD(&kcd
->kcd_node
);
429 memset(kcd
->kcd_buf
, 0xaa, kcp
->kcp_size
- (sizeof *kcd
));
437 splat_kmem_cache_test_destructor(void *ptr
, void *priv
)
439 kmem_cache_priv_t
*kcp
= (kmem_cache_priv_t
*)priv
;
440 kmem_cache_data_t
*kcd
= (kmem_cache_data_t
*)ptr
;
445 memset(kcd
->kcd_buf
, 0xbb, kcp
->kcp_size
- (sizeof *kcd
));
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.
458 splat_kmem_cache_test_reclaim(void *priv
)
460 kmem_cache_priv_t
*kcp
= (kmem_cache_priv_t
*)priv
;
461 kmem_cache_thread_t
*kct
;
462 kmem_cache_data_t
*kcd
;
466 ASSERT(kcp
->kcp_magic
== SPLAT_KMEM_TEST_MAGIC
);
468 /* For each kct thread reclaim some objects */
469 spin_lock(&kcp
->kcp_lock
);
470 for (i
= 0; i
< SPLAT_KMEM_THREADS
; i
++) {
471 kct
= kcp
->kcp_kct
[i
];
475 spin_unlock(&kcp
->kcp_lock
);
476 spin_lock(&kct
->kct_lock
);
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
);
487 spin_unlock(&kct
->kct_lock
);
488 spin_lock(&kcp
->kcp_lock
);
490 spin_unlock(&kcp
->kcp_lock
);
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
);
503 splat_kmem_cache_test_threads(kmem_cache_priv_t
*kcp
, int threads
)
507 spin_lock(&kcp
->kcp_lock
);
508 rc
= (kcp
->kcp_kct_count
== threads
);
509 spin_unlock(&kcp
->kcp_lock
);
515 splat_kmem_cache_test_flags(kmem_cache_priv_t
*kcp
, int flags
)
519 spin_lock(&kcp
->kcp_lock
);
520 rc
= (kcp
->kcp_flags
& flags
);
521 spin_unlock(&kcp
->kcp_lock
);
527 splat_kmem_cache_test_thread(void *arg
)
529 kmem_cache_priv_t
*kcp
= (kmem_cache_priv_t
*)arg
;
530 kmem_cache_thread_t
*kct
;
533 ASSERT(kcp
->kcp_magic
== SPLAT_KMEM_TEST_MAGIC
);
535 /* Assign thread ids */
536 spin_lock(&kcp
->kcp_lock
);
537 if (kcp
->kcp_kct_count
== -1)
538 kcp
->kcp_kct_count
= 0;
540 id
= kcp
->kcp_kct_count
;
541 kcp
->kcp_kct_count
++;
542 spin_unlock(&kcp
->kcp_lock
);
544 kct
= splat_kmem_cache_test_kct_alloc(kcp
, id
);
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
);
554 wait_event(kcp
->kcp_thr_waitq
,
555 splat_kmem_cache_test_flags(kcp
, KCP_FLAG_READY
));
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
);
562 splat_kmem_cache_test_kct_free(kcp
, kct
);
564 spin_lock(&kcp
->kcp_lock
);
568 if ((--kcp
->kcp_kct_count
) == 0)
569 wake_up(&kcp
->kcp_ctl_waitq
);
571 spin_unlock(&kcp
->kcp_lock
);
577 splat_kmem_cache_test(struct file
*file
, void *arg
, char *name
,
578 int size
, int align
, int flags
)
580 kmem_cache_priv_t
*kcp
;
581 kmem_cache_data_t
*kcd
= NULL
;
584 kcp
= splat_kmem_cache_test_kcp_alloc(file
, name
, size
, align
, 0);
586 splat_vprint(file
, name
, "Unable to create '%s'\n", "kcp");
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
) {
597 splat_vprint(file
, name
,
598 "Unable to create '%s'\n",
599 SPLAT_KMEM_CACHE_NAME
);
604 kcd
= kmem_cache_alloc(kcp
->kcp_cache
, KM_SLEEP
);
606 splat_vprint(file
, name
,
607 "Unable to allocate from '%s'\n",
608 SPLAT_KMEM_CACHE_NAME
);
613 if (!kcd
->kcd_flag
) {
614 splat_vprint(file
, name
,
615 "Failed to run contructor for '%s'\n",
616 SPLAT_KMEM_CACHE_NAME
);
621 if (kcd
->kcd_magic
!= kcp
->kcp_magic
) {
622 splat_vprint(file
, name
,
623 "Failed to pass private data to constructor "
624 "for '%s'\n", SPLAT_KMEM_CACHE_NAME
);
629 max
= kcp
->kcp_count
;
630 kmem_cache_free(kcp
->kcp_cache
, kcd
);
632 /* Destroy the entire cache which will force destructors to
633 * run and we can verify one was called for every object */
634 kmem_cache_destroy(kcp
->kcp_cache
);
635 if (kcp
->kcp_count
) {
636 splat_vprint(file
, name
,
637 "Failed to run destructor on all slab objects "
638 "for '%s'\n", SPLAT_KMEM_CACHE_NAME
);
642 splat_kmem_cache_test_kcp_free(kcp
);
643 splat_vprint(file
, name
,
644 "Successfully ran ctors/dtors for %d elements in '%s'\n",
645 max
, SPLAT_KMEM_CACHE_NAME
);
651 kmem_cache_free(kcp
->kcp_cache
, kcd
);
654 kmem_cache_destroy(kcp
->kcp_cache
);
656 splat_kmem_cache_test_kcp_free(kcp
);
662 splat_kmem_cache_thread_test(struct file
*file
, void *arg
, char *name
,
663 int size
, int alloc
, int max_time
)
665 kmem_cache_priv_t
*kcp
;
667 struct timespec start
, stop
, delta
;
671 kcp
= splat_kmem_cache_test_kcp_alloc(file
, name
, size
, 0, alloc
);
673 splat_vprint(file
, name
, "Unable to create '%s'\n", "kcp");
677 (void)snprintf(cache_name
, 32, "%s-%d-%d",
678 SPLAT_KMEM_CACHE_NAME
, size
, alloc
);
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
,
685 if (!kcp
->kcp_cache
) {
686 splat_vprint(file
, name
, "Unable to create '%s'\n", cache_name
);
691 getnstimeofday(&start
);
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
);
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
));
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
);
713 /* Sleep until all thread have finished */
714 wait_event(kcp
->kcp_ctl_waitq
, splat_kmem_cache_test_threads(kcp
, 0));
716 getnstimeofday(&stop
);
717 delta
= timespec_sub(stop
, start
);
719 splat_vprint(file
, name
,
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
*
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
));
734 if (delta
.tv_sec
>= max_time
)
737 if (!rc
&& kcp
->kcp_rc
)
741 kmem_cache_destroy(kcp
->kcp_cache
);
743 splat_kmem_cache_test_kcp_free(kcp
);
747 /* Validate small object cache behavior for dynamic/kmem/vmem caches */
749 splat_kmem_test5(struct file
*file
, void *arg
)
751 char *name
= SPLAT_KMEM_TEST5_NAME
;
754 /* On slab (default + kmem + vmem) */
755 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0, 0);
759 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0, KMC_KMEM
);
763 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0, KMC_VMEM
);
767 /* Off slab (default + kmem + vmem) */
768 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0, KMC_OFFSLAB
);
772 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0,
773 KMC_KMEM
| KMC_OFFSLAB
);
777 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0,
778 KMC_VMEM
| KMC_OFFSLAB
);
784 * Validate large object cache behavior for dynamic/kmem/vmem caches
787 splat_kmem_test6(struct file
*file
, void *arg
)
789 char *name
= SPLAT_KMEM_TEST6_NAME
;
792 /* On slab (default + kmem + vmem) */
793 rc
= splat_kmem_cache_test(file
, arg
, name
, 256*1024, 0, 0);
797 rc
= splat_kmem_cache_test(file
, arg
, name
, 64*1024, 0, KMC_KMEM
);
801 rc
= splat_kmem_cache_test(file
, arg
, name
, 1024*1024, 0, KMC_VMEM
);
805 rc
= splat_kmem_cache_test(file
, arg
, name
, 16*1024*1024, 0, KMC_VMEM
);
809 /* Off slab (default + kmem + vmem) */
810 rc
= splat_kmem_cache_test(file
, arg
, name
, 256*1024, 0, KMC_OFFSLAB
);
814 rc
= splat_kmem_cache_test(file
, arg
, name
, 64*1024, 0,
815 KMC_KMEM
| KMC_OFFSLAB
);
819 rc
= splat_kmem_cache_test(file
, arg
, name
, 1024*1024, 0,
820 KMC_VMEM
| KMC_OFFSLAB
);
824 rc
= splat_kmem_cache_test(file
, arg
, name
, 16*1024*1024, 0,
825 KMC_VMEM
| KMC_OFFSLAB
);
831 * Validate object alignment cache behavior for caches
834 splat_kmem_test7(struct file
*file
, void *arg
)
836 char *name
= SPLAT_KMEM_TEST7_NAME
;
839 for (i
= SPL_KMEM_CACHE_ALIGN
; i
<= PAGE_SIZE
; i
*= 2) {
840 rc
= splat_kmem_cache_test(file
, arg
, name
, 157, i
, 0);
844 rc
= splat_kmem_cache_test(file
, arg
, name
, 157, i
,
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.
865 splat_kmem_test8(struct file
*file
, void *arg
)
867 kmem_cache_priv_t
*kcp
;
868 kmem_cache_thread_t
*kct
;
869 unsigned int spl_kmem_cache_expire_old
;
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
;
876 kcp
= splat_kmem_cache_test_kcp_alloc(file
, SPLAT_KMEM_TEST8_NAME
,
879 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
880 "Unable to create '%s'\n", "kcp");
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
,
891 if (!kcp
->kcp_cache
) {
892 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
893 "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME
);
898 kct
= splat_kmem_cache_test_kct_alloc(kcp
, 0);
900 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
901 "Unable to create '%s'\n", "kct");
906 rc
= splat_kmem_cache_test_kcd_alloc(kcp
, kct
, SPLAT_KMEM_OBJ_COUNT
);
908 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
, "Unable to "
909 "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME
);
913 /* Force reclaim every 1/10 a second for 60 seconds. */
914 for (i
= 0; i
< 600; i
++) {
915 kmem_cache_reap_now(kcp
->kcp_cache
);
916 splat_kmem_cache_test_debug(file
, SPLAT_KMEM_TEST8_NAME
, kcp
);
918 if (kcp
->kcp_count
== 0)
921 set_current_state(TASK_INTERRUPTIBLE
);
922 schedule_timeout(HZ
/ 10);
925 if (kcp
->kcp_count
== 0) {
926 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
927 "Successfully created %d objects "
928 "in cache %s and reclaimed them\n",
929 SPLAT_KMEM_OBJ_COUNT
, SPLAT_KMEM_CACHE_NAME
);
931 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
932 "Failed to reclaim %u/%d objects from cache %s\n",
933 (unsigned)kcp
->kcp_count
,
934 SPLAT_KMEM_OBJ_COUNT
, SPLAT_KMEM_CACHE_NAME
);
938 /* Cleanup our mess (for failure case of time expiring) */
939 splat_kmem_cache_test_kcd_free(kcp
, kct
);
941 splat_kmem_cache_test_kct_free(kcp
, kct
);
943 kmem_cache_destroy(kcp
->kcp_cache
);
945 splat_kmem_cache_test_kcp_free(kcp
);
947 spl_kmem_cache_expire
= spl_kmem_cache_expire_old
;
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.
961 splat_kmem_test9(struct file
*file
, void *arg
)
963 kmem_cache_priv_t
*kcp
;
964 kmem_cache_thread_t
*kct
;
965 unsigned int spl_kmem_cache_expire_old
;
966 int i
, rc
= 0, count
= SPLAT_KMEM_OBJ_COUNT
* 128;
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
;
972 kcp
= splat_kmem_cache_test_kcp_alloc(file
, SPLAT_KMEM_TEST9_NAME
,
975 splat_vprint(file
, SPLAT_KMEM_TEST9_NAME
,
976 "Unable to create '%s'\n", "kcp");
982 kmem_cache_create(SPLAT_KMEM_CACHE_NAME
, kcp
->kcp_size
, 0,
983 splat_kmem_cache_test_constructor
,
984 splat_kmem_cache_test_destructor
,
986 if (!kcp
->kcp_cache
) {
987 splat_vprint(file
, SPLAT_KMEM_TEST9_NAME
,
988 "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME
);
993 kct
= splat_kmem_cache_test_kct_alloc(kcp
, 0);
995 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
996 "Unable to create '%s'\n", "kct");
1001 rc
= splat_kmem_cache_test_kcd_alloc(kcp
, kct
, count
);
1003 splat_vprint(file
, SPLAT_KMEM_TEST9_NAME
, "Unable to "
1004 "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME
);
1008 splat_kmem_cache_test_kcd_free(kcp
, kct
);
1010 for (i
= 0; i
< 60; i
++) {
1011 splat_kmem_cache_test_debug(file
, SPLAT_KMEM_TEST9_NAME
, kcp
);
1013 if (kcp
->kcp_count
== 0)
1016 set_current_state(TASK_INTERRUPTIBLE
);
1017 schedule_timeout(HZ
);
1020 if (kcp
->kcp_count
== 0) {
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
);
1026 splat_vprint(file
, SPLAT_KMEM_TEST9_NAME
,
1027 "Failed to reclaim %u/%d objects from cache %s\n",
1028 (unsigned)kcp
->kcp_count
, count
,
1029 SPLAT_KMEM_CACHE_NAME
);
1034 splat_kmem_cache_test_kct_free(kcp
, kct
);
1036 kmem_cache_destroy(kcp
->kcp_cache
);
1038 splat_kmem_cache_test_kcp_free(kcp
);
1040 spl_kmem_cache_expire
= spl_kmem_cache_expire_old
;
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.
1054 splat_kmem_test10(struct file
*file
, void *arg
)
1056 uint64_t size
, alloc
, rc
= 0;
1058 for (size
= 32; size
<= 1024*1024; size
*= 2) {
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");
1065 for (alloc
= 1; alloc
<= 1024; alloc
*= 2) {
1067 /* Skip tests which exceed 1/2 of physical memory. */
1068 if (size
* alloc
* SPLAT_KMEM_THREADS
> physmem
/ 2)
1071 rc
= splat_kmem_cache_thread_test(file
, arg
,
1072 SPLAT_KMEM_TEST10_NAME
, size
, alloc
, 5);
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
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.
1095 splat_kmem_test11(struct file
*file
, void *arg
)
1097 uint64_t size
, alloc
, rc
;
1100 alloc
= ((4 * physmem
* PAGE_SIZE
) / size
) / SPLAT_KMEM_THREADS
;
1102 splat_vprint(file
, SPLAT_KMEM_TEST11_NAME
, "%-22s %s", "name",
1103 "time (sec)\tslabs \tobjs \thash\n");
1104 splat_vprint(file
, SPLAT_KMEM_TEST11_NAME
, "%-22s %s", "",
1105 " \ttot/max/calc\ttot/max/calc\n");
1107 rc
= splat_kmem_cache_thread_test(file
, arg
,
1108 SPLAT_KMEM_TEST11_NAME
, size
, alloc
, 60);
1114 typedef struct dummy_page
{
1115 struct list_head dp_list
;
1116 char dp_pad
[PAGE_SIZE
- sizeof(struct list_head
)];
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.
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.
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.
1137 splat_kmem_test13(struct file
*file
, void *arg
)
1139 kmem_cache_priv_t
*kcp
;
1140 kmem_cache_thread_t
*kct
;
1142 struct list_head list
;
1143 struct timespec start
, stop
, delta
= { 0, 0 };
1144 int size
, count
, slabs
, fails
= 0;
1145 int i
, rc
= 0, max_time
= 10;
1148 count
= ((physmem
* PAGE_SIZE
) / 4 / size
);
1150 kcp
= splat_kmem_cache_test_kcp_alloc(file
, SPLAT_KMEM_TEST13_NAME
,
1153 splat_vprint(file
, SPLAT_KMEM_TEST13_NAME
,
1154 "Unable to create '%s'\n", "kcp");
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
,
1165 if (!kcp
->kcp_cache
) {
1166 splat_vprint(file
, SPLAT_KMEM_TEST13_NAME
,
1167 "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME
);
1172 kct
= splat_kmem_cache_test_kct_alloc(kcp
, 0);
1174 splat_vprint(file
, SPLAT_KMEM_TEST13_NAME
,
1175 "Unable to create '%s'\n", "kct");
1180 rc
= splat_kmem_cache_test_kcd_alloc(kcp
, kct
, count
);
1182 splat_vprint(file
, SPLAT_KMEM_TEST13_NAME
, "Unable to "
1183 "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME
);
1188 slabs
= kcp
->kcp_cache
->skc_slab_total
;
1189 INIT_LIST_HEAD(&list
);
1190 getnstimeofday(&start
);
1192 /* Apply memory pressure */
1193 while (kcp
->kcp_cache
->skc_slab_total
> (slabs
>> 2)) {
1195 if ((i
% 10000) == 0)
1196 splat_kmem_cache_test_debug(
1197 file
, SPLAT_KMEM_TEST13_NAME
, kcp
);
1199 getnstimeofday(&stop
);
1200 delta
= timespec_sub(stop
, start
);
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
,
1211 dp
= (dummy_page_t
*)__get_free_page(GFP_KERNEL
);
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
);
1221 list_add(&dp
->dp_list
, &list
);
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",
1230 (int)delta
.tv_sec
, (int)delta
.tv_nsec
/ 1000000);
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
);
1239 /* Release remaining kmem cache objects */
1240 splat_kmem_cache_test_kcd_free(kcp
, kct
);
1242 splat_kmem_cache_test_kct_free(kcp
, kct
);
1244 kmem_cache_destroy(kcp
->kcp_cache
);
1246 splat_kmem_cache_test_kcp_free(kcp
);
1252 splat_kmem_init(void)
1254 splat_subsystem_t
*sub
;
1256 sub
= kmalloc(sizeof(*sub
), GFP_KERNEL
);
1260 memset(sub
, 0, sizeof(*sub
));
1261 strncpy(sub
->desc
.name
, SPLAT_KMEM_NAME
, SPLAT_NAME_SIZE
);
1262 strncpy(sub
->desc
.desc
, SPLAT_KMEM_DESC
, SPLAT_DESC_SIZE
);
1263 INIT_LIST_HEAD(&sub
->subsystem_list
);
1264 INIT_LIST_HEAD(&sub
->test_list
);
1265 spin_lock_init(&sub
->test_lock
);
1266 sub
->desc
.id
= SPLAT_SUBSYSTEM_KMEM
;
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
);
1289 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST11_NAME
, SPLAT_KMEM_TEST11_DESC
,
1290 SPLAT_KMEM_TEST11_ID
, splat_kmem_test11
);
1292 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST13_NAME
, SPLAT_KMEM_TEST13_DESC
,
1293 SPLAT_KMEM_TEST13_ID
, splat_kmem_test13
);
1299 splat_kmem_fini(splat_subsystem_t
*sub
)
1302 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST13_ID
);
1304 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST11_ID
);
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
);
1321 splat_kmem_id(void) {
1322 return SPLAT_SUBSYSTEM_KMEM
;