SimpleNetwork.cc revision 9859:1bd310386038
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::printStats(ostream& out) const
210{
211    out << endl;
212    out << "Network Stats" << endl;
213    out << "-------------" << endl;
214    out << endl;
215
216    //
217    // Determine total counts before printing out each switch's stats
218    //
219    std::vector<uint64> total_msg_counts;
220    total_msg_counts.resize(MessageSizeType_NUM);
221    for (MessageSizeType type = MessageSizeType_FIRST;
222         type < MessageSizeType_NUM;
223         ++type) {
224        total_msg_counts[type] = 0;
225    }
226
227    for (int i = 0; i < m_switches.size(); i++) {
228        const std::vector<Throttle*>* throttles =
229            m_switches[i]->getThrottles();
230
231        for (int p = 0; p < throttles->size(); p++) {
232
233            const std::vector<std::vector<int> >& message_counts =
234                ((*throttles)[p])->getCounters();
235
236            for (MessageSizeType type = MessageSizeType_FIRST;
237                 type < MessageSizeType_NUM;
238                 ++type) {
239
240                const std::vector<int> &mct = message_counts[type];
241                int sum = accumulate(mct.begin(), mct.end(), 0);
242                total_msg_counts[type] += uint64(sum);
243            }
244        }
245    }
246    uint64 total_msgs = 0;
247    uint64 total_bytes = 0;
248    for (MessageSizeType type = MessageSizeType_FIRST;
249         type < MessageSizeType_NUM;
250         ++type) {
251
252        if (total_msg_counts[type] > 0) {
253            out << "total_msg_count_" << type << ": " << total_msg_counts[type]
254                << " " << total_msg_counts[type] *
255                uint64(MessageSizeType_to_int(type))
256                << endl;
257
258            total_msgs += total_msg_counts[type];
259
260            total_bytes += total_msg_counts[type] *
261                uint64(MessageSizeType_to_int(type));
262        }
263    }
264
265    out << "total_msgs: " << total_msgs
266        << " total_bytes: " << total_bytes << endl;
267
268    out << endl;
269    for (int i = 0; i < m_switches.size(); i++) {
270        m_switches[i]->printStats(out);
271    }
272}
273
274void
275SimpleNetwork::clearStats()
276{
277    for (int i = 0; i < m_switches.size(); i++) {
278        m_switches[i]->clearStats();
279    }
280}
281
282void
283SimpleNetwork::print(ostream& out) const
284{
285    out << "[SimpleNetwork]";
286}
287
288SimpleNetwork *
289SimpleNetworkParams::create()
290{
291    return new SimpleNetwork(this);
292}
293
294/*
295 * The simple network has an array of switches. These switches have buffers
296 * that need to be accessed for functional reads and writes. Also the links
297 * between different switches have buffers that need to be accessed.
298 */
299bool
300SimpleNetwork::functionalRead(Packet *pkt)
301{
302    for (unsigned int i = 0; i < m_switches.size(); i++) {
303        if (m_switches[i]->functionalRead(pkt)) {
304            return true;
305        }
306    }
307
308    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
309        if (m_buffers_to_free[i]->functionalRead(pkt)) {
310            return true;
311        }
312    }
313
314    return false;
315}
316
317uint32_t
318SimpleNetwork::functionalWrite(Packet *pkt)
319{
320    uint32_t num_functional_writes = 0;
321
322    for (unsigned int i = 0; i < m_switches.size(); i++) {
323        num_functional_writes += m_switches[i]->functionalWrite(pkt);
324    }
325
326    for (unsigned int i = 0; i < m_buffers_to_free.size(); ++i) {
327        num_functional_writes += m_buffers_to_free[i]->functionalWrite(pkt);
328    }
329    return num_functional_writes;
330}
331