DirectoryMemory.cc revision 6285
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/ruby/config/RubyConfig.hh"
43#include "mem/ruby/slicc_interface/AbstractController.hh"
44#include "mem/gems_common/util.hh"
45
46int DirectoryMemory::m_num_directories = 0;
47int DirectoryMemory::m_num_directories_bits = 0;
48int DirectoryMemory::m_total_size_bytes = 0;
49
50DirectoryMemory::DirectoryMemory(const string & name)
51 : m_name(name)
52{
53}
54
55void DirectoryMemory::init(const vector<string> & argv)
56{
57  m_controller = NULL;
58  for (vector<string>::const_iterator it = argv.begin(); it != argv.end(); it++) {
59    if ( (*it) == "version" )
60      m_version = atoi( (*(++it)).c_str() );
61    else if ( (*it) == "size_mb" ) {
62      m_size_bytes = atoi((*(++it)).c_str()) * (1<<20);
63      m_size_bits = log_int(m_size_bytes);
64    } else if ( (*it) == "controller" ) {
65      m_controller = RubySystem::getController((*(++it)));
66    } else
67      assert(0);
68  }
69  assert(m_controller != NULL);
70
71  m_num_entries = m_size_bytes / RubySystem::getBlockSizeBytes();
72  m_entries = new Directory_Entry*[m_num_entries];
73  for (int i=0; i < m_num_entries; i++)
74    m_entries[i] = NULL;
75
76  m_ram = g_system_ptr->getMemoryVector();
77
78  m_num_directories++;
79  m_num_directories_bits = log_int(m_num_directories);
80  m_total_size_bytes += m_size_bytes;
81}
82
83DirectoryMemory::~DirectoryMemory()
84{
85  // free up all the directory entries
86  for (int i=0;i<m_num_entries;i++)
87    if (m_entries[i] != NULL)
88      delete m_entries;
89  if (m_entries != NULL)
90    delete [] m_entries;
91}
92
93void DirectoryMemory::printConfig(ostream& out) const
94{
95  out << "DirectoryMemory module config: " << m_name << endl;
96  out << "  controller: " << m_controller->getName() << endl;
97  out << "  version: " << m_version << endl;
98  out << "  memory_bits: " << m_size_bits << endl;
99  out << "  memory_size_bytes: " << m_size_bytes << endl;
100  out << "  memory_size_Kbytes: " << double(m_size_bytes) / (1<<10) << endl;
101  out << "  memory_size_Mbytes: " << double(m_size_bytes) / (1<<20) << endl;
102  out << "  memory_size_Gbytes: " << double(m_size_bytes) / (1<<30) << endl;
103}
104
105// Static method
106void DirectoryMemory::printGlobalConfig(ostream & out)
107{
108  out << "DirectoryMemory Global Config: " << endl;
109  out << "  number of directory memories: " << m_num_directories << endl;
110  if (m_num_directories > 1) {
111    out << "  number of selection bits: " << m_num_directories_bits << endl;
112    out << "  selection bits: " << RubySystem::getBlockSizeBits()+m_num_directories_bits-1
113        << "-" << RubySystem::getBlockSizeBits() << endl;
114  }
115  out << "  total memory size bytes: " << m_total_size_bytes << endl;
116  out << "  total memory size bits: " << log_int(m_total_size_bytes) << endl;
117
118}
119
120int DirectoryMemory::mapAddressToDirectoryVersion(PhysAddress address)
121{
122  if (m_num_directories_bits == 0) return 0;
123  int ret = address.bitSelect(RubySystem::getBlockSizeBits(),
124                              RubySystem::getBlockSizeBits()+m_num_directories_bits-1);
125  return ret;
126}
127
128// Public method
129bool DirectoryMemory::isPresent(PhysAddress address)
130{
131  bool ret = (mapAddressToDirectoryVersion(address) == m_version);
132  return ret;
133}
134
135int DirectoryMemory::mapAddressToLocalIdx(PhysAddress address)
136{
137  int ret = address.getAddress() >> (RubySystem::getBlockSizeBits() + m_num_directories_bits);
138  return ret;
139}
140
141Directory_Entry& DirectoryMemory::lookup(PhysAddress address)
142{
143  assert(isPresent(address));
144  Directory_Entry* entry;
145  int idx = mapAddressToLocalIdx(address);
146  entry = m_entries[idx];
147  if (entry == NULL) {
148    entry = new Directory_Entry;
149    entry->getDataBlk().assign(m_ram->getBlockPtr(address));
150    m_entries[idx] = entry;
151  }
152  return (*entry);
153}
154/*
155Directory_Entry& DirectoryMemory::lookup(PhysAddress address)
156{
157  assert(isPresent(address));
158  Index index = address.memoryModuleIndex();
159
160  if (index < 0 || index > m_size) {
161    WARN_EXPR(address.getAddress());
162    WARN_EXPR(index);
163    WARN_EXPR(m_size);
164    ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
165  }
166  Directory_Entry* entry = m_entries[index];
167
168  // allocate the directory entry on demand.
169  if (entry == NULL) {
170    entry = new Directory_Entry;
171    entry->getDataBlk().assign(m_ram->getBlockPtr(address));
172
173    // store entry to the table
174    m_entries[index] = entry;
175  }
176
177  return (*entry);
178}
179*/
180
181void DirectoryMemory::invalidateBlock(PhysAddress address)
182{
183  /*
184  assert(isPresent(address));
185
186  Index index = address.memoryModuleIndex();
187
188  if (index < 0 || index > m_size) {
189    ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
190  }
191
192  if(m_entries[index] != NULL){
193    delete m_entries[index];
194    m_entries[index] = NULL;
195  }
196  */
197}
198
199void DirectoryMemory::print(ostream& out) const
200{
201
202}
203
204