Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Text_Raw_Reader.cc

Go to the documentation of this file.
00001 
00016 // ===================================================================
00017 // INCLUDES
00018 
00019 #include <assert.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include "Text_Raw_Reader.hh"
00023 #include "types-and-constants.hh"
00024 // ===================================================================
00025 
00026 
00027 
00028 // ===================================================================
00029 Text_Raw_Reader::Text_Raw_Reader
00030 (const char* const reference_trace_pathname) :
00031   Reference_Reader(reference_trace_pathname) {
00032 
00033   // Reset the clocks to zero.
00034   reference_time = 0;
00035   instruction_time = 0;
00036 
00037 } // Text_Raw_Reader
00038 // ===================================================================
00039 
00040 
00041 
00042 // ===================================================================
00043 void
00044 Text_Raw_Reader::read (reference_record_s* const reference_record) {
00045 
00046   // Attempt to read the next record of the reference trace.
00047   char* read_result = fgets(buffer, BUFFER_SIZE, reference_stream);
00048 
00049   // Did the read fail?
00050   if (read_result == NULL) {
00051 
00052     // Distinguish between an EOF and other errors, but abort
00053     // processing in either case.
00054     if (feof(reference_stream)) {
00055       fprintf(stderr,
00056         "Text_Raw_Reader::read(): Premature EOF\n");
00057     } else {
00058       fprintf(stderr,
00059         "Text_Raw_Reader::read(): Read error\n");
00060     }
00061     exit(1);
00062   }
00063 
00064   // Parse the record type, which should be the first character of the
00065   // buffer.
00066   reference_record->tag = buffer[0];
00067 
00068   // Attempt to parse the rest of the record based on its type.
00069   // Declare and assign some variables here to avoid compiler
00070   // complaints of skipping initialization in switch-case statement.
00071   unsigned int parse_result = 0;
00072   unsigned int items_to_parse = 0;
00073   timestamp_t current_instruction_time = instruction_time;
00074   timestamp_t current_reference_time = reference_time;
00075   switch (reference_record->tag) {
00076 
00077   case TAG_USER_INSTRUCTION_FETCH:
00078   case TAG_KERNEL_INSTRUCTION_FETCH:
00079 
00080     // Tick the instruction clock to reflect that _after_ this event,
00081     // instruction time will have advanced.
00082     instruction_time++;
00083 
00084     // Fall through to read and parse the rest of the record...
00085 
00086   case TAG_USER_READ:
00087   case TAG_KERNEL_READ:
00088   case TAG_USER_WRITE:
00089   case TAG_KERNEL_WRITE:
00090 
00091     // Tick the reference clock to reflect that _after_ this event,
00092     // reference time will have advanced.
00093     reference_time++;
00094 
00095     // Read the remainder of the record.
00096     items_to_parse = 4;
00097     parse_result = sscanf(&buffer[2],
00098         "%qx %hd %lx %lx",
00099         &reference_record->cycle_timestamp,
00100         &reference_record->length,
00101         &reference_record->virtual_page.context_ID,
00102         &reference_record->virtual_address);
00103 
00104     // Place the current instruction and reference times into the
00105     // structure.
00106     reference_record->instruction_timestamp = current_instruction_time;
00107     reference_record->reference_timestamp = current_reference_time;
00108 
00109     // Compute the page ID by isolating the upper bits of the virtual
00110     // address and recording that value in the structure.
00111     reference_record->virtual_page.page_ID =
00112       reference_record->virtual_address >> PAGE_SIZE_ORDER;
00113 
00114     break;
00115 
00116   case TAG_KERNEL_EVENT:
00117 
00118     // Read the remainder of the record.  We care only about the
00119     // timestamp.
00120     items_to_parse = 1;
00121     parse_result = sscanf(&buffer[2],
00122         "%qx",
00123         &reference_record->cycle_timestamp);
00124     reference_record->instruction_timestamp = current_instruction_time;
00125     reference_record->reference_timestamp = current_reference_time;
00126     reference_record->length = 0;
00127     reference_record->virtual_page.context_ID = 0;
00128     reference_record->virtual_page.page_ID = 0;
00129     reference_record->virtual_address = 0;
00130     break;
00131 
00132   case TAG_END_OF_TRACE:
00133 
00134     // Nothing to do.
00135     break;
00136 
00137   default:
00138     fprintf(stderr,
00139       "Text_Raw_Reader::read(): Invalid reference tag %c(%d)\n",
00140       reference_record->tag,
00141       (int)reference_record->tag);
00142     exit(1);
00143 
00144   }
00145 
00146   // If the parse failed, emit the error and exit.
00147   if (parse_result != items_to_parse) {
00148     fprintf(stderr,
00149       "Text_Raw_Reader::read(): Could not parse remainder\n");
00150     fprintf(stderr,
00151       "                         cycle_timestamp = %qx\n",
00152       reference_record->cycle_timestamp);
00153     fprintf(stderr,
00154       "                         instruction_timestamp = %qx\n",
00155       reference_record->instruction_timestamp);
00156     fprintf(stderr,
00157       "                         reference_timestamp = %qx\n",
00158       reference_record->reference_timestamp);
00159     exit(1);
00160   }
00161 
00162 } // read
00163 // ===================================================================

Generated on Fri Jan 31 10:33:35 2003 for Laplace-merge by doxygen1.3-rc2