SimpleNetwork.cc revision 9866:94dac7d7bb88
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 <cassert>
30#include <numeric>
31
32#include "base/cast.hh"
33#include "base/stl_helpers.hh"
34#include "mem/ruby/buffers/MessageBuffer.hh"
35#include "mem/ruby/common/NetDest.hh"
36#include "mem/ruby/network/BasicLink.hh"
37#include "mem/ruby/network/simple/SimpleLink.hh"
38#include "mem/ruby/network/simple/SimpleNetwork.hh"
39#include "mem/ruby/network/simple/Switch.hh"
40#include "mem/ruby/network/simple/Throttle.hh"
41#include "mem/ruby/network/Topology.hh"
42#include "mem/ruby/profiler/Profiler.hh"
43#include "mem/ruby/system/System.hh"
44
45using namespace std;
46using m5::stl_helpers::deletePointers;
47
48SimpleNetwork::SimpleNetwork(const Params *p)
49    : Network(p)
50{
51    m_buffer_size = p->buffer_size;
52    m_endpoint_bandwidth = p->endpoint_bandwidth;
53    m_adaptive_routing = p->adaptive_routing;
54
55    // Note: the parent Network Object constructor is called before the
56    // SimpleNetwork child constructor.  Therefore, the member variables
57    // used below should already be initialized.
58
59    m_endpoint_switches.resize(m_nodes);
60
61    m_in_use.resize(m_virtual_networks);
62    m_ordered.resize(m_virtual_networks);
63    for (int i = 0; i < m_virtual_networks; i++) {
64        m_in_use[i] = false;
65        m_ordered[i] = false;
66    }
67
68    // Allocate to and from queues
69    m_toNetQueues.resize(m_nodes);
70    m_fromNetQueues.resize(m_nodes);
71    for (int node = 0; node < m_nodes; node++) {
72        m_toNetQueues[node].resize(m_virtual_networks);
73        m_fromNetQueues[node].resize(m_virtual_networks);
74        for (int j = 0; j < m_virtual_networks; j++) {
75            m_toNetQueues[node][j] =
76                new MessageBuffer(csprintf("toNet node %d j %d", node, j));
77            m_fromNetQueues[node][j] =
78                new MessageBuffer(csprintf("fromNet node %d j %d", node, j));
79        }
80    }
81
82    // record the routers
83    for (vector<BasicRouter*>::const_iterator i = p->routers.begin();
84         i != p->routers.end(); ++i) {
85        Switch* s = safe_cast<Switch*>(*i);
86        m_switches.push_back(s);
87        s->init_net_ptr(this);
88    }
89}
90
91void
92SimpleNetwork::init()
93{
94    Network::init();
95
96    // The topology pointer should have already been initialized in
97    // the parent class network constructor.
98    assert(m_topology_ptr != NULL);
99    m_topology_ptr->createLinks(this);
100}
101
102SimpleNetwork::~SimpleNetwork()
103{
104    for (int i = 0; i < m_nodes; i++) {
105        deletePointers(m_toNetQueues[i]);
106        deletePointers(m_fromNetQueues[i]);
107    }
108    deletePointers(m_switches);
109    deletePointers(m_buffers_to_free);
110    // delete m_topology_ptr;
111}
112
113// From a switch to an endpoint node
114void
115SimpleNetwork::makeOutLink(SwitchID src, NodeID dest, BasicLink* link,
116                           LinkDirection direction,
117                           const NetDest& routing_table_entry)
118{
119    assert(dest < m_nodes);
120    assert(src < m_switches.size());
121    assert(m_switches[src] != NULL);
122
123    SimpleExtLink *simple_link = safe_cast<SimpleExtLink*>(link);
124
125    m_switches[src]->addOutPort(m_fromNetQueues[dest],
126                                         routing_table_entry,
127                                         simple_link->m_latency,
128                                         simple_link->m_bw_multiplier);
129
130    m_endpoint_switches[dest] = m_switches[src];
131}
132
133// From an endpoint node to a switch
134void
135SimpleNetwork::makeInLink(NodeID src, SwitchID dest, BasicLink* link,
136                          LinkDirection direction,
137                          const NetDest& routing_table_entry)
138{
139    assert(src < m_nodes);
140    m_switches[dest]->addInPort(m_toNetQueues[src]);
141}
142
143// From a switch to a switch
144void
145SimpleNetwork::makeInternalLink(SwitchID src, SwitchID dest, BasicLink* link,
146                                LinkDirection direction,
147                                const NetDest& routing_table_entry)
148{
149    // Create a set of new MessageBuffers
150    std::vector<MessageBuffer*> queues;
151    for (int i = 0; i < m_virtual_networks; i++) {
152        // allocate a buffer
153        MessageBuffer* buffer_ptr = new MessageBuffer;
154        buffer_ptr->setOrdering(true);
155        if (m_buffer_size > 0) {
156            buffer_ptr->resize(m_buffer_size);
157        }
158        queues.push_back(buffer_ptr);
159        // remember to deallocate it
160        m_buffers_to_free.push_back(buffer_ptr);
161    }
162    // Connect it to the two switches
163    SimpleIntLink *simple_link = safe_cast<SimpleIntLink*>(link);
164
165    m_switches[dest]->addInPort(queues);
166    m_switches[src]->addOutPort(queues, routing_table_entry,
167                                         simple_link->m_latency,
168                                         simple_link->m_bw_multiplier);
169}
170
171void
172SimpleNetwork::checkNetworkAllocation(NodeID id, bool ordered, int network_num)
173{
174    assert(id < m_nodes);
175    assert(network_num < m_virtual_networks);
176
177    if (ordered) {
178        m_ordered[network_num] = true;
179    }
180    m_in_use[network_num] = true;
181}
182
183MessageBuffer*
184SimpleNetwork::getToNetQueue(NodeID id, bool ordered, int network_num,
185                             std::string vnet_type)
186{
187    checkNetworkAllocation(id, ordered, network_num);
188    return m_toNetQueues[id][network_num];
189}
190
191MessageBuffer*
192SimpleNetwork::getFromNetQueue(NodeID id, bool ordered, int network_num,
193                               std::string vnet_type)
194{
195    checkNetworkAllocation(id, ordered, network_num);
196    return m_fromNetQueues[id][network_num];
197}
198
199const std::vector<Throttle*>*
200SimpleNetwork::getThrottles(NodeID id) const
201{
202    assert(id >= 0);
203    assert(id < m_nodes);
204    assert(m_endpoint_switches[id] != NULL);
205    return m_endpoint_switches[id]->getThrottles();
206}
207
208void
209SimpleNetwork::regStats()
210{
211    for (MessageSizeType type = MessageSizeType_FIRST;
212         type < MessageSizeType_NUM; ++type) {
213        m_msg_counts[(unsigned int) type]
214            .name(name() + ".msg_count." + MessageSizeType_to_string(type))
215            .flags(Stats::nozero)
216            ;
217        m_msg_bytes[(unsigned int) type]
218            .name(name() + ".msg_byte." + MessageSizeType_to_string(type))
219            .flags(Stats::nozero)
220            ;
221
222        // Now state what the formula is.
223        for (int i = 0; i < m_switches.size(); i++) {
224            m_msg_counts[(unsigned int) type] +=
225                sum(m_switches[i]->getMsgCount(type));
226        }
227
228        m_msg_bytes[(unsigned int) type] =
229            m_msg_counts[(unsigned int) type] * Stats::constant(
230                    Network::MessageSizeType_to_int(type));
231    }
232}
233
234void
235SimpleNetwork::collateStats()
236{
237    for (int i = 0; i < m_switches.size(); i++) {
238        m_switches[i]->collateStats();
239    }
240}
241
242void
243SimpleNetwork::print(ostream& out) const
244{
245    out << "[SimpleNetwork]";
246}
247
248SimpleNetwork *
249SimpleNetworkParams::create()
250{
251    return new SimpleNetwork(this);
252}
253
254/*
255 * The simple network has an array of switches. These switches have buffers
256 * that need to be accessed for functional reads and writes. Also the links
257 * between different switches have buffers that need to be accessed.
258 */
259bool
260SimpleNetwork::functionalRead(Packet *pkt)
261{
262    for (unsigned int i = 0; i < m_switches.size(); i++) {
263        if (m_switches[i]->functionalRead(pkt)) {
264            return true;
265        }
266    }
267
268    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
269        if (m_buffers_to_free[i]->functionalRead(pkt)) {
270            return true;
271        }
272    }
273
274    return false;
275}
276
277uint32_t
278SimpleNetwork::functionalWrite(Packet *pkt)
279{
280    uint32_t num_functional_writes = 0;
281
282    for (unsigned int i = 0; i < m_switches.size(); i++) {
283        num_functional_writes += m_switches[i]->functionalWrite(pkt);
284    }
285
286    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
287        num_functional_writes += m_buffers_to_free[i]->functionalWrite(pkt);
288    }
289    return num_functional_writes;
290}
291