vm-mix2.c 5.6 KB

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