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