/************************************/
/* Comp Arc Project
/* By: Simon Foucher
/************************************/
/************************************/
/* includes and definitions
/************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
typedef int BOOL;
/************************************/
/* Function declarations
/************************************/
/************************************/
/* Global Variables & datatypes
/************************************/
/************************************/
/* Functions
/************************************/
// To clean up gathered strings from text
void stripCRLF(char array[])
{
int i;
for(i=0;i<500 && array[i]!='\0';i++)
if (array[i]=='\r' || array[i]=='\n')
array[i]='\0';
}
void readTrace(FILE *fp)
{
// Following arrays used to store statistical data (related elements share a common index)
char addresses[10000][20];
int frequency[10000];
int predictor[10000]; // See Book p 83; 0 = 00, 1 = 01, 2 = 10, 3 = 11
int predictionGood[10000];
int totalBR = 0, totalGood = 0;
char temp[50];
char tmpAddress[10];
int i, j, k;
// Format arrays and give preditction not taken
for(i = 0; i < 10000; i++){
frequency[i] = 0;
predictor[i] = 0;
predictionGood[i] = 0;
}
// Extract lines of data; break at EOF
while( fgets(temp
, sizeof(temp
), fp
) != NULL
){
stripCRLF(temp); // Clean up the string
//printf("Line: %s", temp);
totalBR++; // Increment total branch count
for(i = 0; ; i++){ // Extract address of branch
tmpAddress[i] = temp[i];
if(tmpAddress[i] == ' ')
break;
}
tmpAddress[i++] = NULL; // Add a proper null terminator and increment index (i points at branch result)
//for(;i<15; i++)
// printf("--temp[%d] = %d\n",i, temp[i]);
for(k = 0; k < 10000; k++) // Find out if the entry already exists
if(addresses
[k
][0] == NULL
|| !strncmp(addresses
[k
], tmpAddress
, 9))
break;
if(k != 9999){ // If it is a new entry, k points at the first empt spot
if(addresses[k][0] == NULL)
//printf("\tNew Entry: %s \tat index: %d \t taken (if 1) '%c' \n\t", tmpAddress, k, temp[i]);
strcpy(addresses
[k
], tmpAddress
);
}
// At this point, i points at the branch result and k at the array index whete the element goes.
// record the statistics
frequency[k]++;
/* Lookup prediction taken, update predictor and statistics
NOTES:
ASCII 48 = 0, 49 = 1
For prediction FSM, See Book p 83;
0 = 00 = Predict Not taken stable
1 = 01 = Predict Not taken transition
2 = 10 = Predict Taken transition
3 = 11 = Predict Taken stable
*/
//Debugging
//printf("\tPrediction was: %d\n", predictor[k]);
switch (predictor[k]) {
case 0: // Prediction NOT taken stable
if(temp[i] == 49){ // if branch IS taken
predictor[k] = 1; // Update predictor to NOT taken unstable
}
if(temp[i] == 48){ // if branch is NOT taken
predictionGood[k] += 1; // Add one to local good prediction count
totalGood += 1; // Add one to global good prediction count
} // No need to update predictor
break;
case 1: // Prediction NOT taken unstable
if(temp[i] == 49){ // if branch IS taken
predictor[k] = 2; // Update predictor to taken unstable
}
if(temp[i] == 48){ // if branch is NOT taken
predictionGood[k] += 1; // Add one to local good prediction count
totalGood += 1; // Add one to global good prediction count
predictor[k] = 0; // Update predictor to NOT taken stable
}
break;
case 2: // Prediction IS taken unstable
if(temp[i] == 49){ // if branch IS taken
predictor[k] = 3; // Update predictor to taken stable
predictionGood[k] += 1; // Add one to local good prediction count
totalGood += 1; // Add one to global good prediction count
}
if(temp[i] == 48){ // if branch is NOT taken
predictor[k] = 1; // Update predictor to not taken unstable
}
break;
case 3: // Prediction IS taken stable
if(temp[i] == 49){ // if branch IS taken
predictionGood[k] += 1; // Add one to local good prediction count
totalGood += 1; // Add one to global good prediction count
}
if(temp[i] == 48){ // if branch is NOT taken
predictor[k] = 2; // Update predictor to taken not stable
}
break;
default:
break;
}
//Debugging
//printf("Now Prediction is: %d\n\n", predictor[k]);
}// End of while getting lines
printf("\n DONE!!\n\nResults:\n");
/* for(k = 0; k < 9999; k++){
if(addresses[k][0] == NULL)
break;
printf("k: %d\t Add: %s\tTaken: %d\tFreq: %d\tRatio: %d\n", k, addresses[k], taken[k], frequency[k], taken[k]*100/frequency[k]);
}
*/
printf("\n\nTotal BR: %d\tTaken: %d\tRatio: %d%\n\n",totalBR
, totalGood
, totalGood
*100/totalBR
);
printf("---------------------------------------------\n");
}// End if readTrace
/*******************************************************************************/
/* Function main (int argc, char *argv[])
/*******************************************************************************/
int main (int argc, char *argv[])
{
FILE *fp;
// FP.trace
if((fp
= fopen("traces/FP.trace","r"))==NULL
) //Open the password file
{
puts("Cannot open FP.trace\n"
"Please make sure it is located in a directory called 'traces/'\n"
"having this program's directory as a root\n");
}
else
printf("Sucessfully opened FP.trace\nStarting analysis\n");
readTrace(fp);
// INT.trace
if((fp
= fopen("traces/INT.trace","r"))==NULL
) //Open the password file
{
puts("Cannot open INT.trace\n"
"Please make sure it is located in a directory called 'traces/'\n"
"having this program's directory as a root\n");
}
else
printf("Sucessfully opened INT.trace\nStarting analysis\n\n");
readTrace(fp);
// MM.trace
if((fp
= fopen("traces/MM.trace","r"))==NULL
) //Open the password file
{
puts("Cannot open MM.trace\n"
"Please make sure it is located in a directory called 'traces/'\n"
"having this program's directory as a root\n");
}
else
printf("Sucessfully opened MM.trace\nStarting analysis\n\n");
readTrace(fp);
// SERV.trace
if((fp
= fopen("traces/SERV.trace","r"))==NULL
) //Open the password file
{
puts("Cannot open SERV.trace\n"
"Please make sure it is located in a directory called 'traces/'\n"
"having this program's directory as a root\n");
}
else
printf("Sucessfully opened SERV.trace\nStarting analysis\n\n");
readTrace(fp);
}