[4a5b2b0e] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[4a5b2b0e] | 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>
|
---|
[44a7ee5] | 35 | #include <mem.h>
|
---|
[2a46e10] | 36 | #include <synch/condvar.h>
|
---|
| 37 | #include <synch/mutex.h>
|
---|
[4a5b2b0e] | 38 |
|
---|
[cb01e1e] | 39 | #define ITEM_SIZE 256
|
---|
[4a5b2b0e] | 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 | */
|
---|
[cb01e1e] | 45 | static void totalmemtest(void)
|
---|
[4a5b2b0e] | 46 | {
|
---|
| 47 | slab_cache_t *cache1;
|
---|
| 48 | slab_cache_t *cache2;
|
---|
| 49 | int i;
|
---|
[a35b458] | 50 |
|
---|
[4a5b2b0e] | 51 | void *data1, *data2;
|
---|
[deada67] | 52 | void *olddata1 = NULL, *olddata2 = NULL;
|
---|
[a35b458] | 53 |
|
---|
[f97f1e51] | 54 | cache1 = slab_cache_create("test_cache1", ITEM_SIZE, 0, NULL, NULL, 0);
|
---|
| 55 | cache2 = slab_cache_create("test_cache2", ITEM_SIZE, 0, NULL, NULL, 0);
|
---|
[a35b458] | 56 |
|
---|
[cb01e1e] | 57 | TPRINTF("Allocating...");
|
---|
[a35b458] | 58 |
|
---|
[4a5b2b0e] | 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);
|
---|
[deada67] | 63 | if ((!data1) || (!data2)) {
|
---|
[4a5b2b0e] | 64 | if (data1)
|
---|
[deada67] | 65 | slab_free(cache1, data1);
|
---|
[4a5b2b0e] | 66 | if (data2)
|
---|
[deada67] | 67 | slab_free(cache2, data2);
|
---|
[4a5b2b0e] | 68 | break;
|
---|
| 69 | }
|
---|
[e32e092] | 70 | memsetb(data1, ITEM_SIZE, 0);
|
---|
| 71 | memsetb(data2, ITEM_SIZE, 0);
|
---|
[deada67] | 72 | *((void **) data1) = olddata1;
|
---|
| 73 | *((void **) data2) = olddata2;
|
---|
[4a5b2b0e] | 74 | olddata1 = data1;
|
---|
| 75 | olddata2 = data2;
|
---|
[cb01e1e] | 76 | } while (true);
|
---|
[a35b458] | 77 |
|
---|
[cb01e1e] | 78 | TPRINTF("done.\n");
|
---|
[a35b458] | 79 |
|
---|
[cb01e1e] | 80 | TPRINTF("Deallocating cache2...");
|
---|
[a35b458] | 81 |
|
---|
[4a5b2b0e] | 82 | /* We do not have memory - now deallocate cache2 */
|
---|
| 83 | while (olddata2) {
|
---|
[deada67] | 84 | data2 = *((void **) olddata2);
|
---|
[4a5b2b0e] | 85 | slab_free(cache2, olddata2);
|
---|
| 86 | olddata2 = data2;
|
---|
| 87 | }
|
---|
[a35b458] | 88 |
|
---|
[cb01e1e] | 89 | TPRINTF("done.\n");
|
---|
[a35b458] | 90 |
|
---|
[cb01e1e] | 91 | TPRINTF("Allocating to cache1...\n");
|
---|
[a35b458] | 92 |
|
---|
[deada67] | 93 | for (i = 0; i < 30; i++) {
|
---|
[4a5b2b0e] | 94 | data1 = slab_alloc(cache1, FRAME_ATOMIC);
|
---|
| 95 | if (!data1) {
|
---|
[cb01e1e] | 96 | TPRINTF("Incorrect memory size - use another test.");
|
---|
[7e13972] | 97 | return;
|
---|
[4a5b2b0e] | 98 | }
|
---|
[e32e092] | 99 | memsetb(data1, ITEM_SIZE, 0);
|
---|
[deada67] | 100 | *((void **) data1) = olddata1;
|
---|
[4a5b2b0e] | 101 | olddata1 = data1;
|
---|
| 102 | }
|
---|
[cb01e1e] | 103 | while (true) {
|
---|
[4a5b2b0e] | 104 | data1 = slab_alloc(cache1, FRAME_ATOMIC);
|
---|
[deada67] | 105 | if (!data1)
|
---|
[4a5b2b0e] | 106 | break;
|
---|
[e32e092] | 107 | memsetb(data1, ITEM_SIZE, 0);
|
---|
[deada67] | 108 | *((void **) data1) = olddata1;
|
---|
[4a5b2b0e] | 109 | olddata1 = data1;
|
---|
| 110 | }
|
---|
[a35b458] | 111 |
|
---|
[cb01e1e] | 112 | TPRINTF("Deallocating cache1...");
|
---|
[a35b458] | 113 |
|
---|
[086a600] | 114 | while (olddata1) {
|
---|
[deada67] | 115 | data1 = *((void **) olddata1);
|
---|
[086a600] | 116 | slab_free(cache1, olddata1);
|
---|
| 117 | olddata1 = data1;
|
---|
| 118 | }
|
---|
[a35b458] | 119 |
|
---|
[cb01e1e] | 120 | TPRINTF("done.\n");
|
---|
[a35b458] | 121 |
|
---|
[7d440e3] | 122 | if (!test_quiet)
|
---|
| 123 | slab_print_list();
|
---|
[a35b458] | 124 |
|
---|
[086a600] | 125 | slab_cache_destroy(cache1);
|
---|
| 126 | slab_cache_destroy(cache2);
|
---|
[4a5b2b0e] | 127 | }
|
---|
| 128 |
|
---|
[70b6de1] | 129 | static slab_cache_t *thr_cache;
|
---|
| 130 | static semaphore_t thr_sem;
|
---|
| 131 | static condvar_t thread_starter;
|
---|
| 132 | static mutex_t starter_mutex;
|
---|
[c5613b72] | 133 |
|
---|
[cb01e1e] | 134 | #define THREADS 8
|
---|
[c5613b72] | 135 |
|
---|
[ff14c520] | 136 | static void slabtest(void *priv)
|
---|
[c5613b72] | 137 | {
|
---|
[96348adc] | 138 | void *data = NULL, *new;
|
---|
[a35b458] | 139 |
|
---|
[8d6d76a] | 140 | thread_detach(THREAD);
|
---|
[a35b458] | 141 |
|
---|
[2a46e10] | 142 | mutex_lock(&starter_mutex);
|
---|
| 143 | condvar_wait(&thread_starter,&starter_mutex);
|
---|
| 144 | mutex_unlock(&starter_mutex);
|
---|
[a35b458] | 145 |
|
---|
[cb01e1e] | 146 | TPRINTF("Starting thread #%" PRIu64 "...\n", THREAD->tid);
|
---|
[c5613b72] | 147 |
|
---|
| 148 | /* Alloc all */
|
---|
[cb01e1e] | 149 | TPRINTF("Thread #%" PRIu64 " allocating...\n", THREAD->tid);
|
---|
[a35b458] | 150 |
|
---|
[cb01e1e] | 151 | while (true) {
|
---|
[c5613b72] | 152 | /* Call with atomic to detect end of memory */
|
---|
| 153 | new = slab_alloc(thr_cache, FRAME_ATOMIC);
|
---|
| 154 | if (!new)
|
---|
| 155 | break;
|
---|
[deada67] | 156 | *((void **) new) = data;
|
---|
[c5613b72] | 157 | data = new;
|
---|
| 158 | }
|
---|
[a35b458] | 159 |
|
---|
[cb01e1e] | 160 | TPRINTF("Thread #%" PRIu64 " releasing...\n", THREAD->tid);
|
---|
[a35b458] | 161 |
|
---|
[c5613b72] | 162 | while (data) {
|
---|
| 163 | new = *((void **)data);
|
---|
[deada67] | 164 | *((void **) data) = NULL;
|
---|
[c5613b72] | 165 | slab_free(thr_cache, data);
|
---|
| 166 | data = new;
|
---|
| 167 | }
|
---|
[a35b458] | 168 |
|
---|
[cb01e1e] | 169 | TPRINTF("Thread #%" PRIu64 " allocating...\n", THREAD->tid);
|
---|
[a35b458] | 170 |
|
---|
[cb01e1e] | 171 | while (true) {
|
---|
[c5613b72] | 172 | /* Call with atomic to detect end of memory */
|
---|
| 173 | new = slab_alloc(thr_cache, FRAME_ATOMIC);
|
---|
| 174 | if (!new)
|
---|
| 175 | break;
|
---|
[deada67] | 176 | *((void **) new) = data;
|
---|
[c5613b72] | 177 | data = new;
|
---|
| 178 | }
|
---|
[a35b458] | 179 |
|
---|
[cb01e1e] | 180 | TPRINTF("Thread #%" PRIu64 " releasing...\n", THREAD->tid);
|
---|
[a35b458] | 181 |
|
---|
[c5613b72] | 182 | while (data) {
|
---|
| 183 | new = *((void **)data);
|
---|
[deada67] | 184 | *((void **) data) = NULL;
|
---|
[c5613b72] | 185 | slab_free(thr_cache, data);
|
---|
| 186 | data = new;
|
---|
| 187 | }
|
---|
[a35b458] | 188 |
|
---|
[cb01e1e] | 189 | TPRINTF("Thread #%" PRIu64 " finished\n", THREAD->tid);
|
---|
[a35b458] | 190 |
|
---|
[7d440e3] | 191 | if (!test_quiet)
|
---|
| 192 | slab_print_list();
|
---|
[a35b458] | 193 |
|
---|
[c5613b72] | 194 | semaphore_up(&thr_sem);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[cb01e1e] | 197 | static void multitest(int size)
|
---|
[c5613b72] | 198 | {
|
---|
| 199 | /* Start 8 threads that just allocate as much as possible,
|
---|
| 200 | * then release everything, then again allocate, then release
|
---|
| 201 | */
|
---|
| 202 | thread_t *t;
|
---|
| 203 | int i;
|
---|
[a35b458] | 204 |
|
---|
[cb01e1e] | 205 | TPRINTF("Running stress test with size %d\n", size);
|
---|
[a35b458] | 206 |
|
---|
[2a46e10] | 207 | condvar_initialize(&thread_starter);
|
---|
[08a19ba] | 208 | mutex_initialize(&starter_mutex, MUTEX_PASSIVE);
|
---|
[a35b458] | 209 |
|
---|
[deada67] | 210 | thr_cache = slab_cache_create("thread_cache", size, 0, NULL, NULL, 0);
|
---|
[c5613b72] | 211 | semaphore_initialize(&thr_sem,0);
|
---|
[f97f1e51] | 212 | for (i = 0; i < THREADS; i++) {
|
---|
[6eef3c4] | 213 | if (!(t = thread_create(slabtest, NULL, TASK, THREAD_FLAG_NONE, "slabtest"))) {
|
---|
[cb01e1e] | 214 | TPRINTF("Could not create thread %d\n", i);
|
---|
[deada67] | 215 | } else
|
---|
[96348adc] | 216 | thread_ready(t);
|
---|
[c5613b72] | 217 | }
|
---|
[2a46e10] | 218 | thread_sleep(1);
|
---|
| 219 | condvar_broadcast(&thread_starter);
|
---|
[a35b458] | 220 |
|
---|
[96348adc] | 221 | for (i = 0; i < THREADS; i++)
|
---|
[c5613b72] | 222 | semaphore_down(&thr_sem);
|
---|
[a35b458] | 223 |
|
---|
[c5613b72] | 224 | slab_cache_destroy(thr_cache);
|
---|
[cb01e1e] | 225 | TPRINTF("Stress test complete.\n");
|
---|
[c5613b72] | 226 | }
|
---|
| 227 |
|
---|
[a000878c] | 228 | const char *test_slab2(void)
|
---|
[4a5b2b0e] | 229 | {
|
---|
[cb01e1e] | 230 | TPRINTF("Running reclaim single-thread test .. pass 1\n");
|
---|
| 231 | totalmemtest();
|
---|
[a35b458] | 232 |
|
---|
[cb01e1e] | 233 | TPRINTF("Running reclaim single-thread test .. pass 2\n");
|
---|
| 234 | totalmemtest();
|
---|
[a35b458] | 235 |
|
---|
[cb01e1e] | 236 | TPRINTF("Reclaim test OK.\n");
|
---|
[a35b458] | 237 |
|
---|
[cb01e1e] | 238 | multitest(128);
|
---|
| 239 | multitest(2048);
|
---|
| 240 | multitest(8192);
|
---|
[a35b458] | 241 |
|
---|
[96348adc] | 242 | return NULL;
|
---|
[4a5b2b0e] | 243 | }
|
---|