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