TimerTable.cc revision 6467
112837Sgabeblack@google.com
212837Sgabeblack@google.com/*
312837Sgabeblack@google.com * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
412837Sgabeblack@google.com * All rights reserved.
512837Sgabeblack@google.com *
612837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
712837Sgabeblack@google.com * modification, are permitted provided that the following conditions are
812837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
912837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
1012837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1112837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1212837Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1312837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1412837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1512837Sgabeblack@google.com * this software without specific prior written permission.
1612837Sgabeblack@google.com *
1712837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1812837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1912837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2012837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2112837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2212837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2312837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2412837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2512837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2612837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2712837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2812837Sgabeblack@google.com */
2912837Sgabeblack@google.com
3012837Sgabeblack@google.com/*
3112954Sgabeblack@google.com * $Id$
3213140Sgabeblack@google.com */
3312837Sgabeblack@google.com
3412837Sgabeblack@google.com#include "mem/ruby/common/Global.hh"
3513140Sgabeblack@google.com#include "mem/ruby/system/TimerTable.hh"
3613140Sgabeblack@google.com#include "mem/ruby/eventqueue/RubyEventQueue.hh"
3713140Sgabeblack@google.com
3813140Sgabeblack@google.comTimerTable::TimerTable()
3913140Sgabeblack@google.com{
4013140Sgabeblack@google.com  m_consumer_ptr  = NULL;
4113140Sgabeblack@google.com  m_next_valid = false;
4212837Sgabeblack@google.com  m_next_address = Address(0);
4312837Sgabeblack@google.com  m_next_time = 0;
4412837Sgabeblack@google.com}
4512954Sgabeblack@google.com
4612954Sgabeblack@google.com
4712954Sgabeblack@google.combool TimerTable::isReady() const
4812837Sgabeblack@google.com{
4912954Sgabeblack@google.com  if (m_map.size() == 0) {
5012954Sgabeblack@google.com    return false;
5112954Sgabeblack@google.com  }
5212837Sgabeblack@google.com
5312954Sgabeblack@google.com  if (!m_next_valid) {
5412837Sgabeblack@google.com    updateNext();
5512837Sgabeblack@google.com  }
5612837Sgabeblack@google.com  assert(m_next_valid);
5712837Sgabeblack@google.com  return (g_eventQueue_ptr->getTime() >= m_next_time);
5812954Sgabeblack@google.com}
5912837Sgabeblack@google.com
6012837Sgabeblack@google.comconst Address& TimerTable::readyAddress() const
6112837Sgabeblack@google.com{
6212837Sgabeblack@google.com  assert(isReady());
6312837Sgabeblack@google.com
6412954Sgabeblack@google.com  if (!m_next_valid) {
6512837Sgabeblack@google.com    updateNext();
6612837Sgabeblack@google.com  }
6712837Sgabeblack@google.com  assert(m_next_valid);
6812837Sgabeblack@google.com  return m_next_address;
6912837Sgabeblack@google.com}
7012954Sgabeblack@google.com
7112837Sgabeblack@google.comvoid TimerTable::set(const Address& address, Time relative_latency)
7212837Sgabeblack@google.com{
7312837Sgabeblack@google.com  assert(address == line_address(address));
7412954Sgabeblack@google.com  assert(relative_latency > 0);
7512837Sgabeblack@google.com  assert(m_map.exist(address) == false);
7612954Sgabeblack@google.com  Time ready_time = g_eventQueue_ptr->getTime() + relative_latency;
7712837Sgabeblack@google.com  m_map.add(address, ready_time);
7812837Sgabeblack@google.com  assert(m_consumer_ptr != NULL);
7912837Sgabeblack@google.com  g_eventQueue_ptr->scheduleEventAbsolute(m_consumer_ptr, ready_time);
8012954Sgabeblack@google.com  m_next_valid = false;
8112837Sgabeblack@google.com
8212954Sgabeblack@google.com  // Don't always recalculate the next ready address
8312837Sgabeblack@google.com  if (ready_time <= m_next_time) {
8412837Sgabeblack@google.com    m_next_valid = false;
8512837Sgabeblack@google.com  }
8612954Sgabeblack@google.com}
8712837Sgabeblack@google.com
8812954Sgabeblack@google.comvoid TimerTable::unset(const Address& address)
8912837Sgabeblack@google.com{
9012837Sgabeblack@google.com  assert(address == line_address(address));
9112837Sgabeblack@google.com  assert(m_map.exist(address) == true);
9212954Sgabeblack@google.com  m_map.remove(address);
9312837Sgabeblack@google.com
9412954Sgabeblack@google.com  // Don't always recalculate the next ready address
9512837Sgabeblack@google.com  if (address == m_next_address) {
9612837Sgabeblack@google.com    m_next_valid = false;
9712837Sgabeblack@google.com  }
9812954Sgabeblack@google.com}
9912837Sgabeblack@google.com
10012954Sgabeblack@google.comvoid TimerTable::print(ostream& out) const
10112837Sgabeblack@google.com{
10212837Sgabeblack@google.com}
10312837Sgabeblack@google.com
10412954Sgabeblack@google.com
10512837Sgabeblack@google.comvoid TimerTable::updateNext() const
10612954Sgabeblack@google.com{
10712837Sgabeblack@google.com  if (m_map.size() == 0) {
10812837Sgabeblack@google.com    assert(m_next_valid == false);
10912837Sgabeblack@google.com    return;
11012954Sgabeblack@google.com  }
11112837Sgabeblack@google.com
11212954Sgabeblack@google.com  Vector<Address> addresses = m_map.keys();
11312837Sgabeblack@google.com  m_next_address = addresses[0];
11412837Sgabeblack@google.com  m_next_time = m_map.lookup(m_next_address);
11512837Sgabeblack@google.com
11612954Sgabeblack@google.com  // Search for the minimum time
11712837Sgabeblack@google.com  int size = addresses.size();
11812954Sgabeblack@google.com  for (int i=1; i<size; i++) {
11912837Sgabeblack@google.com    Address maybe_next_address = addresses[i];
12012837Sgabeblack@google.com    Time maybe_next_time = m_map.lookup(maybe_next_address);
12112837Sgabeblack@google.com    if (maybe_next_time < m_next_time) {
12212954Sgabeblack@google.com      m_next_time = maybe_next_time;
12312954Sgabeblack@google.com      m_next_address= maybe_next_address;
12412837Sgabeblack@google.com    }
12512954Sgabeblack@google.com  }
12612837Sgabeblack@google.com  m_next_valid = true;
12712837Sgabeblack@google.com}
12812837Sgabeblack@google.com