source: mainline/kernel/test/mm/slab2.c@ 00e6288

topic/simplify-dev-export
Last change on this file since 00e6288 was b169619, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 years ago

Deduplicate mem functions

There are a number of functions which are copied between
kernel, libc, and potentially boot too. mem*() functions
are first such offenders. All this duplicate code will
be moved to directory 'common'.

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