Rev 3186 | Go to most recent revision | 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> |
||
34 | #include <mm/frame.h> |
||
768 | palkovsky | 35 | #include <memstr.h> |
842 | palkovsky | 36 | #include <synch/condvar.h> |
37 | #include <synch/mutex.h> |
||
767 | palkovsky | 38 | |
4227 | decky | 39 | #define ITEM_SIZE 256 |
767 | palkovsky | 40 | |
41 | /** Fill memory with 2 caches, when allocation fails, |
||
42 | * free one of the caches. We should have everything in magazines, |
||
43 | * now allocation should clean magazines and allow for full allocation. |
||
44 | */ |
||
4227 | decky | 45 | static void totalmemtest(void) |
767 | palkovsky | 46 | { |
47 | slab_cache_t *cache1; |
||
48 | slab_cache_t *cache2; |
||
49 | int i; |
||
4227 | decky | 50 | |
767 | palkovsky | 51 | void *data1, *data2; |
2053 | decky | 52 | void *olddata1 = NULL, *olddata2 = NULL; |
767 | palkovsky | 53 | |
54 | 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); |
||
2053 | decky | 56 | |
4227 | decky | 57 | TPRINTF("Allocating..."); |
2053 | decky | 58 | |
767 | palkovsky | 59 | /* Use atomic alloc, so that we find end of memory */ |
60 | do { |
||
61 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
||
62 | data2 = slab_alloc(cache2, FRAME_ATOMIC); |
||
2053 | decky | 63 | if ((!data1) || (!data2)) { |
767 | palkovsky | 64 | if (data1) |
2053 | decky | 65 | slab_free(cache1, data1); |
767 | palkovsky | 66 | if (data2) |
2053 | decky | 67 | slab_free(cache2, data2); |
767 | palkovsky | 68 | break; |
69 | } |
||
3104 | svoboda | 70 | memsetb(data1, ITEM_SIZE, 0); |
71 | memsetb(data2, ITEM_SIZE, 0); |
||
2053 | decky | 72 | *((void **) data1) = olddata1; |
73 | *((void **) data2) = olddata2; |
||
767 | palkovsky | 74 | olddata1 = data1; |
75 | olddata2 = data2; |
||
4227 | decky | 76 | } while (true); |
2053 | decky | 77 | |
4227 | decky | 78 | TPRINTF("done.\n"); |
2053 | decky | 79 | |
4227 | decky | 80 | TPRINTF("Deallocating cache2..."); |
81 | |||
767 | palkovsky | 82 | /* We do not have memory - now deallocate cache2 */ |
83 | while (olddata2) { |
||
2053 | decky | 84 | data2 = *((void **) olddata2); |
767 | palkovsky | 85 | slab_free(cache2, olddata2); |
86 | olddata2 = data2; |
||
87 | } |
||
2053 | decky | 88 | |
4227 | decky | 89 | TPRINTF("done.\n"); |
2053 | decky | 90 | |
4227 | decky | 91 | TPRINTF("Allocating to cache1...\n"); |
92 | |||
2053 | decky | 93 | for (i = 0; i < 30; i++) { |
767 | palkovsky | 94 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
95 | if (!data1) { |
||
4227 | decky | 96 | TPRINTF("Incorrect memory size - use another test."); |
2029 | decky | 97 | return; |
767 | palkovsky | 98 | } |
3104 | svoboda | 99 | memsetb(data1, ITEM_SIZE, 0); |
2053 | decky | 100 | *((void **) data1) = olddata1; |
767 | palkovsky | 101 | olddata1 = data1; |
102 | } |
||
4227 | decky | 103 | while (true) { |
767 | palkovsky | 104 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
2053 | decky | 105 | if (!data1) |
767 | palkovsky | 106 | break; |
3104 | svoboda | 107 | memsetb(data1, ITEM_SIZE, 0); |
2053 | decky | 108 | *((void **) data1) = olddata1; |
767 | palkovsky | 109 | olddata1 = data1; |
110 | } |
||
2053 | decky | 111 | |
4227 | decky | 112 | TPRINTF("Deallocating cache1..."); |
2053 | decky | 113 | |
768 | palkovsky | 114 | while (olddata1) { |
2053 | decky | 115 | data1 = *((void **) olddata1); |
768 | palkovsky | 116 | slab_free(cache1, olddata1); |
117 | olddata1 = data1; |
||
118 | } |
||
2053 | decky | 119 | |
4227 | decky | 120 | TPRINTF("done.\n"); |
2053 | decky | 121 | |
4227 | decky | 122 | slab_print_list(); |
123 | |||
768 | palkovsky | 124 | slab_cache_destroy(cache1); |
125 | slab_cache_destroy(cache2); |
||
767 | palkovsky | 126 | } |
127 | |||
2022 | decky | 128 | static slab_cache_t *thr_cache; |
129 | static semaphore_t thr_sem; |
||
130 | static condvar_t thread_starter; |
||
131 | static mutex_t starter_mutex; |
||
773 | palkovsky | 132 | |
4227 | decky | 133 | #define THREADS 8 |
773 | palkovsky | 134 | |
1062 | jermar | 135 | static void slabtest(void *priv) |
773 | palkovsky | 136 | { |
2028 | decky | 137 | void *data = NULL, *new; |
2053 | decky | 138 | |
1658 | vana | 139 | thread_detach(THREAD); |
2053 | decky | 140 | |
842 | palkovsky | 141 | mutex_lock(&starter_mutex); |
142 | condvar_wait(&thread_starter,&starter_mutex); |
||
143 | mutex_unlock(&starter_mutex); |
||
2053 | decky | 144 | |
4227 | decky | 145 | TPRINTF("Starting thread #%" PRIu64 "...\n", THREAD->tid); |
773 | palkovsky | 146 | |
147 | /* Alloc all */ |
||
4227 | decky | 148 | TPRINTF("Thread #%" PRIu64 " allocating...\n", THREAD->tid); |
2053 | decky | 149 | |
4227 | decky | 150 | while (true) { |
773 | palkovsky | 151 | /* Call with atomic to detect end of memory */ |
152 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
||
153 | if (!new) |
||
154 | break; |
||
2053 | decky | 155 | *((void **) new) = data; |
773 | palkovsky | 156 | data = new; |
157 | } |
||
2053 | decky | 158 | |
4227 | decky | 159 | TPRINTF("Thread #%" PRIu64 " releasing...\n", THREAD->tid); |
2053 | decky | 160 | |
773 | palkovsky | 161 | while (data) { |
162 | new = *((void **)data); |
||
2053 | decky | 163 | *((void **) data) = NULL; |
773 | palkovsky | 164 | slab_free(thr_cache, data); |
165 | data = new; |
||
166 | } |
||
2053 | decky | 167 | |
4227 | decky | 168 | TPRINTF("Thread #%" PRIu64 " allocating...\n", THREAD->tid); |
2053 | decky | 169 | |
4227 | decky | 170 | while (true) { |
773 | palkovsky | 171 | /* Call with atomic to detect end of memory */ |
172 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
||
173 | if (!new) |
||
174 | break; |
||
2053 | decky | 175 | *((void **) new) = data; |
773 | palkovsky | 176 | data = new; |
177 | } |
||
2053 | decky | 178 | |
4227 | decky | 179 | TPRINTF("Thread #%" PRIu64 " releasing...\n", THREAD->tid); |
2053 | decky | 180 | |
773 | palkovsky | 181 | while (data) { |
182 | new = *((void **)data); |
||
2053 | decky | 183 | *((void **) data) = NULL; |
773 | palkovsky | 184 | slab_free(thr_cache, data); |
185 | data = new; |
||
186 | } |
||
2053 | decky | 187 | |
4227 | decky | 188 | TPRINTF("Thread #%" PRIu64 " finished\n", THREAD->tid); |
2053 | decky | 189 | |
773 | palkovsky | 190 | slab_print_list(); |
191 | semaphore_up(&thr_sem); |
||
192 | } |
||
193 | |||
4227 | decky | 194 | static void multitest(int size) |
773 | palkovsky | 195 | { |
196 | /* Start 8 threads that just allocate as much as possible, |
||
197 | * then release everything, then again allocate, then release |
||
198 | */ |
||
199 | thread_t *t; |
||
200 | int i; |
||
2053 | decky | 201 | |
4227 | decky | 202 | TPRINTF("Running stress test with size %d\n", size); |
2053 | decky | 203 | |
842 | palkovsky | 204 | condvar_initialize(&thread_starter); |
3186 | jermar | 205 | mutex_initialize(&starter_mutex, MUTEX_PASSIVE); |
4227 | decky | 206 | |
2053 | decky | 207 | thr_cache = slab_cache_create("thread_cache", size, 0, NULL, NULL, 0); |
773 | palkovsky | 208 | semaphore_initialize(&thr_sem,0); |
2028 | decky | 209 | for (i = 0; i < THREADS; i++) { |
2053 | decky | 210 | if (!(t = thread_create(slabtest, NULL, TASK, 0, "slabtest", false))) { |
4227 | decky | 211 | TPRINTF("Could not create thread %d\n", i); |
2053 | decky | 212 | } else |
2028 | decky | 213 | thread_ready(t); |
773 | palkovsky | 214 | } |
842 | palkovsky | 215 | thread_sleep(1); |
216 | condvar_broadcast(&thread_starter); |
||
4227 | decky | 217 | |
2028 | decky | 218 | for (i = 0; i < THREADS; i++) |
773 | palkovsky | 219 | semaphore_down(&thr_sem); |
220 | |||
221 | slab_cache_destroy(thr_cache); |
||
4227 | decky | 222 | TPRINTF("Stress test complete.\n"); |
773 | palkovsky | 223 | } |
224 | |||
4227 | decky | 225 | char *test_slab2(void) |
767 | palkovsky | 226 | { |
4227 | decky | 227 | TPRINTF("Running reclaim single-thread test .. pass 1\n"); |
228 | totalmemtest(); |
||
2028 | decky | 229 | |
4227 | decky | 230 | TPRINTF("Running reclaim single-thread test .. pass 2\n"); |
231 | totalmemtest(); |
||
2053 | decky | 232 | |
4227 | decky | 233 | TPRINTF("Reclaim test OK.\n"); |
2053 | decky | 234 | |
4227 | decky | 235 | multitest(128); |
236 | multitest(2048); |
||
237 | multitest(8192); |
||
238 | |||
2028 | decky | 239 | return NULL; |
767 | palkovsky | 240 | } |