PersistentTable.cc revision 7054
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 "mem/gems_common/util.hh"
30#include "mem/ruby/system/PersistentTable.hh"
31
32// randomize so that handoffs are not locality-aware
33#if 0
34int persistent_randomize[] = {0, 4, 8, 12, 1, 5, 9, 13, 2, 6,
35                              10, 14, 3, 7, 11, 15};
36int persistent_randomize[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
37                              10, 11, 12, 13, 14, 15};
38#endif
39
40PersistentTable::PersistentTable()
41{
42    m_map_ptr = new Map<Address, PersistentTableEntry>;
43}
44
45PersistentTable::~PersistentTable()
46{
47    delete m_map_ptr;
48    m_map_ptr = NULL;
49}
50
51void
52PersistentTable::persistentRequestLock(const Address& address,
53                                       MachineID locker,
54                                       AccessType type)
55{
56#if 0
57    if (locker == m_chip_ptr->getID())
58        cout << "Chip " << m_chip_ptr->getID() << ": " << llocker
59             << " requesting lock for " << address << endl;
60
61    MachineID locker = (MachineID) persistent_randomize[llocker];
62#endif
63
64    assert(address == line_address(address));
65    if (!m_map_ptr->exist(address)) {
66        // Allocate if not present
67        PersistentTableEntry entry;
68        entry.m_starving.add(locker);
69        if (type == AccessType_Write) {
70            entry.m_request_to_write.add(locker);
71        }
72        m_map_ptr->add(address, entry);
73    } else {
74        PersistentTableEntry& entry = m_map_ptr->lookup(address);
75
76        //
77        // Make sure we're not already in the locked set
78        //
79        assert(!(entry.m_starving.isElement(locker)));
80
81        entry.m_starving.add(locker);
82        if (type == AccessType_Write) {
83            entry.m_request_to_write.add(locker);
84        }
85        assert(entry.m_marked.isSubset(entry.m_starving));
86    }
87}
88
89void
90PersistentTable::persistentRequestUnlock(const Address& address,
91                                         MachineID unlocker)
92{
93#if 0
94    if (unlocker == m_chip_ptr->getID())
95        cout << "Chip " << m_chip_ptr->getID() << ": " << uunlocker
96             << " requesting unlock for " << address << endl;
97
98    MachineID unlocker = (MachineID) persistent_randomize[uunlocker];
99#endif
100
101    assert(address == line_address(address));
102    assert(m_map_ptr->exist(address));
103    PersistentTableEntry& entry = m_map_ptr->lookup(address);
104
105    //
106    // Make sure we're in the locked set
107    //
108    assert(entry.m_starving.isElement(unlocker));
109    assert(entry.m_marked.isSubset(entry.m_starving));
110    entry.m_starving.remove(unlocker);
111    entry.m_marked.remove(unlocker);
112    entry.m_request_to_write.remove(unlocker);
113    assert(entry.m_marked.isSubset(entry.m_starving));
114
115    // Deallocate if empty
116    if (entry.m_starving.isEmpty()) {
117        assert(entry.m_marked.isEmpty());
118        m_map_ptr->erase(address);
119    }
120}
121
122bool
123PersistentTable::okToIssueStarving(const Address& address,
124                                   MachineID machId) const
125{
126    assert(address == line_address(address));
127    if (!m_map_ptr->exist(address)) {
128        // No entry present
129        return true;
130    } else if (m_map_ptr->lookup(address).m_starving.isElement(machId)) {
131        // We can't issue another lockdown until are previous unlock
132        // has occurred
133        return false;
134    } else {
135        return m_map_ptr->lookup(address).m_marked.isEmpty();
136    }
137}
138
139MachineID
140PersistentTable::findSmallest(const Address& address) const
141{
142    assert(address == line_address(address));
143    assert(m_map_ptr->exist(address));
144    const PersistentTableEntry& entry = m_map_ptr->lookup(address);
145    return entry.m_starving.smallestElement();
146}
147
148AccessType
149PersistentTable::typeOfSmallest(const Address& address) const
150{
151    assert(address == line_address(address));
152    assert(m_map_ptr->exist(address));
153    const PersistentTableEntry& entry = m_map_ptr->lookup(address);
154    if (entry.m_request_to_write.
155        isElement(entry.m_starving.smallestElement())) {
156        return AccessType_Write;
157    } else {
158        return AccessType_Read;
159    }
160}
161
162void
163PersistentTable::markEntries(const Address& address)
164{
165    assert(address == line_address(address));
166    if (m_map_ptr->exist(address)) {
167        PersistentTableEntry& entry = m_map_ptr->lookup(address);
168
169        // None should be marked
170        assert(entry.m_marked.isEmpty());
171
172        // Mark all the nodes currently in the table
173        entry.m_marked = entry.m_starving;
174    }
175}
176
177bool
178PersistentTable::isLocked(const Address& address) const
179{
180    assert(address == line_address(address));
181
182    // If an entry is present, it must be locked
183    return m_map_ptr->exist(address);
184}
185
186int
187PersistentTable::countStarvingForAddress(const Address& address) const
188{
189    if (m_map_ptr->exist(address)) {
190        PersistentTableEntry& entry = m_map_ptr->lookup(address);
191        return (entry.m_starving.count());
192    } else {
193        return 0;
194    }
195}
196
197int
198PersistentTable::countReadStarvingForAddress(const Address& address) const
199{
200    if (m_map_ptr->exist(address)) {
201        PersistentTableEntry& entry = m_map_ptr->lookup(address);
202        return (entry.m_starving.count() - entry.m_request_to_write.count());
203    } else {
204        return 0;
205    }
206}
207
208void
209PersistentTable::print(ostream& out) const
210{
211}
212
213