(c) versus (C)
[helenos.git] / kernel / test / mm / falloc2.c
blob6ccb7eb531bc160d08a8ba8e462e88e16a5a97b0
1 /*
2 * Copyright (c) 2006 Sergey Bondari
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 #include <print.h>
30 #include <test.h>
31 #include <mm/page.h>
32 #include <mm/frame.h>
33 #include <mm/slab.h>
34 #include <arch/mm/page.h>
35 #include <arch/types.h>
36 #include <atomic.h>
37 #include <debug.h>
38 #include <proc/thread.h>
39 #include <memstr.h>
40 #include <arch.h>
42 #define MAX_FRAMES 256
43 #define MAX_ORDER 8
45 #define THREAD_RUNS 1
46 #define THREADS 8
48 static atomic_t thread_count;
49 static atomic_t thread_fail;
50 static bool sh_quiet;
52 static void falloc(void * arg)
54 int order, run, allocated, i;
55 uint8_t val = THREAD->tid % THREADS;
56 index_t k;
58 uintptr_t * frames = (uintptr_t *) malloc(MAX_FRAMES * sizeof(uintptr_t), FRAME_ATOMIC);
59 if (frames == NULL) {
60 if (!sh_quiet)
61 printf("Thread #%d (cpu%d): Unable to allocate frames\n", THREAD->tid, CPU->id);
62 atomic_inc(&thread_fail);
63 atomic_dec(&thread_count);
64 return;
67 thread_detach(THREAD);
69 for (run = 0; run < THREAD_RUNS; run++) {
70 for (order = 0; order <= MAX_ORDER; order++) {
71 if (!sh_quiet)
72 printf("Thread #%d (cpu%d): Allocating %d frames blocks ... \n", THREAD->tid, CPU->id, 1 << order);
74 allocated = 0;
75 for (i = 0; i < (MAX_FRAMES >> order); i++) {
76 frames[allocated] = (uintptr_t)frame_alloc(order, FRAME_ATOMIC | FRAME_KA);
77 if (frames[allocated]) {
78 memsetb(frames[allocated], FRAME_SIZE << order, val);
79 allocated++;
80 } else
81 break;
84 if (!sh_quiet)
85 printf("Thread #%d (cpu%d): %d blocks allocated.\n", THREAD->tid, CPU->id, allocated);
87 if (!sh_quiet)
88 printf("Thread #%d (cpu%d): Deallocating ... \n", THREAD->tid, CPU->id);
90 for (i = 0; i < allocated; i++) {
91 for (k = 0; k <= ((FRAME_SIZE << order) - 1); k++) {
92 if (((uint8_t *) frames[i])[k] != val) {
93 if (!sh_quiet)
94 printf("Thread #%d (cpu%d): Unexpected data (%d) in block %p offset %#zx\n", THREAD->tid, CPU->id, ((char *) frames[i])[k], frames[i], k);
95 atomic_inc(&thread_fail);
96 goto cleanup;
99 frame_free(KA2PA(frames[i]));
102 if (!sh_quiet)
103 printf("Thread #%d (cpu%d): Finished run.\n", THREAD->tid, CPU->id);
107 cleanup:
108 free(frames);
110 if (!sh_quiet)
111 printf("Thread #%d (cpu%d): Exiting\n", THREAD->tid, CPU->id);
112 atomic_dec(&thread_count);
115 char * test_falloc2(bool quiet)
117 unsigned int i;
118 sh_quiet = quiet;
120 atomic_set(&thread_count, THREADS);
121 atomic_set(&thread_fail, 0);
123 for (i = 0; i < THREADS; i++) {
124 thread_t * thrd = thread_create(falloc, NULL, TASK, 0, "falloc", false);
125 if (!thrd) {
126 if (!quiet)
127 printf("Could not create thread %d\n", i);
128 break;
130 thread_ready(thrd);
133 while (atomic_get(&thread_count) > 0) {
134 if (!quiet)
135 printf("Threads left: %d\n", atomic_get(&thread_count));
136 thread_sleep(1);
139 if (atomic_get(&thread_fail) == 0)
140 return NULL;
142 return "Test failed";