Guest User

TraceCache.cc

a guest
Nov 26th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1. #include <iostream>
  2. #include "TraceCache.h"
  3. #include "def.h"
  4.  
  5.  
  6. using namespace std;
  7.  
  8.  
  9. /***************************************************************************
  10.  *                              Cache Methods                              *
  11.  ***************************************************************************/
  12. Cache::Cache( unsigned size, rep_policy_t rep_policy, unsigned assoc ){
  13.   _size       = size;
  14.   _assoc      = assoc;
  15.   _rep_policy = rep_policy;
  16.   _lines      = _size / _assoc;
  17. }
  18.  
  19.  
  20. bool Cache::exists( unsigned addr ){
  21.   unsigned tag   = addr / _lines;
  22.   unsigned index = addr % _lines;
  23.   if ( _cache.find( index ) != _cache.end() ){
  24.     return ( _cache[index]->exists( tag ));
  25.   }
  26.   return false;
  27. }
  28.  
  29.  
  30. Trace Cache::read( unsigned addr ){
  31.   assert( this->exists( addr ));
  32.   unsigned tag   = addr / _lines;
  33.   unsigned index = addr % _lines;
  34.   return _cache[index]->read( tag );
  35. }
  36.  
  37.  
  38. void Cache::updateCount( unsigned addr ){
  39.   assert( this->exists( addr ));
  40.   unsigned tag   = addr / _lines;
  41.   unsigned index = addr % _lines;
  42.   _cache[index]->updateCount( tag );
  43. }
  44.  
  45.  
  46. void Cache::write( Trace trace ){
  47.   assert( _cache.size() <= _lines );
  48.   unsigned addr  = trace.getFetchAddr();
  49.   unsigned tag   = addr / _lines;
  50.   unsigned index = addr % _lines;
  51.   if ( _cache.find( index ) == _cache.end() ){
  52.     if ( DBG_TCACHE ) std::cout << "    TCACHE: Creating new line #" << index << std::endl;
  53.     _cache[index] = new CacheLine( _assoc, _rep_policy );
  54.   }
  55.   _cache[index]->write( tag, trace );
  56. }
  57.  
  58.  
  59.  
  60. /***************************************************************************
  61.  *                            CacheLine Methods                            *
  62.  ***************************************************************************/
  63. CacheLine::CacheLine( unsigned assoc, rep_policy_t rep_policy ){
  64.   _assoc      = assoc;
  65.   _rep_policy = rep_policy;
  66. }
  67.  
  68.  
  69. bool CacheLine::exists( unsigned tag ){
  70.   return ( _cache_line.find( tag ) != _cache_line.end() );
  71. }
  72.  
  73.  
  74. Trace CacheLine::read( unsigned tag ){
  75.   assert( exists( tag ));
  76.   return *(_cache_line[tag].trace);
  77. }
  78.  
  79.  
  80. void CacheLine::updateCount( unsigned tag ){
  81.   assert( exists( tag ));
  82.  
  83.   if ( LRU == _rep_policy || MRU == _rep_policy ){
  84.     for( CacheLine_t::iterator it = _cache_line.begin(), it_last = _cache_line.end(); it != it_last; ++it )
  85.       if ( it->second.frecency < _cache_line[tag].frecency ) ++(it->second.frecency);
  86.     _cache_line[tag].frecency = 1;
  87.   }else if ( LFU == _rep_policy || MFU == _rep_policy ){
  88.     ++_cache_line[tag].frecency;
  89.   }else{
  90.     std::cout << "    TCLINE: Unknown replacement policy" << std::endl;
  91.     assert(0);
  92.   }
  93. }
  94.  
  95.  
  96. void CacheLine::write( unsigned tag, Trace trace ){
  97.   if ( this->isFull() ) this->evict();
  98.   assert( !this->isFull() );
  99.  
  100.   if ( LRU == _rep_policy || MRU == _rep_policy )
  101.     for( CacheLine_t::iterator it = _cache_line.begin(), it_last = _cache_line.end(); it != it_last; ++it )
  102.       ++(it->second.frecency);
  103.   CacheBlock_t block;
  104.   block.trace = new Trace(2, 1);
  105.   *block.trace = trace;
  106.   block.frecency = 1;
  107.  
  108.   _cache_line[tag] = block;
  109. }
  110.  
  111.  
  112. void CacheLine::evict(){
  113.   assert( _cache_line.size() == _assoc );
  114.  
  115.   CacheLine_t::iterator it_evicted_trace;
  116.  
  117.   if ( LRU == _rep_policy || MFU == _rep_policy ){
  118.     it_evicted_trace = max();
  119.   }else if ( MRU == _rep_policy || LFU == _rep_policy ){
  120.     it_evicted_trace = min();
  121.   }else{
  122.     std::cout << "    TCLINE: Unknown replacement policy" << std::endl;
  123.     assert(0);
  124.   }
  125.  
  126.   if ( LRU == _rep_policy ) assert( it_evicted_trace->second.frecency == _assoc );
  127.   if ( MRU == _rep_policy ) assert( it_evicted_trace->second.frecency == 1 );
  128.   _cache_line.erase( it_evicted_trace );
  129. }
  130.  
  131.  
  132. CacheLine_t::iterator CacheLine::min(){
  133.   unsigned frecency_min = _cache_line.begin()->second.frecency;
  134.   CacheLine_t::iterator it_min = _cache_line.begin();
  135.  
  136.   for( CacheLine_t::iterator it = _cache_line.begin(), it_last = _cache_line.end(); it != it_last; ++it ){
  137.     if ( it->second.frecency < frecency_min ) it_min = it;
  138.   }
  139.   return it_min;
  140. }
  141.  
  142.  
  143. CacheLine_t::iterator CacheLine::max(){
  144.   unsigned frecency_max = _cache_line.begin()->second.frecency;
  145.   CacheLine_t::iterator it_max = _cache_line.begin();
  146.  
  147.   for( CacheLine_t::iterator it = _cache_line.begin(), it_last = _cache_line.end(); it != it_last; ++it ){
  148.     if ( it->second.frecency > frecency_max ) it_max = it;
  149.   }
  150.   return it_max;
  151. }
  152.  
  153.  
  154.  
  155. /***************************************************************************
  156.  *                              Trace Methods                              *
  157.  ***************************************************************************/
  158. Trace::Trace( unsigned max_insns, unsigned max_branches ){
  159.   assert( max_insns > 0 );
  160.   assert( max_branches > 0 );
  161.   assert( max_insns >= max_branches );
  162.  
  163.   _max_insns = max_insns;
  164.   _max_branches = max_branches;
  165. }
  166.  
  167.  
  168. unsigned Trace::getFetchAddr(){
  169.   return _insn_seq[0].pc;
  170. }
  171.  
  172.  
  173. void Trace::grow_insn( insn_packet_t insn_packet ){
  174.   _insn_seq.push_back( insn_packet );
  175.   _trace_fallthru = insn_packet.next_pc;
  176. }
  177.  
  178.  
  179. void Trace::grow_br( insn_packet_t insn_packet, unsigned trace_fallthru, unsigned trace_target ){
  180.   _insn_seq.push_back( insn_packet );
  181.   _br_mask = ( _br_mask << 1 ) + 1;
  182.  
  183.   // Note: _br_flags is modified for all branches. This is incorrect if the trace ends in a branch
  184.   // This has to be checked and corrected just before the trace is used
  185.   _br_flags <<= 1;
  186.   if ( insn_packet.next_pc != insn_packet.pc + 8 )
  187.     _br_flags += 1;
  188.  
  189.   _trace_target   = trace_target;
  190.   _trace_fallthru = trace_fallthru;
  191. }
  192.  
  193.  
  194. bool Trace::isFull(){
  195.   return (( this->getSize() == _max_insns ) | ( _br_mask == ( 2 << _max_branches - 1)));
  196. }
Advertisement
Add Comment
Please, Sign In to add comment