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>
|
---|
31 | #include <print.h>
|
---|
32 | #include <proc/thread.h>
|
---|
33 | #include <arch.h>
|
---|
34 | #include <panic.h>
|
---|
35 | #include <memstr.h>
|
---|
36 |
|
---|
37 | #define VAL_COUNT 1024
|
---|
38 |
|
---|
39 | void * data[VAL_COUNT];
|
---|
40 |
|
---|
41 | static void testit(int size, int count)
|
---|
42 | {
|
---|
43 | slab_cache_t *cache;
|
---|
44 | int i;
|
---|
45 |
|
---|
46 | printf("Creating cache, object size: %d.\n", size);
|
---|
47 | cache = slab_cache_create("test_cache", size, 0, NULL, NULL,
|
---|
48 | SLAB_CACHE_NOMAGAZINE);
|
---|
49 | printf("Allocating %d items...", count);
|
---|
50 | for (i=0; i < count; i++) {
|
---|
51 | data[i] = slab_alloc(cache, 0);
|
---|
52 | memsetb((__address)data[i], size, 0);
|
---|
53 | }
|
---|
54 | printf("done.\n");
|
---|
55 | printf("Freeing %d items...", count);
|
---|
56 | for (i=0; i < count; i++) {
|
---|
57 | slab_free(cache, data[i]);
|
---|
58 | }
|
---|
59 | printf("done.\n");
|
---|
60 |
|
---|
61 | printf("Allocating %d items...", count);
|
---|
62 | for (i=0; i < count; i++) {
|
---|
63 | data[i] = slab_alloc(cache, 0);
|
---|
64 | memsetb((__address)data[i], size, 0);
|
---|
65 | }
|
---|
66 | printf("done.\n");
|
---|
67 |
|
---|
68 | printf("Freeing %d items...", count/2);
|
---|
69 | for (i=count-1; i >= count/2; i--) {
|
---|
70 | slab_free(cache, data[i]);
|
---|
71 | }
|
---|
72 | printf("done.\n");
|
---|
73 |
|
---|
74 | printf("Allocating %d items...", count/2);
|
---|
75 | for (i=count/2; i < count; i++) {
|
---|
76 | data[i] = slab_alloc(cache, 0);
|
---|
77 | memsetb((__address)data[i], size, 0);
|
---|
78 | }
|
---|
79 | printf("done.\n");
|
---|
80 | printf("Freeing %d items...", count);
|
---|
81 | for (i=0; i < count; i++) {
|
---|
82 | slab_free(cache, data[i]);
|
---|
83 | }
|
---|
84 | printf("done.\n");
|
---|
85 | slab_cache_destroy(cache);
|
---|
86 |
|
---|
87 | printf("Test complete.\n");
|
---|
88 | }
|
---|
89 |
|
---|
90 | static void testsimple(void)
|
---|
91 | {
|
---|
92 | testit(100, VAL_COUNT);
|
---|
93 | testit(200, VAL_COUNT);
|
---|
94 | testit(1024, VAL_COUNT);
|
---|
95 | testit(2048, 512);
|
---|
96 | testit(4000, 128);
|
---|
97 | testit(8192, 128);
|
---|
98 | testit(16384, 128);
|
---|
99 | testit(16385, 128);
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | #define THREADS 6
|
---|
104 | #define THR_MEM_COUNT 1024
|
---|
105 | #define THR_MEM_SIZE 128
|
---|
106 |
|
---|
107 | void * thr_data[THREADS][THR_MEM_COUNT];
|
---|
108 | slab_cache_t *thr_cache;
|
---|
109 | semaphore_t thr_sem;
|
---|
110 |
|
---|
111 | static void slabtest(void *data)
|
---|
112 | {
|
---|
113 | int offs = (int)(__native) data;
|
---|
114 | int i,j;
|
---|
115 |
|
---|
116 | printf("Starting thread #%d...\n",THREAD->tid);
|
---|
117 | for (j=0; j<10; j++) {
|
---|
118 | for (i=0; i<THR_MEM_COUNT; i++)
|
---|
119 | thr_data[offs][i] = slab_alloc(thr_cache,0);
|
---|
120 | for (i=0; i<THR_MEM_COUNT/2; i++)
|
---|
121 | slab_free(thr_cache, thr_data[offs][i]);
|
---|
122 | for (i=0; i< THR_MEM_COUNT/2; i++)
|
---|
123 | thr_data[offs][i] = slab_alloc(thr_cache, 0);
|
---|
124 | for (i=0; i<THR_MEM_COUNT;i++)
|
---|
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);
|
---|
139 | semaphore_initialize(&thr_sem,0);
|
---|
140 | for (i=0; i<THREADS; i++) {
|
---|
141 | if (!(t = thread_create(slabtest, (void *)(__native)i, TASK, 0, "slabtest")))
|
---|
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 | }
|
---|