/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- mallocthread
- malloctest
- mallocstress
1 /*
2 * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3 * The President and Fellows of Harvard College.
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 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * Test code for kmalloc.
32 */
33 #include <types.h>
34 #include <lib.h>
35 #include <thread.h>
36 #include <synch.h>
37 #include <test.h>
38
39 /*
40 * Test kmalloc; allocate ITEMSIZE bytes NTRIES times, freeing
41 * somewhat later.
42 *
43 * The total of ITEMSIZE * NTRIES is intended to exceed the size of
44 * available memory.
45 *
46 * mallocstress does the same thing, but from NTHREADS different
47 * threads at once.
48 */
49
50 #define NTRIES 1200
51 #define ITEMSIZE 997
52 #define NTHREADS 8
53
54 static
55 void
56 mallocthread(void *sm, unsigned long num)
57 {
58 struct semaphore *sem = sm;
59 void *ptr;
60 void *oldptr=NULL;
61 void *oldptr2=NULL;
62 int i;
63
64 for (i=0; i<NTRIES; i++) {
65 ptr = kmalloc(ITEMSIZE);
66 if (ptr==NULL) {
67 if (sem) {
68 kprintf("thread %lu: kmalloc returned NULL\n",
69 num);
70 V(sem);
71 return;
72 }
73 kprintf("kmalloc returned null; test failed.\n");
74 return;
75 }
76 if (oldptr2) {
77 kfree(oldptr2);
78 }
79 oldptr2 = oldptr;
80 oldptr = ptr;
81 }
82 if (oldptr2) {
83 kfree(oldptr2);
84 }
85 if (oldptr) {
86 kfree(oldptr);
87 }
88 if (sem) {
89 V(sem);
90 }
91 }
92
93 int
94 malloctest(int nargs, char **args)
95 {
96 (void)nargs;
97 (void)args;
98
99 kprintf("Starting kmalloc test...\n");
100 mallocthread(NULL, 0);
101 kprintf("kmalloc test done\n");
102
103 return 0;
104 }
105
106 int
107 mallocstress(int nargs, char **args)
108 {
109 struct semaphore *sem;
110 int i, result;
111
112 (void)nargs;
113 (void)args;
114
115 sem = sem_create("mallocstress", 0);
116 if (sem == NULL) {
117 panic("mallocstress: sem_create failed\n");
118 }
119
120 kprintf("Starting kmalloc stress test...\n");
121
122 for (i=0; i<NTHREADS; i++) {
123 result = thread_fork("mallocstress", NULL,
124 mallocthread, sem, i);
125 if (result) {
126 panic("mallocstress: thread_fork failed: %s\n",
127 strerror(result));
128 }
129 }
130
131 for (i=0; i<NTHREADS; i++) {
132 P(sem);
133 }
134
135 sem_destroy(sem);
136 kprintf("kmalloc stress test done\n");
137
138 return 0;
139 }