| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Sergey Bondari
|
|---|
| 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/page.h>
|
|---|
| 31 | #include <mm/frame.h>
|
|---|
| 32 | #include <stdlib.h>
|
|---|
| 33 | #include <arch/mm/page.h>
|
|---|
| 34 | #include <typedefs.h>
|
|---|
| 35 | #include <atomic.h>
|
|---|
| 36 | #include <proc/thread.h>
|
|---|
| 37 | #include <mem.h>
|
|---|
| 38 | #include <arch.h>
|
|---|
| 39 |
|
|---|
| 40 | #define MAX_FRAMES 256
|
|---|
| 41 |
|
|---|
| 42 | #define THREAD_RUNS 1
|
|---|
| 43 | #define THREADS 8
|
|---|
| 44 |
|
|---|
| 45 | static atomic_t thread_cnt;
|
|---|
| 46 | static atomic_t thread_fail;
|
|---|
| 47 |
|
|---|
| 48 | static void falloc(void *arg)
|
|---|
| 49 | {
|
|---|
| 50 | uint8_t val = THREAD->tid % THREADS;
|
|---|
| 51 |
|
|---|
| 52 | uintptr_t *frames = (uintptr_t *)
|
|---|
| 53 | malloc(MAX_FRAMES * sizeof(uintptr_t));
|
|---|
| 54 | if (frames == NULL) {
|
|---|
| 55 | TPRINTF("Thread #%" PRIu64 " (cpu%u): "
|
|---|
| 56 | "Unable to allocate frames\n", THREAD->tid, CPU->id);
|
|---|
| 57 | atomic_inc(&thread_fail);
|
|---|
| 58 | atomic_dec(&thread_cnt);
|
|---|
| 59 | return;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | thread_detach(THREAD);
|
|---|
| 63 |
|
|---|
| 64 | for (unsigned int run = 0; run < THREAD_RUNS; run++) {
|
|---|
| 65 | for (size_t count = 1; count <= MAX_FRAMES; count++) {
|
|---|
| 66 | size_t bytes = FRAMES2SIZE(count);
|
|---|
| 67 |
|
|---|
| 68 | TPRINTF("Thread #%" PRIu64 " (cpu%u): "
|
|---|
| 69 | "Allocating %zu frames blocks (%zu bytes) ... \n", THREAD->tid,
|
|---|
| 70 | CPU->id, count, bytes);
|
|---|
| 71 |
|
|---|
| 72 | unsigned int allocated = 0;
|
|---|
| 73 | for (unsigned int i = 0; i < (MAX_FRAMES / count); i++) {
|
|---|
| 74 | frames[allocated] = frame_alloc(count, FRAME_ATOMIC, 0);
|
|---|
| 75 | if (frames[allocated]) {
|
|---|
| 76 | memsetb((void *) PA2KA(frames[allocated]), bytes, val);
|
|---|
| 77 | allocated++;
|
|---|
| 78 | } else
|
|---|
| 79 | break;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 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);
|
|---|
| 87 |
|
|---|
| 88 | for (unsigned int i = 0; i < allocated; i++) {
|
|---|
| 89 | for (size_t k = 0; k < bytes; k++) {
|
|---|
| 90 | if (((uint8_t *) PA2KA(frames[i]))[k] != val) {
|
|---|
| 91 | TPRINTF("Thread #%" PRIu64 " (cpu%u): "
|
|---|
| 92 | "Unexpected data (%c) in block %zu offset %zu\n",
|
|---|
| 93 | THREAD->tid, CPU->id, ((char *) PA2KA(frames[i]))[k],
|
|---|
| 94 | frames[i], k);
|
|---|
| 95 | atomic_inc(&thread_fail);
|
|---|
| 96 | goto cleanup;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | frame_free(frames[i], count);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | TPRINTF("Thread #%" PRIu64 " (cpu%u): "
|
|---|
| 103 | "Finished run.\n", THREAD->tid, CPU->id);
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | cleanup:
|
|---|
| 108 | free(frames);
|
|---|
| 109 |
|
|---|
| 110 | TPRINTF("Thread #%" PRIu64 " (cpu%u): Exiting\n",
|
|---|
| 111 | THREAD->tid, CPU->id);
|
|---|
| 112 | atomic_dec(&thread_cnt);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | const char *test_falloc2(void)
|
|---|
| 116 | {
|
|---|
| 117 | atomic_store(&thread_cnt, THREADS);
|
|---|
| 118 | atomic_store(&thread_fail, 0);
|
|---|
| 119 |
|
|---|
| 120 | for (unsigned int i = 0; i < THREADS; i++) {
|
|---|
| 121 | thread_t *thrd = thread_create(falloc, NULL, TASK,
|
|---|
| 122 | THREAD_FLAG_NONE, "falloc2");
|
|---|
| 123 | if (!thrd) {
|
|---|
| 124 | TPRINTF("Could not create thread %u\n", i);
|
|---|
| 125 | break;
|
|---|
| 126 | }
|
|---|
| 127 | thread_ready(thrd);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | while (atomic_load(&thread_cnt) > 0) {
|
|---|
| 131 | TPRINTF("Threads left: %zu\n",
|
|---|
| 132 | atomic_load(&thread_cnt));
|
|---|
| 133 | thread_sleep(1);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | if (atomic_load(&thread_fail) == 0)
|
|---|
| 137 | return NULL;
|
|---|
| 138 |
|
|---|
| 139 | return "Test failed";
|
|---|
| 140 | }
|
|---|