Rev 2042 | Rev 2053 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
759 | palkovsky | 1 | /* |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | #include <test.h> |
||
30 | #include <mm/slab.h> |
||
762 | palkovsky | 31 | #include <print.h> |
766 | palkovsky | 32 | #include <proc/thread.h> |
33 | #include <arch.h> |
||
768 | palkovsky | 34 | #include <memstr.h> |
759 | palkovsky | 35 | |
762 | palkovsky | 36 | #define VAL_COUNT 1024 |
37 | |||
2022 | decky | 38 | static void * data[VAL_COUNT]; |
762 | palkovsky | 39 | |
766 | palkovsky | 40 | static void testit(int size, int count) |
759 | palkovsky | 41 | { |
762 | palkovsky | 42 | slab_cache_t *cache; |
43 | int i; |
||
44 | |||
766 | palkovsky | 45 | printf("Creating cache, object size: %d.\n", size); |
46 | cache = slab_cache_create("test_cache", size, 0, NULL, NULL, |
||
772 | palkovsky | 47 | SLAB_CACHE_NOMAGAZINE); |
766 | palkovsky | 48 | printf("Allocating %d items...", count); |
2028 | decky | 49 | for (i = 0; i < count; i++) { |
762 | palkovsky | 50 | data[i] = slab_alloc(cache, 0); |
2028 | decky | 51 | memsetb((uintptr_t) data[i], size, 0); |
762 | palkovsky | 52 | } |
53 | printf("done.\n"); |
||
766 | palkovsky | 54 | printf("Freeing %d items...", count); |
2028 | decky | 55 | for (i = 0; i < count; i++) { |
762 | palkovsky | 56 | slab_free(cache, data[i]); |
57 | } |
||
58 | printf("done.\n"); |
||
764 | palkovsky | 59 | |
766 | palkovsky | 60 | printf("Allocating %d items...", count); |
2028 | decky | 61 | for (i = 0; i < count; i++) { |
764 | palkovsky | 62 | data[i] = slab_alloc(cache, 0); |
2028 | decky | 63 | memsetb((uintptr_t) data[i], size, 0); |
764 | palkovsky | 64 | } |
65 | printf("done.\n"); |
||
66 | |||
2028 | decky | 67 | printf("Freeing %d items...", count / 2); |
68 | for (i = count - 1; i >= count / 2; i--) { |
||
764 | palkovsky | 69 | slab_free(cache, data[i]); |
70 | } |
||
71 | printf("done.\n"); |
||
72 | |||
2028 | decky | 73 | printf("Allocating %d items...", count / 2); |
74 | for (i = count / 2; i < count; i++) { |
||
764 | palkovsky | 75 | data[i] = slab_alloc(cache, 0); |
2028 | decky | 76 | memsetb((uintptr_t) data[i], size, 0); |
764 | palkovsky | 77 | } |
78 | printf("done.\n"); |
||
766 | palkovsky | 79 | printf("Freeing %d items...", count); |
2028 | decky | 80 | for (i = 0; i < count; i++) { |
764 | palkovsky | 81 | slab_free(cache, data[i]); |
82 | } |
||
83 | printf("done.\n"); |
||
84 | slab_cache_destroy(cache); |
||
85 | |||
86 | printf("Test complete.\n"); |
||
759 | palkovsky | 87 | } |
766 | palkovsky | 88 | |
89 | static void testsimple(void) |
||
90 | { |
||
91 | testit(100, VAL_COUNT); |
||
92 | testit(200, VAL_COUNT); |
||
93 | testit(1024, VAL_COUNT); |
||
94 | testit(2048, 512); |
||
95 | testit(4000, 128); |
||
96 | testit(8192, 128); |
||
97 | testit(16384, 128); |
||
98 | testit(16385, 128); |
||
99 | } |
||
100 | |||
101 | #define THREADS 6 |
||
102 | #define THR_MEM_COUNT 1024 |
||
103 | #define THR_MEM_SIZE 128 |
||
104 | |||
2028 | decky | 105 | static void * thr_data[THREADS][THR_MEM_COUNT]; |
106 | static slab_cache_t *thr_cache; |
||
107 | static semaphore_t thr_sem; |
||
766 | palkovsky | 108 | |
1062 | jermar | 109 | static void slabtest(void *data) |
766 | palkovsky | 110 | { |
2028 | decky | 111 | int offs = (int) (unative_t) data; |
112 | int i, j; |
||
1658 | vana | 113 | |
114 | thread_detach(THREAD); |
||
115 | |||
766 | palkovsky | 116 | printf("Starting thread #%d...\n",THREAD->tid); |
2028 | decky | 117 | for (j = 0; j < 10; j++) { |
118 | for (i = 0; i < THR_MEM_COUNT; i++) |
||
766 | palkovsky | 119 | thr_data[offs][i] = slab_alloc(thr_cache,0); |
2028 | decky | 120 | for (i = 0; i < THR_MEM_COUNT / 2; i++) |
766 | palkovsky | 121 | slab_free(thr_cache, thr_data[offs][i]); |
2028 | decky | 122 | for (i = 0; i < THR_MEM_COUNT / 2; i++) |
766 | palkovsky | 123 | thr_data[offs][i] = slab_alloc(thr_cache, 0); |
2028 | decky | 124 | for (i = 0; i < THR_MEM_COUNT; i++) |
766 | palkovsky | 125 | slab_free(thr_cache, thr_data[offs][i]); |
126 | } |
||
127 | printf("Thread #%d finished\n", THREAD->tid); |
||
128 | semaphore_up(&thr_sem); |
||
129 | } |
||
130 | |||
131 | static void testthreads(void) |
||
132 | { |
||
133 | thread_t *t; |
||
134 | int i; |
||
135 | |||
136 | thr_cache = slab_cache_create("thread_cache", THR_MEM_SIZE, 0, |
||
137 | NULL, NULL, |
||
138 | SLAB_CACHE_NOMAGAZINE); |
||
2028 | decky | 139 | semaphore_initialize(&thr_sem, 0); |
140 | for (i = 0; i < THREADS; i++) { |
||
2042 | decky | 141 | if (!(t = thread_create(slabtest, (void *) (unative_t) i, TASK, 0, "slabtest", false))) |
2028 | decky | 142 | printf("Could not create thread %d\n", i); |
143 | else |
||
144 | thread_ready(t); |
||
766 | palkovsky | 145 | } |
146 | |||
2028 | decky | 147 | for (i = 0; i < THREADS; i++) |
766 | palkovsky | 148 | semaphore_down(&thr_sem); |
149 | |||
150 | slab_cache_destroy(thr_cache); |
||
151 | printf("Test complete.\n"); |
||
152 | |||
153 | } |
||
154 | |||
2050 | decky | 155 | char * test_slab1(bool quiet) |
766 | palkovsky | 156 | { |
157 | testsimple(); |
||
158 | testthreads(); |
||
2028 | decky | 159 | |
160 | return NULL; |
||
766 | palkovsky | 161 | } |