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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since beb368f5 was a4dfcd2, checked in by Jakub Jermar <jakub@…>, 17 years ago

Fix formatter.

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[4e147a6]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[4e147a6]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>
[a294ad0]31#include <print.h>
[14e5d88]32#include <proc/thread.h>
33#include <arch.h>
[086a600]34#include <memstr.h>
[a294ad0]35
36#define VAL_COUNT 1024
37
[70b6de1]38static void * data[VAL_COUNT];
[4e147a6]39
[deada67]40static void testit(int size, int count, bool quiet)
[4e147a6]41{
[a294ad0]42 slab_cache_t *cache;
43 int i;
44
[deada67]45 if (!quiet)
46 printf("Creating cache, object size: %d.\n", size);
47
48 cache = slab_cache_create("test_cache", size, 0, NULL, NULL,
49 SLAB_CACHE_NOMAGAZINE);
50
51 if (!quiet)
52 printf("Allocating %d items...", count);
53
[96348adc]54 for (i = 0; i < count; i++) {
[a294ad0]55 data[i] = slab_alloc(cache, 0);
[e32e092]56 memsetb(data[i], size, 0);
[a294ad0]57 }
[deada67]58
59 if (!quiet) {
60 printf("done.\n");
61 printf("Freeing %d items...", count);
62 }
63
64 for (i = 0; i < count; i++)
[a294ad0]65 slab_free(cache, data[i]);
[deada67]66
67 if (!quiet) {
68 printf("done.\n");
69 printf("Allocating %d items...", count);
[a294ad0]70 }
[deada67]71
[96348adc]72 for (i = 0; i < count; i++) {
[bc504ef2]73 data[i] = slab_alloc(cache, 0);
[e32e092]74 memsetb(data[i], size, 0);
[bc504ef2]75 }
[deada67]76
77 if (!quiet) {
78 printf("done.\n");
79 printf("Freeing %d items...", count / 2);
80 }
81
82 for (i = count - 1; i >= count / 2; i--)
[bc504ef2]83 slab_free(cache, data[i]);
[deada67]84
85 if (!quiet) {
86 printf("done.\n");
87 printf("Allocating %d items...", count / 2);
[bc504ef2]88 }
[deada67]89
[96348adc]90 for (i = count / 2; i < count; i++) {
[bc504ef2]91 data[i] = slab_alloc(cache, 0);
[e32e092]92 memsetb(data[i], size, 0);
[bc504ef2]93 }
[deada67]94
95 if (!quiet) {
96 printf("done.\n");
97 printf("Freeing %d items...", count);
[bc504ef2]98 }
[deada67]99
100 for (i = 0; i < count; i++)
101 slab_free(cache, data[i]);
102
103 if (!quiet)
104 printf("done.\n");
[bc504ef2]105 slab_cache_destroy(cache);
[deada67]106
107 if (!quiet)
108 printf("Test complete.\n");
[4e147a6]109}
[14e5d88]110
[deada67]111static void testsimple(bool quiet)
[14e5d88]112{
[deada67]113 testit(100, VAL_COUNT, quiet);
114 testit(200, VAL_COUNT, quiet);
115 testit(1024, VAL_COUNT, quiet);
116 testit(2048, 512, quiet);
117 testit(4000, 128, quiet);
118 testit(8192, 128, quiet);
119 testit(16384, 128, quiet);
120 testit(16385, 128, quiet);
[14e5d88]121}
122
123#define THREADS 6
124#define THR_MEM_COUNT 1024
125#define THR_MEM_SIZE 128
126
[96348adc]127static void * thr_data[THREADS][THR_MEM_COUNT];
128static slab_cache_t *thr_cache;
129static semaphore_t thr_sem;
[deada67]130static bool sh_quiet;
[14e5d88]131
[ff14c520]132static void slabtest(void *data)
[14e5d88]133{
[96348adc]134 int offs = (int) (unative_t) data;
135 int i, j;
[8d6d76a]136
137 thread_detach(THREAD);
138
[deada67]139 if (!sh_quiet)
[cd8ad52]140 printf("Starting thread #%" PRIu64 "...\n", THREAD->tid);
[deada67]141
[96348adc]142 for (j = 0; j < 10; j++) {
143 for (i = 0; i < THR_MEM_COUNT; i++)
[14e5d88]144 thr_data[offs][i] = slab_alloc(thr_cache,0);
[96348adc]145 for (i = 0; i < THR_MEM_COUNT / 2; i++)
[14e5d88]146 slab_free(thr_cache, thr_data[offs][i]);
[96348adc]147 for (i = 0; i < THR_MEM_COUNT / 2; i++)
[14e5d88]148 thr_data[offs][i] = slab_alloc(thr_cache, 0);
[96348adc]149 for (i = 0; i < THR_MEM_COUNT; i++)
[14e5d88]150 slab_free(thr_cache, thr_data[offs][i]);
151 }
[deada67]152
153 if (!sh_quiet)
[a4dfcd2]154 printf("Thread #%" PRIu64 " finished\n", THREAD->tid);
[deada67]155
[14e5d88]156 semaphore_up(&thr_sem);
157}
158
[deada67]159static void testthreads(bool quiet)
[14e5d88]160{
161 thread_t *t;
162 int i;
163
[deada67]164 thr_cache = slab_cache_create("thread_cache", THR_MEM_SIZE, 0, NULL, NULL,
165 SLAB_CACHE_NOMAGAZINE);
[96348adc]166 semaphore_initialize(&thr_sem, 0);
167 for (i = 0; i < THREADS; i++) {
[deada67]168 if (!(t = thread_create(slabtest, (void *) (unative_t) i, TASK, 0, "slabtest", false))) {
169 if (!quiet)
170 printf("Could not create thread %d\n", i);
171 } else
[96348adc]172 thread_ready(t);
[14e5d88]173 }
174
[96348adc]175 for (i = 0; i < THREADS; i++)
[14e5d88]176 semaphore_down(&thr_sem);
177
178 slab_cache_destroy(thr_cache);
179
[deada67]180 if (!quiet)
181 printf("Test complete.\n");
[14e5d88]182}
183
[95155b0c]184char * test_slab1(bool quiet)
[14e5d88]185{
[deada67]186 sh_quiet = quiet;
187
188 testsimple(quiet);
189 testthreads(quiet);
[96348adc]190
191 return NULL;
[14e5d88]192}
Note: See TracBrowser for help on using the repository browser.