Subversion Repositories HelenOS

Rev

Rev 3343 | Details | Compare with Previous | Last modification | View Log | RSS feed

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