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