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