DirectoryMemory.cc revision 9104
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 "debug/RubyCache.hh"
31#include "debug/RubyStats.hh"
32#include "mem/ruby/slicc_interface/RubySlicc_Util.hh"
33#include "mem/ruby/system/DirectoryMemory.hh"
34#include "mem/ruby/system/System.hh"
35
36using namespace std;
37
38int DirectoryMemory::m_num_directories = 0;
39int DirectoryMemory::m_num_directories_bits = 0;
40uint64_t DirectoryMemory::m_total_size_bytes = 0;
41int DirectoryMemory::m_numa_high_bit = 0;
42
43DirectoryMemory::DirectoryMemory(const Params *p)
44    : SimObject(p)
45{
46    m_version = p->version;
47    m_size_bytes = p->size;
48    m_size_bits = floorLog2(m_size_bytes);
49    m_num_entries = 0;
50    m_use_map = p->use_map;
51    m_map_levels = p->map_levels;
52    m_numa_high_bit = p->numa_high_bit;
53}
54
55void
56DirectoryMemory::init()
57{
58    m_num_entries = m_size_bytes / RubySystem::getBlockSizeBytes();
59
60    if (m_use_map) {
61        m_sparseMemory = new SparseMemory(m_map_levels);
62        g_system_ptr->registerSparseMemory(m_sparseMemory);
63    } else {
64        m_entries = new AbstractEntry*[m_num_entries];
65        for (int i = 0; i < m_num_entries; i++)
66            m_entries[i] = NULL;
67        m_ram = g_system_ptr->getMemoryVector();
68    }
69
70    m_num_directories++;
71    m_num_directories_bits = floorLog2(m_num_directories);
72    m_total_size_bytes += m_size_bytes;
73
74    if (m_numa_high_bit == 0) {
75        m_numa_high_bit = RubySystem::getMemorySizeBits() - 1;
76    }
77    assert(m_numa_high_bit != 0);
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
96DirectoryMemory::printConfig(ostream& out) const
97{
98    out << "DirectoryMemory module config: " << m_name << endl
99        << "  version: " << m_version << endl
100        << "  memory_bits: " << m_size_bits << endl
101        << "  memory_size_bytes: " << m_size_bytes << endl
102        << "  memory_size_Kbytes: " << double(m_size_bytes) / (1<<10) << endl
103        << "  memory_size_Mbytes: " << double(m_size_bytes) / (1<<20) << endl
104        << "  memory_size_Gbytes: " << double(m_size_bytes) / (1<<30) << endl;
105}
106
107// Static method
108void
109DirectoryMemory::printGlobalConfig(ostream & out)
110{
111    out << "DirectoryMemory Global Config: " << endl;
112    out << "  number of directory memories: " << m_num_directories << endl;
113    if (m_num_directories > 1) {
114        out << "  number of selection bits: " << m_num_directories_bits << endl
115            << "  selection bits: " << m_numa_high_bit
116            << "-" << m_numa_high_bit-m_num_directories_bits
117            << endl;
118    }
119    out << "  total memory size bytes: " << m_total_size_bytes << endl;
120    out << "  total memory bits: " << floorLog2(m_total_size_bytes) << endl;
121}
122
123uint64
124DirectoryMemory::mapAddressToDirectoryVersion(PhysAddress address)
125{
126    if (m_num_directories_bits == 0)
127        return 0;
128
129    uint64 ret = address.bitSelect(m_numa_high_bit - m_num_directories_bits + 1,
130                                   m_numa_high_bit);
131    return ret;
132}
133
134bool
135DirectoryMemory::isPresent(PhysAddress address)
136{
137    bool ret = (mapAddressToDirectoryVersion(address) == m_version);
138    return ret;
139}
140
141uint64
142DirectoryMemory::mapAddressToLocalIdx(PhysAddress address)
143{
144    uint64 ret;
145    if (m_num_directories_bits > 0) {
146        ret = address.bitRemove(m_numa_high_bit - m_num_directories_bits + 1,
147                                m_numa_high_bit);
148    } else {
149        ret = address.getAddress();
150    }
151
152    return ret >> (RubySystem::getBlockSizeBits());
153}
154
155AbstractEntry*
156DirectoryMemory::lookup(PhysAddress address)
157{
158    assert(isPresent(address));
159    DPRINTF(RubyCache, "Looking up address: %s\n", address);
160
161    if (m_use_map) {
162        return m_sparseMemory->lookup(address);
163    } else {
164        uint64_t idx = mapAddressToLocalIdx(address);
165        assert(idx < m_num_entries);
166        return m_entries[idx];
167    }
168}
169
170AbstractEntry*
171DirectoryMemory::allocate(const PhysAddress& address, AbstractEntry* entry)
172{
173    assert(isPresent(address));
174    uint64 idx;
175    DPRINTF(RubyCache, "Looking up address: %s\n", address);
176
177    if (m_use_map) {
178        m_sparseMemory->add(address, entry);
179        entry->changePermission(AccessPermission_Read_Write);
180    } else {
181        idx = mapAddressToLocalIdx(address);
182        assert(idx < m_num_entries);
183        entry->getDataBlk().assign(m_ram->getBlockPtr(address));
184        entry->changePermission(AccessPermission_Read_Only);
185        m_entries[idx] = entry;
186    }
187
188    return entry;
189}
190
191void
192DirectoryMemory::invalidateBlock(PhysAddress address)
193{
194    if (m_use_map) {
195        assert(m_sparseMemory->exist(address));
196        m_sparseMemory->remove(address);
197    }
198#if 0
199    else {
200        assert(isPresent(address));
201
202        Index index = address.memoryModuleIndex();
203
204        if (index < 0 || index > m_size) {
205            ERROR_MSG("Directory Memory Assertion: "
206                      "accessing memory out of range.");
207        }
208
209        if (m_entries[index] != NULL){
210            delete m_entries[index];
211            m_entries[index] = NULL;
212        }
213    }
214#endif
215}
216
217void
218DirectoryMemory::print(ostream& out) const
219{
220}
221
222void
223DirectoryMemory::printStats(ostream& out) const
224{
225    if (m_use_map) {
226        m_sparseMemory->printStats(out);
227    }
228}
229
230void
231DirectoryMemory::recordRequestType(DirectoryRequestType requestType) {
232    DPRINTF(RubyStats, "Recorded statistic: %s\n",
233            DirectoryRequestType_to_string(requestType));
234}
235
236DirectoryMemory *
237RubyDirectoryMemoryParams::create()
238{
239    return new DirectoryMemory(this);
240}
241