parallelvm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. * parallelvm.c: highly parallelized VM stress test.
  31. *
  32. * This test probably won't run with only 512k of physical memory
  33. * (unless maybe if you have a *really* gonzo VM system) because each
  34. * of its processes needs to allocate a kernel stack, and those add up
  35. * quickly.
  36. */
  37. #include <sys/types.h>
  38. #include <sys/wait.h>
  39. #include <stdarg.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. #include <err.h>
  45. #define NJOBS 24
  46. #define DIM 35
  47. #define NMATS 11
  48. #define JOBSIZE ((NMATS+1)*DIM*DIM*sizeof(int))
  49. static const int right_answers[NJOBS] = {
  50. -1337312809,
  51. 356204544,
  52. -537881911,
  53. -65406976,
  54. 1952063315,
  55. -843894784,
  56. 1597000869,
  57. -993925120,
  58. 838840559,
  59. -1616928768,
  60. -182386335,
  61. -364554240,
  62. 251084843,
  63. -61403136,
  64. 295326333,
  65. 1488013312,
  66. 1901440647,
  67. 0,
  68. -1901440647,
  69. -1488013312,
  70. -295326333,
  71. 61403136,
  72. -251084843,
  73. 364554240,
  74. };
  75. ////////////////////////////////////////////////////////////
  76. struct matrix {
  77. int m_data[DIM][DIM];
  78. };
  79. ////////////////////////////////////////////////////////////
  80. /*
  81. * Use this instead of just calling printf so we know each printout
  82. * is atomic; this prevents the lines from getting intermingled.
  83. */
  84. static
  85. void
  86. say(const char *fmt, ...)
  87. {
  88. char buf[256];
  89. va_list ap;
  90. va_start(ap, fmt);
  91. vsnprintf(buf, sizeof(buf), fmt, ap);
  92. va_end(ap);
  93. write(STDOUT_FILENO, buf, strlen(buf));
  94. }
  95. ////////////////////////////////////////////////////////////
  96. static
  97. void
  98. multiply(struct matrix *res, const struct matrix *m1, const struct matrix *m2)
  99. {
  100. int i, j, k;
  101. for (i=0; i<DIM; i++) {
  102. for (j=0; j<DIM; j++) {
  103. int val=0;
  104. for (k=0; k<DIM; k++) {
  105. val += m1->m_data[i][k]*m2->m_data[k][j];
  106. }
  107. res->m_data[i][j] = val;
  108. }
  109. }
  110. }
  111. static
  112. void
  113. addeq(struct matrix *m1, const struct matrix *m2)
  114. {
  115. int i, j;
  116. for (i=0; i<DIM; i++) {
  117. for (j=0; j<DIM; j++) {
  118. m1->m_data[i][j] += m2->m_data[i][j];
  119. }
  120. }
  121. }
  122. static
  123. int
  124. trace(const struct matrix *m1)
  125. {
  126. int i, t=0;
  127. for (i=0; i<DIM; i++) {
  128. t += m1->m_data[i][i];
  129. }
  130. return t;
  131. }
  132. ////////////////////////////////////////////////////////////
  133. static struct matrix mats[NMATS];
  134. static
  135. void
  136. populate_initial_matrixes(int mynum)
  137. {
  138. int i,j;
  139. struct matrix *m = &mats[0];
  140. for (i=0; i<DIM; i++) {
  141. for (j=0; j<DIM; j++) {
  142. m->m_data[i][j] = mynum+i-2*j;
  143. }
  144. }
  145. multiply(&mats[1], &mats[0], &mats[0]);
  146. }
  147. static
  148. void
  149. compute(int n)
  150. {
  151. struct matrix tmp;
  152. int i, j;
  153. for (i=0,j=n-1; i<j; i++,j--) {
  154. multiply(&tmp, &mats[i], &mats[j]);
  155. addeq(&mats[n], &tmp);
  156. }
  157. }
  158. static
  159. void
  160. computeall(int mynum)
  161. {
  162. int i;
  163. populate_initial_matrixes(mynum);
  164. for (i=2; i<NMATS; i++) {
  165. compute(i);
  166. }
  167. }
  168. static
  169. int
  170. answer(void)
  171. {
  172. return trace(&mats[NMATS-1]);
  173. }
  174. static
  175. void
  176. go(int mynum)
  177. {
  178. int r;
  179. say("Process %d (pid %d) starting computation...\n", mynum,
  180. (int) getpid());
  181. computeall(mynum);
  182. r = answer();
  183. if (r != right_answers[mynum]) {
  184. say("Process %d answer %d: FAILED, should be %d\n",
  185. mynum, r, right_answers[mynum]);
  186. exit(1);
  187. }
  188. say("Process %d answer %d: passed\n", mynum, r);
  189. exit(0);
  190. }
  191. ////////////////////////////////////////////////////////////
  192. static
  193. int
  194. status_is_failure(int status)
  195. {
  196. /* Proper interpretation of Unix exit status */
  197. if (WIFSIGNALED(status)) {
  198. return 1;
  199. }
  200. if (!WIFEXITED(status)) {
  201. /* ? */
  202. return 1;
  203. }
  204. status = WEXITSTATUS(status);
  205. return status != 0;
  206. }
  207. static
  208. void
  209. makeprocs(void)
  210. {
  211. int i, status, failcount;
  212. pid_t pids[NJOBS];
  213. printf("Job size approximately %lu bytes\n", (unsigned long) JOBSIZE);
  214. printf("Forking %d jobs; total load %luk\n", NJOBS,
  215. (unsigned long) (NJOBS * JOBSIZE)/1024);
  216. for (i=0; i<NJOBS; i++) {
  217. pids[i] = fork();
  218. if (pids[i]<0) {
  219. warn("fork");
  220. }
  221. if (pids[i]==0) {
  222. /* child */
  223. go(i);
  224. }
  225. }
  226. failcount=0;
  227. for (i=0; i<NJOBS; i++) {
  228. if (pids[i]<0) {
  229. failcount++;
  230. }
  231. else {
  232. if (waitpid(pids[i], &status, 0)<0) {
  233. err(1, "waitpid");
  234. }
  235. if (status_is_failure(status)) {
  236. failcount++;
  237. }
  238. }
  239. }
  240. if (failcount>0) {
  241. printf("%d subprocesses failed\n", failcount);
  242. exit(1);
  243. }
  244. printf("Test complete\n");
  245. }
  246. int
  247. main()
  248. {
  249. makeprocs();
  250. return 0;
  251. }