/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* Test code for kmalloc.31*/32#include <types.h>33#include <lib.h>34#include <thread.h>35#include <synch.h>36#include <test.h>3738/*39* Test kmalloc; allocate ITEMSIZE bytes NTRIES times, freeing40* somewhat later.41*42* The total of ITEMSIZE * NTRIES is intended to exceed the size of43* available memory.44*45* mallocstress does the same thing, but from NTHREADS different46* threads at once.47*/4849#define NTRIES 120050#define ITEMSIZE 99751#define NTHREADS 85253static54void55mallocthread(void *sm, unsigned long num)56{57struct semaphore *sem = sm;58void *ptr;59void *oldptr=NULL;60void *oldptr2=NULL;61int i;6263for (i=0; i<NTRIES; i++) {64ptr = kmalloc(ITEMSIZE);65if (ptr==NULL) {66if (sem) {67kprintf("thread %lu: kmalloc returned NULL\n",68num);69V(sem);70return;71}72kprintf("kmalloc returned null; test failed.\n");73return;74}75if (oldptr2) {76kfree(oldptr2);77}78oldptr2 = oldptr;79oldptr = ptr;80}81if (oldptr2) {82kfree(oldptr2);83}84if (oldptr) {85kfree(oldptr);86}87if (sem) {88V(sem);89}90}9192int93malloctest(int nargs, char **args)94{95(void)nargs;96(void)args;9798kprintf("Starting kmalloc test...\n");99mallocthread(NULL, 0);100kprintf("kmalloc test done\n");101102return 0;103}104105int106mallocstress(int nargs, char **args)107{108struct semaphore *sem;109int i, result;110111(void)nargs;112(void)args;113114sem = sem_create("mallocstress", 0);115if (sem == NULL) {116panic("mallocstress: sem_create failed\n");117}118119kprintf("Starting kmalloc stress test...\n");120121for (i=0; i<NTHREADS; i++) {122result = thread_fork("mallocstress",123mallocthread, sem, i,124NULL);125if (result) {126panic("mallocstress: thread_fork failed: %s\n",127strerror(result));128}129}130131for (i=0; i<NTHREADS; i++) {132P(sem);133}134135sem_destroy(sem);136kprintf("kmalloc stress test done\n");137138return 0;139}140141142