Rev 2071 | Rev 3104 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2071 | Rev 2216 | ||
---|---|---|---|
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 <memstr.h> |
34 | #include <memstr.h> |
35 | 35 | ||
36 | #define VAL_COUNT 1024 |
36 | #define VAL_COUNT 1024 |
37 | 37 | ||
38 | static void * data[VAL_COUNT]; |
38 | static void * data[VAL_COUNT]; |
39 | 39 | ||
40 | static void testit(int size, int count, bool quiet) |
40 | static void testit(int size, int count, bool quiet) |
41 | { |
41 | { |
42 | slab_cache_t *cache; |
42 | slab_cache_t *cache; |
43 | int i; |
43 | int i; |
44 | 44 | ||
45 | if (!quiet) |
45 | if (!quiet) |
46 | printf("Creating cache, object size: %d.\n", size); |
46 | printf("Creating cache, object size: %d.\n", size); |
47 | 47 | ||
48 | cache = slab_cache_create("test_cache", size, 0, NULL, NULL, |
48 | cache = slab_cache_create("test_cache", size, 0, NULL, NULL, |
49 | SLAB_CACHE_NOMAGAZINE); |
49 | SLAB_CACHE_NOMAGAZINE); |
50 | 50 | ||
51 | if (!quiet) |
51 | if (!quiet) |
52 | printf("Allocating %d items...", count); |
52 | printf("Allocating %d items...", count); |
53 | 53 | ||
54 | for (i = 0; i < count; i++) { |
54 | for (i = 0; i < count; i++) { |
55 | data[i] = slab_alloc(cache, 0); |
55 | data[i] = slab_alloc(cache, 0); |
56 | memsetb((uintptr_t) data[i], size, 0); |
56 | memsetb((uintptr_t) data[i], size, 0); |
57 | } |
57 | } |
58 | 58 | ||
59 | if (!quiet) { |
59 | if (!quiet) { |
60 | printf("done.\n"); |
60 | printf("done.\n"); |
61 | printf("Freeing %d items...", count); |
61 | printf("Freeing %d items...", count); |
62 | } |
62 | } |
63 | 63 | ||
64 | for (i = 0; i < count; i++) |
64 | for (i = 0; i < count; i++) |
65 | slab_free(cache, data[i]); |
65 | slab_free(cache, data[i]); |
66 | 66 | ||
67 | if (!quiet) { |
67 | if (!quiet) { |
68 | printf("done.\n"); |
68 | printf("done.\n"); |
69 | printf("Allocating %d items...", count); |
69 | printf("Allocating %d items...", count); |
70 | } |
70 | } |
71 | 71 | ||
72 | for (i = 0; i < count; i++) { |
72 | for (i = 0; i < count; i++) { |
73 | data[i] = slab_alloc(cache, 0); |
73 | data[i] = slab_alloc(cache, 0); |
74 | memsetb((uintptr_t) data[i], size, 0); |
74 | memsetb((uintptr_t) data[i], size, 0); |
75 | } |
75 | } |
76 | 76 | ||
77 | if (!quiet) { |
77 | if (!quiet) { |
78 | printf("done.\n"); |
78 | printf("done.\n"); |
79 | printf("Freeing %d items...", count / 2); |
79 | printf("Freeing %d items...", count / 2); |
80 | } |
80 | } |
81 | 81 | ||
82 | for (i = count - 1; i >= count / 2; i--) |
82 | for (i = count - 1; i >= count / 2; i--) |
83 | slab_free(cache, data[i]); |
83 | slab_free(cache, data[i]); |
84 | 84 | ||
85 | if (!quiet) { |
85 | if (!quiet) { |
86 | printf("done.\n"); |
86 | printf("done.\n"); |
87 | printf("Allocating %d items...", count / 2); |
87 | printf("Allocating %d items...", count / 2); |
88 | } |
88 | } |
89 | 89 | ||
90 | for (i = count / 2; i < count; i++) { |
90 | for (i = count / 2; i < count; i++) { |
91 | data[i] = slab_alloc(cache, 0); |
91 | data[i] = slab_alloc(cache, 0); |
92 | memsetb((uintptr_t) data[i], size, 0); |
92 | memsetb((uintptr_t) data[i], size, 0); |
93 | } |
93 | } |
94 | 94 | ||
95 | if (!quiet) { |
95 | if (!quiet) { |
96 | printf("done.\n"); |
96 | printf("done.\n"); |
97 | printf("Freeing %d items...", count); |
97 | printf("Freeing %d items...", count); |
98 | } |
98 | } |
99 | 99 | ||
100 | for (i = 0; i < count; i++) |
100 | for (i = 0; i < count; i++) |
101 | slab_free(cache, data[i]); |
101 | slab_free(cache, data[i]); |
102 | 102 | ||
103 | if (!quiet) |
103 | if (!quiet) |
104 | printf("done.\n"); |
104 | printf("done.\n"); |
105 | slab_cache_destroy(cache); |
105 | slab_cache_destroy(cache); |
106 | 106 | ||
107 | if (!quiet) |
107 | if (!quiet) |
108 | printf("Test complete.\n"); |
108 | printf("Test complete.\n"); |
109 | } |
109 | } |
110 | 110 | ||
111 | static void testsimple(bool quiet) |
111 | static void testsimple(bool quiet) |
112 | { |
112 | { |
113 | testit(100, VAL_COUNT, quiet); |
113 | testit(100, VAL_COUNT, quiet); |
114 | testit(200, VAL_COUNT, quiet); |
114 | testit(200, VAL_COUNT, quiet); |
115 | testit(1024, VAL_COUNT, quiet); |
115 | testit(1024, VAL_COUNT, quiet); |
116 | testit(2048, 512, quiet); |
116 | testit(2048, 512, quiet); |
117 | testit(4000, 128, quiet); |
117 | testit(4000, 128, quiet); |
118 | testit(8192, 128, quiet); |
118 | testit(8192, 128, quiet); |
119 | testit(16384, 128, quiet); |
119 | testit(16384, 128, quiet); |
120 | testit(16385, 128, quiet); |
120 | testit(16385, 128, quiet); |
121 | } |
121 | } |
122 | 122 | ||
123 | #define THREADS 6 |
123 | #define THREADS 6 |
124 | #define THR_MEM_COUNT 1024 |
124 | #define THR_MEM_COUNT 1024 |
125 | #define THR_MEM_SIZE 128 |
125 | #define THR_MEM_SIZE 128 |
126 | 126 | ||
127 | static void * thr_data[THREADS][THR_MEM_COUNT]; |
127 | static void * thr_data[THREADS][THR_MEM_COUNT]; |
128 | static slab_cache_t *thr_cache; |
128 | static slab_cache_t *thr_cache; |
129 | static semaphore_t thr_sem; |
129 | static semaphore_t thr_sem; |
130 | static bool sh_quiet; |
130 | static bool sh_quiet; |
131 | 131 | ||
132 | static void slabtest(void *data) |
132 | static void slabtest(void *data) |
133 | { |
133 | { |
134 | int offs = (int) (unative_t) data; |
134 | int offs = (int) (unative_t) data; |
135 | int i, j; |
135 | int i, j; |
136 | 136 | ||
137 | thread_detach(THREAD); |
137 | thread_detach(THREAD); |
138 | 138 | ||
139 | if (!sh_quiet) |
139 | if (!sh_quiet) |
140 | printf("Starting thread #%d...\n", THREAD->tid); |
140 | printf("Starting thread #%llu...\n", THREAD->tid); |
141 | 141 | ||
142 | for (j = 0; j < 10; j++) { |
142 | for (j = 0; j < 10; j++) { |
143 | for (i = 0; i < THR_MEM_COUNT; i++) |
143 | for (i = 0; i < THR_MEM_COUNT; i++) |
144 | thr_data[offs][i] = slab_alloc(thr_cache,0); |
144 | thr_data[offs][i] = slab_alloc(thr_cache,0); |
145 | for (i = 0; i < THR_MEM_COUNT / 2; i++) |
145 | for (i = 0; i < THR_MEM_COUNT / 2; i++) |
146 | slab_free(thr_cache, thr_data[offs][i]); |
146 | slab_free(thr_cache, thr_data[offs][i]); |
147 | for (i = 0; i < THR_MEM_COUNT / 2; i++) |
147 | for (i = 0; i < THR_MEM_COUNT / 2; i++) |
148 | thr_data[offs][i] = slab_alloc(thr_cache, 0); |
148 | thr_data[offs][i] = slab_alloc(thr_cache, 0); |
149 | for (i = 0; i < THR_MEM_COUNT; i++) |
149 | for (i = 0; i < THR_MEM_COUNT; i++) |
150 | slab_free(thr_cache, thr_data[offs][i]); |
150 | slab_free(thr_cache, thr_data[offs][i]); |
151 | } |
151 | } |
152 | 152 | ||
153 | if (!sh_quiet) |
153 | if (!sh_quiet) |
154 | printf("Thread #%d finished\n", THREAD->tid); |
154 | printf("Thread #%llu finished\n", THREAD->tid); |
155 | 155 | ||
156 | semaphore_up(&thr_sem); |
156 | semaphore_up(&thr_sem); |
157 | } |
157 | } |
158 | 158 | ||
159 | static void testthreads(bool quiet) |
159 | static void testthreads(bool quiet) |
160 | { |
160 | { |
161 | thread_t *t; |
161 | thread_t *t; |
162 | int i; |
162 | int i; |
163 | 163 | ||
164 | thr_cache = slab_cache_create("thread_cache", THR_MEM_SIZE, 0, NULL, NULL, |
164 | thr_cache = slab_cache_create("thread_cache", THR_MEM_SIZE, 0, NULL, NULL, |
165 | SLAB_CACHE_NOMAGAZINE); |
165 | SLAB_CACHE_NOMAGAZINE); |
166 | semaphore_initialize(&thr_sem, 0); |
166 | semaphore_initialize(&thr_sem, 0); |
167 | for (i = 0; i < THREADS; i++) { |
167 | for (i = 0; i < THREADS; i++) { |
168 | if (!(t = thread_create(slabtest, (void *) (unative_t) i, TASK, 0, "slabtest", false))) { |
168 | if (!(t = thread_create(slabtest, (void *) (unative_t) i, TASK, 0, "slabtest", false))) { |
169 | if (!quiet) |
169 | if (!quiet) |
170 | printf("Could not create thread %d\n", i); |
170 | printf("Could not create thread %d\n", i); |
171 | } else |
171 | } else |
172 | thread_ready(t); |
172 | thread_ready(t); |
173 | } |
173 | } |
174 | 174 | ||
175 | for (i = 0; i < THREADS; i++) |
175 | for (i = 0; i < THREADS; i++) |
176 | semaphore_down(&thr_sem); |
176 | semaphore_down(&thr_sem); |
177 | 177 | ||
178 | slab_cache_destroy(thr_cache); |
178 | slab_cache_destroy(thr_cache); |
179 | 179 | ||
180 | if (!quiet) |
180 | if (!quiet) |
181 | printf("Test complete.\n"); |
181 | printf("Test complete.\n"); |
182 | } |
182 | } |
183 | 183 | ||
184 | char * test_slab1(bool quiet) |
184 | char * test_slab1(bool quiet) |
185 | { |
185 | { |
186 | sh_quiet = quiet; |
186 | sh_quiet = quiet; |
187 | 187 | ||
188 | testsimple(quiet); |
188 | testsimple(quiet); |
189 | testthreads(quiet); |
189 | testthreads(quiet); |
190 | 190 | ||
191 | return NULL; |
191 | return NULL; |
192 | } |
192 | } |
193 | 193 |