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