Rev 2042 | Rev 2071 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
767 | palkovsky | 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 <print.h> |
||
32 | #include <proc/thread.h> |
||
33 | #include <arch.h> |
||
34 | #include <mm/frame.h> |
||
768 | palkovsky | 35 | #include <memstr.h> |
842 | palkovsky | 36 | #include <synch/condvar.h> |
37 | #include <synch/mutex.h> |
||
767 | palkovsky | 38 | |
39 | #define ITEM_SIZE 256 |
||
40 | |||
41 | /** Fill memory with 2 caches, when allocation fails, |
||
42 | * free one of the caches. We should have everything in magazines, |
||
43 | * now allocation should clean magazines and allow for full allocation. |
||
44 | */ |
||
45 | static void totalmemtest(void) |
||
46 | { |
||
47 | slab_cache_t *cache1; |
||
48 | slab_cache_t *cache2; |
||
49 | int i; |
||
50 | |||
51 | void *data1, *data2; |
||
52 | void *olddata1=NULL, *olddata2=NULL; |
||
53 | |||
54 | cache1 = slab_cache_create("cache1_tst", ITEM_SIZE, 0, NULL, NULL, 0); |
||
55 | cache2 = slab_cache_create("cache2_tst", ITEM_SIZE, 0, NULL, NULL, 0); |
||
56 | |||
57 | printf("Allocating..."); |
||
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 | } |
||
1780 | jermar | 69 | memsetb((uintptr_t)data1, ITEM_SIZE, 0); |
70 | memsetb((uintptr_t)data2, ITEM_SIZE, 0); |
||
767 | palkovsky | 71 | *((void **)data1) = olddata1; |
72 | *((void **)data2) = olddata2; |
||
73 | olddata1 = data1; |
||
74 | olddata2 = data2; |
||
2028 | decky | 75 | } while(1); |
767 | palkovsky | 76 | printf("done.\n"); |
77 | /* We do not have memory - now deallocate cache2 */ |
||
78 | printf("Deallocating cache2..."); |
||
79 | while (olddata2) { |
||
80 | data2 = *((void **)olddata2); |
||
81 | slab_free(cache2, olddata2); |
||
82 | olddata2 = data2; |
||
83 | } |
||
84 | printf("done.\n"); |
||
85 | |||
86 | printf("Allocating to cache1...\n"); |
||
87 | for (i=0; i<30; i++) { |
||
88 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
||
89 | if (!data1) { |
||
2029 | decky | 90 | printf("Incorrect memory size - use another test."); |
91 | return; |
||
767 | palkovsky | 92 | } |
1780 | jermar | 93 | memsetb((uintptr_t)data1, ITEM_SIZE, 0); |
767 | palkovsky | 94 | *((void **)data1) = olddata1; |
95 | olddata1 = data1; |
||
96 | } |
||
97 | while (1) { |
||
98 | data1 = slab_alloc(cache1, FRAME_ATOMIC); |
||
99 | if (!data1) { |
||
100 | break; |
||
101 | } |
||
1780 | jermar | 102 | memsetb((uintptr_t)data1, ITEM_SIZE, 0); |
767 | palkovsky | 103 | *((void **)data1) = olddata1; |
104 | olddata1 = data1; |
||
105 | } |
||
768 | palkovsky | 106 | printf("Deallocating cache1..."); |
107 | while (olddata1) { |
||
108 | data1 = *((void **)olddata1); |
||
109 | slab_free(cache1, olddata1); |
||
110 | olddata1 = data1; |
||
111 | } |
||
112 | printf("done.\n"); |
||
113 | slab_print_list(); |
||
114 | slab_cache_destroy(cache1); |
||
115 | slab_cache_destroy(cache2); |
||
767 | palkovsky | 116 | } |
117 | |||
2022 | decky | 118 | static slab_cache_t *thr_cache; |
119 | static semaphore_t thr_sem; |
||
120 | static condvar_t thread_starter; |
||
121 | static mutex_t starter_mutex; |
||
773 | palkovsky | 122 | |
123 | #define THREADS 8 |
||
124 | |||
1062 | jermar | 125 | static void slabtest(void *priv) |
773 | palkovsky | 126 | { |
2028 | decky | 127 | void *data = NULL, *new; |
773 | palkovsky | 128 | |
1658 | vana | 129 | thread_detach(THREAD); |
130 | |||
842 | palkovsky | 131 | mutex_lock(&starter_mutex); |
132 | condvar_wait(&thread_starter,&starter_mutex); |
||
133 | mutex_unlock(&starter_mutex); |
||
134 | |||
773 | palkovsky | 135 | printf("Starting thread #%d...\n",THREAD->tid); |
136 | |||
137 | /* Alloc all */ |
||
138 | printf("Thread #%d allocating...\n", THREAD->tid); |
||
139 | while (1) { |
||
140 | /* Call with atomic to detect end of memory */ |
||
141 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
||
142 | if (!new) |
||
143 | break; |
||
144 | *((void **)new) = data; |
||
145 | data = new; |
||
146 | } |
||
147 | printf("Thread #%d releasing...\n", THREAD->tid); |
||
148 | while (data) { |
||
149 | new = *((void **)data); |
||
1666 | palkovsky | 150 | *((void **)data) = NULL; |
773 | palkovsky | 151 | slab_free(thr_cache, data); |
152 | data = new; |
||
153 | } |
||
154 | printf("Thread #%d allocating...\n", THREAD->tid); |
||
155 | while (1) { |
||
156 | /* Call with atomic to detect end of memory */ |
||
157 | new = slab_alloc(thr_cache, FRAME_ATOMIC); |
||
158 | if (!new) |
||
159 | break; |
||
160 | *((void **)new) = data; |
||
161 | data = new; |
||
162 | } |
||
163 | printf("Thread #%d releasing...\n", THREAD->tid); |
||
164 | while (data) { |
||
165 | new = *((void **)data); |
||
1666 | palkovsky | 166 | *((void **)data) = NULL; |
773 | palkovsky | 167 | slab_free(thr_cache, data); |
168 | data = new; |
||
169 | } |
||
170 | |||
171 | printf("Thread #%d finished\n", THREAD->tid); |
||
172 | slab_print_list(); |
||
173 | semaphore_up(&thr_sem); |
||
174 | } |
||
175 | |||
176 | static void multitest(int size) |
||
177 | { |
||
178 | /* Start 8 threads that just allocate as much as possible, |
||
179 | * then release everything, then again allocate, then release |
||
180 | */ |
||
181 | thread_t *t; |
||
182 | int i; |
||
183 | |||
184 | printf("Running stress test with size %d\n", size); |
||
842 | palkovsky | 185 | condvar_initialize(&thread_starter); |
186 | mutex_initialize(&starter_mutex); |
||
187 | |||
773 | palkovsky | 188 | thr_cache = slab_cache_create("thread_cache", size, 0, |
189 | NULL, NULL, |
||
190 | 0); |
||
191 | semaphore_initialize(&thr_sem,0); |
||
2028 | decky | 192 | for (i = 0; i < THREADS; i++) { |
2042 | decky | 193 | if (!(t = thread_create(slabtest, NULL, TASK, 0, "slabtest", false))) |
2028 | decky | 194 | printf("Could not create thread %d\n", i); |
195 | else |
||
196 | thread_ready(t); |
||
773 | palkovsky | 197 | } |
842 | palkovsky | 198 | thread_sleep(1); |
199 | condvar_broadcast(&thread_starter); |
||
773 | palkovsky | 200 | |
2028 | decky | 201 | for (i = 0; i < THREADS; i++) |
773 | palkovsky | 202 | semaphore_down(&thr_sem); |
203 | |||
204 | slab_cache_destroy(thr_cache); |
||
205 | printf("Stress test complete.\n"); |
||
206 | } |
||
207 | |||
2050 | decky | 208 | char * test_slab2(bool quiet) |
767 | palkovsky | 209 | { |
2028 | decky | 210 | printf("Running reclaim single-thread test .. pass 1\n"); |
767 | palkovsky | 211 | totalmemtest(); |
2028 | decky | 212 | printf("Running reclaim single-thread test .. pass 2\n"); |
768 | palkovsky | 213 | totalmemtest(); |
214 | printf("Reclaim test OK.\n"); |
||
773 | palkovsky | 215 | |
216 | multitest(128); |
||
217 | multitest(2048); |
||
780 | palkovsky | 218 | multitest(8192); |
773 | palkovsky | 219 | printf("All done.\n"); |
2028 | decky | 220 | |
221 | return NULL; |
||
767 | palkovsky | 222 | } |