/*BEGIN_LEGAL Intel Open Source License Copyright (c) 2002-2005 Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. END_LEGAL */ // caches.H // Mark Charney // $Id: caches.H,v 1.2 2003/05/06 20:05:40 rscohn1 Exp $ #ifndef _CACHES_H_ # define _CACHES_H_ #include #include #include "my-types.H" #include "log-util.H" #include "globals.H" //////////////////////////////////////////////////////////////////////////// using namespace std; class ASSOC_CACHE_T { UINT capacity; UINT assoc; UINT linesz; UINT rows; UINT congruence_classes; UINT cc_mask; UINT line_shift; UINT cc_shift; UINT tag_shift; UINT64* tags; UINT64* lru; UINT64 hits; UINT64 misses; public: ASSOC_CACHE_T(UINT arg_capacity, UINT arg_assoc, UINT arg_linesz) : capacity(arg_capacity), assoc(arg_assoc), linesz(arg_linesz), hits(0), misses(0) { assert(is_power_of_2(linesz)); assert(is_power_of_2(capacity)); rows = capacity / linesz; congruence_classes = rows / assoc; line_shift = ilog(linesz); cc_mask = congruence_classes - 1; cc_shift = ilog(congruence_classes); tag_shift = cc_shift + line_shift; tags = new UINT64[rows]; lru = new UINT64[rows]; my_alloc += 2*sizeof(UINT64)*rows; wipe_directory(); } ~ASSOC_CACHE_T() { delete [] tags; delete [] lru; } void wipe_directory(void) { for(UINT i=0;i> tag_shift); return tag; } UINT get_cc(UINT64 ea) const { UINT cc = (ea >> line_shift) & cc_mask; return cc; } void report(ofstream* log, UINT i, UINT64 insts, char* prefix) { char* s = " multi_cache_"; *log << prefix << s << i << " Capacity = " << capacity << endl; *log << prefix << s << i << " Assoc = " << assoc << endl; *log << prefix << s << i << " Linesz = " << linesz<< endl; *log << prefix << s << i << " Congruence_classes = " << congruence_classes << endl; *log << prefix << s << i << " Rows = " << rows << endl; *log << prefix << s << i << " Hits = " << hits << endl; *log << prefix << s << i << " Misses = " << misses << endl; *log << prefix << s << i << " Refs = " << hits+misses << endl; *log << prefix << s << i << " Misses_per_inst = " << setprecision(4) << 1.0*misses/insts << endl; } void ref(UINT64 ip, UINT64 ea, REF_CODE_ENUM type) { //FIXME: ignoring type const UINT cc = get_cc(ea); const UINT64 tag = get_tag(ea); const UINT base_idx = cc*assoc; UINT64 last_lru = lru[base_idx]; UINT lru_idx = base_idx; for(UINT a=0;areport(log, i, icount, prefix); } } void reset(bool flush) { for(UINT i=0;ireset(flush); } } void ref(UINT64 ip, UINT64 ea, REF_CODE_ENUM type) { for(UINT i=0;iref(ip,ea,type); } } }; /**********************************************************************/ class DIRMAP_CACHE_T { UINT capacity; UINT linesz; UINT rows; UINT row_mask; UINT line_shift; UINT64* tags; UINT64 hits; UINT64 misses; public: DIRMAP_CACHE_T(UINT arg_capacity, UINT arg_linesz) // CONS : capacity(arg_capacity), linesz(arg_linesz), hits(0), misses(0) { assert( linesz <= capacity); /* UINT x = 1<> line_shift); return tag; } UINT get_row(UINT64 ea) const { UINT row = (ea >> line_shift) & row_mask; return row; } void report(ofstream* log, UINT i, UINT64 insts, char* prefix) { char* s = " filter_cache_"; *log << prefix << s << i << " Capacity = " << capacity << endl; *log << prefix << s << i << " Linesz = " << linesz<< endl; *log << prefix << s << i << " Rows = " << rows << endl; *log << prefix << s << i << " Hits = " << hits << endl; *log << prefix << s << i << " Misses = " << misses << endl; *log << prefix << s << i << " Refs = " << hits+misses << endl; *log << prefix << s << i << " Misses_per_inst = " << setprecision(4) << 1.0*misses/insts << endl; } bool ref(UINT64 ip, UINT64 ea, REF_CODE_ENUM t) { UINT row = get_row(ea); UINT64 tag = get_tag(ea); if (tags[row] == tag) { hits++; return false; } misses++; tags[row] = tag; return true; } }; /**********************************************************************/ class FILTER_CACHES_T { UINT ncaches; DIRMAP_CACHE_T** cache; UINT64 misses[LAST_REF_CODE]; UINT64 refs[LAST_REF_CODE]; int fd; #define NRECS 1024 IPEA_T ipea[NRECS]; // buffer for optional trace UINT p; void trace_write(UINT64 ip, UINT64 ea) { ipea[p].ip = ip; ipea[p++].ea = ea; if (p == NRECS) { trace_dump(); } } void trace_dump(void) { write(fd, &ipea, p*sizeof(IPEA_T)); p = 0; } public: FILTER_CACHES_T(UINT n, const char* file) // CONS : ncaches(n), fd(0), p(0) { if (file) { fd = open(file,O_CREAT|O_WRONLY|O_TRUNC, S_IWRITE|S_IREAD); if (fd == -1) { cerr << "Could not open file " << file << endl; exit(1); } } for(UINT i=0;ireport(log, i,insts, prefix); } UINT64 all_data_misses = 0; for(UINT i=0;i