Switch.cc revision 10301:44839e8febbd
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 <numeric>
30
31#include "base/cast.hh"
32#include "base/stl_helpers.hh"
33#include "mem/ruby/network/MessageBuffer.hh"
34#include "mem/ruby/network/simple/PerfectSwitch.hh"
35#include "mem/ruby/network/simple/SimpleNetwork.hh"
36#include "mem/ruby/network/simple/Switch.hh"
37#include "mem/ruby/network/simple/Throttle.hh"
38
39using namespace std;
40using m5::stl_helpers::deletePointers;
41using m5::stl_helpers::operator<<;
42
43Switch::Switch(const Params *p) : BasicRouter(p)
44{
45    m_perfect_switch = new PerfectSwitch(m_id, this, p->virt_nets);
46}
47
48Switch::~Switch()
49{
50    delete m_perfect_switch;
51
52    // Delete throttles (one per output port)
53    deletePointers(m_throttles);
54
55    // Delete MessageBuffers
56    deletePointers(m_buffers_to_free);
57}
58
59void
60Switch::init()
61{
62    BasicRouter::init();
63    m_perfect_switch->init(m_network_ptr);
64}
65
66void
67Switch::addInPort(const vector<MessageBuffer*>& in)
68{
69    m_perfect_switch->addInPort(in);
70
71    for (int i = 0; i < in.size(); i++) {
72        in[i]->setReceiver(this);
73    }
74}
75
76void
77Switch::addOutPort(const vector<MessageBuffer*>& out,
78    const NetDest& routing_table_entry, Cycles link_latency, int bw_multiplier)
79{
80    // Create a throttle
81    Throttle* throttle_ptr = new Throttle(m_id, m_throttles.size(),
82            link_latency, bw_multiplier, m_network_ptr->getEndpointBandwidth(),
83            this);
84    m_throttles.push_back(throttle_ptr);
85
86    // Create one buffer per vnet (these are intermediaryQueues)
87    vector<MessageBuffer*> intermediateBuffers;
88    for (int i = 0; i < out.size(); i++) {
89        out[i]->setSender(this);
90
91        MessageBuffer* buffer_ptr = new MessageBuffer;
92        // Make these queues ordered
93        buffer_ptr->setOrdering(true);
94        if (m_network_ptr->getBufferSize() > 0) {
95            buffer_ptr->resize(m_network_ptr->getBufferSize());
96        }
97
98        intermediateBuffers.push_back(buffer_ptr);
99        m_buffers_to_free.push_back(buffer_ptr);
100
101        buffer_ptr->setSender(this);
102        buffer_ptr->setReceiver(this);
103    }
104
105    // Hook the queues to the PerfectSwitch
106    m_perfect_switch->addOutPort(intermediateBuffers, routing_table_entry);
107
108    // Hook the queues to the Throttle
109    throttle_ptr->addLinks(intermediateBuffers, out);
110}
111
112const Throttle*
113Switch::getThrottle(LinkID link_number) const
114{
115    assert(m_throttles[link_number] != NULL);
116    return m_throttles[link_number];
117}
118
119void
120Switch::regStats()
121{
122    for (int link = 0; link < m_throttles.size(); link++) {
123        m_throttles[link]->regStats(name());
124    }
125
126    m_avg_utilization.name(name() + ".percent_links_utilized");
127    for (unsigned int i = 0; i < m_throttles.size(); i++) {
128        m_avg_utilization += m_throttles[i]->getUtilization();
129    }
130    m_avg_utilization /= Stats::constant(m_throttles.size());
131
132    for (unsigned int type = MessageSizeType_FIRST;
133         type < MessageSizeType_NUM; ++type) {
134        m_msg_counts[type]
135            .name(name() + ".msg_count." +
136                MessageSizeType_to_string(MessageSizeType(type)))
137            .flags(Stats::nozero)
138            ;
139        m_msg_bytes[type]
140            .name(name() + ".msg_bytes." +
141                MessageSizeType_to_string(MessageSizeType(type)))
142            .flags(Stats::nozero)
143            ;
144
145        for (unsigned int i = 0; i < m_throttles.size(); i++) {
146            m_msg_counts[type] += m_throttles[i]->getMsgCount(type);
147        }
148        m_msg_bytes[type] = m_msg_counts[type] * Stats::constant(
149                Network::MessageSizeType_to_int(MessageSizeType(type)));
150    }
151}
152
153void
154Switch::resetStats()
155{
156    m_perfect_switch->clearStats();
157    for (int i = 0; i < m_throttles.size(); i++) {
158        m_throttles[i]->clearStats();
159    }
160}
161
162void
163Switch::collateStats()
164{
165    m_perfect_switch->collateStats();
166    for (int i = 0; i < m_throttles.size(); i++) {
167        m_throttles[i]->collateStats();
168    }
169}
170
171void
172Switch::print(std::ostream& out) const
173{
174    // FIXME printing
175    out << "[Switch]";
176}
177
178bool
179Switch::functionalRead(Packet *pkt)
180{
181    // Access the buffers in the switch for performing a functional read
182    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
183        if (m_buffers_to_free[i]->functionalRead(pkt)) {
184            return true;
185        }
186    }
187    return false;
188}
189
190uint32_t
191Switch::functionalWrite(Packet *pkt)
192{
193    // Access the buffers in the switch for performing a functional write
194    uint32_t num_functional_writes = 0;
195    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
196        num_functional_writes += m_buffers_to_free[i]->functionalWrite(pkt);
197    }
198    return num_functional_writes;
199}
200
201Switch *
202SwitchParams::create()
203{
204    return new Switch(this);
205}
206