Main Page   Compound List   File List   Compound Members   File Members  

main.cc

Go to the documentation of this file.
00001 
00015 // ===================================================================
00016 #include <math.h> // For pow()
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 // ===================================================================
00020 
00021 
00022 
00023 // =================================================================
00024 // TYPES
00025 // Definitions of types for each field in the raw reference trace.
00026 // This is taken from tracer.cc in Laplace itself.
00027 
00030 typedef char tag_t;
00031 
00033 typedef unsigned long long int timestamp_t;
00034 
00036 typedef unsigned char length_t;
00037 
00040 typedef unsigned long int address_space_ID_t;
00041 
00043 typedef unsigned long int address_t;
00044 
00047 struct reference_record_s {
00048 
00049   tag_t tag;
00050   timestamp_t timestamp;
00051   length_t length;
00052   address_space_ID_t address_space_ID;
00053   address_t virtual_address;
00054 
00055 };
00056 // =================================================================
00057 
00058 
00059 
00060 // =================================================================
00061 // GLOBALS and CONSTANTS
00062 
00064 const tag_t TAG_END_OF_TRACE = 'e';
00065 // ===================================================================
00066 
00067 
00068 
00069 // ===================================================================
00078 void
00079 process_trace () {
00080 
00081   // Allocate a single reference record into which the binary source
00082   // will be read, one record at a time.
00083   reference_record_s binary_record;
00084 
00085   // The previous timestamp.
00086   timestamp_t previous_timestamp = 0;
00087 
00088   // The previous virtual address space ID (tracked without regard to
00089   // reference type).
00090   address_space_ID_t previous_address_space_ID = 0;
00091 
00092   // An array storing the previous virtual addresses of each type,
00093   // initialized.  Note that for efficiency, we allocate an array that
00094   // is large enough so that there is an entry for each possible tag
00095   // value.
00096   const unsigned int bits_per_byte = 8;
00097   const unsigned int number_possible_tags =
00098     (unsigned int)pow(2.0, (double)(sizeof(tag_t) * bits_per_byte));
00099   address_t previous_address[number_possible_tags];
00100   for (unsigned int i = 0; i < number_possible_tags; i++) {
00101     previous_address[i] = 0;
00102   }
00103 
00104   // A record number counter, used for debugging purposes.
00105   unsigned long long int record_number = 1;
00106 
00107   // Read binary records until the end of the trace has been reached.
00108   do {
00109 
00110     // Attempt to read the next record.
00111     size_t read_result = fread(&binary_record,
00112              sizeof(binary_record),
00113              1,
00114              stdin);
00115 
00116     // Ensure that the read succeeded; otherwise, bail out.
00117     if (read_result != 1) {
00118 
00119       if (feof(stdin)) {
00120   fprintf(stderr, "Premature EOF\n");
00121       } else {
00122   fprintf(stderr, "Failed in reading binary record\n");
00123       }
00124 
00125       exit(1);
00126 
00127     }
00128 
00129     // Have we reached the end of the trace?
00130     if (binary_record.tag == TAG_END_OF_TRACE) {
00131 
00132       // Yes, so emit the ending tag.
00133       printf("%c\n", binary_record.tag);
00134 
00135     } else {
00136 
00137       // Calculate the timestamp difference and adopt the new
00138       // timestamp.
00139       timestamp_t timestamp_difference =
00140   binary_record.timestamp - previous_timestamp;
00141       previous_timestamp = binary_record.timestamp;
00142 
00143       // Calculate the address space ID to emit.  Is it the same as
00144       // the previous address space ID, emit zero; otherwise, emit the
00145       // changed address space ID.
00146       address_space_ID_t address_space_ID = 0;
00147       if (binary_record.address_space_ID !=
00148     previous_address_space_ID) {
00149   address_space_ID = binary_record.address_space_ID;
00150   previous_address_space_ID = address_space_ID;
00151       }
00152 
00153       // Calculate the virtual address difference based on the
00154       // reference type.
00155       char address_difference_sign;
00156       address_t address_difference_magnitude;
00157       if (binary_record.virtual_address <
00158     previous_address[binary_record.tag]) {
00159 
00160   address_difference_sign = '-';
00161   address_difference_magnitude =
00162     previous_address[binary_record.tag] -
00163     binary_record.virtual_address;
00164 
00165       } else {
00166 
00167   address_difference_sign = '+';
00168   address_difference_magnitude =
00169     binary_record.virtual_address - 
00170     previous_address[binary_record.tag];
00171 
00172       }
00173       previous_address[binary_record.tag] =
00174   binary_record.virtual_address;
00175 
00176       printf("%c %qx %hx %lx %c %lx\n",
00177        binary_record.tag,
00178        timestamp_difference,
00179        binary_record.length,
00180        address_space_ID,
00181        address_difference_sign,
00182        address_difference_magnitude);
00183 
00184       record_number++;
00185 
00186     }
00187 
00188   } while (binary_record.tag != TAG_END_OF_TRACE);
00189 
00190 } // process_trace
00191 // ===================================================================
00192 
00193 
00194 
00195 // ===================================================================
00209 int
00210 main (const unsigned int argc, const char** const argv) {
00211 
00212   // Were the correct number of arguments passed?
00213   if (argc != 1) {
00214 
00215     // No.  Emit usage and exit.
00216     fprintf(stderr, "Usage: %s <NO ARGUMENTS>\n", argv[0]);
00217     exit(1);
00218 
00219   }
00220 
00221   // Perform the reference handling.
00222   process_trace();
00223 
00224   // Exit successfully.
00225   return 0;
00226 
00227 } // main
00228 // ===================================================================

Generated on Tue Jan 21 10:43:36 2003 for instruction-level-reduction by doxygen1.3-rc2