Rev 1666 | Rev 1787 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1666 | Rev 1780 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <test.h> |
29 | #include <test.h> |
30 | #include <mm/slab.h> |
30 | #include <mm/slab.h> |
31 | #include <print.h> |
31 | #include <print.h> |
32 | #include <proc/thread.h> |
32 | #include <proc/thread.h> |
33 | #include <arch.h> |
33 | #include <arch.h> |
34 | #include <panic.h> |
34 | #include <panic.h> |
35 | #include <mm/frame.h> |
35 | #include <mm/frame.h> |
36 | #include <memstr.h> |
36 | #include <memstr.h> |
37 | #include <synch/condvar.h> |
37 | #include <synch/condvar.h> |
38 | #include <synch/mutex.h> |
38 | #include <synch/mutex.h> |
39 | 39 | ||
40 | #define ITEM_SIZE 256 |
40 | #define ITEM_SIZE 256 |
41 | 41 | ||
42 | /** Fill memory with 2 caches, when allocation fails, |
42 | /** Fill memory with 2 caches, when allocation fails, |
43 | * free one of the caches. We should have everything in magazines, |
43 | * free one of the caches. We should have everything in magazines, |
44 | * now allocation should clean magazines and allow for full allocation. |
44 | * now allocation should clean magazines and allow for full allocation. |
45 | */ |
45 | */ |
46 | static void totalmemtest(void) |
46 | static void totalmemtest(void) |
47 | { |
47 | { |
48 | slab_cache_t *cache1; |
48 | slab_cache_t *cache1; |
49 | slab_cache_t *cache2; |
49 | slab_cache_t *cache2; |
50 | int i; |
50 | int i; |
51 | 51 | ||
52 | void *data1, *data2; |
52 | void *data1, *data2; |
53 | void *olddata1=NULL, *olddata2=NULL; |
53 | void *olddata1=NULL, *olddata2=NULL; |
54 | 54 | ||
55 | cache1 = slab_cache_create("cache1_tst", ITEM_SIZE, 0, NULL, NULL, 0); |
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); |
56 | cache2 = slab_cache_create("cache2_tst", ITEM_SIZE, 0, NULL, NULL, 0); |
57 | 57 | ||
58 | printf("Allocating..."); |
58 | printf("Allocating..."); |
59 | /* Use atomic alloc, so that we find end of memory */ |
59 | /* Use atomic alloc, so that we find end of memory */ |
60 | do { |
60 | do { |
61 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
61 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
62 | data2 = slab_alloc(cache2, FRAME_ATOMIC); |
62 | data2 = slab_alloc(cache2, FRAME_ATOMIC); |
63 | if (!data1 || !data2) { |
63 | if (!data1 || !data2) { |
64 | if (data1) |
64 | if (data1) |
65 | slab_free(cache1,data1); |
65 | slab_free(cache1,data1); |
66 | if (data2) |
66 | if (data2) |
67 | slab_free(cache2,data2); |
67 | slab_free(cache2,data2); |
68 | break; |
68 | break; |
69 | } |
69 | } |
70 | memsetb((__address)data1, ITEM_SIZE, 0); |
70 | memsetb((uintptr_t)data1, ITEM_SIZE, 0); |
71 | memsetb((__address)data2, ITEM_SIZE, 0); |
71 | memsetb((uintptr_t)data2, ITEM_SIZE, 0); |
72 | *((void **)data1) = olddata1; |
72 | *((void **)data1) = olddata1; |
73 | *((void **)data2) = olddata2; |
73 | *((void **)data2) = olddata2; |
74 | olddata1 = data1; |
74 | olddata1 = data1; |
75 | olddata2 = data2; |
75 | olddata2 = data2; |
76 | }while(1); |
76 | }while(1); |
77 | printf("done.\n"); |
77 | printf("done.\n"); |
78 | /* We do not have memory - now deallocate cache2 */ |
78 | /* We do not have memory - now deallocate cache2 */ |
79 | printf("Deallocating cache2..."); |
79 | printf("Deallocating cache2..."); |
80 | while (olddata2) { |
80 | while (olddata2) { |
81 | data2 = *((void **)olddata2); |
81 | data2 = *((void **)olddata2); |
82 | slab_free(cache2, olddata2); |
82 | slab_free(cache2, olddata2); |
83 | olddata2 = data2; |
83 | olddata2 = data2; |
84 | } |
84 | } |
85 | printf("done.\n"); |
85 | printf("done.\n"); |
86 | 86 | ||
87 | printf("Allocating to cache1...\n"); |
87 | printf("Allocating to cache1...\n"); |
88 | for (i=0; i<30; i++) { |
88 | for (i=0; i<30; i++) { |
89 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
89 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
90 | if (!data1) { |
90 | if (!data1) { |
91 | panic("Incorrect memory size - use another test."); |
91 | panic("Incorrect memory size - use another test."); |
92 | } |
92 | } |
93 | memsetb((__address)data1, ITEM_SIZE, 0); |
93 | memsetb((uintptr_t)data1, ITEM_SIZE, 0); |
94 | *((void **)data1) = olddata1; |
94 | *((void **)data1) = olddata1; |
95 | olddata1 = data1; |
95 | olddata1 = data1; |
96 | } |
96 | } |
97 | while (1) { |
97 | while (1) { |
98 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
98 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
99 | if (!data1) { |
99 | if (!data1) { |
100 | break; |
100 | break; |
101 | } |
101 | } |
102 | memsetb((__address)data1, ITEM_SIZE, 0); |
102 | memsetb((uintptr_t)data1, ITEM_SIZE, 0); |
103 | *((void **)data1) = olddata1; |
103 | *((void **)data1) = olddata1; |
104 | olddata1 = data1; |
104 | olddata1 = data1; |
105 | } |
105 | } |
106 | printf("Deallocating cache1..."); |
106 | printf("Deallocating cache1..."); |
107 | while (olddata1) { |
107 | while (olddata1) { |
108 | data1 = *((void **)olddata1); |
108 | data1 = *((void **)olddata1); |
109 | slab_free(cache1, olddata1); |
109 | slab_free(cache1, olddata1); |
110 | olddata1 = data1; |
110 | olddata1 = data1; |
111 | } |
111 | } |
112 | printf("done.\n"); |
112 | printf("done.\n"); |
113 | slab_print_list(); |
113 | slab_print_list(); |
114 | slab_cache_destroy(cache1); |
114 | slab_cache_destroy(cache1); |
115 | slab_cache_destroy(cache2); |
115 | slab_cache_destroy(cache2); |
116 | } |
116 | } |
117 | 117 | ||
118 | slab_cache_t *thr_cache; |
118 | slab_cache_t *thr_cache; |
119 | semaphore_t thr_sem; |
119 | semaphore_t thr_sem; |
120 | condvar_t thread_starter; |
120 | condvar_t thread_starter; |
121 | mutex_t starter_mutex; |
121 | mutex_t starter_mutex; |
122 | 122 | ||
123 | #define THREADS 8 |
123 | #define THREADS 8 |
124 | 124 | ||
125 | static void slabtest(void *priv) |
125 | static void slabtest(void *priv) |
126 | { |
126 | { |
127 | void *data=NULL, *new; |
127 | void *data=NULL, *new; |
128 | 128 | ||
129 | thread_detach(THREAD); |
129 | thread_detach(THREAD); |
130 | 130 | ||
131 | mutex_lock(&starter_mutex); |
131 | mutex_lock(&starter_mutex); |
132 | condvar_wait(&thread_starter,&starter_mutex); |
132 | condvar_wait(&thread_starter,&starter_mutex); |
133 | mutex_unlock(&starter_mutex); |
133 | mutex_unlock(&starter_mutex); |
134 | 134 | ||
135 | printf("Starting thread #%d...\n",THREAD->tid); |
135 | printf("Starting thread #%d...\n",THREAD->tid); |
136 | 136 | ||
137 | /* Alloc all */ |
137 | /* Alloc all */ |
138 | printf("Thread #%d allocating...\n", THREAD->tid); |
138 | printf("Thread #%d allocating...\n", THREAD->tid); |
139 | while (1) { |
139 | while (1) { |
140 | /* Call with atomic to detect end of memory */ |
140 | /* Call with atomic to detect end of memory */ |
141 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
141 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
142 | if (!new) |
142 | if (!new) |
143 | break; |
143 | break; |
144 | *((void **)new) = data; |
144 | *((void **)new) = data; |
145 | data = new; |
145 | data = new; |
146 | } |
146 | } |
147 | printf("Thread #%d releasing...\n", THREAD->tid); |
147 | printf("Thread #%d releasing...\n", THREAD->tid); |
148 | while (data) { |
148 | while (data) { |
149 | new = *((void **)data); |
149 | new = *((void **)data); |
150 | *((void **)data) = NULL; |
150 | *((void **)data) = NULL; |
151 | slab_free(thr_cache, data); |
151 | slab_free(thr_cache, data); |
152 | data = new; |
152 | data = new; |
153 | } |
153 | } |
154 | printf("Thread #%d allocating...\n", THREAD->tid); |
154 | printf("Thread #%d allocating...\n", THREAD->tid); |
155 | while (1) { |
155 | while (1) { |
156 | /* Call with atomic to detect end of memory */ |
156 | /* Call with atomic to detect end of memory */ |
157 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
157 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
158 | if (!new) |
158 | if (!new) |
159 | break; |
159 | break; |
160 | *((void **)new) = data; |
160 | *((void **)new) = data; |
161 | data = new; |
161 | data = new; |
162 | } |
162 | } |
163 | printf("Thread #%d releasing...\n", THREAD->tid); |
163 | printf("Thread #%d releasing...\n", THREAD->tid); |
164 | while (data) { |
164 | while (data) { |
165 | new = *((void **)data); |
165 | new = *((void **)data); |
166 | *((void **)data) = NULL; |
166 | *((void **)data) = NULL; |
167 | slab_free(thr_cache, data); |
167 | slab_free(thr_cache, data); |
168 | data = new; |
168 | data = new; |
169 | } |
169 | } |
170 | 170 | ||
171 | 171 | ||
172 | printf("Thread #%d finished\n", THREAD->tid); |
172 | printf("Thread #%d finished\n", THREAD->tid); |
173 | slab_print_list(); |
173 | slab_print_list(); |
174 | semaphore_up(&thr_sem); |
174 | semaphore_up(&thr_sem); |
175 | } |
175 | } |
176 | 176 | ||
177 | 177 | ||
178 | static void multitest(int size) |
178 | static void multitest(int size) |
179 | { |
179 | { |
180 | /* Start 8 threads that just allocate as much as possible, |
180 | /* Start 8 threads that just allocate as much as possible, |
181 | * then release everything, then again allocate, then release |
181 | * then release everything, then again allocate, then release |
182 | */ |
182 | */ |
183 | thread_t *t; |
183 | thread_t *t; |
184 | int i; |
184 | int i; |
185 | 185 | ||
186 | printf("Running stress test with size %d\n", size); |
186 | printf("Running stress test with size %d\n", size); |
187 | condvar_initialize(&thread_starter); |
187 | condvar_initialize(&thread_starter); |
188 | mutex_initialize(&starter_mutex); |
188 | mutex_initialize(&starter_mutex); |
189 | 189 | ||
190 | thr_cache = slab_cache_create("thread_cache", size, 0, |
190 | thr_cache = slab_cache_create("thread_cache", size, 0, |
191 | NULL, NULL, |
191 | NULL, NULL, |
192 | 0); |
192 | 0); |
193 | semaphore_initialize(&thr_sem,0); |
193 | semaphore_initialize(&thr_sem,0); |
194 | for (i=0; i<THREADS; i++) { |
194 | for (i=0; i<THREADS; i++) { |
195 | if (!(t = thread_create(slabtest, NULL, TASK, 0, "slabtest"))) |
195 | if (!(t = thread_create(slabtest, NULL, TASK, 0, "slabtest"))) |
196 | panic("could not create thread\n"); |
196 | panic("could not create thread\n"); |
197 | thread_ready(t); |
197 | thread_ready(t); |
198 | } |
198 | } |
199 | thread_sleep(1); |
199 | thread_sleep(1); |
200 | condvar_broadcast(&thread_starter); |
200 | condvar_broadcast(&thread_starter); |
201 | 201 | ||
202 | for (i=0; i<THREADS; i++) |
202 | for (i=0; i<THREADS; i++) |
203 | semaphore_down(&thr_sem); |
203 | semaphore_down(&thr_sem); |
204 | 204 | ||
205 | slab_cache_destroy(thr_cache); |
205 | slab_cache_destroy(thr_cache); |
206 | printf("Stress test complete.\n"); |
206 | printf("Stress test complete.\n"); |
207 | } |
207 | } |
208 | 208 | ||
209 | void test(void) |
209 | void test(void) |
210 | { |
210 | { |
211 | printf("Running reclaim single-thread test .. pass1\n"); |
211 | printf("Running reclaim single-thread test .. pass1\n"); |
212 | totalmemtest(); |
212 | totalmemtest(); |
213 | printf("Running reclaim single-thread test .. pass2\n"); |
213 | printf("Running reclaim single-thread test .. pass2\n"); |
214 | totalmemtest(); |
214 | totalmemtest(); |
215 | printf("Reclaim test OK.\n"); |
215 | printf("Reclaim test OK.\n"); |
216 | 216 | ||
217 | multitest(128); |
217 | multitest(128); |
218 | multitest(2048); |
218 | multitest(2048); |
219 | multitest(8192); |
219 | multitest(8192); |
220 | printf("All done.\n"); |
220 | printf("All done.\n"); |
221 | } |
221 | } |
222 | 222 |