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