malloctest.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * 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>
  37. /*
  38. * Test kmalloc; allocate ITEMSIZE bytes NTRIES times, freeing
  39. * somewhat later.
  40. *
  41. * The total of ITEMSIZE * NTRIES is intended to exceed the size of
  42. * available memory.
  43. *
  44. * mallocstress does the same thing, but from NTHREADS different
  45. * threads at once.
  46. */
  47. #define NTRIES 1200
  48. #define ITEMSIZE 997
  49. #define NTHREADS 8
  50. static
  51. void
  52. mallocthread(void *sm, unsigned long num)
  53. {
  54. struct semaphore *sem = sm;
  55. void *ptr;
  56. void *oldptr=NULL;
  57. void *oldptr2=NULL;
  58. int i;
  59. for (i=0; i<NTRIES; i++) {
  60. ptr = kmalloc(ITEMSIZE);
  61. if (ptr==NULL) {
  62. if (sem) {
  63. kprintf("thread %lu: kmalloc returned NULL\n",
  64. num);
  65. V(sem);
  66. return;
  67. }
  68. kprintf("kmalloc returned null; test failed.\n");
  69. return;
  70. }
  71. if (oldptr2) {
  72. kfree(oldptr2);
  73. }
  74. oldptr2 = oldptr;
  75. oldptr = ptr;
  76. }
  77. if (oldptr2) {
  78. kfree(oldptr2);
  79. }
  80. if (oldptr) {
  81. kfree(oldptr);
  82. }
  83. if (sem) {
  84. V(sem);
  85. }
  86. }
  87. int
  88. malloctest(int nargs, char **args)
  89. {
  90. (void)nargs;
  91. (void)args;
  92. kprintf("Starting kmalloc test...\n");
  93. mallocthread(NULL, 0);
  94. kprintf("kmalloc test done\n");
  95. return 0;
  96. }
  97. int
  98. mallocstress(int nargs, char **args)
  99. {
  100. struct semaphore *sem;
  101. int i, result;
  102. (void)nargs;
  103. (void)args;
  104. sem = sem_create("mallocstress", 0);
  105. if (sem == NULL) {
  106. panic("mallocstress: sem_create failed\n");
  107. }
  108. kprintf("Starting kmalloc stress test...\n");
  109. for (i=0; i<NTHREADS; i++) {
  110. result = thread_fork("mallocstress", NULL,
  111. mallocthread, sem, i);
  112. if (result) {
  113. panic("mallocstress: thread_fork failed: %s\n",
  114. strerror(result));
  115. }
  116. }
  117. for (i=0; i<NTHREADS; i++) {
  118. P(sem);
  119. }
  120. sem_destroy(sem);
  121. kprintf("kmalloc stress test done\n");
  122. return 0;
  123. }