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