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