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