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