source: mainline/kernel/test/mm/slab2.c@ a188131

Last change on this file since a188131 was 597fa24, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Enable static initialization of kernel synchronization primitives

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