Switch.cc revision 9230:33eb3c8a98b9
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/protocol/MessageSizeType.hh"
34#include "mem/ruby/buffers/MessageBuffer.hh"
35#include "mem/ruby/network/simple/PerfectSwitch.hh"
36#include "mem/ruby/network/simple/SimpleNetwork.hh"
37#include "mem/ruby/network/simple/Switch.hh"
38#include "mem/ruby/network/simple/Throttle.hh"
39
40using namespace std;
41using m5::stl_helpers::deletePointers;
42using m5::stl_helpers::operator<<;
43
44Switch::Switch(SwitchID sid, SimpleNetwork* network_ptr)
45{
46    m_perfect_switch_ptr = new PerfectSwitch(sid, network_ptr);
47    m_switch_id = sid;
48}
49
50Switch::~Switch()
51{
52    delete m_perfect_switch_ptr;
53
54    // Delete throttles (one per output port)
55    deletePointers(m_throttles);
56
57    // Delete MessageBuffers
58    deletePointers(m_buffers_to_free);
59}
60
61void
62Switch::addInPort(const vector<MessageBuffer*>& in)
63{
64    m_perfect_switch_ptr->addInPort(in);
65}
66
67void
68Switch::addOutPort(const vector<MessageBuffer*>& out,
69    const NetDest& routing_table_entry, int link_latency, int bw_multiplier)
70{
71    Throttle* throttle_ptr = NULL;
72    SimpleNetwork* net_ptr =
73        safe_cast<SimpleNetwork*>(RubySystem::getNetwork());
74
75    // Create a throttle
76    throttle_ptr = new Throttle(m_switch_id, m_throttles.size(), link_latency,
77                                bw_multiplier, net_ptr->getEndpointBandwidth(),
78                                net_ptr);
79    m_throttles.push_back(throttle_ptr);
80
81    // Create one buffer per vnet (these are intermediaryQueues)
82    vector<MessageBuffer*> intermediateBuffers;
83    for (int i = 0; i < out.size(); i++) {
84        MessageBuffer* buffer_ptr = new MessageBuffer;
85        // Make these queues ordered
86        buffer_ptr->setOrdering(true);
87        if (net_ptr->getBufferSize() > 0) {
88            buffer_ptr->resize(net_ptr->getBufferSize());
89        }
90        intermediateBuffers.push_back(buffer_ptr);
91        m_buffers_to_free.push_back(buffer_ptr);
92  }
93
94    // Hook the queues to the PerfectSwitch
95    m_perfect_switch_ptr->addOutPort(intermediateBuffers, routing_table_entry);
96
97    // Hook the queues to the Throttle
98    throttle_ptr->addLinks(intermediateBuffers, out);
99}
100
101void
102Switch::clearRoutingTables()
103{
104    m_perfect_switch_ptr->clearRoutingTables();
105}
106
107void
108Switch::clearBuffers()
109{
110    m_perfect_switch_ptr->clearBuffers();
111    for (int i = 0; i < m_throttles.size(); i++) {
112        if (m_throttles[i] != NULL) {
113            m_throttles[i]->clear();
114        }
115    }
116}
117
118void
119Switch::reconfigureOutPort(const NetDest& routing_table_entry)
120{
121    m_perfect_switch_ptr->reconfigureOutPort(routing_table_entry);
122}
123
124const Throttle*
125Switch::getThrottle(LinkID link_number) const
126{
127    assert(m_throttles[link_number] != NULL);
128    return m_throttles[link_number];
129}
130
131const vector<Throttle*>*
132Switch::getThrottles() const
133{
134    return &m_throttles;
135}
136
137void
138Switch::printStats(std::ostream& out) const
139{
140    ccprintf(out, "switch_%d_inlinks: %d\n", m_switch_id,
141        m_perfect_switch_ptr->getInLinks());
142    ccprintf(out, "switch_%d_outlinks: %d\n", m_switch_id,
143        m_perfect_switch_ptr->getOutLinks());
144
145    // Average link utilizations
146    double average_utilization = 0.0;
147    int throttle_count = 0;
148
149    for (int i = 0; i < m_throttles.size(); i++) {
150        Throttle* throttle_ptr = m_throttles[i];
151        if (throttle_ptr) {
152            average_utilization += throttle_ptr->getUtilization();
153            throttle_count++;
154        }
155    }
156    average_utilization =
157        throttle_count == 0 ? 0 : average_utilization / throttle_count;
158
159    // Individual link utilizations
160    out << "links_utilized_percent_switch_" << m_switch_id << ": "
161        << average_utilization << endl;
162
163    for (int link = 0; link < m_throttles.size(); link++) {
164        Throttle* throttle_ptr = m_throttles[link];
165        if (throttle_ptr != NULL) {
166            out << "  links_utilized_percent_switch_" << m_switch_id
167                << "_link_" << link << ": "
168                << throttle_ptr->getUtilization() << " bw: "
169                << throttle_ptr->getLinkBandwidth()
170                << " base_latency: " << throttle_ptr->getLatency() << endl;
171        }
172    }
173    out << endl;
174
175    // Traffic breakdown
176    for (int link = 0; link < m_throttles.size(); link++) {
177        Throttle* throttle_ptr = m_throttles[link];
178        if (!throttle_ptr)
179            continue;
180
181        const vector<vector<int> >& message_counts =
182            throttle_ptr->getCounters();
183        for (int int_type = 0; int_type < MessageSizeType_NUM; int_type++) {
184            MessageSizeType type = MessageSizeType(int_type);
185            const vector<int> &mct = message_counts[type];
186            int sum = accumulate(mct.begin(), mct.end(), 0);
187            if (sum == 0)
188                continue;
189
190            out << "  outgoing_messages_switch_" << m_switch_id
191                << "_link_" << link << "_" << type << ": " << sum << " "
192                << sum * RubySystem::getNetwork()->MessageSizeType_to_int(type)
193                << " ";
194            out << mct;
195            out << " base_latency: "
196                << throttle_ptr->getLatency() << endl;
197        }
198    }
199    out << endl;
200}
201
202void
203Switch::clearStats()
204{
205    m_perfect_switch_ptr->clearStats();
206    for (int i = 0; i < m_throttles.size(); i++) {
207        if (m_throttles[i] != NULL)
208            m_throttles[i]->clearStats();
209    }
210}
211
212void
213Switch::print(std::ostream& out) const
214{
215    // FIXME printing
216    out << "[Switch]";
217}
218