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