source: mainline/kernel/test/mm/falloc2.c@ dc5c303

Last change on this file since dc5c303 was b169619, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 23 months ago

Deduplicate mem functions

There are a number of functions which are copied between
kernel, libc, and potentially boot too. mem*() functions
are first such offenders. All this duplicate code will
be moved to directory 'common'.

  • Property mode set to 100644
File size: 4.1 KB
Line 
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 <memw.h>
38#include <arch.h>
39
40#define MAX_FRAMES 256
41
42#define THREAD_RUNS 1
43#define THREADS 8
44
45static atomic_size_t thread_cnt;
46static atomic_size_t thread_fail;
47
48static 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 for (unsigned int run = 0; run < THREAD_RUNS; run++) {
63 for (size_t count = 1; count <= MAX_FRAMES; count++) {
64 size_t bytes = FRAMES2SIZE(count);
65
66 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
67 "Allocating %zu frames blocks (%zu bytes) ... \n", THREAD->tid,
68 CPU->id, count, bytes);
69
70 unsigned int allocated = 0;
71 for (unsigned int i = 0; i < (MAX_FRAMES / count); i++) {
72 frames[allocated] = frame_alloc(count, FRAME_ATOMIC, 0);
73 if (frames[allocated]) {
74 memsetb((void *) PA2KA(frames[allocated]), bytes, val);
75 allocated++;
76 } else
77 break;
78 }
79
80 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
81 "%u blocks allocated.\n", THREAD->tid, CPU->id,
82 allocated);
83 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
84 "Deallocating ... \n", THREAD->tid, CPU->id);
85
86 for (unsigned int i = 0; i < allocated; i++) {
87 for (size_t k = 0; k < bytes; k++) {
88 if (((uint8_t *) PA2KA(frames[i]))[k] != val) {
89 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
90 "Unexpected data (%c) in block %zu offset %zu\n",
91 THREAD->tid, CPU->id, ((char *) PA2KA(frames[i]))[k],
92 frames[i], k);
93 atomic_inc(&thread_fail);
94 goto cleanup;
95 }
96 }
97 frame_free(frames[i], count);
98 }
99
100 TPRINTF("Thread #%" PRIu64 " (cpu%u): "
101 "Finished run.\n", THREAD->tid, CPU->id);
102 }
103 }
104
105cleanup:
106 free(frames);
107
108 TPRINTF("Thread #%" PRIu64 " (cpu%u): Exiting\n",
109 THREAD->tid, CPU->id);
110 atomic_dec(&thread_cnt);
111}
112
113const char *test_falloc2(void)
114{
115 atomic_store(&thread_cnt, THREADS);
116 atomic_store(&thread_fail, 0);
117
118 for (unsigned int i = 0; i < THREADS; i++) {
119 thread_t *thrd = thread_create(falloc, NULL, TASK,
120 THREAD_FLAG_NONE, "falloc2");
121 if (!thrd) {
122 TPRINTF("Could not create thread %u\n", i);
123 break;
124 }
125 thread_ready(thrd);
126 }
127
128 while (atomic_load(&thread_cnt) > 0) {
129 TPRINTF("Threads left: %zu\n",
130 atomic_load(&thread_cnt));
131 thread_sleep(1);
132 }
133
134 if (atomic_load(&thread_fail) == 0)
135 return NULL;
136
137 return "Test failed";
138}
Note: See TracBrowser for help on using the repository browser.