vm-mix1.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. /* This is creating a program that has
  6. * a few more text pages than the average
  7. * program we usually have.
  8. */
  9. // #define DEBUG
  10. extern void call_all();
  11. void write_data(unsigned int array[], unsigned int start);
  12. void read_data(unsigned int array[], unsigned int start, const char *array_name);
  13. void print_data(unsigned int array[]);
  14. void do_work(unsigned int start);
  15. #define PAGE_SIZE (4096)
  16. #define DATA_BYTES (3 * 1024 * 1024)
  17. #define PAGES (DATA_BYTES / PAGE_SIZE)
  18. #define ELEM_SIZE (sizeof(unsigned int))
  19. #define ELEMS ((PAGE_SIZE * PAGES / sizeof(unsigned int)) / 2)
  20. #define ELEMS_PER_PAGE (PAGE_SIZE / ELEM_SIZE)
  21. #define NUM_REFS (2)
  22. #define STACK_PAGES_USED (9)
  23. #define STACK_ARRAY_ELEMS (PAGE_SIZE * STACK_PAGES_USED / sizeof(unsigned int))
  24. unsigned int init[] = {
  25. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  26. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  27. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  28. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  29. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  30. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  31. };
  32. #define INIT_ARRAY_ELEMS (sizeof(init) / sizeof(int))
  33. unsigned int array1[ELEMS];
  34. unsigned int array2[ELEMS];
  35. void
  36. write_data(unsigned int array[], unsigned int start)
  37. {
  38. unsigned int ref = 0;
  39. unsigned int i = 0;
  40. for (ref = 0; ref < NUM_REFS-1; ref++) {
  41. for (i=0; i<ELEMS; i++) {
  42. array[i] = start + (i / ELEMS_PER_PAGE);
  43. }
  44. }
  45. }
  46. void
  47. read_data(unsigned int array[], unsigned int start, const char *array_name)
  48. {
  49. unsigned int ref = 0;
  50. unsigned int i = 0;
  51. for (ref = 0; ref < NUM_REFS-1; ref++) {
  52. for (i=0; i<ELEMS; i++) {
  53. if (array[i] != (start + (i / ELEMS_PER_PAGE))) {
  54. printf("FAILED in file %s at line %d %s[%d] = %u != %u ref = %d\n",
  55. __FILE__, __LINE__, array_name, i, array[i], i, ref+1);
  56. exit(1);
  57. }
  58. }
  59. }
  60. }
  61. void
  62. print_data(unsigned int array[])
  63. {
  64. unsigned int i = 0;
  65. unsigned int count = 0;
  66. for (i=0; i<ELEMS; i+= (ELEMS_PER_PAGE)) {
  67. printf("[%10u] = %10u ", i, array[i]);
  68. if (((count+1) % 4) == 0) {
  69. printf("\n");
  70. }
  71. count++;
  72. }
  73. printf("\n");
  74. }
  75. void
  76. do_work(unsigned int start)
  77. {
  78. unsigned int stack_array[STACK_ARRAY_ELEMS];
  79. unsigned int i = 0;
  80. unsigned int array1_start = start;
  81. unsigned int array2_start = start + (ELEMS / (ELEMS_PER_PAGE)) + 10;
  82. printf("Checking uninitialized array1\n");
  83. /* check the uninitialized array1 before initialization */
  84. for (i=0; i<ELEMS; i++) {
  85. if (array1[i] != 0) {
  86. printf("FAILED in file %s at line %d: array1[%d] = %u != %d\n", __FILE__, __LINE__, i, array1[i], 0);
  87. exit(1);
  88. }
  89. }
  90. printf("Checking uninitialized array2\n");
  91. /* check the uninitialized array2 before initialization */
  92. for (i=0; i<ELEMS; i++) {
  93. if (array2[i] != 0) {
  94. printf("FAILED in file %s at line %d: array2[%d] = %u != %d\n", __FILE__, __LINE__, i, array2[i], 0);
  95. exit(1);
  96. }
  97. }
  98. for (i=0; i<STACK_ARRAY_ELEMS; i++) {
  99. stack_array[i] = i * 1000;
  100. }
  101. for (i=0; i<2; i++) {
  102. call_all();
  103. write_data(array1, array1_start);
  104. call_all();
  105. printf("Checking initialized array1\n");
  106. read_data(array1, array1_start, "array1");
  107. }
  108. /* check the uninitialized array2 again before initialization */
  109. printf("Checking initialized array2 again\n");
  110. for (i=0; i<ELEMS; i++) {
  111. if (array2[i] != 0) {
  112. printf("FAILED in file %s at line %d: array2[%d] = %u != %d\n", __FILE__, __LINE__, i, array2[i], 0);
  113. exit(1);
  114. }
  115. }
  116. printf("Checking initialized stack_array\n");
  117. for (i=0; i<STACK_ARRAY_ELEMS; i++) {
  118. if (stack_array[i] != i * 1000) {
  119. printf("FAILED in file %s at line %d: stack_array[%d] = %u != %d\n", __FILE__, __LINE__, i, stack_array[i], i);
  120. exit(1);
  121. }
  122. }
  123. printf("Checking initialized init\n");
  124. /* check the initialized array */
  125. for (i=0; i<INIT_ARRAY_ELEMS; i++) {
  126. if (init[i] != i) {
  127. printf("FAILED in file %s at line %d: init[%d] = %u != %d\n", __FILE__, __LINE__, i, init[i], i);
  128. exit(1);
  129. }
  130. }
  131. for (i=0; i<2; i++) {
  132. call_all();
  133. write_data(array2, array2_start);
  134. call_all();
  135. printf("Checking initialized array2\n");
  136. read_data(array2, array2_start, "array2");
  137. }
  138. printf("Checking initialized stack_array\n");
  139. for (i=0; i<STACK_ARRAY_ELEMS; i++) {
  140. if (stack_array[i] != i * 1000) {
  141. printf("FAILED in file %s at line %d: stack_array[%d] = %u != %d\n", __FILE__, __LINE__, i, stack_array[i], i);
  142. exit(1);
  143. }
  144. }
  145. /* check the initialized array */
  146. printf("Checking initialized init\n");
  147. for (i=0; i<INIT_ARRAY_ELEMS; i++) {
  148. if (init[i] != i) {
  149. printf("FAILED in file %s at line %d: init[%d] = %u != %d\n", __FILE__, __LINE__, i, init[i], i);
  150. exit(1);
  151. }
  152. }
  153. printf("Checking initialized array1 for the last time\n");
  154. read_data(array1, array1_start, "array1");
  155. printf("Checking initialized array2 for the last time\n");
  156. read_data(array2, array2_start, "array2");
  157. printf("SUCCEEDED\n");
  158. }
  159. int
  160. main()
  161. {
  162. #ifdef DEBUG
  163. printf("PAGE_SIZE = %d\n", PAGE_SIZE);
  164. printf("DATA_BYTES = %d\n", DATA_BYTES);
  165. printf("ELEMS = %d\n", ELEMS);
  166. printf("ELEMS_PER_PAGE = %d\n", ELEMS_PER_PAGE);
  167. printf("PAGES = %d\n", PAGES);
  168. printf("Array elements = %d\n", ELEMS);
  169. printf("Pages per array = %d\n", ((ELEMS * sizeof(unsigned int)) / PAGE_SIZE));
  170. #endif
  171. do_work(1);
  172. #ifdef DEBUG
  173. printf("array 1\n");
  174. print_data(array1);
  175. printf("array 2\n");
  176. print_data(array2);
  177. #endif
  178. exit(0);
  179. }