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