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