source: mainline/kernel/test/mm/slab1.c@ bab75df6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bab75df6 was bab75df6, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Let kernel code get printf via the standard stdio header. Clean up unused includes.

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