123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- #include <types.h>
- #include <lib.h>
- #include <thread.h>
- #include <synch.h>
- #include <test.h>
- #define NTRIES 1200
- #define ITEMSIZE 997
- #define NTHREADS 8
- static
- void
- mallocthread(void *sm, unsigned long num)
- {
- struct semaphore *sem = sm;
- void *ptr;
- void *oldptr=NULL;
- void *oldptr2=NULL;
- int i;
- for (i=0; i<NTRIES; i++) {
- ptr = kmalloc(ITEMSIZE);
- if (ptr==NULL) {
- if (sem) {
- kprintf("thread %lu: kmalloc returned NULL\n",
- num);
- V(sem);
- return;
- }
- kprintf("kmalloc returned null; test failed.\n");
- return;
- }
- if (oldptr2) {
- kfree(oldptr2);
- }
- oldptr2 = oldptr;
- oldptr = ptr;
- }
- if (oldptr2) {
- kfree(oldptr2);
- }
- if (oldptr) {
- kfree(oldptr);
- }
- if (sem) {
- V(sem);
- }
- }
- int
- malloctest(int nargs, char **args)
- {
- (void)nargs;
- (void)args;
- kprintf("Starting kmalloc test...\n");
- mallocthread(NULL, 0);
- kprintf("kmalloc test done\n");
- return 0;
- }
- int
- mallocstress(int nargs, char **args)
- {
- struct semaphore *sem;
- int i, result;
- (void)nargs;
- (void)args;
- sem = sem_create("mallocstress", 0);
- if (sem == NULL) {
- panic("mallocstress: sem_create failed\n");
- }
- kprintf("Starting kmalloc stress test...\n");
- for (i=0; i<NTHREADS; i++) {
- result = thread_fork("mallocstress", NULL,
- mallocthread, sem, i);
- if (result) {
- panic("mallocstress: thread_fork failed: %s\n",
- strerror(result));
- }
- }
- for (i=0; i<NTHREADS; i++) {
- P(sem);
- }
- sem_destroy(sem);
- kprintf("kmalloc stress test done\n");
- return 0;
- }
|