DirectoryMemory.cc revision 7025
1
2/*
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * DirectoryMemory.cc
32 *
33 * Description: See DirectoryMemory.hh
34 *
35 * $Id$
36 *
37 */
38
39#include "mem/ruby/system/System.hh"
40#include "mem/ruby/system/DirectoryMemory.hh"
41#include "mem/ruby/slicc_interface/RubySlicc_Util.hh"
42#include "mem/gems_common/util.hh"
43
44int DirectoryMemory::m_num_directories = 0;
45int DirectoryMemory::m_num_directories_bits = 0;
46uint64_t DirectoryMemory::m_total_size_bytes = 0;
47
48DirectoryMemory::DirectoryMemory(const Params *p)
49    : SimObject(p)
50{
51    m_version = p->version;
52    m_size_bytes = p->size;
53    m_size_bits = log_int(m_size_bytes);
54    m_num_entries = 0;
55    m_use_map = p->use_map;
56    m_map_levels = p->map_levels;
57}
58
59void DirectoryMemory::init()
60{
61  m_num_entries = m_size_bytes / RubySystem::getBlockSizeBytes();
62
63  if (m_use_map) {
64      int entry_bits = log_int(m_num_entries);
65      assert(entry_bits >= m_map_levels);
66      m_sparseMemory = new SparseMemory(entry_bits, m_map_levels);
67  } else {
68      m_entries = new Directory_Entry*[m_num_entries];
69      for (int i=0; i < m_num_entries; i++)
70          m_entries[i] = NULL;
71  }
72
73  m_ram = g_system_ptr->getMemoryVector();
74
75  m_num_directories++;
76  m_num_directories_bits = log_int(m_num_directories);
77  m_total_size_bytes += m_size_bytes;
78}
79
80DirectoryMemory::~DirectoryMemory()
81{
82  // free up all the directory entries
83  if (m_entries != NULL) {
84      for (uint64 i = 0; i < m_num_entries; i++) {
85          if (m_entries[i] != NULL) {
86              delete m_entries[i];
87          }
88      }
89      delete [] m_entries;
90  } else if (m_use_map) {
91      delete m_sparseMemory;
92  }
93}
94
95void DirectoryMemory::printConfig(ostream& out) const
96{
97  out << "DirectoryMemory module config: " << m_name << endl;
98  out << "  version: " << m_version << endl;
99  out << "  memory_bits: " << m_size_bits << endl;
100  out << "  memory_size_bytes: " << m_size_bytes << endl;
101  out << "  memory_size_Kbytes: " << double(m_size_bytes) / (1<<10) << endl;
102  out << "  memory_size_Mbytes: " << double(m_size_bytes) / (1<<20) << endl;
103  out << "  memory_size_Gbytes: " << double(m_size_bytes) / (1<<30) << endl;
104}
105
106// Static method
107void DirectoryMemory::printGlobalConfig(ostream & out)
108{
109  out << "DirectoryMemory Global Config: " << endl;
110  out << "  number of directory memories: " << m_num_directories << endl;
111  if (m_num_directories > 1) {
112    out << "  number of selection bits: " << m_num_directories_bits << endl;
113    out << "  selection bits: " << RubySystem::getBlockSizeBits()+m_num_directories_bits-1
114        << "-" << RubySystem::getBlockSizeBits() << endl;
115  }
116  out << "  total memory size bytes: " << m_total_size_bytes << endl;
117  out << "  total memory bits: " << log_int(m_total_size_bytes) << endl;
118
119}
120
121uint64 DirectoryMemory::mapAddressToDirectoryVersion(PhysAddress address)
122{
123  if (m_num_directories_bits == 0) return 0;
124  uint64 ret = address.bitSelect(RubySystem::getBlockSizeBits(),
125                              RubySystem::getBlockSizeBits()+m_num_directories_bits-1);
126  return ret;
127}
128
129// Public method
130bool DirectoryMemory::isPresent(PhysAddress address)
131{
132  bool ret = (mapAddressToDirectoryVersion(address) == m_version);
133  return ret;
134}
135
136uint64 DirectoryMemory::mapAddressToLocalIdx(PhysAddress address)
137{
138  uint64 ret = address.getAddress()
139      >> (RubySystem::getBlockSizeBits() + m_num_directories_bits);
140  return ret;
141}
142
143Directory_Entry& DirectoryMemory::lookup(PhysAddress address)
144{
145  assert(isPresent(address));
146  Directory_Entry* entry;
147  uint64 idx;
148  DEBUG_EXPR(CACHE_COMP, HighPrio, address);
149
150  if (m_use_map) {
151    if (m_sparseMemory->exist(address)) {
152      entry = m_sparseMemory->lookup(address);
153      assert(entry != NULL);
154    } else {
155      //
156      // Note: SparseMemory internally creates a new Directory Entry
157      //
158      m_sparseMemory->add(address);
159      entry = m_sparseMemory->lookup(address);
160    }
161  } else {
162    idx = mapAddressToLocalIdx(address);
163    assert(idx < m_num_entries);
164    entry = m_entries[idx];
165
166    if (entry == NULL) {
167      entry = new Directory_Entry();
168      entry->getDataBlk().assign(m_ram->getBlockPtr(address));
169      m_entries[idx] = entry;
170    }
171  }
172
173  return (*entry);
174}
175/*
176Directory_Entry& DirectoryMemory::lookup(PhysAddress address)
177{
178  assert(isPresent(address));
179  Index index = address.memoryModuleIndex();
180
181  if (index < 0 || index > m_size) {
182    WARN_EXPR(address.getAddress());
183    WARN_EXPR(index);
184    WARN_EXPR(m_size);
185    ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
186  }
187  Directory_Entry* entry = m_entries[index];
188
189  // allocate the directory entry on demand.
190  if (entry == NULL) {
191    entry = new Directory_Entry;
192    entry->getDataBlk().assign(m_ram->getBlockPtr(address));
193
194    // store entry to the table
195    m_entries[index] = entry;
196  }
197
198  return (*entry);
199}
200*/
201
202void DirectoryMemory::invalidateBlock(PhysAddress address)
203{
204
205  if (m_use_map) {
206    assert(m_sparseMemory->exist(address));
207    m_sparseMemory->remove(address);
208  }
209  /*
210  else {
211    assert(isPresent(address));
212
213    Index index = address.memoryModuleIndex();
214
215    if (index < 0 || index > m_size) {
216      ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
217    }
218
219    if(m_entries[index] != NULL){
220      delete m_entries[index];
221      m_entries[index] = NULL;
222    }
223  }
224  */
225
226
227}
228
229void DirectoryMemory::print(ostream& out) const
230{
231
232}
233
234void DirectoryMemory::printStats(ostream& out) const
235{
236    if (m_use_map) {
237        m_sparseMemory->printStats(out);
238    }
239}
240
241DirectoryMemory *
242RubyDirectoryMemoryParams::create()
243{
244    return new DirectoryMemory(this);
245}
246