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