Switch.cc revision 6145:15cca6ab723a
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.C
32 *
33 * Description: See Switch.h
34 *
35 * $Id$
36 *
37 */
38
39
40#include "Switch.hh"
41#include "PerfectSwitch.hh"
42#include "MessageBuffer.hh"
43#include "Throttle.hh"
44#include "MessageSizeType.hh"
45#include "Network.hh"
46#include "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    if(FINITE_BUFFERING) {
86      buffer_ptr->setSize(FINITE_BUFFER_SIZE);
87    }
88    intermediateBuffers.insertAtBottom(buffer_ptr);
89    m_buffers_to_free.insertAtBottom(buffer_ptr);
90  }
91
92  // Hook the queues to the PerfectSwitch
93  m_perfect_switch_ptr->addOutPort(intermediateBuffers, routing_table_entry);
94
95  // Hook the queues to the Throttle
96  throttle_ptr->addLinks(intermediateBuffers, out);
97
98}
99
100void Switch::clearRoutingTables()
101{
102  m_perfect_switch_ptr->clearRoutingTables();
103}
104
105void Switch::clearBuffers()
106{
107  m_perfect_switch_ptr->clearBuffers();
108  for (int i=0; i<m_throttles.size(); i++) {
109    if (m_throttles[i] != NULL) {
110      m_throttles[i]->clear();
111    }
112  }
113}
114
115void Switch::reconfigureOutPort(const NetDest& routing_table_entry)
116{
117  m_perfect_switch_ptr->reconfigureOutPort(routing_table_entry);
118}
119
120const Throttle* Switch::getThrottle(LinkID link_number) const
121{
122  assert(m_throttles[link_number] != NULL);
123  return m_throttles[link_number];
124}
125
126const Vector<Throttle*>* Switch::getThrottles() const
127{
128  return &m_throttles;
129}
130
131void Switch::printStats(ostream& out) const
132{
133  out << "switch_" << m_switch_id << "_inlinks: " << m_perfect_switch_ptr->getInLinks() << endl;
134  out << "switch_" << m_switch_id << "_outlinks: " << m_perfect_switch_ptr->getOutLinks() << endl;
135
136  // Average link utilizations
137  double average_utilization = 0.0;
138  int throttle_count = 0;
139
140  for (int i=0; i<m_throttles.size(); i++) {
141    Throttle* throttle_ptr = m_throttles[i];
142    if (throttle_ptr != NULL) {
143      average_utilization += throttle_ptr->getUtilization();
144      throttle_count++;
145    }
146  }
147  average_utilization = (throttle_count == 0) ? 0 : average_utilization / float(throttle_count);
148
149  // Individual link utilizations
150  out << "links_utilized_percent_switch_" << m_switch_id << ": " << average_utilization << endl;
151  for (int link=0; link<m_throttles.size(); link++) {
152    Throttle* throttle_ptr = m_throttles[link];
153    if (throttle_ptr != NULL) {
154      out << "  links_utilized_percent_switch_" << m_switch_id << "_link_" << link << ": "
155          << throttle_ptr->getUtilization() << " bw: " << throttle_ptr->getLinkBandwidth()
156          << " base_latency: " << throttle_ptr->getLatency() << endl;
157    }
158  }
159  out << endl;
160
161  // Traffic breakdown
162  for (int link=0; link<m_throttles.size(); link++) {
163    Throttle* throttle_ptr = m_throttles[link];
164    if (throttle_ptr != NULL) {
165      const Vector<Vector<int> >& message_counts = throttle_ptr->getCounters();
166      for (int int_type=0; int_type<MessageSizeType_NUM; int_type++) {
167        MessageSizeType type = MessageSizeType(int_type);
168        int sum = message_counts[type].sum();
169        if (sum != 0) {
170          out << "  outgoing_messages_switch_" << m_switch_id << "_link_" << link << "_" << type
171              << ": " << sum << " " << sum * MessageSizeType_to_int(type)
172              << " " << message_counts[type] << " base_latency: " << throttle_ptr->getLatency() << endl;
173        }
174      }
175    }
176  }
177  out << endl;
178}
179
180void Switch::clearStats()
181{
182  m_perfect_switch_ptr->clearStats();
183  for (int i=0; i<m_throttles.size(); i++) {
184    if (m_throttles[i] != NULL) {
185      m_throttles[i]->clearStats();
186    }
187  }
188}
189
190void Switch::printConfig(ostream& out) const
191{
192  m_perfect_switch_ptr->printConfig(out);
193  for (int i=0; i<m_throttles.size(); i++) {
194    if (m_throttles[i] != NULL) {
195      m_throttles[i]->printConfig(out);
196    }
197  }
198}
199
200void Switch::print(ostream& out) const
201{
202  // FIXME printing
203  out << "[Switch]";
204}
205
206