os161-1.99
 All Data Structures
malloctest.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Test code for kmalloc.
00032  */
00033 #include <types.h>
00034 #include <lib.h>
00035 #include <thread.h>
00036 #include <synch.h>
00037 #include <test.h>
00038 
00039 /*
00040  * Test kmalloc; allocate ITEMSIZE bytes NTRIES times, freeing
00041  * somewhat later.
00042  *
00043  * The total of ITEMSIZE * NTRIES is intended to exceed the size of
00044  * available memory.
00045  *
00046  * mallocstress does the same thing, but from NTHREADS different
00047  * threads at once.
00048  */
00049 
00050 #define NTRIES   1200
00051 #define ITEMSIZE  997
00052 #define NTHREADS  8
00053 
00054 static
00055 void
00056 mallocthread(void *sm, unsigned long num)
00057 {
00058         struct semaphore *sem = sm;
00059         void *ptr;
00060         void *oldptr=NULL;
00061         void *oldptr2=NULL;
00062         int i;
00063 
00064         for (i=0; i<NTRIES; i++) {
00065                 ptr = kmalloc(ITEMSIZE);
00066                 if (ptr==NULL) {
00067                         if (sem) {
00068                                 kprintf("thread %lu: kmalloc returned NULL\n",
00069                                         num);
00070                                 V(sem);
00071                                 return;
00072                         }
00073                         kprintf("kmalloc returned null; test failed.\n");
00074                         return;
00075                 }
00076                 if (oldptr2) {
00077                         kfree(oldptr2);
00078                 }
00079                 oldptr2 = oldptr;
00080                 oldptr = ptr;
00081         }
00082         if (oldptr2) {
00083                 kfree(oldptr2);
00084         }
00085         if (oldptr) {
00086                 kfree(oldptr);
00087         }
00088         if (sem) {
00089                 V(sem);
00090         }
00091 }
00092 
00093 int
00094 malloctest(int nargs, char **args)
00095 {
00096         (void)nargs;
00097         (void)args;
00098 
00099         kprintf("Starting kmalloc test...\n");
00100         mallocthread(NULL, 0);
00101         kprintf("kmalloc test done\n");
00102 
00103         return 0;
00104 }
00105 
00106 int
00107 mallocstress(int nargs, char **args)
00108 {
00109         struct semaphore *sem;
00110         int i, result;
00111 
00112         (void)nargs;
00113         (void)args;
00114 
00115         sem = sem_create("mallocstress", 0);
00116         if (sem == NULL) {
00117                 panic("mallocstress: sem_create failed\n");
00118         }
00119 
00120         kprintf("Starting kmalloc stress test...\n");
00121 
00122         for (i=0; i<NTHREADS; i++) {
00123                 result = thread_fork("mallocstress", NULL,
00124                                      mallocthread, sem, i);
00125                 if (result) {
00126                         panic("mallocstress: thread_fork failed: %s\n",
00127                               strerror(result));
00128                 }
00129         }
00130 
00131         for (i=0; i<NTHREADS; i++) {
00132                 P(sem);
00133         }
00134 
00135         sem_destroy(sem);
00136         kprintf("kmalloc stress test done\n");
00137 
00138         return 0;
00139 }
 All Data Structures