Subversion Repositories HelenOS-historic

Rev

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