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

Last change on this file was 0f4f1b2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 18 months ago

Add (and use) functions thread_start() and thread_detach()

Mostly cosmetic, with thread_start() replacing calls to thread_ready(),
but not consuming the passed reference, and thread_detach() being
synonym for thread_put(). Makes the code's function more obvious.

Also modify some threaded tests to use thread_join() for waiting,
instead of counting threads with atomics or semaphores.

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