def __init__(self, argv): # key is instruction address # value is number of correct predictions self.correct_prediction_dict = defaultdict(lambda: 0) self.history_table = [ History(self.history_size) for foo in xrange(2**self.num_bits_for_history) ] self.predictors = [ BranchPredictor(self.predictor_size) for foo in xrange(2**self.history_size) ] self.parseCommandLineArgs(argv) self.btb = BranchTargetBuffer(self.btb_size) hist_file = open(self.hist_file_name) guess_int = lambda x: int(x, 0) self.hist_file_data = [ map(guess_int, line.strip().split()) for line in hist_file.read().split('\r') if line ] hist_file.close()
def __init__(self, argv): self.cold_misses = 0 self.num_branches = 0 self.btb_hit_count = 0 self.num_of_btb_entries = 1 self.file_name = 'BranchHistory.txt' self.parseCommandLineArgs(argv) self.history_file = file(self.file_name, 'r') self.findCommonBranch(self.history_file) self.trace_file = file(self.file_name, 'r') self.btb = BranchTargetBuffer(self.num_of_btb_entries) self.local_history_table = [0 for i in xrange(4)] self.predictors = [0 for i in xrange(4)] self.correct_prediction = 0
def __init__(self, argv): # key is instruction address # value is number of correct predictions self.correct_prediction_dict = defaultdict (lambda : 0) self.history_table = [History (self.history_size) for foo in xrange (2 ** self.num_bits_for_history)] self.predictors = [BranchPredictor (self.predictor_size) for foo in xrange (2 ** self.history_size)] self.parseCommandLineArgs (argv) self.btb = BranchTargetBuffer (self.btb_size) hist_file = open (self.hist_file_name) guess_int = lambda x : int (x, 0) self.hist_file_data = [map (guess_int, line.strip().split()) for line in hist_file.read().split('\r') if line] hist_file.close ()
class BranchPredictor: def __init__(self, argv): self.cold_misses = 0 self.num_branches = 0 self.btb_hit_count = 0 self.num_of_btb_entries = 1 self.file_name = 'BranchHistory.txt' self.parseCommandLineArgs(argv) self.history_file = file(self.file_name, 'r') self.findCommonBranch(self.history_file) self.trace_file = file(self.file_name, 'r') self.btb = BranchTargetBuffer(self.num_of_btb_entries) self.local_history_table = [0 for i in xrange(4)] self.predictors = [0 for i in xrange(4)] self.correct_prediction = 0 def resetStatCounts (self) : self.cold_misses = 0 self.num_branches = 0 self.btb_hit_count = 0 self.correct_prediction = 0 def findCommonBranch(self, history_file): addresses_dict = dict() for line in self.history_file: pc_address = line.split()[0] if pc_address not in addresses_dict: addresses_dict[pc_address] = 0 else: addresses_dict[pc_address] += 1 self.common_branch = sorted(addresses_dict, key=addresses_dict.get, reverse=True)[0] def simulate(self) : addresses_dict = dict() self.common_branch_count = 0 self.common_branch_correct_count = 0 for line in self.trace_file: self.num_branches += 1 split_line = line.split() if split_line[0] == self.common_branch: self.common_branch_count += 1 pc_address = int(split_line[0], 16) branch_address = int(split_line[1], 16) # Check for BTB cold misses if pc_address not in addresses_dict: addresses_dict[pc_address] = branch_address self.cold_misses += 1 btb_hit, branch_address = self.btb.lookup(pc_address) if not btb_hit: branch_address = int(split_line[1], 16) else: self.btb_hit_count += 1 # Prediction code # Look in the history table, from that value # go to the predictor corresponding to that value. LHT_index = pc_address%4 predictor_index = self.local_history_table[LHT_index] taken = self.predictors[predictor_index] if int(split_line[2]) == taken: self.correct_prediction += 1 if split_line[0] == self.common_branch: self.common_branch_correct_count += 1 else : pass self.local_history_table[LHT_index] = (predictor_index*2 + int(split_line[2]))%4 self.predictors[predictor_index] = int(split_line[2]) def printStats(self): template = """\ a. BTB hit rate : %f b. Misprediction rate : %f c. Common Branch : %s Common Branch contribution : %f out of %f d. Conflict misses : %d e. Correct predictions : %d""" values = (float (self.btb_hit_count) / self.num_branches, 1 - float (self.correct_prediction) / self.num_branches, self.common_branch, float (self.common_branch_count) / self.num_branches, float (self.correct_prediction) / self.num_branches, self.btb.conflict_misses, self.correct_prediction) print template % values def parseCommandLineArgs(self, argv): try: opts, args = getopt.getopt(argv, 'e:f:') except getopt.GetoptError: help_message() sys.exit(2) file_found = False for opt, arg in opts: if opt in ("-e"): self.num_of_btb_entries = int(arg) elif opt in ('-f'): self.file_name = arg file_found = True else: help_message() sys.exit() if file_found is False: help_message() sys.exit()
class Simulator: num_bits_for_history = 3 history_size = 4 predictor_size = 2 def __init__(self, argv): # key is instruction address # value is number of correct predictions self.correct_prediction_dict = defaultdict(lambda: 0) self.history_table = [ History(self.history_size) for foo in xrange(2**self.num_bits_for_history) ] self.predictors = [ BranchPredictor(self.predictor_size) for foo in xrange(2**self.history_size) ] self.parseCommandLineArgs(argv) self.btb = BranchTargetBuffer(self.btb_size) hist_file = open(self.hist_file_name) guess_int = lambda x: int(x, 0) self.hist_file_data = [ map(guess_int, line.strip().split()) for line in hist_file.read().split('\r') if line ] hist_file.close() def simulate(self): for addr, target, result in self.hist_file_data: hist_index = addr % (2**self.num_bits_for_history) history = self.history_table[hist_index] predictor = self.predictors[history.getNumericValue()] predicted_outcome = predictor.predict() if predicted_outcome == result: self.correct_prediction_dict[addr] += 1 # Now update the predictor and history according to # actual value if result == 1: predictor.branchTaken() history.branchTaken() else: predictor.branchNotTaken() history.branchNotTaken() # Simulating BTB try: self.btb.getTarget(addr) except: self.btb.load(addr, target) def resetCounters(self): self.correct_prediction_dict = defaultdict(lambda: 0) self.btb.resetCounters() def printStats(self): btb_hit_rate = ( float(self.btb.num_accesses - self.btb.total_miss_count) / self.btb.num_accesses) num_branches = len(self.hist_file_data) correct_predictions = sum([ self.correct_prediction_dict[key] for key in self.correct_prediction_dict ]) misprediction_rate = 1 - float(correct_predictions) / num_branches counts = defaultdict(lambda: 0) for addr, foo, bar in self.hist_file_data: counts[addr] += 1 counts = counts.items() counts.sort(key=lambda x: x[1], reverse=True) most_common_branch = counts[0][0] template = """\ a. BTB hit rate : %f b. Misprediction rate : %f c. Common Branch : %s Common Branch correct predictions : %d d. Conflict misses : %d e. Correct predictions : %d""" values = (btb_hit_rate, misprediction_rate, hex(most_common_branch), self.correct_prediction_dict[most_common_branch], self.btb.conflict_miss_count, correct_predictions) print template % values def parseCommandLineArgs(self, argv): try: opts, args = getopt.getopt(argv, 'e:f:') except getopt.GetoptError: help_message() sys.exit(2) file_found = False for opt, arg in opts: if opt in ("-e"): self.btb_size = int(arg) elif opt in ('-f'): self.hist_file_name = arg file_found = True else: help_message() sys.exit() if file_found is False: help_message() sys.exit()
class Simulator: num_bits_for_history = 3 history_size = 4 predictor_size = 2 def __init__(self, argv): # key is instruction address # value is number of correct predictions self.correct_prediction_dict = defaultdict (lambda : 0) self.history_table = [History (self.history_size) for foo in xrange (2 ** self.num_bits_for_history)] self.predictors = [BranchPredictor (self.predictor_size) for foo in xrange (2 ** self.history_size)] self.parseCommandLineArgs (argv) self.btb = BranchTargetBuffer (self.btb_size) hist_file = open (self.hist_file_name) guess_int = lambda x : int (x, 0) self.hist_file_data = [map (guess_int, line.strip().split()) for line in hist_file.read().split('\r') if line] hist_file.close () def simulate (self): for addr, target, result in self.hist_file_data : hist_index = addr % (2 ** self.num_bits_for_history) history = self.history_table [hist_index] predictor = self.predictors [history.getNumericValue ()] predicted_outcome = predictor.predict () if predicted_outcome == result : self.correct_prediction_dict [addr] += 1 # Now update the predictor and history according to # actual value if result == 1 : predictor.branchTaken () history.branchTaken () else : predictor.branchNotTaken () history.branchNotTaken () # Simulating BTB try : self.btb.getTarget (addr) except : self.btb.load (addr, target) def resetCounters (self) : self.correct_prediction_dict = defaultdict (lambda : 0) self.btb.resetCounters () def printStats(self): btb_hit_rate = (float (self.btb.num_accesses - self.btb.total_miss_count) / self.btb.num_accesses) num_branches = len (self.hist_file_data) correct_predictions = sum ([self.correct_prediction_dict [key] for key in self.correct_prediction_dict]) misprediction_rate = 1 - float (correct_predictions) / num_branches counts = defaultdict (lambda : 0) for addr, foo, bar in self.hist_file_data : counts [addr] += 1 counts = counts.items() counts.sort (key = lambda x : x [1], reverse = True) most_common_branch = counts [0] [0] template = """\ a. BTB hit rate : %f b. Misprediction rate : %f c. Common Branch : %s Common Branch correct predictions : %d d. Conflict misses : %d e. Correct predictions : %d""" values = (btb_hit_rate, misprediction_rate, hex (most_common_branch), self.correct_prediction_dict [most_common_branch], self.btb.conflict_miss_count, correct_predictions) print template % values def parseCommandLineArgs(self, argv): try: opts, args = getopt.getopt(argv, 'e:f:') except getopt.GetoptError: help_message() sys.exit(2) file_found = False for opt, arg in opts: if opt in ("-e"): self.btb_size = int(arg) elif opt in ('-f'): self.hist_file_name = arg file_found = True else: help_message() sys.exit() if file_found is False: help_message() sys.exit()