4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD$");
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/malloc.h>
40 #include <sys/kthread.h>
41 #include <sys/condvar.h>
43 #include <sys/atomic.h>
45 #include <machine/cpu_counter.h>
47 MODULE(MODULE_CLASS_MISC
, allocfree
, NULL
);
49 static size_t sz
= 128;
51 static int count
= 100000;
52 static uint64_t total
;
56 static void (*method
)(void);
58 static volatile u_int barrier2
;
60 static struct pool pool
;
61 static pool_cache_t cache
;
64 handle_props(prop_dictionary_t props
)
68 num
= prop_dictionary_get(props
, "size");
69 if (num
!= NULL
&& prop_object_type(num
) == PROP_TYPE_NUMBER
) {
70 sz
= (size_t)prop_number_integer_value(num
);
72 sz
= min(sz
, 1024*1024);
74 num
= prop_dictionary_get(props
, "count");
75 if (num
!= NULL
&& prop_object_type(num
) == PROP_TYPE_NUMBER
) {
76 count
= (int)prop_number_integer_value(num
);
77 count
= min(count
, 1);
79 num
= prop_dictionary_get(props
, "timing");
80 if (num
!= NULL
&& prop_object_type(num
) == PROP_TYPE_NUMBER
) {
81 timing
= (int)prop_number_integer_value(num
);
90 p
= kmem_alloc(sz
, KM_SLEEP
);
102 p
= malloc(sz
, M_DEVBUF
, M_WAITOK
);
114 p
= pool_get(&pool
, PR_WAITOK
);
126 p
= pool_cache_get(cache
, PR_WAITOK
);
129 pool_cache_put(cache
, p
);
134 test_thread(void *cookie
)
136 struct timespec s
, e
, t
;
142 memset(&t
, 0, sizeof(t
));
147 while (barrier
< nthreads
) {
153 atomic_inc_uint(&barrier2
);
154 while (barrier2
< nthreads
) {
159 for (lcv
= count
; lcv
!= 0; lcv
--) {
165 for (lcv
= count
; lcv
!= 0; lcv
--) {
169 timespecsub(&e
, &s
, &e
);
170 timespecadd(&e
, &t
, &t
);
178 total
+= x
* 1000000000LL / cpu_frequency(curcpu());
180 total
+= timespec2ns(&t
);
192 run2(int nt
, void (*func
)(void))
195 CPU_INFO_ITERATOR cii
;
201 for (CPU_INFO_FOREACH(cii
, ci
)) {
205 error
= kthread_create(PRI_NONE
, KTHREAD_MPSAFE
,
206 ci
, test_thread
, NULL
, NULL
, "test");
222 printf("\t%d", (int)(total
/ nthreads
/ count
));
230 run2(nt
, malloc_method
);
231 run2(nt
, kmem_method
);
232 run2(nt
, pool_method
);
233 run2(nt
, cache_method
);
243 for (i
= 1; i
<= ncpu
; i
++) {
244 printf("%d\t%d", sz
, i
);
250 allocfree_modcmd(modcmd_t cmd
, void *arg
)
255 case MODULE_CMD_INIT
:
257 timer
= (timing
? "cpu_counter" : "nanotime");
258 printf("=> using %s() for timings\n", timer
);
259 printf("SIZE\tNCPU\tMALLOC\tKMEM\tPOOL\tCACHE\n");
260 mutex_init(&lock
, MUTEX_DEFAULT
, IPL_NONE
);
261 cv_init(&cv
, "testcv");
262 pool_init(&pool
, sz
, 0, 0, 0, "tpool",
263 &pool_allocator_nointr
, IPL_NONE
);
264 cache
= pool_cache_init(sz
, 0, 0, 0, "tcache",
265 NULL
, IPL_NONE
, NULL
, NULL
, NULL
);
268 pool_cache_destroy(cache
);
269 mutex_destroy(&lock
);
273 case MODULE_CMD_FINI
:
274 /* XXX in theory, threads could still be running. */