source: mainline/kernel/test/mm/falloc2.c@ 0b5203b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0b5203b was aafed15, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Declare malloc() etc in standard <stdlib.h> rather than <mm/slab.h>

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[078a0a1]1/*
[df4ed85]2 * Copyright (c) 2006 Sergey Bondari
[078a0a1]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 */
[96348adc]28
[078a0a1]29#include <test.h>
30#include <mm/page.h>
31#include <mm/frame.h>
[aafed15]32#include <stdlib.h>
[078a0a1]33#include <arch/mm/page.h>
[d99c1d2]34#include <typedefs.h>
[23684b7]35#include <atomic.h>
[078a0a1]36#include <proc/thread.h>
[44a7ee5]37#include <mem.h>
[4a2f4bb]38#include <arch.h>
[078a0a1]39
[b0c2075]40#define MAX_FRAMES 256
[078a0a1]41
[cb01e1e]42#define THREAD_RUNS 1
43#define THREADS 8
[078a0a1]44
[aab5e46]45static atomic_t thread_cnt;
[96348adc]46static atomic_t thread_fail;
[078a0a1]47
[cb01e1e]48static void falloc(void *arg)
[4a2f4bb]49{
[7f1c620]50 uint8_t val = THREAD->tid % THREADS;
[a35b458]51
[8cbf1c3]52 uintptr_t *frames = (uintptr_t *)
[11b285d]53 malloc(MAX_FRAMES * sizeof(uintptr_t));
[96348adc]54 if (frames == NULL) {
[abfc9f3]55 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
56 "Unable to allocate frames\n", THREAD->tid, CPU->id);
[96348adc]57 atomic_inc(&thread_fail);
[aab5e46]58 atomic_dec(&thread_cnt);
[96348adc]59 return;
60 }
[a35b458]61
[8d6d76a]62 thread_detach(THREAD);
[a35b458]63
[abfc9f3]64 for (unsigned int run = 0; run < THREAD_RUNS; run++) {
[b0c2075]65 for (size_t count = 1; count <= MAX_FRAMES; count++) {
66 size_t bytes = FRAMES2SIZE(count);
[a35b458]67
[abfc9f3]68 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
[b0c2075]69 "Allocating %zu frames blocks (%zu bytes) ... \n", THREAD->tid,
70 CPU->id, count, bytes);
[a35b458]71
[abfc9f3]72 unsigned int allocated = 0;
[b0c2075]73 for (unsigned int i = 0; i < (MAX_FRAMES / count); i++) {
[cd3b380]74 frames[allocated] = frame_alloc(count, FRAME_ATOMIC, 0);
[7ee0e2f]75 if (frames[allocated]) {
[cd3b380]76 memsetb((void *) PA2KA(frames[allocated]), bytes, val);
[078a0a1]77 allocated++;
[96348adc]78 } else
[078a0a1]79 break;
80 }
[a35b458]81
[abfc9f3]82 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
83 "%u blocks allocated.\n", THREAD->tid, CPU->id,
84 allocated);
85 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
86 "Deallocating ... \n", THREAD->tid, CPU->id);
[a35b458]87
[abfc9f3]88 for (unsigned int i = 0; i < allocated; i++) {
[b0c2075]89 for (size_t k = 0; k < bytes; k++) {
[cd3b380]90 if (((uint8_t *) PA2KA(frames[i]))[k] != val) {
[abfc9f3]91 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
[8cbf1c3]92 "Unexpected data (%c) in block %zu offset %zu\n",
[cd3b380]93 THREAD->tid, CPU->id, ((char *) PA2KA(frames[i]))[k],
[abfc9f3]94 frames[i], k);
[96348adc]95 atomic_inc(&thread_fail);
96 goto cleanup;
[bd6e392]97 }
98 }
[cd3b380]99 frame_free(frames[i], count);
[078a0a1]100 }
[a35b458]101
[abfc9f3]102 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
103 "Finished run.\n", THREAD->tid, CPU->id);
[078a0a1]104 }
105 }
[a35b458]106
[cb01e1e]107cleanup:
[4a2f4bb]108 free(frames);
[a35b458]109
[abfc9f3]110 TPRINTF("Thread #%" PRIu64 " (cpu%u): Exiting\n",
111 THREAD->tid, CPU->id);
[aab5e46]112 atomic_dec(&thread_cnt);
[078a0a1]113}
114
[a000878c]115const char *test_falloc2(void)
[4a2f4bb]116{
[aab5e46]117 atomic_store(&thread_cnt, THREADS);
[e3306d04]118 atomic_store(&thread_fail, 0);
[a35b458]119
[abfc9f3]120 for (unsigned int i = 0; i < THREADS; i++) {
[6eef3c4]121 thread_t *thrd = thread_create(falloc, NULL, TASK,
122 THREAD_FLAG_NONE, "falloc2");
[96348adc]123 if (!thrd) {
[cb01e1e]124 TPRINTF("Could not create thread %u\n", i);
[96348adc]125 break;
126 }
127 thread_ready(thrd);
[078a0a1]128 }
[a35b458]129
[aab5e46]130 while (atomic_load(&thread_cnt) > 0) {
[077842c]131 TPRINTF("Threads left: %zu\n",
[aab5e46]132 atomic_load(&thread_cnt));
[96348adc]133 thread_sleep(1);
134 }
[a35b458]135
[036e97c]136 if (atomic_load(&thread_fail) == 0)
[96348adc]137 return NULL;
[a35b458]138
[96348adc]139 return "Test failed";
[078a0a1]140}
Note: See TracBrowser for help on using the repository browser.