Throttle.cc (9508:dde110931867) Throttle.cc (9863:9483739f83ee)
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
31#include "base/cast.hh"
32#include "base/cprintf.hh"
33#include "debug/RubyNetwork.hh"
34#include "mem/ruby/buffers/MessageBuffer.hh"
35#include "mem/ruby/network/simple/Throttle.hh"
36#include "mem/ruby/network/Network.hh"
37#include "mem/ruby/slicc_interface/NetworkMessage.hh"
38#include "mem/ruby/system/System.hh"
39
40using namespace std;
41
42const int HIGH_RANGE = 256;
43const int ADJUST_INTERVAL = 50000;
44const int MESSAGE_SIZE_MULTIPLIER = 1000;
45//const int BROADCAST_SCALING = 4; // Have a 16p system act like a 64p systems
46const int BROADCAST_SCALING = 1;
47const int PRIORITY_SWITCH_LIMIT = 128;
48
49static int network_message_to_size(NetworkMessage* net_msg_ptr);
50
51Throttle::Throttle(int sID, NodeID node, Cycles link_latency,
52 int link_bandwidth_multiplier, int endpoint_bandwidth,
53 ClockedObject *em)
54 : Consumer(em)
55{
56 init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth);
57 m_sID = sID;
58}
59
60Throttle::Throttle(NodeID node, Cycles link_latency,
61 int link_bandwidth_multiplier, int endpoint_bandwidth,
62 ClockedObject *em)
63 : Consumer(em)
64{
65 init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth);
66 m_sID = 0;
67}
68
69void
70Throttle::init(NodeID node, Cycles link_latency,
71 int link_bandwidth_multiplier, int endpoint_bandwidth)
72{
73 m_node = node;
74 m_vnets = 0;
75
76 assert(link_bandwidth_multiplier > 0);
77 m_link_bandwidth_multiplier = link_bandwidth_multiplier;
78 m_link_latency = link_latency;
79 m_endpoint_bandwidth = endpoint_bandwidth;
80
81 m_wakeups_wo_switch = 0;
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
31#include "base/cast.hh"
32#include "base/cprintf.hh"
33#include "debug/RubyNetwork.hh"
34#include "mem/ruby/buffers/MessageBuffer.hh"
35#include "mem/ruby/network/simple/Throttle.hh"
36#include "mem/ruby/network/Network.hh"
37#include "mem/ruby/slicc_interface/NetworkMessage.hh"
38#include "mem/ruby/system/System.hh"
39
40using namespace std;
41
42const int HIGH_RANGE = 256;
43const int ADJUST_INTERVAL = 50000;
44const int MESSAGE_SIZE_MULTIPLIER = 1000;
45//const int BROADCAST_SCALING = 4; // Have a 16p system act like a 64p systems
46const int BROADCAST_SCALING = 1;
47const int PRIORITY_SWITCH_LIMIT = 128;
48
49static int network_message_to_size(NetworkMessage* net_msg_ptr);
50
51Throttle::Throttle(int sID, NodeID node, Cycles link_latency,
52 int link_bandwidth_multiplier, int endpoint_bandwidth,
53 ClockedObject *em)
54 : Consumer(em)
55{
56 init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth);
57 m_sID = sID;
58}
59
60Throttle::Throttle(NodeID node, Cycles link_latency,
61 int link_bandwidth_multiplier, int endpoint_bandwidth,
62 ClockedObject *em)
63 : Consumer(em)
64{
65 init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth);
66 m_sID = 0;
67}
68
69void
70Throttle::init(NodeID node, Cycles link_latency,
71 int link_bandwidth_multiplier, int endpoint_bandwidth)
72{
73 m_node = node;
74 m_vnets = 0;
75
76 assert(link_bandwidth_multiplier > 0);
77 m_link_bandwidth_multiplier = link_bandwidth_multiplier;
78 m_link_latency = link_latency;
79 m_endpoint_bandwidth = endpoint_bandwidth;
80
81 m_wakeups_wo_switch = 0;
82 clearStats();
83}
84
82
85void
86Throttle::clear()
87{
88 for (int counter = 0; counter < m_vnets; counter++) {
89 m_in[counter]->clear();
90 m_out[counter]->clear();
91 }
83 m_msg_counts.resize(MessageSizeType_NUM);
84 m_msg_bytes.resize(MessageSizeType_NUM);
85
86 m_link_utilization_proxy = 0;
92}
93
94void
95Throttle::addLinks(const std::vector<MessageBuffer*>& in_vec,
96 const std::vector<MessageBuffer*>& out_vec)
97{
98 assert(in_vec.size() == out_vec.size());
99 for (int i=0; i<in_vec.size(); i++) {
100 addVirtualNetwork(in_vec[i], out_vec[i]);
101 }
87}
88
89void
90Throttle::addLinks(const std::vector<MessageBuffer*>& in_vec,
91 const std::vector<MessageBuffer*>& out_vec)
92{
93 assert(in_vec.size() == out_vec.size());
94 for (int i=0; i<in_vec.size(); i++) {
95 addVirtualNetwork(in_vec[i], out_vec[i]);
96 }
102
103 m_message_counters.resize(MessageSizeType_NUM);
104 for (int i = 0; i < MessageSizeType_NUM; i++) {
105 m_message_counters[i].resize(in_vec.size());
106 for (int j = 0; j<m_message_counters[i].size(); j++) {
107 m_message_counters[i][j] = 0;
108 }
109 }
110}
111
112void
113Throttle::addVirtualNetwork(MessageBuffer* in_ptr, MessageBuffer* out_ptr)
114{
115 m_units_remaining.push_back(0);
116 m_in.push_back(in_ptr);
117 m_out.push_back(out_ptr);
118
119 // Set consumer and description
120 m_in[m_vnets]->setConsumer(this);
121
122 string desc = "[Queue to Throttle " + to_string(m_sID) + " " +
123 to_string(m_node) + "]";
124 m_in[m_vnets]->setDescription(desc);
125 m_vnets++;
126}
127
128void
129Throttle::wakeup()
130{
131 // Limits the number of message sent to a limited number of bytes/cycle.
132 assert(getLinkBandwidth() > 0);
133 int bw_remaining = getLinkBandwidth();
134
135 // Give the highest numbered link priority most of the time
136 m_wakeups_wo_switch++;
137 int highest_prio_vnet = m_vnets-1;
138 int lowest_prio_vnet = 0;
139 int counter = 1;
140 bool schedule_wakeup = false;
141
142 // invert priorities to avoid starvation seen in the component network
143 if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) {
144 m_wakeups_wo_switch = 0;
145 highest_prio_vnet = 0;
146 lowest_prio_vnet = m_vnets-1;
147 counter = -1;
148 }
149
150 for (int vnet = highest_prio_vnet;
151 (vnet * counter) >= (counter * lowest_prio_vnet);
152 vnet -= counter) {
153
154 assert(m_out[vnet] != NULL);
155 assert(m_in[vnet] != NULL);
156 assert(m_units_remaining[vnet] >= 0);
157
158 while (bw_remaining > 0 &&
159 (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) &&
160 m_out[vnet]->areNSlotsAvailable(1)) {
161
162 // See if we are done transferring the previous message on
163 // this virtual network
164 if (m_units_remaining[vnet] == 0 && m_in[vnet]->isReady()) {
165 // Find the size of the message we are moving
166 MsgPtr msg_ptr = m_in[vnet]->peekMsgPtr();
167 NetworkMessage* net_msg_ptr =
168 safe_cast<NetworkMessage*>(msg_ptr.get());
169 m_units_remaining[vnet] +=
170 network_message_to_size(net_msg_ptr);
171
172 DPRINTF(RubyNetwork, "throttle: %d my bw %d bw spent "
173 "enqueueing net msg %d time: %lld.\n",
174 m_node, getLinkBandwidth(), m_units_remaining[vnet],
175 g_system_ptr->curCycle());
176
177 // Move the message
178 m_out[vnet]->enqueue(m_in[vnet]->peekMsgPtr(), m_link_latency);
179 m_in[vnet]->pop();
180
181 // Count the message
97}
98
99void
100Throttle::addVirtualNetwork(MessageBuffer* in_ptr, MessageBuffer* out_ptr)
101{
102 m_units_remaining.push_back(0);
103 m_in.push_back(in_ptr);
104 m_out.push_back(out_ptr);
105
106 // Set consumer and description
107 m_in[m_vnets]->setConsumer(this);
108
109 string desc = "[Queue to Throttle " + to_string(m_sID) + " " +
110 to_string(m_node) + "]";
111 m_in[m_vnets]->setDescription(desc);
112 m_vnets++;
113}
114
115void
116Throttle::wakeup()
117{
118 // Limits the number of message sent to a limited number of bytes/cycle.
119 assert(getLinkBandwidth() > 0);
120 int bw_remaining = getLinkBandwidth();
121
122 // Give the highest numbered link priority most of the time
123 m_wakeups_wo_switch++;
124 int highest_prio_vnet = m_vnets-1;
125 int lowest_prio_vnet = 0;
126 int counter = 1;
127 bool schedule_wakeup = false;
128
129 // invert priorities to avoid starvation seen in the component network
130 if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) {
131 m_wakeups_wo_switch = 0;
132 highest_prio_vnet = 0;
133 lowest_prio_vnet = m_vnets-1;
134 counter = -1;
135 }
136
137 for (int vnet = highest_prio_vnet;
138 (vnet * counter) >= (counter * lowest_prio_vnet);
139 vnet -= counter) {
140
141 assert(m_out[vnet] != NULL);
142 assert(m_in[vnet] != NULL);
143 assert(m_units_remaining[vnet] >= 0);
144
145 while (bw_remaining > 0 &&
146 (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) &&
147 m_out[vnet]->areNSlotsAvailable(1)) {
148
149 // See if we are done transferring the previous message on
150 // this virtual network
151 if (m_units_remaining[vnet] == 0 && m_in[vnet]->isReady()) {
152 // Find the size of the message we are moving
153 MsgPtr msg_ptr = m_in[vnet]->peekMsgPtr();
154 NetworkMessage* net_msg_ptr =
155 safe_cast<NetworkMessage*>(msg_ptr.get());
156 m_units_remaining[vnet] +=
157 network_message_to_size(net_msg_ptr);
158
159 DPRINTF(RubyNetwork, "throttle: %d my bw %d bw spent "
160 "enqueueing net msg %d time: %lld.\n",
161 m_node, getLinkBandwidth(), m_units_remaining[vnet],
162 g_system_ptr->curCycle());
163
164 // Move the message
165 m_out[vnet]->enqueue(m_in[vnet]->peekMsgPtr(), m_link_latency);
166 m_in[vnet]->pop();
167
168 // Count the message
182 m_message_counters[net_msg_ptr->getMessageSize()][vnet]++;
169 m_msg_counts[net_msg_ptr->getMessageSize()][vnet]++;
183
184 DPRINTF(RubyNetwork, "%s\n", *m_out[vnet]);
185 }
186
187 // Calculate the amount of bandwidth we spent on this message
188 int diff = m_units_remaining[vnet] - bw_remaining;
189 m_units_remaining[vnet] = max(0, diff);
190 bw_remaining = max(0, -diff);
191 }
192
193 if (bw_remaining > 0 &&
194 (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) &&
195 !m_out[vnet]->areNSlotsAvailable(1)) {
196 DPRINTF(RubyNetwork, "vnet: %d", vnet);
197 // schedule me to wakeup again because I'm waiting for my
198 // output queue to become available
199 schedule_wakeup = true;
200 }
201 }
202
203 // We should only wake up when we use the bandwidth
204 // This is only mostly true
205 // assert(bw_remaining != getLinkBandwidth());
206
207 // Record that we used some or all of the link bandwidth this cycle
208 double ratio = 1.0 - (double(bw_remaining) / double(getLinkBandwidth()));
209
210 // If ratio = 0, we used no bandwidth, if ratio = 1, we used all
170
171 DPRINTF(RubyNetwork, "%s\n", *m_out[vnet]);
172 }
173
174 // Calculate the amount of bandwidth we spent on this message
175 int diff = m_units_remaining[vnet] - bw_remaining;
176 m_units_remaining[vnet] = max(0, diff);
177 bw_remaining = max(0, -diff);
178 }
179
180 if (bw_remaining > 0 &&
181 (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) &&
182 !m_out[vnet]->areNSlotsAvailable(1)) {
183 DPRINTF(RubyNetwork, "vnet: %d", vnet);
184 // schedule me to wakeup again because I'm waiting for my
185 // output queue to become available
186 schedule_wakeup = true;
187 }
188 }
189
190 // We should only wake up when we use the bandwidth
191 // This is only mostly true
192 // assert(bw_remaining != getLinkBandwidth());
193
194 // Record that we used some or all of the link bandwidth this cycle
195 double ratio = 1.0 - (double(bw_remaining) / double(getLinkBandwidth()));
196
197 // If ratio = 0, we used no bandwidth, if ratio = 1, we used all
211 linkUtilized(ratio);
198 m_link_utilization_proxy += ratio;
212
213 if (bw_remaining > 0 && !schedule_wakeup) {
214 // We have extra bandwidth and our output buffer was
215 // available, so we must not have anything else to do until
216 // another message arrives.
217 DPRINTF(RubyNetwork, "%s not scheduled again\n", *this);
218 } else {
219 DPRINTF(RubyNetwork, "%s scheduled again\n", *this);
220
221 // We are out of bandwidth for this cycle, so wakeup next
222 // cycle and continue
223 scheduleEvent(Cycles(1));
224 }
225}
226
227void
199
200 if (bw_remaining > 0 && !schedule_wakeup) {
201 // We have extra bandwidth and our output buffer was
202 // available, so we must not have anything else to do until
203 // another message arrives.
204 DPRINTF(RubyNetwork, "%s not scheduled again\n", *this);
205 } else {
206 DPRINTF(RubyNetwork, "%s scheduled again\n", *this);
207
208 // We are out of bandwidth for this cycle, so wakeup next
209 // cycle and continue
210 scheduleEvent(Cycles(1));
211 }
212}
213
214void
228Throttle::printStats(ostream& out) const
215Throttle::regStats(string parent)
229{
216{
230 out << "utilized_percent: " << getUtilization() << endl;
217 m_link_utilization
218 .name(parent + csprintf(".throttle%i", m_node) + ".link_utilization");
219
220 for (MessageSizeType type = MessageSizeType_FIRST;
221 type < MessageSizeType_NUM; ++type) {
222 m_msg_counts[(unsigned int)type]
223 .init(m_vnets)
224 .name(parent + csprintf(".throttle%i", m_node) + ".msg_count." +
225 MessageSizeType_to_string(type))
226 .flags(Stats::nozero)
227 ;
228 m_msg_bytes[(unsigned int) type]
229 .name(parent + csprintf(".throttle%i", m_node) + ".msg_bytes." +
230 MessageSizeType_to_string(type))
231 .flags(Stats::nozero)
232 ;
233
234 m_msg_bytes[(unsigned int) type] = m_msg_counts[type] * Stats::constant(
235 Network::MessageSizeType_to_int(type));
236 }
231}
232
233void
234Throttle::clearStats()
235{
237}
238
239void
240Throttle::clearStats()
241{
236 m_ruby_start = g_system_ptr->curCycle();
237 m_links_utilized = 0.0;
238
239 for (int i = 0; i < m_message_counters.size(); i++) {
240 for (int j = 0; j < m_message_counters[i].size(); j++) {
241 m_message_counters[i][j] = 0;
242 }
243 }
242 m_link_utilization_proxy = 0;
244}
245
243}
244
246double
247Throttle::getUtilization() const
245void
246Throttle::collateStats()
248{
247{
249 return 100.0 * double(m_links_utilized) /
250 double(g_system_ptr->curCycle()-m_ruby_start);
248 m_link_utilization = 100.0 * m_link_utilization_proxy
249 / (double(g_system_ptr->curCycle() - g_ruby_start));
251}
252
253void
254Throttle::print(ostream& out) const
255{
256 ccprintf(out, "[%i bw: %i]", m_node, getLinkBandwidth());
257}
258
259int
260network_message_to_size(NetworkMessage* net_msg_ptr)
261{
262 assert(net_msg_ptr != NULL);
263
264 int size = Network::MessageSizeType_to_int(net_msg_ptr->getMessageSize());
265 size *= MESSAGE_SIZE_MULTIPLIER;
266
267 // Artificially increase the size of broadcast messages
268 if (BROADCAST_SCALING > 1 && net_msg_ptr->getDestination().isBroadcast())
269 size *= BROADCAST_SCALING;
270
271 return size;
272}
250}
251
252void
253Throttle::print(ostream& out) const
254{
255 ccprintf(out, "[%i bw: %i]", m_node, getLinkBandwidth());
256}
257
258int
259network_message_to_size(NetworkMessage* net_msg_ptr)
260{
261 assert(net_msg_ptr != NULL);
262
263 int size = Network::MessageSizeType_to_int(net_msg_ptr->getMessageSize());
264 size *= MESSAGE_SIZE_MULTIPLIER;
265
266 // Artificially increase the size of broadcast messages
267 if (BROADCAST_SCALING > 1 && net_msg_ptr->getDestination().isBroadcast())
268 size *= BROADCAST_SCALING;
269
270 return size;
271}