allocfree.c revision 1.2 1 1.2 christos /* $NetBSD: allocfree.c,v 1.2 2016/03/11 18:26:40 christos Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 ad * by Andrew Doran.
9 1.1 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad *
19 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 ad */
31 1.1 ad
32 1.1 ad #include <sys/cdefs.h>
33 1.2 christos __KERNEL_RCSID(0, "$NetBSD: allocfree.c,v 1.2 2016/03/11 18:26:40 christos Exp $");
34 1.1 ad
35 1.1 ad #include <sys/param.h>
36 1.1 ad #include <sys/kernel.h>
37 1.1 ad #include <sys/module.h>
38 1.1 ad #include <sys/kmem.h>
39 1.1 ad #include <sys/malloc.h>
40 1.1 ad #include <sys/kthread.h>
41 1.1 ad #include <sys/condvar.h>
42 1.1 ad #include <sys/cpu.h>
43 1.1 ad #include <sys/atomic.h>
44 1.1 ad
45 1.1 ad #include <machine/cpu_counter.h>
46 1.1 ad
47 1.1 ad MODULE(MODULE_CLASS_MISC, allocfree, NULL);
48 1.1 ad
49 1.1 ad static size_t sz = 128;
50 1.1 ad static int nthreads;
51 1.1 ad static int count = 100000;
52 1.1 ad static uint64_t total;
53 1.1 ad static kmutex_t lock;
54 1.1 ad static kcondvar_t cv;
55 1.1 ad static int nrun;
56 1.1 ad static void (*method)(void);
57 1.1 ad static int barrier;
58 1.1 ad static volatile u_int barrier2;
59 1.1 ad static int timing;
60 1.1 ad static struct pool pool;
61 1.1 ad static pool_cache_t cache;
62 1.1 ad
63 1.1 ad static void
64 1.1 ad handle_props(prop_dictionary_t props)
65 1.1 ad {
66 1.1 ad prop_number_t num;
67 1.1 ad
68 1.1 ad num = prop_dictionary_get(props, "size");
69 1.1 ad if (num != NULL && prop_object_type(num) == PROP_TYPE_NUMBER) {
70 1.1 ad sz = (size_t)prop_number_integer_value(num);
71 1.1 ad sz = max(sz, 1);
72 1.1 ad sz = min(sz, 1024*1024);
73 1.1 ad }
74 1.1 ad num = prop_dictionary_get(props, "count");
75 1.1 ad if (num != NULL && prop_object_type(num) == PROP_TYPE_NUMBER) {
76 1.1 ad count = (int)prop_number_integer_value(num);
77 1.1 ad count = min(count, 1);
78 1.1 ad }
79 1.1 ad num = prop_dictionary_get(props, "timing");
80 1.1 ad if (num != NULL && prop_object_type(num) == PROP_TYPE_NUMBER) {
81 1.1 ad timing = (int)prop_number_integer_value(num);
82 1.1 ad }
83 1.1 ad }
84 1.1 ad
85 1.1 ad static void
86 1.1 ad kmem_method(void)
87 1.1 ad {
88 1.1 ad int *p;
89 1.1 ad
90 1.1 ad p = kmem_alloc(sz, KM_SLEEP);
91 1.1 ad if (p != NULL) {
92 1.1 ad *p = 1;
93 1.1 ad kmem_free(p, sz);
94 1.1 ad }
95 1.1 ad }
96 1.1 ad
97 1.1 ad static void
98 1.1 ad malloc_method(void)
99 1.1 ad {
100 1.1 ad int *p;
101 1.1 ad
102 1.1 ad p = malloc(sz, M_DEVBUF, M_WAITOK);
103 1.1 ad if (p != NULL) {
104 1.1 ad *p = 1;
105 1.1 ad free(p, M_DEVBUF);
106 1.1 ad }
107 1.1 ad }
108 1.1 ad
109 1.1 ad static void
110 1.1 ad pool_method(void)
111 1.1 ad {
112 1.1 ad int *p;
113 1.1 ad
114 1.1 ad p = pool_get(&pool, PR_WAITOK);
115 1.1 ad if (p != NULL) {
116 1.1 ad *p = 1;
117 1.1 ad pool_put(&pool, p);
118 1.1 ad }
119 1.1 ad }
120 1.1 ad
121 1.1 ad static void
122 1.1 ad cache_method(void)
123 1.1 ad {
124 1.1 ad int *p;
125 1.1 ad
126 1.1 ad p = pool_cache_get(cache, PR_WAITOK);
127 1.1 ad if (p != NULL) {
128 1.1 ad *p = 1;
129 1.1 ad pool_cache_put(cache, p);
130 1.1 ad }
131 1.1 ad }
132 1.1 ad
133 1.1 ad static void
134 1.1 ad test_thread(void *cookie)
135 1.1 ad {
136 1.1 ad struct timespec s, e, t;
137 1.1 ad int lcv;
138 1.1 ad uint64_t x;
139 1.1 ad
140 1.1 ad kpreempt_disable();
141 1.1 ad
142 1.1 ad memset(&t, 0, sizeof(t));
143 1.1 ad x = 0;
144 1.1 ad
145 1.1 ad mutex_enter(&lock);
146 1.1 ad barrier++;
147 1.1 ad while (barrier < nthreads) {
148 1.1 ad cv_wait(&cv, &lock);
149 1.1 ad }
150 1.1 ad cv_broadcast(&cv);
151 1.1 ad mutex_exit(&lock);
152 1.1 ad
153 1.1 ad atomic_inc_uint(&barrier2);
154 1.1 ad while (barrier2 < nthreads) {
155 1.1 ad nullop(NULL);
156 1.1 ad }
157 1.1 ad
158 1.1 ad if (timing) {
159 1.1 ad for (lcv = count; lcv != 0; lcv--) {
160 1.1 ad x -= cpu_counter();
161 1.1 ad (*method)();
162 1.1 ad x += cpu_counter();
163 1.1 ad }
164 1.1 ad } else {
165 1.1 ad for (lcv = count; lcv != 0; lcv--) {
166 1.1 ad nanotime(&s);
167 1.1 ad (*method)();
168 1.1 ad nanotime(&e);
169 1.1 ad timespecsub(&e, &s, &e);
170 1.1 ad timespecadd(&e, &t, &t);
171 1.1 ad }
172 1.1 ad }
173 1.1 ad
174 1.1 ad mutex_enter(&lock);
175 1.1 ad barrier = 0;
176 1.1 ad barrier2 = 0;
177 1.1 ad if (timing) {
178 1.1 ad total += x * 1000000000LL / cpu_frequency(curcpu());
179 1.1 ad } else {
180 1.1 ad total += timespec2ns(&t);
181 1.1 ad }
182 1.1 ad if (--nrun == 0) {
183 1.1 ad cv_broadcast(&cv);
184 1.1 ad }
185 1.1 ad mutex_exit(&lock);
186 1.1 ad
187 1.1 ad kpreempt_enable();
188 1.1 ad kthread_exit(0);
189 1.1 ad }
190 1.1 ad
191 1.1 ad static void
192 1.1 ad run2(int nt, void (*func)(void))
193 1.1 ad {
194 1.1 ad struct cpu_info *ci;
195 1.1 ad CPU_INFO_ITERATOR cii;
196 1.1 ad int error;
197 1.1 ad
198 1.1 ad nthreads = nt;
199 1.1 ad total = 0;
200 1.1 ad method = func;
201 1.1 ad for (CPU_INFO_FOREACH(cii, ci)) {
202 1.1 ad if (nt-- == 0) {
203 1.1 ad break;
204 1.1 ad }
205 1.1 ad error = kthread_create(PRI_NONE, KTHREAD_MPSAFE,
206 1.1 ad ci, test_thread, NULL, NULL, "test");
207 1.1 ad if (error == 0) {
208 1.1 ad nrun++;
209 1.1 ad } else {
210 1.1 ad nthreads--;
211 1.1 ad }
212 1.1 ad }
213 1.1 ad mutex_enter(&lock);
214 1.1 ad cv_broadcast(&cv);
215 1.1 ad while (nrun > 0) {
216 1.1 ad cv_wait(&cv, &lock);
217 1.1 ad }
218 1.1 ad mutex_exit(&lock);
219 1.1 ad if (nthreads == 0) {
220 1.1 ad printf("FAILED\n");
221 1.1 ad } else {
222 1.1 ad printf("\t%d", (int)(total / nthreads / count));
223 1.1 ad }
224 1.1 ad }
225 1.1 ad
226 1.1 ad static void
227 1.1 ad run1(int nt)
228 1.1 ad {
229 1.1 ad
230 1.1 ad run2(nt, malloc_method);
231 1.1 ad run2(nt, kmem_method);
232 1.1 ad run2(nt, pool_method);
233 1.1 ad run2(nt, cache_method);
234 1.1 ad printf("\n");
235 1.1 ad
236 1.1 ad }
237 1.1 ad
238 1.1 ad static void
239 1.1 ad run0(void)
240 1.1 ad {
241 1.1 ad int i;
242 1.1 ad
243 1.1 ad for (i = 1; i <= ncpu; i++) {
244 1.2 christos printf("%zu\t%d", sz, i);
245 1.1 ad run1(i);
246 1.1 ad }
247 1.1 ad }
248 1.1 ad
249 1.1 ad static int
250 1.1 ad allocfree_modcmd(modcmd_t cmd, void *arg)
251 1.1 ad {
252 1.1 ad const char *timer;
253 1.1 ad
254 1.1 ad switch (cmd) {
255 1.1 ad case MODULE_CMD_INIT:
256 1.1 ad handle_props(arg);
257 1.1 ad timer = (timing ? "cpu_counter" : "nanotime");
258 1.1 ad printf("=> using %s() for timings\n", timer);
259 1.1 ad printf("SIZE\tNCPU\tMALLOC\tKMEM\tPOOL\tCACHE\n");
260 1.1 ad mutex_init(&lock, MUTEX_DEFAULT, IPL_NONE);
261 1.1 ad cv_init(&cv, "testcv");
262 1.1 ad pool_init(&pool, sz, 0, 0, 0, "tpool",
263 1.1 ad &pool_allocator_nointr, IPL_NONE);
264 1.1 ad cache = pool_cache_init(sz, 0, 0, 0, "tcache",
265 1.1 ad NULL, IPL_NONE, NULL, NULL, NULL);
266 1.1 ad run0();
267 1.1 ad pool_destroy(&pool);
268 1.1 ad pool_cache_destroy(cache);
269 1.1 ad mutex_destroy(&lock);
270 1.1 ad cv_destroy(&cv);
271 1.1 ad return 0;
272 1.1 ad
273 1.1 ad case MODULE_CMD_FINI:
274 1.1 ad /* XXX in theory, threads could still be running. */
275 1.1 ad return 0;
276 1.1 ad
277 1.1 ad default:
278 1.1 ad return ENOTTY;
279 1.1 ad }
280 1.1 ad }
281