Throttle.cc revision 9465:4ae4f3f4b870
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, int 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, int 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, int link_latency, int link_bandwidth_multiplier,
71               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
85void
86Throttle::clear()
87{
88    for (int counter = 0; counter < m_vnets; counter++) {
89        m_in[counter]->clear();
90        m_out[counter]->clear();
91    }
92}
93
94void
95Throttle::addLinks(const std::vector<MessageBuffer*>& in_vec,
96    const std::vector<MessageBuffer*>& out_vec, ClockedObject *em)
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], em);
101    }
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                            ClockedObject *em)
115{
116    m_units_remaining.push_back(0);
117    m_in.push_back(in_ptr);
118    m_out.push_back(out_ptr);
119
120    // Set consumer and description
121    m_in[m_vnets]->setConsumer(this);
122    m_in[m_vnets]->setClockObj(em);
123
124    string desc = "[Queue to Throttle " + to_string(m_sID) + " " +
125        to_string(m_node) + "]";
126    m_in[m_vnets]->setDescription(desc);
127    m_vnets++;
128}
129
130void
131Throttle::wakeup()
132{
133    // Limits the number of message sent to a limited number of bytes/cycle.
134    assert(getLinkBandwidth() > 0);
135    int bw_remaining = getLinkBandwidth();
136
137    // Give the highest numbered link priority most of the time
138    m_wakeups_wo_switch++;
139    int highest_prio_vnet = m_vnets-1;
140    int lowest_prio_vnet = 0;
141    int counter = 1;
142    bool schedule_wakeup = false;
143
144    // invert priorities to avoid starvation seen in the component network
145    if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) {
146        m_wakeups_wo_switch = 0;
147        highest_prio_vnet = 0;
148        lowest_prio_vnet = m_vnets-1;
149        counter = -1;
150    }
151
152    for (int vnet = highest_prio_vnet;
153         (vnet * counter) >= (counter * lowest_prio_vnet);
154         vnet -= counter) {
155
156        assert(m_out[vnet] != NULL);
157        assert(m_in[vnet] != NULL);
158        assert(m_units_remaining[vnet] >= 0);
159
160        while (bw_remaining > 0 &&
161            (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) &&
162            m_out[vnet]->areNSlotsAvailable(1)) {
163
164            // See if we are done transferring the previous message on
165            // this virtual network
166            if (m_units_remaining[vnet] == 0 && m_in[vnet]->isReady()) {
167                // Find the size of the message we are moving
168                MsgPtr msg_ptr = m_in[vnet]->peekMsgPtr();
169                NetworkMessage* net_msg_ptr =
170                    safe_cast<NetworkMessage*>(msg_ptr.get());
171                m_units_remaining[vnet] +=
172                    network_message_to_size(net_msg_ptr);
173
174                DPRINTF(RubyNetwork, "throttle: %d my bw %d bw spent "
175                        "enqueueing net msg %d time: %lld.\n",
176                        m_node, getLinkBandwidth(), m_units_remaining[vnet],
177                        g_system_ptr->getTime());
178
179                // Move the message
180                m_out[vnet]->enqueue(m_in[vnet]->peekMsgPtr(), m_link_latency);
181                m_in[vnet]->pop();
182
183                // Count the message
184                m_message_counters[net_msg_ptr->getMessageSize()][vnet]++;
185
186                DPRINTF(RubyNetwork, "%s\n", *m_out[vnet]);
187            }
188
189            // Calculate the amount of bandwidth we spent on this message
190            int diff = m_units_remaining[vnet] - bw_remaining;
191            m_units_remaining[vnet] = max(0, diff);
192            bw_remaining = max(0, -diff);
193        }
194
195        if (bw_remaining > 0 &&
196            (m_in[vnet]->isReady() || m_units_remaining[vnet] > 0) &&
197            !m_out[vnet]->areNSlotsAvailable(1)) {
198            DPRINTF(RubyNetwork, "vnet: %d", vnet);
199            // schedule me to wakeup again because I'm waiting for my
200            // output queue to become available
201            schedule_wakeup = true;
202        }
203    }
204
205    // We should only wake up when we use the bandwidth
206    // This is only mostly true
207    // assert(bw_remaining != getLinkBandwidth());
208
209    // Record that we used some or all of the link bandwidth this cycle
210    double ratio = 1.0 - (double(bw_remaining) / double(getLinkBandwidth()));
211
212    // If ratio = 0, we used no bandwidth, if ratio = 1, we used all
213    linkUtilized(ratio);
214
215    if (bw_remaining > 0 && !schedule_wakeup) {
216        // We have extra bandwidth and our output buffer was
217        // available, so we must not have anything else to do until
218        // another message arrives.
219        DPRINTF(RubyNetwork, "%s not scheduled again\n", *this);
220    } else {
221        DPRINTF(RubyNetwork, "%s scheduled again\n", *this);
222
223        // We are out of bandwidth for this cycle, so wakeup next
224        // cycle and continue
225        scheduleEvent(1);
226    }
227}
228
229void
230Throttle::printStats(ostream& out) const
231{
232    out << "utilized_percent: " << getUtilization() << endl;
233}
234
235void
236Throttle::clearStats()
237{
238    m_ruby_start = g_system_ptr->getTime();
239    m_links_utilized = 0.0;
240
241    for (int i = 0; i < m_message_counters.size(); i++) {
242        for (int j = 0; j < m_message_counters[i].size(); j++) {
243            m_message_counters[i][j] = 0;
244        }
245    }
246}
247
248double
249Throttle::getUtilization() const
250{
251    return 100.0 * double(m_links_utilized) /
252        double(g_system_ptr->getTime()-m_ruby_start);
253}
254
255void
256Throttle::print(ostream& out) const
257{
258    ccprintf(out,  "[%i bw: %i]", m_node, getLinkBandwidth());
259}
260
261int
262network_message_to_size(NetworkMessage* net_msg_ptr)
263{
264    assert(net_msg_ptr != NULL);
265
266    int size = Network::MessageSizeType_to_int(net_msg_ptr->getMessageSize());
267    size *=  MESSAGE_SIZE_MULTIPLIER;
268
269    // Artificially increase the size of broadcast messages
270    if (BROADCAST_SCALING > 1 && net_msg_ptr->getDestination().isBroadcast())
271        size *= BROADCAST_SCALING;
272
273    return size;
274}
275