Switch.cc revision 7002:48a19d52d939
1
2/*
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * Switch.cc
32 *
33 * Description: See Switch.hh
34 *
35 * $Id$
36 *
37 */
38
39
40#include "mem/ruby/network/simple/Switch.hh"
41#include "mem/ruby/network/simple/PerfectSwitch.hh"
42#include "mem/ruby/buffers/MessageBuffer.hh"
43#include "mem/ruby/network/simple/Throttle.hh"
44#include "mem/protocol/MessageSizeType.hh"
45#include "mem/ruby/network/Network.hh"
46#include "mem/protocol/Protocol.hh"
47
48Switch::Switch(SwitchID sid, SimpleNetwork* network_ptr)
49{
50  m_perfect_switch_ptr = new PerfectSwitch(sid, network_ptr);
51  m_switch_id = sid;
52  m_throttles.setSize(0);
53}
54
55Switch::~Switch()
56{
57  delete m_perfect_switch_ptr;
58
59  // Delete throttles (one per output port)
60  m_throttles.deletePointers();
61
62  // Delete MessageBuffers
63  m_buffers_to_free.deletePointers();
64}
65
66void Switch::addInPort(const Vector<MessageBuffer*>& in)
67{
68  m_perfect_switch_ptr->addInPort(in);
69}
70
71void Switch::addOutPort(const Vector<MessageBuffer*>& out, const NetDest& routing_table_entry, int link_latency, int bw_multiplier)
72{
73  Throttle* throttle_ptr = NULL;
74
75  // Create a throttle
76  throttle_ptr = new Throttle(m_switch_id, m_throttles.size(), link_latency, bw_multiplier);
77  m_throttles.insertAtBottom(throttle_ptr);
78
79  // Create one buffer per vnet (these are intermediaryQueues)
80  Vector<MessageBuffer*> intermediateBuffers;
81  for (int i=0; i<out.size(); i++) {
82    MessageBuffer* buffer_ptr = new MessageBuffer;
83    // Make these queues ordered
84    buffer_ptr->setOrdering(true);
85    Network* net_ptr = RubySystem::getNetwork();
86    if(net_ptr->getBufferSize() > 0) {
87      buffer_ptr->setSize(net_ptr->getBufferSize());
88    }
89    intermediateBuffers.insertAtBottom(buffer_ptr);
90    m_buffers_to_free.insertAtBottom(buffer_ptr);
91  }
92
93  // Hook the queues to the PerfectSwitch
94  m_perfect_switch_ptr->addOutPort(intermediateBuffers, routing_table_entry);
95
96  // Hook the queues to the Throttle
97  throttle_ptr->addLinks(intermediateBuffers, out);
98
99}
100
101void Switch::clearRoutingTables()
102{
103  m_perfect_switch_ptr->clearRoutingTables();
104}
105
106void Switch::clearBuffers()
107{
108  m_perfect_switch_ptr->clearBuffers();
109  for (int i=0; i<m_throttles.size(); i++) {
110    if (m_throttles[i] != NULL) {
111      m_throttles[i]->clear();
112    }
113  }
114}
115
116void Switch::reconfigureOutPort(const NetDest& routing_table_entry)
117{
118  m_perfect_switch_ptr->reconfigureOutPort(routing_table_entry);
119}
120
121const Throttle* Switch::getThrottle(LinkID link_number) const
122{
123  assert(m_throttles[link_number] != NULL);
124  return m_throttles[link_number];
125}
126
127const Vector<Throttle*>* Switch::getThrottles() const
128{
129  return &m_throttles;
130}
131
132void Switch::printStats(std::ostream& out) const
133{
134  using namespace std;
135
136  out << "switch_" << m_switch_id << "_inlinks: " << m_perfect_switch_ptr->getInLinks() << endl;
137  out << "switch_" << m_switch_id << "_outlinks: " << m_perfect_switch_ptr->getOutLinks() << endl;
138
139  // Average link utilizations
140  double average_utilization = 0.0;
141  int throttle_count = 0;
142
143  for (int i=0; i<m_throttles.size(); i++) {
144    Throttle* throttle_ptr = m_throttles[i];
145    if (throttle_ptr != NULL) {
146      average_utilization += throttle_ptr->getUtilization();
147      throttle_count++;
148    }
149  }
150  average_utilization = (throttle_count == 0) ? 0 : average_utilization / float(throttle_count);
151
152  // Individual link utilizations
153  out << "links_utilized_percent_switch_" << m_switch_id << ": " << average_utilization << endl;
154  for (int link=0; link<m_throttles.size(); link++) {
155    Throttle* throttle_ptr = m_throttles[link];
156    if (throttle_ptr != NULL) {
157      out << "  links_utilized_percent_switch_" << m_switch_id << "_link_" << link << ": "
158          << throttle_ptr->getUtilization() << " bw: " << throttle_ptr->getLinkBandwidth()
159          << " base_latency: " << throttle_ptr->getLatency() << endl;
160    }
161  }
162  out << endl;
163
164  // Traffic breakdown
165  for (int link=0; link<m_throttles.size(); link++) {
166    Throttle* throttle_ptr = m_throttles[link];
167    if (throttle_ptr != NULL) {
168      const Vector<Vector<int> >& message_counts = throttle_ptr->getCounters();
169      for (int int_type=0; int_type<MessageSizeType_NUM; int_type++) {
170        MessageSizeType type = MessageSizeType(int_type);
171        int sum = message_counts[type].sum();
172        if (sum != 0) {
173          out << "  outgoing_messages_switch_" << m_switch_id << "_link_" << link << "_" << type
174              << ": " << sum << " " << sum * (RubySystem::getNetwork()->MessageSizeType_to_int(type))
175              << " " << message_counts[type] << " base_latency: " << throttle_ptr->getLatency() << endl;
176        }
177      }
178    }
179  }
180  out << endl;
181}
182
183void Switch::clearStats()
184{
185  m_perfect_switch_ptr->clearStats();
186  for (int i=0; i<m_throttles.size(); i++) {
187    if (m_throttles[i] != NULL) {
188      m_throttles[i]->clearStats();
189    }
190  }
191}
192
193void Switch::printConfig(std::ostream& out) const
194{
195  m_perfect_switch_ptr->printConfig(out);
196  for (int i=0; i<m_throttles.size(); i++) {
197    if (m_throttles[i] != NULL) {
198      m_throttles[i]->printConfig(out);
199    }
200  }
201}
202
203void Switch::print(std::ostream& out) const
204{
205  // FIXME printing
206  out << "[Switch]";
207}
208
209