Subversion Repositories HelenOS

Rev

Rev 2022 | Rev 2029 | 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
 
2022 decky 39
static 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, 
772 palkovsky 48
				  SLAB_CACHE_NOMAGAZINE);	
766 palkovsky 49
	printf("Allocating %d items...", count);
2028 decky 50
	for (i = 0; i < count; i++) {
762 palkovsky 51
		data[i] = slab_alloc(cache, 0);
2028 decky 52
		memsetb((uintptr_t) data[i], size, 0);
762 palkovsky 53
	}
54
	printf("done.\n");
766 palkovsky 55
	printf("Freeing %d items...", count);
2028 decky 56
	for (i = 0; i < count; i++) {
762 palkovsky 57
		slab_free(cache, data[i]);
58
	}
59
	printf("done.\n");
764 palkovsky 60
 
766 palkovsky 61
	printf("Allocating %d items...", count);
2028 decky 62
	for (i = 0; i < count; i++) {
764 palkovsky 63
		data[i] = slab_alloc(cache, 0);
2028 decky 64
		memsetb((uintptr_t) data[i], size, 0);
764 palkovsky 65
	}
66
	printf("done.\n");
67
 
2028 decky 68
	printf("Freeing %d items...", count / 2);
69
	for (i = count - 1; i >= count / 2; i--) {
764 palkovsky 70
		slab_free(cache, data[i]);
71
	}
72
	printf("done.\n");	
73
 
2028 decky 74
	printf("Allocating %d items...", count / 2);
75
	for (i = count / 2; i < count; i++) {
764 palkovsky 76
		data[i] = slab_alloc(cache, 0);
2028 decky 77
		memsetb((uintptr_t) data[i], size, 0);
764 palkovsky 78
	}
79
	printf("done.\n");
766 palkovsky 80
	printf("Freeing %d items...", count);
2028 decky 81
	for (i = 0; i < count; i++) {
764 palkovsky 82
		slab_free(cache, data[i]);
83
	}
84
	printf("done.\n");	
85
	slab_cache_destroy(cache);
86
 
87
	printf("Test complete.\n");
759 palkovsky 88
}
766 palkovsky 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
#define THREADS     6
103
#define THR_MEM_COUNT   1024
104
#define THR_MEM_SIZE    128
105
 
2028 decky 106
static void * thr_data[THREADS][THR_MEM_COUNT];
107
static slab_cache_t *thr_cache;
108
static semaphore_t thr_sem;
766 palkovsky 109
 
1062 jermar 110
static void slabtest(void *data)
766 palkovsky 111
{
2028 decky 112
	int offs = (int) (unative_t) data;
113
	int i, j;
1658 vana 114
 
115
	thread_detach(THREAD);
116
 
766 palkovsky 117
	printf("Starting thread #%d...\n",THREAD->tid);
2028 decky 118
	for (j = 0; j < 10; j++) {
119
		for (i = 0; i < THR_MEM_COUNT; i++)
766 palkovsky 120
			thr_data[offs][i] = slab_alloc(thr_cache,0);
2028 decky 121
		for (i = 0; i < THR_MEM_COUNT / 2; i++)
766 palkovsky 122
			slab_free(thr_cache, thr_data[offs][i]);
2028 decky 123
		for (i = 0; i < THR_MEM_COUNT / 2; i++)
766 palkovsky 124
			thr_data[offs][i] = slab_alloc(thr_cache, 0);
2028 decky 125
		for (i = 0; i < THR_MEM_COUNT; i++)
766 palkovsky 126
			slab_free(thr_cache, thr_data[offs][i]);
127
	}
128
	printf("Thread #%d finished\n", THREAD->tid);
129
	semaphore_up(&thr_sem);
130
}
131
 
132
static void testthreads(void)
133
{
134
	thread_t *t;
135
	int i;
136
 
137
	thr_cache = slab_cache_create("thread_cache", THR_MEM_SIZE, 0, 
138
				      NULL, NULL, 
139
				      SLAB_CACHE_NOMAGAZINE);
2028 decky 140
	semaphore_initialize(&thr_sem, 0);
141
	for (i = 0; i < THREADS; i++) {  
142
		if (!(t = thread_create(slabtest, (void *) (unative_t) i, TASK, 0, "slabtest")))
143
			printf("Could not create thread %d\n", i);
144
		else
145
			thread_ready(t);
766 palkovsky 146
	}
147
 
2028 decky 148
	for (i = 0; i < THREADS; i++)
766 palkovsky 149
		semaphore_down(&thr_sem);
150
 
151
	slab_cache_destroy(thr_cache);
152
	printf("Test complete.\n");
153
 
154
}
155
 
2028 decky 156
char * test_slab1(void)
766 palkovsky 157
{
158
	testsimple();
159
	testthreads();
2028 decky 160
 
161
	return NULL;
766 palkovsky 162
}