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
<= spl_kmem_alloc_warn
)) {
101 for (i
= 0; i
< SPLAT_KMEM_ALLOC_COUNT
; i
++) {
102 ptr
[i
] = kmem_alloc(size
, KM_SLEEP
);
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
<= spl_kmem_alloc_warn
)) {
133 for (i
= 0; i
< SPLAT_KMEM_ALLOC_COUNT
; i
++) {
134 ptr
[i
] = kmem_zalloc(size
, KM_SLEEP
);
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;
175 * Test up to 4x the maximum kmem_alloc() size to ensure both
176 * the kmem_alloc() and vmem_alloc() call paths are used.
178 while ((!rc
) && (size
<= (4 * spl_kmem_alloc_max
))) {
181 for (i
= 0; i
< SPLAT_VMEM_ALLOC_COUNT
; i
++) {
182 ptr
[i
] = vmem_alloc(size
, KM_SLEEP
);
187 for (i
= 0; i
< SPLAT_VMEM_ALLOC_COUNT
; i
++)
189 vmem_free(ptr
[i
], size
);
191 splat_vprint(file
, SPLAT_KMEM_TEST3_NAME
,
192 "%d byte allocations, %d/%d successful\n",
193 size
, count
, SPLAT_VMEM_ALLOC_COUNT
);
194 if (count
!= SPLAT_VMEM_ALLOC_COUNT
)
204 splat_kmem_test4(struct file
*file
, void *arg
)
206 void *ptr
[SPLAT_VMEM_ALLOC_COUNT
];
207 int size
= PAGE_SIZE
;
208 int i
, j
, count
, rc
= 0;
211 * Test up to 4x the maximum kmem_zalloc() size to ensure both
212 * the kmem_zalloc() and vmem_zalloc() call paths are used.
214 while ((!rc
) && (size
<= (4 * spl_kmem_alloc_max
))) {
217 for (i
= 0; i
< SPLAT_VMEM_ALLOC_COUNT
; i
++) {
218 ptr
[i
] = vmem_zalloc(size
, KM_SLEEP
);
223 /* Ensure buffer has been zero filled */
224 for (i
= 0; i
< SPLAT_VMEM_ALLOC_COUNT
; i
++) {
225 for (j
= 0; j
< size
; j
++) {
226 if (((char *)ptr
[i
])[j
] != '\0') {
227 splat_vprint(file
, SPLAT_KMEM_TEST4_NAME
,
228 "%d-byte allocation was "
229 "not zeroed\n", size
);
235 for (i
= 0; i
< SPLAT_VMEM_ALLOC_COUNT
; i
++)
237 vmem_free(ptr
[i
], size
);
239 splat_vprint(file
, SPLAT_KMEM_TEST4_NAME
,
240 "%d byte allocations, %d/%d successful\n",
241 size
, count
, SPLAT_VMEM_ALLOC_COUNT
);
242 if (count
!= SPLAT_VMEM_ALLOC_COUNT
)
251 #define SPLAT_KMEM_TEST_MAGIC 0x004488CCUL
252 #define SPLAT_KMEM_CACHE_NAME "kmem_test"
253 #define SPLAT_KMEM_OBJ_COUNT 1024
254 #define SPLAT_KMEM_OBJ_RECLAIM 32 /* objects */
255 #define SPLAT_KMEM_THREADS 32
257 #define KCP_FLAG_READY 0x01
259 typedef struct kmem_cache_data
{
260 unsigned long kcd_magic
;
261 struct list_head kcd_node
;
266 typedef struct kmem_cache_thread
{
269 struct list_head kct_list
;
270 } kmem_cache_thread_t
;
272 typedef struct kmem_cache_priv
{
273 unsigned long kcp_magic
;
274 struct file
*kcp_file
;
275 kmem_cache_t
*kcp_cache
;
277 wait_queue_head_t kcp_ctl_waitq
;
278 wait_queue_head_t kcp_thr_waitq
;
281 kmem_cache_thread_t
*kcp_kct
[SPLAT_KMEM_THREADS
];
289 static kmem_cache_priv_t
*
290 splat_kmem_cache_test_kcp_alloc(struct file
*file
, char *name
,
291 int size
, int align
, int alloc
)
293 kmem_cache_priv_t
*kcp
;
295 kcp
= kmem_zalloc(sizeof(kmem_cache_priv_t
), KM_SLEEP
);
299 kcp
->kcp_magic
= SPLAT_KMEM_TEST_MAGIC
;
300 kcp
->kcp_file
= file
;
301 kcp
->kcp_cache
= NULL
;
302 spin_lock_init(&kcp
->kcp_lock
);
303 init_waitqueue_head(&kcp
->kcp_ctl_waitq
);
304 init_waitqueue_head(&kcp
->kcp_thr_waitq
);
306 kcp
->kcp_kct_count
= -1;
307 kcp
->kcp_size
= size
;
308 kcp
->kcp_align
= align
;
310 kcp
->kcp_alloc
= alloc
;
317 splat_kmem_cache_test_kcp_free(kmem_cache_priv_t
*kcp
)
319 kmem_free(kcp
, sizeof(kmem_cache_priv_t
));
322 static kmem_cache_thread_t
*
323 splat_kmem_cache_test_kct_alloc(kmem_cache_priv_t
*kcp
, int id
)
325 kmem_cache_thread_t
*kct
;
327 ASSERT3S(id
, <, SPLAT_KMEM_THREADS
);
328 ASSERT(kcp
->kcp_kct
[id
] == NULL
);
330 kct
= kmem_zalloc(sizeof(kmem_cache_thread_t
), KM_SLEEP
);
334 spin_lock_init(&kct
->kct_lock
);
336 INIT_LIST_HEAD(&kct
->kct_list
);
338 spin_lock(&kcp
->kcp_lock
);
339 kcp
->kcp_kct
[id
] = kct
;
340 spin_unlock(&kcp
->kcp_lock
);
346 splat_kmem_cache_test_kct_free(kmem_cache_priv_t
*kcp
,
347 kmem_cache_thread_t
*kct
)
349 spin_lock(&kcp
->kcp_lock
);
350 kcp
->kcp_kct
[kct
->kct_id
] = NULL
;
351 spin_unlock(&kcp
->kcp_lock
);
353 kmem_free(kct
, sizeof(kmem_cache_thread_t
));
357 splat_kmem_cache_test_kcd_free(kmem_cache_priv_t
*kcp
,
358 kmem_cache_thread_t
*kct
)
360 kmem_cache_data_t
*kcd
;
362 spin_lock(&kct
->kct_lock
);
363 while (!list_empty(&kct
->kct_list
)) {
364 kcd
= list_entry(kct
->kct_list
.next
,
365 kmem_cache_data_t
, kcd_node
);
366 list_del(&kcd
->kcd_node
);
367 spin_unlock(&kct
->kct_lock
);
369 kmem_cache_free(kcp
->kcp_cache
, kcd
);
371 spin_lock(&kct
->kct_lock
);
373 spin_unlock(&kct
->kct_lock
);
377 splat_kmem_cache_test_kcd_alloc(kmem_cache_priv_t
*kcp
,
378 kmem_cache_thread_t
*kct
, int count
)
380 kmem_cache_data_t
*kcd
;
383 for (i
= 0; i
< count
; i
++) {
384 kcd
= kmem_cache_alloc(kcp
->kcp_cache
, KM_SLEEP
);
386 splat_kmem_cache_test_kcd_free(kcp
, kct
);
390 spin_lock(&kct
->kct_lock
);
391 list_add_tail(&kcd
->kcd_node
, &kct
->kct_list
);
392 spin_unlock(&kct
->kct_lock
);
399 splat_kmem_cache_test_debug(struct file
*file
, char *name
,
400 kmem_cache_priv_t
*kcp
)
404 splat_vprint(file
, name
, "%s cache objects %d",
405 kcp
->kcp_cache
->skc_name
, kcp
->kcp_count
);
407 if (kcp
->kcp_cache
->skc_flags
& (KMC_KMEM
| KMC_VMEM
)) {
408 splat_vprint(file
, name
, ", slabs %u/%u objs %u/%u",
409 (unsigned)kcp
->kcp_cache
->skc_slab_alloc
,
410 (unsigned)kcp
->kcp_cache
->skc_slab_total
,
411 (unsigned)kcp
->kcp_cache
->skc_obj_alloc
,
412 (unsigned)kcp
->kcp_cache
->skc_obj_total
);
414 if (!(kcp
->kcp_cache
->skc_flags
& KMC_NOMAGAZINE
)) {
415 splat_vprint(file
, name
, "%s", "mags");
417 for_each_online_cpu(j
)
418 splat_print(file
, "%u/%u ",
419 kcp
->kcp_cache
->skc_mag
[j
]->skm_avail
,
420 kcp
->kcp_cache
->skc_mag
[j
]->skm_size
);
424 splat_print(file
, "%s\n", "");
428 splat_kmem_cache_test_constructor(void *ptr
, void *priv
, int flags
)
430 kmem_cache_priv_t
*kcp
= (kmem_cache_priv_t
*)priv
;
431 kmem_cache_data_t
*kcd
= (kmem_cache_data_t
*)ptr
;
434 kcd
->kcd_magic
= kcp
->kcp_magic
;
435 INIT_LIST_HEAD(&kcd
->kcd_node
);
437 memset(kcd
->kcd_buf
, 0xaa, kcp
->kcp_size
- (sizeof *kcd
));
445 splat_kmem_cache_test_destructor(void *ptr
, void *priv
)
447 kmem_cache_priv_t
*kcp
= (kmem_cache_priv_t
*)priv
;
448 kmem_cache_data_t
*kcd
= (kmem_cache_data_t
*)ptr
;
453 memset(kcd
->kcd_buf
, 0xbb, kcp
->kcp_size
- (sizeof *kcd
));
461 * Generic reclaim function which assumes that all objects may
462 * be reclaimed at any time. We free a small percentage of the
463 * objects linked off the kcp or kct[] every time we are called.
466 splat_kmem_cache_test_reclaim(void *priv
)
468 kmem_cache_priv_t
*kcp
= (kmem_cache_priv_t
*)priv
;
469 kmem_cache_thread_t
*kct
;
470 kmem_cache_data_t
*kcd
;
474 ASSERT(kcp
->kcp_magic
== SPLAT_KMEM_TEST_MAGIC
);
476 /* For each kct thread reclaim some objects */
477 spin_lock(&kcp
->kcp_lock
);
478 for (i
= 0; i
< SPLAT_KMEM_THREADS
; i
++) {
479 kct
= kcp
->kcp_kct
[i
];
483 spin_unlock(&kcp
->kcp_lock
);
484 spin_lock(&kct
->kct_lock
);
486 count
= SPLAT_KMEM_OBJ_RECLAIM
;
487 while (count
> 0 && !list_empty(&kct
->kct_list
)) {
488 kcd
= list_entry(kct
->kct_list
.next
,
489 kmem_cache_data_t
, kcd_node
);
490 list_del(&kcd
->kcd_node
);
491 list_add(&kcd
->kcd_node
, &reclaim
);
495 spin_unlock(&kct
->kct_lock
);
496 spin_lock(&kcp
->kcp_lock
);
498 spin_unlock(&kcp
->kcp_lock
);
500 /* Freed outside the spin lock */
501 while (!list_empty(&reclaim
)) {
502 kcd
= list_entry(reclaim
.next
, kmem_cache_data_t
, kcd_node
);
503 list_del(&kcd
->kcd_node
);
504 kmem_cache_free(kcp
->kcp_cache
, kcd
);
511 splat_kmem_cache_test_threads(kmem_cache_priv_t
*kcp
, int threads
)
515 spin_lock(&kcp
->kcp_lock
);
516 rc
= (kcp
->kcp_kct_count
== threads
);
517 spin_unlock(&kcp
->kcp_lock
);
523 splat_kmem_cache_test_flags(kmem_cache_priv_t
*kcp
, int flags
)
527 spin_lock(&kcp
->kcp_lock
);
528 rc
= (kcp
->kcp_flags
& flags
);
529 spin_unlock(&kcp
->kcp_lock
);
535 splat_kmem_cache_test_thread(void *arg
)
537 kmem_cache_priv_t
*kcp
= (kmem_cache_priv_t
*)arg
;
538 kmem_cache_thread_t
*kct
;
541 ASSERT(kcp
->kcp_magic
== SPLAT_KMEM_TEST_MAGIC
);
543 /* Assign thread ids */
544 spin_lock(&kcp
->kcp_lock
);
545 if (kcp
->kcp_kct_count
== -1)
546 kcp
->kcp_kct_count
= 0;
548 id
= kcp
->kcp_kct_count
;
549 kcp
->kcp_kct_count
++;
550 spin_unlock(&kcp
->kcp_lock
);
552 kct
= splat_kmem_cache_test_kct_alloc(kcp
, id
);
558 /* Wait for all threads to have started and report they are ready */
559 if (kcp
->kcp_kct_count
== SPLAT_KMEM_THREADS
)
560 wake_up(&kcp
->kcp_ctl_waitq
);
562 wait_event(kcp
->kcp_thr_waitq
,
563 splat_kmem_cache_test_flags(kcp
, KCP_FLAG_READY
));
565 /* Create and destroy objects */
566 rc
= splat_kmem_cache_test_kcd_alloc(kcp
, kct
, kcp
->kcp_alloc
);
567 splat_kmem_cache_test_kcd_free(kcp
, kct
);
570 splat_kmem_cache_test_kct_free(kcp
, kct
);
572 spin_lock(&kcp
->kcp_lock
);
576 if ((--kcp
->kcp_kct_count
) == 0)
577 wake_up(&kcp
->kcp_ctl_waitq
);
579 spin_unlock(&kcp
->kcp_lock
);
585 splat_kmem_cache_test(struct file
*file
, void *arg
, char *name
,
586 int size
, int align
, int flags
)
588 kmem_cache_priv_t
*kcp
;
589 kmem_cache_data_t
*kcd
= NULL
;
592 kcp
= splat_kmem_cache_test_kcp_alloc(file
, name
, size
, align
, 0);
594 splat_vprint(file
, name
, "Unable to create '%s'\n", "kcp");
599 kmem_cache_create(SPLAT_KMEM_CACHE_NAME
,
600 kcp
->kcp_size
, kcp
->kcp_align
,
601 splat_kmem_cache_test_constructor
,
602 splat_kmem_cache_test_destructor
,
603 NULL
, kcp
, NULL
, flags
);
604 if (!kcp
->kcp_cache
) {
605 splat_vprint(file
, name
,
606 "Unable to create '%s'\n",
607 SPLAT_KMEM_CACHE_NAME
);
612 kcd
= kmem_cache_alloc(kcp
->kcp_cache
, KM_SLEEP
);
614 splat_vprint(file
, name
,
615 "Unable to allocate from '%s'\n",
616 SPLAT_KMEM_CACHE_NAME
);
621 if (!kcd
->kcd_flag
) {
622 splat_vprint(file
, name
,
623 "Failed to run contructor for '%s'\n",
624 SPLAT_KMEM_CACHE_NAME
);
629 if (kcd
->kcd_magic
!= kcp
->kcp_magic
) {
630 splat_vprint(file
, name
,
631 "Failed to pass private data to constructor "
632 "for '%s'\n", SPLAT_KMEM_CACHE_NAME
);
637 max
= kcp
->kcp_count
;
638 kmem_cache_free(kcp
->kcp_cache
, kcd
);
640 /* Destroy the entire cache which will force destructors to
641 * run and we can verify one was called for every object */
642 kmem_cache_destroy(kcp
->kcp_cache
);
643 if (kcp
->kcp_count
) {
644 splat_vprint(file
, name
,
645 "Failed to run destructor on all slab objects "
646 "for '%s'\n", SPLAT_KMEM_CACHE_NAME
);
650 splat_kmem_cache_test_kcp_free(kcp
);
651 splat_vprint(file
, name
,
652 "Successfully ran ctors/dtors for %d elements in '%s'\n",
653 max
, SPLAT_KMEM_CACHE_NAME
);
659 kmem_cache_free(kcp
->kcp_cache
, kcd
);
662 kmem_cache_destroy(kcp
->kcp_cache
);
664 splat_kmem_cache_test_kcp_free(kcp
);
670 splat_kmem_cache_thread_test(struct file
*file
, void *arg
, char *name
,
671 int size
, int alloc
, int max_time
)
673 kmem_cache_priv_t
*kcp
;
675 struct timespec start
, stop
, delta
;
679 kcp
= splat_kmem_cache_test_kcp_alloc(file
, name
, size
, 0, alloc
);
681 splat_vprint(file
, name
, "Unable to create '%s'\n", "kcp");
685 (void)snprintf(cache_name
, 32, "%s-%d-%d",
686 SPLAT_KMEM_CACHE_NAME
, size
, alloc
);
688 kmem_cache_create(cache_name
, kcp
->kcp_size
, 0,
689 splat_kmem_cache_test_constructor
,
690 splat_kmem_cache_test_destructor
,
691 splat_kmem_cache_test_reclaim
,
693 if (!kcp
->kcp_cache
) {
694 splat_vprint(file
, name
, "Unable to create '%s'\n", cache_name
);
699 getnstimeofday(&start
);
701 for (i
= 0; i
< SPLAT_KMEM_THREADS
; i
++) {
702 thr
= thread_create(NULL
, 0,
703 splat_kmem_cache_test_thread
,
704 kcp
, 0, &p0
, TS_RUN
, minclsyspri
);
711 /* Sleep until all threads have started, then set the ready
712 * flag and wake them all up for maximum concurrency. */
713 wait_event(kcp
->kcp_ctl_waitq
,
714 splat_kmem_cache_test_threads(kcp
, SPLAT_KMEM_THREADS
));
716 spin_lock(&kcp
->kcp_lock
);
717 kcp
->kcp_flags
|= KCP_FLAG_READY
;
718 spin_unlock(&kcp
->kcp_lock
);
719 wake_up_all(&kcp
->kcp_thr_waitq
);
721 /* Sleep until all thread have finished */
722 wait_event(kcp
->kcp_ctl_waitq
, splat_kmem_cache_test_threads(kcp
, 0));
724 getnstimeofday(&stop
);
725 delta
= timespec_sub(stop
, start
);
727 splat_vprint(file
, name
,
729 "%lu/%lu/%lu\t%lu/%lu/%lu\n",
730 kcp
->kcp_cache
->skc_name
,
731 delta
.tv_sec
, delta
.tv_nsec
,
732 (unsigned long)kcp
->kcp_cache
->skc_slab_total
,
733 (unsigned long)kcp
->kcp_cache
->skc_slab_max
,
734 (unsigned long)(kcp
->kcp_alloc
*
736 SPL_KMEM_CACHE_OBJ_PER_SLAB
),
737 (unsigned long)kcp
->kcp_cache
->skc_obj_total
,
738 (unsigned long)kcp
->kcp_cache
->skc_obj_max
,
739 (unsigned long)(kcp
->kcp_alloc
*
740 SPLAT_KMEM_THREADS
));
742 if (delta
.tv_sec
>= max_time
)
745 if (!rc
&& kcp
->kcp_rc
)
749 kmem_cache_destroy(kcp
->kcp_cache
);
751 splat_kmem_cache_test_kcp_free(kcp
);
755 /* Validate small object cache behavior for dynamic/kmem/vmem caches */
757 splat_kmem_test5(struct file
*file
, void *arg
)
759 char *name
= SPLAT_KMEM_TEST5_NAME
;
762 /* On slab (default + kmem + vmem) */
763 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0, 0);
767 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0, KMC_KMEM
);
771 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0, KMC_VMEM
);
775 /* Off slab (default + kmem + vmem) */
776 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0, KMC_OFFSLAB
);
780 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0,
781 KMC_KMEM
| KMC_OFFSLAB
);
785 rc
= splat_kmem_cache_test(file
, arg
, name
, 128, 0,
786 KMC_VMEM
| KMC_OFFSLAB
);
792 * Validate large object cache behavior for dynamic/kmem/vmem caches
795 splat_kmem_test6(struct file
*file
, void *arg
)
797 char *name
= SPLAT_KMEM_TEST6_NAME
;
800 /* On slab (default + kmem + vmem) */
801 rc
= splat_kmem_cache_test(file
, arg
, name
, 256*1024, 0, 0);
805 rc
= splat_kmem_cache_test(file
, arg
, name
, 64*1024, 0, KMC_KMEM
);
809 rc
= splat_kmem_cache_test(file
, arg
, name
, 1024*1024, 0, KMC_VMEM
);
813 rc
= splat_kmem_cache_test(file
, arg
, name
, 16*1024*1024, 0, KMC_VMEM
);
817 /* Off slab (default + kmem + vmem) */
818 rc
= splat_kmem_cache_test(file
, arg
, name
, 256*1024, 0, KMC_OFFSLAB
);
822 rc
= splat_kmem_cache_test(file
, arg
, name
, 64*1024, 0,
823 KMC_KMEM
| KMC_OFFSLAB
);
827 rc
= splat_kmem_cache_test(file
, arg
, name
, 1024*1024, 0,
828 KMC_VMEM
| KMC_OFFSLAB
);
832 rc
= splat_kmem_cache_test(file
, arg
, name
, 16*1024*1024, 0,
833 KMC_VMEM
| KMC_OFFSLAB
);
839 * Validate object alignment cache behavior for caches
842 splat_kmem_test7(struct file
*file
, void *arg
)
844 char *name
= SPLAT_KMEM_TEST7_NAME
;
847 for (i
= SPL_KMEM_CACHE_ALIGN
; i
<= PAGE_SIZE
; i
*= 2) {
848 rc
= splat_kmem_cache_test(file
, arg
, name
, 157, i
, 0);
852 rc
= splat_kmem_cache_test(file
, arg
, name
, 157, i
,
862 * Validate kmem_cache_reap() by requesting the slab cache free any objects
863 * it can. For a few reasons this may not immediately result in more free
864 * memory even if objects are freed. First off, due to fragmentation we
865 * may not be able to reclaim any slabs. Secondly, even if we do we fully
866 * clear some slabs we will not want to immediately reclaim all of them
867 * because we may contend with cache allocations and thrash. What we want
868 * to see is the slab size decrease more gradually as it becomes clear they
869 * will not be needed. This should be achievable in less than a minute.
870 * If it takes longer than this something has gone wrong.
873 splat_kmem_test8(struct file
*file
, void *arg
)
875 kmem_cache_priv_t
*kcp
;
876 kmem_cache_thread_t
*kct
;
877 unsigned int spl_kmem_cache_expire_old
;
880 /* Enable cache aging just for this test if it is disabled */
881 spl_kmem_cache_expire_old
= spl_kmem_cache_expire
;
882 spl_kmem_cache_expire
= KMC_EXPIRE_AGE
;
884 kcp
= splat_kmem_cache_test_kcp_alloc(file
, SPLAT_KMEM_TEST8_NAME
,
887 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
888 "Unable to create '%s'\n", "kcp");
894 kmem_cache_create(SPLAT_KMEM_CACHE_NAME
, kcp
->kcp_size
, 0,
895 splat_kmem_cache_test_constructor
,
896 splat_kmem_cache_test_destructor
,
897 splat_kmem_cache_test_reclaim
,
899 if (!kcp
->kcp_cache
) {
900 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
901 "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME
);
906 kct
= splat_kmem_cache_test_kct_alloc(kcp
, 0);
908 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
909 "Unable to create '%s'\n", "kct");
914 rc
= splat_kmem_cache_test_kcd_alloc(kcp
, kct
, SPLAT_KMEM_OBJ_COUNT
);
916 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
, "Unable to "
917 "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME
);
921 /* Force reclaim every 1/10 a second for 60 seconds. */
922 for (i
= 0; i
< 600; i
++) {
923 kmem_cache_reap_now(kcp
->kcp_cache
);
924 splat_kmem_cache_test_debug(file
, SPLAT_KMEM_TEST8_NAME
, kcp
);
926 if (kcp
->kcp_count
== 0)
929 set_current_state(TASK_INTERRUPTIBLE
);
930 schedule_timeout(HZ
/ 10);
933 if (kcp
->kcp_count
== 0) {
934 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
935 "Successfully created %d objects "
936 "in cache %s and reclaimed them\n",
937 SPLAT_KMEM_OBJ_COUNT
, SPLAT_KMEM_CACHE_NAME
);
939 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
940 "Failed to reclaim %u/%d objects from cache %s\n",
941 (unsigned)kcp
->kcp_count
,
942 SPLAT_KMEM_OBJ_COUNT
, SPLAT_KMEM_CACHE_NAME
);
946 /* Cleanup our mess (for failure case of time expiring) */
947 splat_kmem_cache_test_kcd_free(kcp
, kct
);
949 splat_kmem_cache_test_kct_free(kcp
, kct
);
951 kmem_cache_destroy(kcp
->kcp_cache
);
953 splat_kmem_cache_test_kcp_free(kcp
);
955 spl_kmem_cache_expire
= spl_kmem_cache_expire_old
;
960 /* Test cache aging, we have allocated a large number of objects thus
961 * creating a large number of slabs and then free'd them all. However,
962 * since there should be little memory pressure at the moment those
963 * slabs have not been freed. What we want to see is the slab size
964 * decrease gradually as it becomes clear they will not be be needed.
965 * This should be achievable in less than minute. If it takes longer
966 * than this something has gone wrong.
969 splat_kmem_test9(struct file
*file
, void *arg
)
971 kmem_cache_priv_t
*kcp
;
972 kmem_cache_thread_t
*kct
;
973 unsigned int spl_kmem_cache_expire_old
;
974 int i
, rc
= 0, count
= SPLAT_KMEM_OBJ_COUNT
* 128;
976 /* Enable cache aging just for this test if it is disabled */
977 spl_kmem_cache_expire_old
= spl_kmem_cache_expire
;
978 spl_kmem_cache_expire
= KMC_EXPIRE_AGE
;
980 kcp
= splat_kmem_cache_test_kcp_alloc(file
, SPLAT_KMEM_TEST9_NAME
,
983 splat_vprint(file
, SPLAT_KMEM_TEST9_NAME
,
984 "Unable to create '%s'\n", "kcp");
990 kmem_cache_create(SPLAT_KMEM_CACHE_NAME
, kcp
->kcp_size
, 0,
991 splat_kmem_cache_test_constructor
,
992 splat_kmem_cache_test_destructor
,
994 if (!kcp
->kcp_cache
) {
995 splat_vprint(file
, SPLAT_KMEM_TEST9_NAME
,
996 "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME
);
1001 kct
= splat_kmem_cache_test_kct_alloc(kcp
, 0);
1003 splat_vprint(file
, SPLAT_KMEM_TEST8_NAME
,
1004 "Unable to create '%s'\n", "kct");
1009 rc
= splat_kmem_cache_test_kcd_alloc(kcp
, kct
, count
);
1011 splat_vprint(file
, SPLAT_KMEM_TEST9_NAME
, "Unable to "
1012 "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME
);
1016 splat_kmem_cache_test_kcd_free(kcp
, kct
);
1018 for (i
= 0; i
< 60; i
++) {
1019 splat_kmem_cache_test_debug(file
, SPLAT_KMEM_TEST9_NAME
, kcp
);
1021 if (kcp
->kcp_count
== 0)
1024 set_current_state(TASK_INTERRUPTIBLE
);
1025 schedule_timeout(HZ
);
1028 if (kcp
->kcp_count
== 0) {
1029 splat_vprint(file
, SPLAT_KMEM_TEST9_NAME
,
1030 "Successfully created %d objects "
1031 "in cache %s and reclaimed them\n",
1032 count
, SPLAT_KMEM_CACHE_NAME
);
1034 splat_vprint(file
, SPLAT_KMEM_TEST9_NAME
,
1035 "Failed to reclaim %u/%d objects from cache %s\n",
1036 (unsigned)kcp
->kcp_count
, count
,
1037 SPLAT_KMEM_CACHE_NAME
);
1042 splat_kmem_cache_test_kct_free(kcp
, kct
);
1044 kmem_cache_destroy(kcp
->kcp_cache
);
1046 splat_kmem_cache_test_kcp_free(kcp
);
1048 spl_kmem_cache_expire
= spl_kmem_cache_expire_old
;
1054 * This test creates N threads with a shared kmem cache. They then all
1055 * concurrently allocate and free from the cache to stress the locking and
1056 * concurrent cache performance. If any one test takes longer than 5
1057 * seconds to complete it is treated as a failure and may indicate a
1058 * performance regression. On my test system no one test takes more
1059 * than 1 second to complete so a 5x slowdown likely a problem.
1062 splat_kmem_test10(struct file
*file
, void *arg
)
1064 uint64_t size
, alloc
, rc
= 0;
1066 for (size
= 32; size
<= 1024*1024; size
*= 2) {
1068 splat_vprint(file
, SPLAT_KMEM_TEST10_NAME
, "%-22s %s", "name",
1069 "time (sec)\tslabs \tobjs \thash\n");
1070 splat_vprint(file
, SPLAT_KMEM_TEST10_NAME
, "%-22s %s", "",
1071 " \ttot/max/calc\ttot/max/calc\n");
1073 for (alloc
= 1; alloc
<= 1024; alloc
*= 2) {
1075 /* Skip tests which exceed 1/2 of physical memory. */
1076 if (size
* alloc
* SPLAT_KMEM_THREADS
> physmem
/ 2)
1079 rc
= splat_kmem_cache_thread_test(file
, arg
,
1080 SPLAT_KMEM_TEST10_NAME
, size
, alloc
, 5);
1091 * This test creates N threads with a shared kmem cache which overcommits
1092 * memory by 4x. This makes it impossible for the slab to satify the
1093 * thread requirements without having its reclaim hook run which will
1094 * free objects back for use. This behavior is triggered by the linum VM
1095 * detecting a low memory condition on the node and invoking the shrinkers.
1096 * This should allow all the threads to complete while avoiding deadlock
1097 * and for the most part out of memory events. This is very tough on the
1098 * system so it is possible the test app may get oom'ed. This particular
1099 * test has proven troublesome on 32-bit archs with limited virtual
1100 * address space so it only run on 64-bit systems.
1103 splat_kmem_test11(struct file
*file
, void *arg
)
1105 uint64_t size
, alloc
, rc
;
1108 alloc
= ((4 * physmem
* PAGE_SIZE
) / size
) / SPLAT_KMEM_THREADS
;
1110 splat_vprint(file
, SPLAT_KMEM_TEST11_NAME
, "%-22s %s", "name",
1111 "time (sec)\tslabs \tobjs \thash\n");
1112 splat_vprint(file
, SPLAT_KMEM_TEST11_NAME
, "%-22s %s", "",
1113 " \ttot/max/calc\ttot/max/calc\n");
1115 rc
= splat_kmem_cache_thread_test(file
, arg
,
1116 SPLAT_KMEM_TEST11_NAME
, size
, alloc
, 60);
1122 typedef struct dummy_page
{
1123 struct list_head dp_list
;
1124 char dp_pad
[PAGE_SIZE
- sizeof(struct list_head
)];
1128 * This test is designed to verify that direct reclaim is functioning as
1129 * expected. We allocate a large number of objects thus creating a large
1130 * number of slabs. We then apply memory pressure and expect that the
1131 * direct reclaim path can easily recover those slabs. The registered
1132 * reclaim function will free the objects and the slab shrinker will call
1133 * it repeatedly until at least a single slab can be freed.
1135 * Note it may not be possible to reclaim every last slab via direct reclaim
1136 * without a failure because the shrinker_rwsem may be contended. For this
1137 * reason, quickly reclaiming 3/4 of the slabs is considered a success.
1139 * This should all be possible within 10 seconds. For reference, on a
1140 * system with 2G of memory this test takes roughly 0.2 seconds to run.
1141 * It may take longer on larger memory systems but should still easily
1142 * complete in the alloted 10 seconds.
1145 splat_kmem_test13(struct file
*file
, void *arg
)
1147 kmem_cache_priv_t
*kcp
;
1148 kmem_cache_thread_t
*kct
;
1150 struct list_head list
;
1151 struct timespec start
, stop
, delta
= { 0, 0 };
1152 int size
, count
, slabs
, fails
= 0;
1153 int i
, rc
= 0, max_time
= 10;
1156 count
= ((physmem
* PAGE_SIZE
) / 4 / size
);
1158 kcp
= splat_kmem_cache_test_kcp_alloc(file
, SPLAT_KMEM_TEST13_NAME
,
1161 splat_vprint(file
, SPLAT_KMEM_TEST13_NAME
,
1162 "Unable to create '%s'\n", "kcp");
1168 kmem_cache_create(SPLAT_KMEM_CACHE_NAME
, kcp
->kcp_size
, 0,
1169 splat_kmem_cache_test_constructor
,
1170 splat_kmem_cache_test_destructor
,
1171 splat_kmem_cache_test_reclaim
,
1173 if (!kcp
->kcp_cache
) {
1174 splat_vprint(file
, SPLAT_KMEM_TEST13_NAME
,
1175 "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME
);
1180 kct
= splat_kmem_cache_test_kct_alloc(kcp
, 0);
1182 splat_vprint(file
, SPLAT_KMEM_TEST13_NAME
,
1183 "Unable to create '%s'\n", "kct");
1188 rc
= splat_kmem_cache_test_kcd_alloc(kcp
, kct
, count
);
1190 splat_vprint(file
, SPLAT_KMEM_TEST13_NAME
, "Unable to "
1191 "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME
);
1196 slabs
= kcp
->kcp_cache
->skc_slab_total
;
1197 INIT_LIST_HEAD(&list
);
1198 getnstimeofday(&start
);
1200 /* Apply memory pressure */
1201 while (kcp
->kcp_cache
->skc_slab_total
> (slabs
>> 2)) {
1203 if ((i
% 10000) == 0)
1204 splat_kmem_cache_test_debug(
1205 file
, SPLAT_KMEM_TEST13_NAME
, kcp
);
1207 getnstimeofday(&stop
);
1208 delta
= timespec_sub(stop
, start
);
1209 if (delta
.tv_sec
>= max_time
) {
1210 splat_vprint(file
, SPLAT_KMEM_TEST13_NAME
,
1211 "Failed to reclaim 3/4 of cache in %ds, "
1212 "%u/%u slabs remain\n", max_time
,
1213 (unsigned)kcp
->kcp_cache
->skc_slab_total
,
1219 dp
= (dummy_page_t
*)__get_free_page(GFP_KERNEL
);
1222 splat_vprint(file
, SPLAT_KMEM_TEST13_NAME
,
1223 "Failed (%d) to allocate page with %u "
1224 "slabs still in the cache\n", fails
,
1225 (unsigned)kcp
->kcp_cache
->skc_slab_total
);
1229 list_add(&dp
->dp_list
, &list
);
1234 splat_vprint(file
, SPLAT_KMEM_TEST13_NAME
,
1235 "Successfully created %u slabs and with %d alloc "
1236 "failures reclaimed 3/4 of them in %d.%03ds\n",
1238 (int)delta
.tv_sec
, (int)delta
.tv_nsec
/ 1000000);
1240 /* Release memory pressure pages */
1241 while (!list_empty(&list
)) {
1242 dp
= list_entry(list
.next
, dummy_page_t
, dp_list
);
1243 list_del_init(&dp
->dp_list
);
1244 free_page((unsigned long)dp
);
1247 /* Release remaining kmem cache objects */
1248 splat_kmem_cache_test_kcd_free(kcp
, kct
);
1250 splat_kmem_cache_test_kct_free(kcp
, kct
);
1252 kmem_cache_destroy(kcp
->kcp_cache
);
1254 splat_kmem_cache_test_kcp_free(kcp
);
1260 splat_kmem_init(void)
1262 splat_subsystem_t
*sub
;
1264 sub
= kmalloc(sizeof(*sub
), GFP_KERNEL
);
1268 memset(sub
, 0, sizeof(*sub
));
1269 strncpy(sub
->desc
.name
, SPLAT_KMEM_NAME
, SPLAT_NAME_SIZE
);
1270 strncpy(sub
->desc
.desc
, SPLAT_KMEM_DESC
, SPLAT_DESC_SIZE
);
1271 INIT_LIST_HEAD(&sub
->subsystem_list
);
1272 INIT_LIST_HEAD(&sub
->test_list
);
1273 spin_lock_init(&sub
->test_lock
);
1274 sub
->desc
.id
= SPLAT_SUBSYSTEM_KMEM
;
1276 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST1_NAME
, SPLAT_KMEM_TEST1_DESC
,
1277 SPLAT_KMEM_TEST1_ID
, splat_kmem_test1
);
1278 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST2_NAME
, SPLAT_KMEM_TEST2_DESC
,
1279 SPLAT_KMEM_TEST2_ID
, splat_kmem_test2
);
1280 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST3_NAME
, SPLAT_KMEM_TEST3_DESC
,
1281 SPLAT_KMEM_TEST3_ID
, splat_kmem_test3
);
1282 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST4_NAME
, SPLAT_KMEM_TEST4_DESC
,
1283 SPLAT_KMEM_TEST4_ID
, splat_kmem_test4
);
1284 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST5_NAME
, SPLAT_KMEM_TEST5_DESC
,
1285 SPLAT_KMEM_TEST5_ID
, splat_kmem_test5
);
1286 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST6_NAME
, SPLAT_KMEM_TEST6_DESC
,
1287 SPLAT_KMEM_TEST6_ID
, splat_kmem_test6
);
1288 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST7_NAME
, SPLAT_KMEM_TEST7_DESC
,
1289 SPLAT_KMEM_TEST7_ID
, splat_kmem_test7
);
1290 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST8_NAME
, SPLAT_KMEM_TEST8_DESC
,
1291 SPLAT_KMEM_TEST8_ID
, splat_kmem_test8
);
1292 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST9_NAME
, SPLAT_KMEM_TEST9_DESC
,
1293 SPLAT_KMEM_TEST9_ID
, splat_kmem_test9
);
1294 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST10_NAME
, SPLAT_KMEM_TEST10_DESC
,
1295 SPLAT_KMEM_TEST10_ID
, splat_kmem_test10
);
1297 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST11_NAME
, SPLAT_KMEM_TEST11_DESC
,
1298 SPLAT_KMEM_TEST11_ID
, splat_kmem_test11
);
1300 SPLAT_TEST_INIT(sub
, SPLAT_KMEM_TEST13_NAME
, SPLAT_KMEM_TEST13_DESC
,
1301 SPLAT_KMEM_TEST13_ID
, splat_kmem_test13
);
1307 splat_kmem_fini(splat_subsystem_t
*sub
)
1310 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST13_ID
);
1312 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST11_ID
);
1314 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST10_ID
);
1315 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST9_ID
);
1316 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST8_ID
);
1317 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST7_ID
);
1318 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST6_ID
);
1319 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST5_ID
);
1320 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST4_ID
);
1321 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST3_ID
);
1322 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST2_ID
);
1323 SPLAT_TEST_FINI(sub
, SPLAT_KMEM_TEST1_ID
);
1329 splat_kmem_id(void) {
1330 return SPLAT_SUBSYSTEM_KMEM
;