DirectoryMemory.cc revision 7563
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        int entry_bits = floorLog2(m_num_entries);
60        assert(entry_bits >= m_map_levels);
61        m_sparseMemory = new SparseMemory(entry_bits, m_map_levels);
62    } else {
63        m_entries = new Directory_Entry*[m_num_entries];
64        for (int i = 0; i < m_num_entries; i++)
65            m_entries[i] = NULL;
66        m_ram = g_system_ptr->getMemoryVector();
67    }
68
69    m_num_directories++;
70    m_num_directories_bits = floorLog2(m_num_directories);
71    m_total_size_bytes += m_size_bytes;
72
73    if (m_numa_high_bit == 0) {
74        m_numa_high_bit = RubySystem::getMemorySizeBits() - 1;
75    }
76    assert(m_numa_high_bit != 0);
77}
78
79DirectoryMemory::~DirectoryMemory()
80{
81    // free up all the directory entries
82    if (m_entries != NULL) {
83        for (uint64 i = 0; i < m_num_entries; i++) {
84            if (m_entries[i] != NULL) {
85                delete m_entries[i];
86            }
87        }
88        delete [] m_entries;
89    } else if (m_use_map) {
90        delete m_sparseMemory;
91    }
92}
93
94void
95DirectoryMemory::printConfig(ostream& out) const
96{
97    out << "DirectoryMemory module config: " << m_name << endl
98        << "  version: " << m_version << endl
99        << "  memory_bits: " << m_size_bits << endl
100        << "  memory_size_bytes: " << m_size_bytes << endl
101        << "  memory_size_Kbytes: " << double(m_size_bytes) / (1<<10) << endl
102        << "  memory_size_Mbytes: " << double(m_size_bytes) / (1<<20) << endl
103        << "  memory_size_Gbytes: " << double(m_size_bytes) / (1<<30) << endl;
104}
105
106// Static method
107void
108DirectoryMemory::printGlobalConfig(ostream & out)
109{
110    out << "DirectoryMemory Global Config: " << endl;
111    out << "  number of directory memories: " << m_num_directories << endl;
112    if (m_num_directories > 1) {
113        out << "  number of selection bits: " << m_num_directories_bits << endl
114            << "  selection bits: " << m_numa_high_bit
115            << "-" << m_numa_high_bit-m_num_directories_bits
116            << endl;
117    }
118    out << "  total memory size bytes: " << m_total_size_bytes << endl;
119    out << "  total memory bits: " << floorLog2(m_total_size_bytes) << endl;
120}
121
122uint64
123DirectoryMemory::mapAddressToDirectoryVersion(PhysAddress address)
124{
125    if (m_num_directories_bits == 0)
126        return 0;
127
128    uint64 ret = address.bitSelect(m_numa_high_bit - m_num_directories_bits + 1,
129                                   m_numa_high_bit);
130    return ret;
131}
132
133bool
134DirectoryMemory::isPresent(PhysAddress address)
135{
136    bool ret = (mapAddressToDirectoryVersion(address) == m_version);
137    return ret;
138}
139
140uint64
141DirectoryMemory::mapAddressToLocalIdx(PhysAddress address)
142{
143    uint64 ret;
144    if (m_num_directories_bits > 0) {
145        ret = address.bitRemove(m_numa_high_bit - m_num_directories_bits + 1,
146                                m_numa_high_bit);
147    } else {
148        ret = address.getAddress();
149    }
150
151    return ret >> (RubySystem::getBlockSizeBits());
152}
153
154Directory_Entry&
155DirectoryMemory::lookup(PhysAddress address)
156{
157    assert(isPresent(address));
158    Directory_Entry* entry;
159    uint64 idx;
160    DEBUG_EXPR(CACHE_COMP, HighPrio, address);
161
162    if (m_use_map) {
163        if (m_sparseMemory->exist(address)) {
164            entry = m_sparseMemory->lookup(address);
165            assert(entry != NULL);
166        } else {
167            // Note: SparseMemory internally creates a new Directory Entry
168            m_sparseMemory->add(address);
169            entry = m_sparseMemory->lookup(address);
170        }
171    } else {
172        idx = mapAddressToLocalIdx(address);
173        assert(idx < m_num_entries);
174        entry = m_entries[idx];
175
176        if (entry == NULL) {
177            entry = new Directory_Entry();
178            entry->getDataBlk().assign(m_ram->getBlockPtr(address));
179            m_entries[idx] = entry;
180        }
181    }
182
183    return *entry;
184}
185
186#if 0
187Directory_Entry&
188DirectoryMemory::lookup(PhysAddress address)
189{
190    assert(isPresent(address));
191    Index index = address.memoryModuleIndex();
192
193    if (index < 0 || index > m_size) {
194        WARN_EXPR(address.getAddress());
195        WARN_EXPR(index);
196        WARN_EXPR(m_size);
197        ERROR_MSG("Directory Memory Assertion: accessing memory out of range");
198    }
199    Directory_Entry* entry = m_entries[index];
200
201    // allocate the directory entry on demand.
202    if (entry == NULL) {
203        entry = new Directory_Entry;
204        entry->getDataBlk().assign(m_ram->getBlockPtr(address));
205
206        // store entry to the table
207        m_entries[index] = entry;
208    }
209
210    return *entry;
211}
212#endif
213
214void
215DirectoryMemory::invalidateBlock(PhysAddress address)
216{
217    if (m_use_map) {
218        assert(m_sparseMemory->exist(address));
219        m_sparseMemory->remove(address);
220    }
221#if 0
222    else {
223        assert(isPresent(address));
224
225        Index index = address.memoryModuleIndex();
226
227        if (index < 0 || index > m_size) {
228            ERROR_MSG("Directory Memory Assertion: "
229                      "accessing memory out of range.");
230        }
231
232        if (m_entries[index] != NULL){
233            delete m_entries[index];
234            m_entries[index] = NULL;
235        }
236    }
237#endif
238}
239
240void
241DirectoryMemory::print(ostream& out) const
242{
243}
244
245void
246DirectoryMemory::printStats(ostream& out) const
247{
248    if (m_use_map) {
249        m_sparseMemory->printStats(out);
250    }
251}
252
253DirectoryMemory *
254RubyDirectoryMemoryParams::create()
255{
256    return new DirectoryMemory(this);
257}
258