TimerTable.cc revision 7455
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/ruby/common/Global.hh" 30#include "mem/ruby/eventqueue/RubyEventQueue.hh" 31#include "mem/ruby/system/TimerTable.hh" 32 33TimerTable::TimerTable() 34{ 35 m_consumer_ptr = NULL; 36 m_next_valid = false; 37 m_next_address = Address(0); 38 m_next_time = 0; 39} 40 41bool 42TimerTable::isReady() const 43{ 44 if (m_map.empty()) 45 return false; 46 47 if (!m_next_valid) { 48 updateNext(); 49 } 50 assert(m_next_valid); 51 return (g_eventQueue_ptr->getTime() >= m_next_time); 52} 53 54const Address& 55TimerTable::readyAddress() const 56{ 57 assert(isReady()); 58 59 if (!m_next_valid) { 60 updateNext(); 61 } 62 assert(m_next_valid); 63 return m_next_address; 64} 65 66void 67TimerTable::set(const Address& address, Time relative_latency) 68{ 69 assert(address == line_address(address)); 70 assert(relative_latency > 0); 71 assert(!m_map.count(address)); 72 Time ready_time = g_eventQueue_ptr->getTime() + relative_latency; 73 m_map[address] = ready_time; 74 assert(m_consumer_ptr != NULL); 75 g_eventQueue_ptr->scheduleEventAbsolute(m_consumer_ptr, ready_time); 76 m_next_valid = false; 77 78 // Don't always recalculate the next ready address 79 if (ready_time <= m_next_time) { 80 m_next_valid = false; 81 } 82} 83 84void 85TimerTable::unset(const Address& address) 86{ 87 assert(address == line_address(address)); 88 assert(m_map.count(address)); 89 m_map.erase(address); 90 91 // Don't always recalculate the next ready address 92 if (address == m_next_address) { 93 m_next_valid = false; 94 } 95} 96 97void 98TimerTable::print(std::ostream& out) const 99{ 100} 101 102void 103TimerTable::updateNext() const 104{ 105 if (m_map.empty()) { 106 assert(!m_next_valid); 107 return; 108 } 109 110 AddressMap::const_iterator i = m_map.begin(); 111 AddressMap::const_iterator end = m_map.end(); 112 113 m_next_address = i->first; 114 m_next_time = i->second; 115 ++i; 116 117 for (; i != end; ++i) { 118 if (i->second < m_next_time) { 119 m_next_address = i->first; 120 m_next_time = i->second; 121 } 122 } 123 124 m_next_valid = true; 125} 126