os161-1.99
 All Data Structures
vm-mix2.c
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <unistd.h>
00005 
00006 /* This is creating a program that has
00007  * a few more text pages than the average
00008  * program we usually have.
00009  */
00010 
00011 // #define DEBUG
00012 
00013 extern void     call_all_loop(int count);
00014 void write_data(unsigned int array[], unsigned int start);
00015 void read_data(unsigned int array[], unsigned int start, const char *array_name);
00016 void print_data(unsigned int array[]);
00017 void do_work(unsigned int start);
00018 
00019 #define PAGE_SIZE           (4096)
00020 #define DATA_BYTES          (3 * 1024 * 1024)
00021 #define PAGES               (DATA_BYTES / PAGE_SIZE)
00022 #define ELEM_SIZE           (sizeof(unsigned int))
00023 #define ELEMS               ((PAGE_SIZE * PAGES / sizeof(unsigned int)) / 2)
00024 #define ELEMS_PER_PAGE      (PAGE_SIZE / ELEM_SIZE)
00025 
00026 #define TLB_SIZE            (64)
00027 #define LOCAL_REFS          (TLB_SIZE - 6)    /* leave a few pages for code and stack */
00028 
00029 #define STACK_PAGES_USED    (9)
00030 #define STACK_ARRAY_ELEMS   (PAGE_SIZE * STACK_PAGES_USED / sizeof(unsigned int))
00031 
00032 #define MIN(a,b)            ((a < b) ? a : b)
00033 
00034 unsigned int init[] = {
00035   0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
00036  10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
00037  20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
00038  30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
00039  40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
00040  50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
00041 };
00042 
00043 #define INIT_ARRAY_ELEMS     (sizeof(init) / sizeof(int))
00044 
00045 unsigned int array1[ELEMS];
00046 unsigned int array2[ELEMS];
00047 
00048 void
00049 write_data(unsigned int array[], unsigned int start)
00050 {
00051         unsigned int i = 0;
00052   unsigned int k = 0;
00053   unsigned int end = 0;
00054 
00055         for (k=0; k<ELEMS; k += PAGE_SIZE) {
00056                 end = MIN(ELEMS, (k + (LOCAL_REFS * ELEMS_PER_PAGE)));
00057                 for (i=k; i<end; i++) {
00058                         array[i] = start + (i / ELEMS_PER_PAGE);
00059                 }
00060 #ifdef DEBUG
00061                 printf("from k = %d to i = %d end %d\n", i, k, end);
00062 #endif
00063         }
00064 }
00065 
00066 void
00067 read_data(unsigned int array[], unsigned int start, const char *array_name)
00068 {
00069         unsigned int i = 0;
00070         unsigned int j = start;
00071 
00072         j = start;
00073         for (i=0; i<ELEMS; i++) {
00074           if (array[i] != (start + (i / ELEMS_PER_PAGE))) {
00075                         printf("FAILED in file %s at line %d %s[%d] = %u != %u\n", 
00076                                                         __FILE__, __LINE__, array_name, i, array[i], i);
00077                         exit(1);
00078                 }
00079         }
00080 }
00081 
00082 void
00083 print_data(unsigned int array[])
00084 {
00085         unsigned int i = 0;
00086   unsigned int count = 0;
00087 
00088         for (i=0; i<ELEMS; i+= (ELEMS_PER_PAGE)) {
00089                 printf("[%10u] = %10u ", i, array[i]);
00090     if (((count+1) % 4) == 0) {
00091       printf("\n");
00092     }
00093     count++;
00094         }
00095   printf("\n");
00096 }
00097 
00098 void
00099 do_work(unsigned int start)
00100 {
00101         unsigned int stack_array[STACK_ARRAY_ELEMS];
00102         unsigned int i = 0;
00103   unsigned int array1_start = start;
00104   unsigned int array2_start = start + (ELEMS / (ELEMS_PER_PAGE)) + 10;
00105 
00106   printf("Checking uninitialized array1\n");
00107         /* check the uninitialized array1 before initialization */
00108         for (i=0; i<ELEMS; i++) {
00109                 if (array1[i] != 0) {
00110                         printf("FAILED in file %s at line %d: array1[%d] = %u != %d\n", __FILE__, __LINE__, i, array1[i], 0);
00111                         exit(1);
00112                 }
00113         }
00114 
00115   printf("Checking uninitialized array2\n");
00116         /* check the uninitialized array2 before initialization */
00117         for (i=0; i<ELEMS; i++) {
00118                 if (array2[i] != 0) {
00119                         printf("FAILED in file %s at line %d: array2[%d] = %u != %d\n", __FILE__, __LINE__, i, array2[i], 0);
00120                         exit(1);
00121                 }
00122         }
00123 
00124         for (i=0; i<STACK_ARRAY_ELEMS; i++) {
00125                 stack_array[i] = i * 1000;
00126         }
00127 
00128         for (i=0; i<2; i++) {
00129           call_all_loop(1);
00130           write_data(array1, array1_start);
00131           call_all_loop(1);
00132     printf("Checking initialized array1\n");
00133           read_data(array1, array1_start, "array1");
00134         }
00135 
00136         /* check the uninitialized array2 again before initialization */
00137   printf("Checking initialized array2 again\n");
00138         for (i=0; i<ELEMS; i++) {
00139                 if (array2[i] != 0) {
00140                         printf("FAILED in file %s at line %d: array2[%d] = %u != %d\n", __FILE__, __LINE__, i, array2[i], 0);
00141                         exit(1);
00142                 }
00143         }
00144 
00145   printf("Checking initialized stack_array\n");
00146         for (i=0; i<STACK_ARRAY_ELEMS; i++) {
00147                 if (stack_array[i] != i * 1000) {
00148                         printf("FAILED in file %s at line %d: stack_array[%d] = %u != %d\n", __FILE__, __LINE__, i, stack_array[i], i);
00149                         exit(1);
00150                 }
00151         }
00152 
00153   printf("Checking initialized init\n");
00154         /* check the initialized array */
00155         for (i=0; i<INIT_ARRAY_ELEMS; i++) {
00156                 if (init[i] != i) {
00157                         printf("FAILED in file %s at line %d: init[%d] = %u != %d\n", __FILE__, __LINE__, i, init[i], i);
00158                         exit(1);
00159                 }
00160         }
00161 
00162         for (i=0; i<2; i++) {
00163           call_all_loop(20);
00164           write_data(array2, array2_start);
00165           call_all_loop(20);
00166     printf("Checking initialized array2\n");
00167           read_data(array2, array2_start, "array2");
00168         }
00169 
00170   printf("Checking initialized stack_array\n");
00171         for (i=0; i<STACK_ARRAY_ELEMS; i++) {
00172                 if (stack_array[i] != i * 1000) {
00173                         printf("FAILED in file %s at line %d: stack_array[%d] = %u != %d\n", __FILE__, __LINE__, i, stack_array[i], i);
00174                         exit(1);
00175                 }
00176         }
00177 
00178         /* check the initialized array */
00179   printf("Checking initialized init\n");
00180         for (i=0; i<INIT_ARRAY_ELEMS; i++) {
00181                 if (init[i] != i) {
00182                         printf("FAILED in file %s at line %d: init[%d] = %u != %d\n", __FILE__, __LINE__, i, init[i], i);
00183                         exit(1);
00184                 }
00185         }
00186 
00187 
00188   printf("Checking initialized array1 for the last time\n");
00189         read_data(array1, array1_start, "array1");
00190   printf("Checking initialized array2 for the last time\n");
00191         read_data(array2, array2_start, "array2");
00192 
00193 
00194         printf("SUCCEEDED\n");
00195 }
00196 
00197 int
00198 main()
00199 {
00200 
00201 #ifdef DEBUG
00202   printf("PAGE_SIZE = %d\n", PAGE_SIZE);
00203   printf("DATA_BYTES = %d\n", DATA_BYTES);
00204   printf("ELEMS = %d\n", ELEMS);
00205   printf("ELEMS_PER_PAGE = %d\n", ELEMS_PER_PAGE);
00206   printf("PAGES = %d\n", PAGES);
00207   printf("Array elements = %d\n", ELEMS);
00208   printf("Pages per array = %d\n", ((ELEMS * sizeof(unsigned int)) / PAGE_SIZE));
00209 #endif
00210 
00211   do_work(1);
00212 #ifdef DEBUG
00213         printf("array 1\n");
00214         print_data(array1);
00215         printf("array 2\n");
00216         print_data(array2);
00217 #endif
00218   exit(0);
00219 }
00220 
 All Data Structures