2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
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.
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.
29 #include "mem/gems_common/util.hh"
30 #include "mem/ruby/slicc_interface/RubySlicc_Util.hh"
31 #include "mem/ruby/system/DirectoryMemory.hh"
32 #include "mem/ruby/system/System.hh"
34 int DirectoryMemory::m_num_directories
= 0;
35 int DirectoryMemory::m_num_directories_bits
= 0;
36 uint64_t DirectoryMemory::m_total_size_bytes
= 0;
37 int DirectoryMemory::m_numa_high_bit
= 0;
39 DirectoryMemory::DirectoryMemory(const Params
*p
)
42 m_version
= p
->version
;
43 m_size_bytes
= p
->size
;
44 m_size_bits
= log_int(m_size_bytes
);
46 m_use_map
= p
->use_map
;
47 m_map_levels
= p
->map_levels
;
48 m_numa_high_bit
= p
->numa_high_bit
;
52 DirectoryMemory::init()
54 m_num_entries
= m_size_bytes
/ RubySystem::getBlockSizeBytes();
57 int entry_bits
= log_int(m_num_entries
);
58 assert(entry_bits
>= m_map_levels
);
59 m_sparseMemory
= new SparseMemory(entry_bits
, m_map_levels
);
61 m_entries
= new Directory_Entry
*[m_num_entries
];
62 for (int i
= 0; i
< m_num_entries
; i
++)
64 m_ram
= g_system_ptr
->getMemoryVector();
68 m_num_directories_bits
= log_int(m_num_directories
);
69 m_total_size_bytes
+= m_size_bytes
;
71 if (m_numa_high_bit
== 0) {
72 m_numa_high_bit
= RubySystem::getMemorySizeBits();
74 assert(m_numa_high_bit
!= 0);
77 DirectoryMemory::~DirectoryMemory()
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
) {
87 } else if (m_use_map
) {
88 delete m_sparseMemory
;
93 DirectoryMemory::printConfig(ostream
& out
) const
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
;
106 DirectoryMemory::printGlobalConfig(ostream
& out
)
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
116 out
<< " total memory size bytes: " << m_total_size_bytes
<< endl
;
117 out
<< " total memory bits: " << log_int(m_total_size_bytes
) << endl
;
121 DirectoryMemory::mapAddressToDirectoryVersion(PhysAddress address
)
123 if (m_num_directories_bits
== 0)
126 uint64 ret
= address
.bitSelect(m_numa_high_bit
- m_num_directories_bits
,
132 DirectoryMemory::isPresent(PhysAddress address
)
134 bool ret
= (mapAddressToDirectoryVersion(address
) == m_version
);
139 DirectoryMemory::mapAddressToLocalIdx(PhysAddress address
)
141 uint64 ret
= address
.bitRemove(m_numa_high_bit
- m_num_directories_bits
,
143 >> (RubySystem::getBlockSizeBits());
148 DirectoryMemory::lookup(PhysAddress address
)
150 assert(isPresent(address
));
151 Directory_Entry
* entry
;
153 DEBUG_EXPR(CACHE_COMP
, HighPrio
, address
);
156 if (m_sparseMemory
->exist(address
)) {
157 entry
= m_sparseMemory
->lookup(address
);
158 assert(entry
!= NULL
);
160 // Note: SparseMemory internally creates a new Directory Entry
161 m_sparseMemory
->add(address
);
162 entry
= m_sparseMemory
->lookup(address
);
165 idx
= mapAddressToLocalIdx(address
);
166 assert(idx
< m_num_entries
);
167 entry
= m_entries
[idx
];
170 entry
= new Directory_Entry();
171 entry
->getDataBlk().assign(m_ram
->getBlockPtr(address
));
172 m_entries
[idx
] = entry
;
181 DirectoryMemory::lookup(PhysAddress address
)
183 assert(isPresent(address
));
184 Index index
= address
.memoryModuleIndex();
186 if (index
< 0 || index
> m_size
) {
187 WARN_EXPR(address
.getAddress());
190 ERROR_MSG("Directory Memory Assertion: accessing memory out of range");
192 Directory_Entry
* entry
= m_entries
[index
];
194 // allocate the directory entry on demand.
196 entry
= new Directory_Entry
;
197 entry
->getDataBlk().assign(m_ram
->getBlockPtr(address
));
199 // store entry to the table
200 m_entries
[index
] = entry
;
208 DirectoryMemory::invalidateBlock(PhysAddress address
)
211 assert(m_sparseMemory
->exist(address
));
212 m_sparseMemory
->remove(address
);
216 assert(isPresent(address
));
218 Index index
= address
.memoryModuleIndex();
220 if (index
< 0 || index
> m_size
) {
221 ERROR_MSG("Directory Memory Assertion: "
222 "accessing memory out of range.");
225 if (m_entries
[index
] != NULL
){
226 delete m_entries
[index
];
227 m_entries
[index
] = NULL
;
234 DirectoryMemory::print(ostream
& out
) const
239 DirectoryMemory::printStats(ostream
& out
) const
242 m_sparseMemory
->printStats(out
);
247 RubyDirectoryMemoryParams::create()
249 return new DirectoryMemory(this);