Rev 3343 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3343 | Rev 3862 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Ondrej Palkovsky |
2 | * Copyright (c) 2006 Ondrej Palkovsky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <test.h> |
29 | #include <test.h> |
30 | #include <mm/slab.h> |
30 | #include <mm/slab.h> |
31 | #include <print.h> |
31 | #include <print.h> |
32 | #include <proc/thread.h> |
32 | #include <proc/thread.h> |
33 | #include <arch.h> |
33 | #include <arch.h> |
- | 34 | #include <arch/asm.h> |
|
34 | #include <mm/frame.h> |
35 | #include <mm/frame.h> |
35 | #include <memstr.h> |
36 | #include <memstr.h> |
36 | #include <synch/condvar.h> |
37 | #include <synch/condvar.h> |
37 | #include <synch/mutex.h> |
38 | #include <synch/mutex.h> |
38 | 39 | ||
39 | #define ITEM_SIZE 256 |
40 | #define ITEM_SIZE 256 |
40 | 41 | ||
41 | /** Fill memory with 2 caches, when allocation fails, |
42 | /** Fill memory with 2 caches, when allocation fails, |
42 | * free one of the caches. We should have everything in magazines, |
43 | * free one of the caches. We should have everything in magazines, |
43 | * now allocation should clean magazines and allow for full allocation. |
44 | * now allocation should clean magazines and allow for full allocation. |
44 | */ |
45 | */ |
45 | static void totalmemtest(bool quiet) |
46 | static void totalmemtest(bool quiet) |
46 | { |
47 | { |
47 | slab_cache_t *cache1; |
48 | slab_cache_t *cache1; |
48 | slab_cache_t *cache2; |
49 | slab_cache_t *cache2; |
49 | int i; |
50 | int i; |
50 | 51 | ||
51 | void *data1, *data2; |
52 | void *data1, *data2; |
52 | void *olddata1 = NULL, *olddata2 = NULL; |
53 | void *olddata1 = NULL, *olddata2 = NULL; |
53 | 54 | ||
54 | cache1 = slab_cache_create("cache1_tst", ITEM_SIZE, 0, NULL, NULL, 0); |
55 | cache1 = slab_cache_create("cache1_tst", ITEM_SIZE, 0, NULL, NULL, 0); |
55 | cache2 = slab_cache_create("cache2_tst", ITEM_SIZE, 0, NULL, NULL, 0); |
56 | cache2 = slab_cache_create("cache2_tst", ITEM_SIZE, 0, NULL, NULL, 0); |
56 | 57 | ||
57 | if (!quiet) |
58 | if (!quiet) |
58 | printf("Allocating..."); |
59 | printf("Allocating..."); |
59 | 60 | ||
60 | /* Use atomic alloc, so that we find end of memory */ |
61 | /* Use atomic alloc, so that we find end of memory */ |
61 | do { |
62 | do { |
62 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
63 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
63 | data2 = slab_alloc(cache2, FRAME_ATOMIC); |
64 | data2 = slab_alloc(cache2, FRAME_ATOMIC); |
64 | if ((!data1) || (!data2)) { |
65 | if ((!data1) || (!data2)) { |
65 | if (data1) |
66 | if (data1) |
66 | slab_free(cache1, data1); |
67 | slab_free(cache1, data1); |
67 | if (data2) |
68 | if (data2) |
68 | slab_free(cache2, data2); |
69 | slab_free(cache2, data2); |
69 | break; |
70 | break; |
70 | } |
71 | } |
71 | memsetb(data1, ITEM_SIZE, 0); |
72 | memsetb(data1, ITEM_SIZE, 0); |
72 | memsetb(data2, ITEM_SIZE, 0); |
73 | memsetb(data2, ITEM_SIZE, 0); |
73 | *((void **) data1) = olddata1; |
74 | *((void **) data1) = olddata1; |
74 | *((void **) data2) = olddata2; |
75 | *((void **) data2) = olddata2; |
75 | olddata1 = data1; |
76 | olddata1 = data1; |
76 | olddata2 = data2; |
77 | olddata2 = data2; |
77 | } while (1); |
78 | } while (1); |
78 | 79 | ||
79 | if (!quiet) { |
80 | if (!quiet) { |
80 | printf("done.\n"); |
81 | printf("done.\n"); |
81 | printf("Deallocating cache2..."); |
82 | printf("Deallocating cache2..."); |
82 | } |
83 | } |
83 | 84 | ||
84 | /* We do not have memory - now deallocate cache2 */ |
85 | /* We do not have memory - now deallocate cache2 */ |
85 | while (olddata2) { |
86 | while (olddata2) { |
86 | data2 = *((void **) olddata2); |
87 | data2 = *((void **) olddata2); |
87 | slab_free(cache2, olddata2); |
88 | slab_free(cache2, olddata2); |
88 | olddata2 = data2; |
89 | olddata2 = data2; |
89 | } |
90 | } |
90 | 91 | ||
91 | if (!quiet) { |
92 | if (!quiet) { |
92 | printf("done.\n"); |
93 | printf("done.\n"); |
93 | printf("Allocating to cache1...\n"); |
94 | printf("Allocating to cache1...\n"); |
94 | } |
95 | } |
95 | 96 | ||
96 | for (i = 0; i < 30; i++) { |
97 | for (i = 0; i < 30; i++) { |
97 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
98 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
98 | if (!data1) { |
99 | if (!data1) { |
99 | if (!quiet) |
100 | if (!quiet) |
100 | printf("Incorrect memory size - use another test."); |
101 | printf("Incorrect memory size - use another test."); |
101 | return; |
102 | return; |
102 | } |
103 | } |
103 | memsetb(data1, ITEM_SIZE, 0); |
104 | memsetb(data1, ITEM_SIZE, 0); |
104 | *((void **) data1) = olddata1; |
105 | *((void **) data1) = olddata1; |
105 | olddata1 = data1; |
106 | olddata1 = data1; |
106 | } |
107 | } |
107 | while (1) { |
108 | while (1) { |
108 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
109 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
109 | if (!data1) |
110 | if (!data1) |
110 | break; |
111 | break; |
111 | memsetb(data1, ITEM_SIZE, 0); |
112 | memsetb(data1, ITEM_SIZE, 0); |
112 | *((void **) data1) = olddata1; |
113 | *((void **) data1) = olddata1; |
113 | olddata1 = data1; |
114 | olddata1 = data1; |
114 | } |
115 | } |
115 | 116 | ||
116 | if (!quiet) |
117 | if (!quiet) |
117 | printf("Deallocating cache1..."); |
118 | printf("Deallocating cache1..."); |
118 | 119 | ||
119 | while (olddata1) { |
120 | while (olddata1) { |
120 | data1 = *((void **) olddata1); |
121 | data1 = *((void **) olddata1); |
121 | slab_free(cache1, olddata1); |
122 | slab_free(cache1, olddata1); |
122 | olddata1 = data1; |
123 | olddata1 = data1; |
123 | } |
124 | } |
124 | 125 | ||
125 | if (!quiet) { |
126 | if (!quiet) { |
126 | printf("done.\n"); |
127 | printf("done.\n"); |
127 | slab_print_list(); |
128 | slab_print_list(); |
128 | } |
129 | } |
129 | 130 | ||
130 | slab_cache_destroy(cache1); |
131 | slab_cache_destroy(cache1); |
131 | slab_cache_destroy(cache2); |
132 | slab_cache_destroy(cache2); |
132 | } |
133 | } |
133 | 134 | ||
134 | static slab_cache_t *thr_cache; |
135 | static slab_cache_t *thr_cache; |
135 | static semaphore_t thr_sem; |
136 | static semaphore_t thr_sem; |
136 | static condvar_t thread_starter; |
137 | static condvar_t thread_starter; |
137 | static mutex_t starter_mutex; |
138 | static mutex_t starter_mutex; |
138 | static bool sh_quiet; |
139 | static bool sh_quiet; |
139 | 140 | ||
140 | #define THREADS 8 |
141 | #define THREADS 8 |
141 | 142 | ||
142 | static void slabtest(void *priv) |
143 | static void slabtest(void *priv) |
143 | { |
144 | { |
144 | void *data = NULL, *new; |
145 | void *data = NULL, *new; |
145 | 146 | ||
146 | thread_detach(THREAD); |
147 | thread_detach(THREAD); |
147 | 148 | ||
148 | mutex_lock(&starter_mutex); |
149 | mutex_lock(&starter_mutex); |
149 | condvar_wait(&thread_starter,&starter_mutex); |
150 | condvar_wait(&thread_starter,&starter_mutex); |
150 | mutex_unlock(&starter_mutex); |
151 | mutex_unlock(&starter_mutex); |
151 | 152 | ||
152 | if (!sh_quiet) |
153 | if (!sh_quiet) |
153 | printf("Starting thread #%" PRIu64 "...\n", THREAD->tid); |
154 | printf("Starting thread #%" PRIu64 "...\n", THREAD->tid); |
154 | 155 | ||
155 | /* Alloc all */ |
156 | /* Alloc all */ |
156 | if (!sh_quiet) |
157 | if (!sh_quiet) |
157 | printf("Thread #%" PRIu64 " allocating...\n", THREAD->tid); |
158 | printf("Thread #%" PRIu64 " allocating...\n", THREAD->tid); |
158 | 159 | ||
159 | while (1) { |
160 | while (1) { |
160 | /* Call with atomic to detect end of memory */ |
161 | /* Call with atomic to detect end of memory */ |
161 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
162 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
162 | if (!new) |
163 | if (!new) |
163 | break; |
164 | break; |
164 | *((void **) new) = data; |
165 | *((void **) new) = data; |
165 | data = new; |
166 | data = new; |
166 | } |
167 | } |
167 | 168 | ||
168 | if (!sh_quiet) |
169 | if (!sh_quiet) |
169 | printf("Thread #%" PRIu64 " releasing...\n", THREAD->tid); |
170 | printf("Thread #%" PRIu64 " releasing...\n", THREAD->tid); |
170 | 171 | ||
171 | while (data) { |
172 | while (data) { |
172 | new = *((void **)data); |
173 | new = *((void **)data); |
173 | *((void **) data) = NULL; |
174 | *((void **) data) = NULL; |
174 | slab_free(thr_cache, data); |
175 | slab_free(thr_cache, data); |
175 | data = new; |
176 | data = new; |
176 | } |
177 | } |
177 | 178 | ||
178 | if (!sh_quiet) |
179 | if (!sh_quiet) |
179 | printf("Thread #%" PRIu64 " allocating...\n", THREAD->tid); |
180 | printf("Thread #%" PRIu64 " allocating...\n", THREAD->tid); |
180 | 181 | ||
181 | while (1) { |
182 | while (1) { |
182 | /* Call with atomic to detect end of memory */ |
183 | /* Call with atomic to detect end of memory */ |
183 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
184 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
184 | if (!new) |
185 | if (!new) |
185 | break; |
186 | break; |
186 | *((void **) new) = data; |
187 | *((void **) new) = data; |
187 | data = new; |
188 | data = new; |
188 | } |
189 | } |
189 | 190 | ||
190 | if (!sh_quiet) |
191 | if (!sh_quiet) |
191 | printf("Thread #%" PRIu64 " releasing...\n", THREAD->tid); |
192 | printf("Thread #%" PRIu64 " releasing...\n", THREAD->tid); |
192 | 193 | ||
193 | while (data) { |
194 | while (data) { |
194 | new = *((void **)data); |
195 | new = *((void **)data); |
195 | *((void **) data) = NULL; |
196 | *((void **) data) = NULL; |
196 | slab_free(thr_cache, data); |
197 | slab_free(thr_cache, data); |
197 | data = new; |
198 | data = new; |
198 | } |
199 | } |
199 | 200 | ||
200 | if (!sh_quiet) |
201 | if (!sh_quiet) |
201 | printf("Thread #%" PRIu64 " finished\n", THREAD->tid); |
202 | printf("Thread #%" PRIu64 " finished\n", THREAD->tid); |
202 | 203 | ||
203 | slab_print_list(); |
204 | slab_print_list(); |
204 | semaphore_up(&thr_sem); |
205 | semaphore_up(&thr_sem); |
205 | } |
206 | } |
206 | 207 | ||
207 | static void multitest(int size, bool quiet) |
208 | static void multitest(int size, bool quiet) |
208 | { |
209 | { |
209 | /* Start 8 threads that just allocate as much as possible, |
210 | /* Start 8 threads that just allocate as much as possible, |
210 | * then release everything, then again allocate, then release |
211 | * then release everything, then again allocate, then release |
211 | */ |
212 | */ |
212 | thread_t *t; |
213 | thread_t *t; |
213 | int i; |
214 | int i; |
214 | 215 | ||
215 | if (!quiet) |
216 | if (!quiet) |
216 | printf("Running stress test with size %d\n", size); |
217 | printf("Running stress test with size %d\n", size); |
217 | 218 | ||
218 | condvar_initialize(&thread_starter); |
219 | condvar_initialize(&thread_starter); |
219 | mutex_initialize(&starter_mutex, MUTEX_PASSIVE); |
220 | mutex_initialize(&starter_mutex, MUTEX_PASSIVE); |
220 | 221 | ||
221 | thr_cache = slab_cache_create("thread_cache", size, 0, NULL, NULL, 0); |
222 | thr_cache = slab_cache_create("thread_cache", size, 0, NULL, NULL, 0); |
222 | semaphore_initialize(&thr_sem,0); |
223 | semaphore_initialize(&thr_sem,0); |
223 | for (i = 0; i < THREADS; i++) { |
224 | for (i = 0; i < THREADS; i++) { |
224 | if (!(t = thread_create(slabtest, NULL, TASK, 0, "slabtest", false))) { |
225 | if (!(t = thread_create(slabtest, NULL, TASK, 0, "slabtest", false))) { |
225 | if (!quiet) |
226 | if (!quiet) |
226 | printf("Could not create thread %d\n", i); |
227 | printf("Could not create thread %d\n", i); |
227 | } else |
228 | } else |
228 | thread_ready(t); |
229 | thread_ready(t); |
229 | } |
230 | } |
230 | thread_sleep(1); |
231 | thread_sleep(1); |
231 | condvar_broadcast(&thread_starter); |
232 | condvar_broadcast(&thread_starter); |
232 | 233 | ||
233 | for (i = 0; i < THREADS; i++) |
234 | for (i = 0; i < THREADS; i++) |
234 | semaphore_down(&thr_sem); |
235 | semaphore_down(&thr_sem); |
235 | 236 | ||
236 | slab_cache_destroy(thr_cache); |
237 | slab_cache_destroy(thr_cache); |
237 | if (!quiet) |
238 | if (!quiet) |
238 | printf("Stress test complete.\n"); |
239 | printf("Stress test complete.\n"); |
239 | } |
240 | } |
240 | 241 | ||
241 | char * test_slab2(bool quiet) |
242 | char * test_slab2(bool quiet) |
242 | { |
243 | { |
243 | sh_quiet = quiet; |
244 | sh_quiet = quiet; |
244 | 245 | ||
245 | if (!quiet) |
246 | if (!quiet) |
246 | printf("Running reclaim single-thread test .. pass 1\n"); |
247 | printf("Running reclaim single-thread test .. pass 1\n"); |
247 | totalmemtest(quiet); |
248 | totalmemtest(quiet); |
248 | if (!quiet) |
249 | if (!quiet) |
249 | printf("Running reclaim single-thread test .. pass 2\n"); |
250 | printf("Running reclaim single-thread test .. pass 2\n"); |
250 | totalmemtest(quiet); |
251 | totalmemtest(quiet); |
251 | if (!quiet) |
252 | if (!quiet) |
252 | printf("Reclaim test OK.\n"); |
253 | printf("Reclaim test OK.\n"); |
253 | 254 | ||
254 | multitest(128, quiet); |
255 | multitest(128, quiet); |
255 | multitest(2048, quiet); |
256 | multitest(2048, quiet); |
256 | multitest(8192, quiet); |
257 | multitest(8192, quiet); |
257 | 258 | ||
258 | return NULL; |
259 | return NULL; |
259 | } |
260 | } |
260 | 261 |