Throttle.cc revision 10919
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/network/simple/Throttle.hh"
35#include "mem/ruby/network/MessageBuffer.hh"
36#include "mem/ruby/network/Network.hh"
37#include "mem/ruby/slicc_interface/Message.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(Message* net_msg_ptr);
48
49Throttle::Throttle(int sID, RubySystem *rs, NodeID node, Cycles link_latency,
50                   int link_bandwidth_multiplier, int endpoint_bandwidth,
51                   ClockedObject *em)
52    : Consumer(em), m_ruby_system(rs)
53{
54    init(node, link_latency, link_bandwidth_multiplier, endpoint_bandwidth);
55    m_sID = sID;
56}
57
58Throttle::Throttle(RubySystem *rs, NodeID node, Cycles link_latency,
59                   int link_bandwidth_multiplier, int endpoint_bandwidth,
60                   ClockedObject *em)
61    : Consumer(em), m_ruby_system(rs)
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
77    m_link_latency = link_latency;
78    m_endpoint_bandwidth = endpoint_bandwidth;
79
80    m_wakeups_wo_switch = 0;
81    m_link_utilization_proxy = 0;
82}
83
84void
85Throttle::addLinks(const vector<MessageBuffer*>& in_vec,
86                   const vector<MessageBuffer*>& out_vec)
87{
88    assert(in_vec.size() == out_vec.size());
89
90    for (int vnet = 0; vnet < in_vec.size(); ++vnet) {
91        MessageBuffer *in_ptr = in_vec[vnet];
92        MessageBuffer *out_ptr = out_vec[vnet];
93
94        m_vnets++;
95        m_units_remaining.push_back(0);
96        m_in.push_back(in_ptr);
97        m_out.push_back(out_ptr);
98
99        // Set consumer and description
100        in_ptr->setConsumer(this);
101        string desc = "[Queue to Throttle " + to_string(m_sID) + " " +
102            to_string(m_node) + "]";
103        in_ptr->setDescription(desc);
104    }
105}
106
107void
108Throttle::operateVnet(int vnet, int &bw_remaining, bool &schedule_wakeup,
109                      MessageBuffer *in, MessageBuffer *out)
110{
111    if (out == nullptr || in == nullptr) {
112        return;
113    }
114    assert(m_units_remaining[vnet] >= 0);
115
116    while (bw_remaining > 0 && (in->isReady() || m_units_remaining[vnet] > 0) &&
117                                out->areNSlotsAvailable(1)) {
118
119        // See if we are done transferring the previous message on
120        // this virtual network
121        if (m_units_remaining[vnet] == 0 && in->isReady()) {
122            // Find the size of the message we are moving
123            MsgPtr msg_ptr = in->peekMsgPtr();
124            Message *net_msg_ptr = msg_ptr.get();
125            m_units_remaining[vnet] +=
126                network_message_to_size(net_msg_ptr);
127
128            DPRINTF(RubyNetwork, "throttle: %d my bw %d bw spent "
129                    "enqueueing net msg %d time: %lld.\n",
130                    m_node, getLinkBandwidth(), m_units_remaining[vnet],
131                    m_ruby_system->curCycle());
132
133            // Move the message
134            in->dequeue();
135            out->enqueue(msg_ptr, m_link_latency);
136
137            // Count the message
138            m_msg_counts[net_msg_ptr->getMessageSize()][vnet]++;
139            DPRINTF(RubyNetwork, "%s\n", *out);
140        }
141
142        // Calculate the amount of bandwidth we spent on this message
143        int diff = m_units_remaining[vnet] - bw_remaining;
144        m_units_remaining[vnet] = max(0, diff);
145        bw_remaining = max(0, -diff);
146    }
147
148    if (bw_remaining > 0 && (in->isReady() || m_units_remaining[vnet] > 0) &&
149                             !out->areNSlotsAvailable(1)) {
150        DPRINTF(RubyNetwork, "vnet: %d", vnet);
151
152        // schedule me to wakeup again because I'm waiting for my
153        // output queue to become available
154        schedule_wakeup = true;
155    }
156}
157
158void
159Throttle::wakeup()
160{
161    // Limits the number of message sent to a limited number of bytes/cycle.
162    assert(getLinkBandwidth() > 0);
163    int bw_remaining = getLinkBandwidth();
164
165    m_wakeups_wo_switch++;
166    bool schedule_wakeup = false;
167
168    // variable for deciding the direction in which to iterate
169    bool iteration_direction = false;
170
171
172    // invert priorities to avoid starvation seen in the component network
173    if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) {
174        m_wakeups_wo_switch = 0;
175        iteration_direction = true;
176    }
177
178    if (iteration_direction) {
179        for (int vnet = 0; vnet < m_vnets; ++vnet) {
180            operateVnet(vnet, bw_remaining, schedule_wakeup,
181                        m_in[vnet], m_out[vnet]);
182        }
183    } else {
184        for (int vnet = m_vnets-1; vnet >= 0; --vnet) {
185            operateVnet(vnet, bw_remaining, schedule_wakeup,
186                        m_in[vnet], m_out[vnet]);
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
198    m_link_utilization_proxy += ratio;
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
215Throttle::regStats(string parent)
216{
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(Network::getNumberOfVirtualNetworks())
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    }
237}
238
239void
240Throttle::clearStats()
241{
242    m_link_utilization_proxy = 0;
243}
244
245void
246Throttle::collateStats()
247{
248    double time_delta = double(m_ruby_system->curCycle() -
249                               m_ruby_system->getStartCycle());
250
251    m_link_utilization = 100.0 * m_link_utilization_proxy / time_delta;
252}
253
254void
255Throttle::print(ostream& out) const
256{
257    ccprintf(out,  "[%i bw: %i]", m_node, getLinkBandwidth());
258}
259
260int
261network_message_to_size(Message *net_msg_ptr)
262{
263    assert(net_msg_ptr != NULL);
264
265    int size = Network::MessageSizeType_to_int(net_msg_ptr->getMessageSize());
266    size *=  MESSAGE_SIZE_MULTIPLIER;
267
268    // Artificially increase the size of broadcast messages
269    if (BROADCAST_SCALING > 1 && net_msg_ptr->getDestination().isBroadcast())
270        size *= BROADCAST_SCALING;
271
272    return size;
273}
274