WireBuffer.cc revision 11116:d6fb95dbf3e2
1/*
2 * Copyright (c) 2010 Advanced Micro Devices, Inc.
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 * Author:  Lisa Hsu
29 *
30 */
31
32#include <algorithm>
33#include <functional>
34
35#include "base/cprintf.hh"
36#include "base/stl_helpers.hh"
37#include "mem/ruby/structures/WireBuffer.hh"
38#include "mem/ruby/system/RubySystem.hh"
39
40using namespace std;
41
42// Output operator definition
43
44ostream&
45operator<<(ostream& out, const WireBuffer& obj)
46{
47    obj.print(out);
48    out << flush;
49    return out;
50}
51
52
53// ****************************************************************
54
55// CONSTRUCTOR
56WireBuffer::WireBuffer(const Params *p)
57    : SimObject(p)
58{
59    m_msg_counter = 0;
60}
61
62void
63WireBuffer::init()
64{
65}
66
67WireBuffer::~WireBuffer()
68{
69}
70
71void
72WireBuffer::enqueue(MsgPtr message, Tick current_time, Tick delta)
73{
74    m_msg_counter++;
75    Tick arrival_time = current_time + delta;
76    assert(arrival_time > current_time);
77
78    Message* msg_ptr = message.get();
79    msg_ptr->setLastEnqueueTime(arrival_time);
80    m_message_queue.push_back(message);
81    if (m_consumer_ptr != NULL) {
82        m_consumer_ptr->
83            scheduleEventAbsolute(arrival_time);
84    } else {
85        panic("No Consumer for WireBuffer! %s\n", *this);
86    }
87}
88
89void
90WireBuffer::dequeue(Tick current_time)
91{
92    assert(isReady(current_time));
93    pop_heap(m_message_queue.begin(), m_message_queue.end(),
94        greater<MsgPtr>());
95    m_message_queue.pop_back();
96}
97
98const Message*
99WireBuffer::peek()
100{
101    Message* msg_ptr = m_message_queue.front().get();
102    assert(msg_ptr != NULL);
103    return msg_ptr;
104}
105
106void
107WireBuffer::recycle(Tick current_time, Tick recycle_latency)
108{
109    // Because you don't want anything reordered, make sure the recycle latency
110    // is just 1 cycle. As a result, you really want to use this only in
111    // Wire-like situations because you don't want to deadlock as a result of
112    // being stuck behind something if you're not actually supposed to.
113    assert(isReady(current_time));
114    MsgPtr node = m_message_queue.front();
115    pop_heap(m_message_queue.begin(), m_message_queue.end(), greater<MsgPtr>());
116
117    Tick future_time = current_time + recycle_latency;
118    node->setLastEnqueueTime(future_time);
119
120    m_message_queue.back() = node;
121    push_heap(m_message_queue.begin(), m_message_queue.end(),
122        greater<MsgPtr>());
123    m_consumer_ptr->
124        scheduleEventAbsolute(future_time);
125}
126
127bool
128WireBuffer::isReady(Tick current_time)
129{
130    return ((!m_message_queue.empty()) &&
131            (m_message_queue.front()->getLastEnqueueTime() <= current_time));
132}
133
134void
135WireBuffer::print(ostream& out) const
136{
137}
138
139void
140WireBuffer::wakeup()
141{
142}
143
144WireBuffer *
145RubyWireBufferParams::create()
146{
147    return new WireBuffer(this);
148}
149