AbstractController.cc revision 10005:8c2b0dc16ccd
1/*
2 * Copyright (c) 2011 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 "mem/ruby/slicc_interface/AbstractController.hh"
30#include "mem/ruby/system/Sequencer.hh"
31#include "mem/ruby/system/System.hh"
32
33AbstractController::AbstractController(const Params *p)
34    : ClockedObject(p), Consumer(this), m_fully_busy_cycles(0),
35    m_request_count(0)
36{
37    m_version = p->version;
38    m_clusterID = p->cluster_id;
39
40    m_transitions_per_cycle = p->transitions_per_cycle;
41    m_buffer_size = p->buffer_size;
42    m_recycle_latency = p->recycle_latency;
43    m_number_of_TBEs = p->number_of_TBEs;
44    m_is_blocking = false;
45
46    if (m_version == 0) {
47        // Combine the statistics from all controllers
48        // of this particular type.
49        Stats::registerDumpCallback(new StatsCallback(this));
50    }
51}
52
53void
54AbstractController::init()
55{
56    params()->ruby_system->registerAbstractController(this);
57}
58
59void
60AbstractController::clearStats()
61{
62    m_requestProfileMap.clear();
63    m_request_count = 0;
64
65    m_delayHistogram.clear();
66
67    uint32_t size = Network::getNumberOfVirtualNetworks();
68    m_delayVCHistogram.resize(size);
69    for (uint32_t i = 0; i < size; i++) {
70        m_delayVCHistogram[i].clear();
71    }
72
73    Sequencer *seq = getSequencer();
74    if (seq != NULL) {
75        seq->clearStats();
76    }
77}
78
79void
80AbstractController::profileRequest(const std::string &request)
81{
82    m_request_count++;
83
84    // if it doesn't exist, conveniently, it will be created with the
85    // default value which is 0
86    m_requestProfileMap[request]++;
87}
88
89void
90AbstractController::profileMsgDelay(uint32_t virtualNetwork, Cycles delay)
91{
92    assert(virtualNetwork < m_delayVCHistogram.size());
93    m_delayHistogram.add(delay);
94    m_delayVCHistogram[virtualNetwork].add(delay);
95}
96
97void
98AbstractController::connectWithPeer(AbstractController *c)
99{
100    getQueuesFromPeer(c);
101    c->getQueuesFromPeer(this);
102}
103
104void
105AbstractController::stallBuffer(MessageBuffer* buf, Address addr)
106{
107    if (m_waiting_buffers.count(addr) == 0) {
108        MsgVecType* msgVec = new MsgVecType;
109        msgVec->resize(m_in_ports, NULL);
110        m_waiting_buffers[addr] = msgVec;
111    }
112    (*(m_waiting_buffers[addr]))[m_cur_in_port] = buf;
113}
114
115void
116AbstractController::wakeUpBuffers(Address addr)
117{
118    if (m_waiting_buffers.count(addr) > 0) {
119        //
120        // Wake up all possible lower rank (i.e. lower priority) buffers that could
121        // be waiting on this message.
122        //
123        for (int in_port_rank = m_cur_in_port - 1;
124             in_port_rank >= 0;
125             in_port_rank--) {
126            if ((*(m_waiting_buffers[addr]))[in_port_rank] != NULL) {
127                (*(m_waiting_buffers[addr]))[in_port_rank]->reanalyzeMessages(addr);
128            }
129        }
130        delete m_waiting_buffers[addr];
131        m_waiting_buffers.erase(addr);
132    }
133}
134
135void
136AbstractController::wakeUpAllBuffers(Address addr)
137{
138    if (m_waiting_buffers.count(addr) > 0) {
139        //
140        // Wake up all possible lower rank (i.e. lower priority) buffers that could
141        // be waiting on this message.
142        //
143        for (int in_port_rank = m_in_ports - 1;
144             in_port_rank >= 0;
145             in_port_rank--) {
146            if ((*(m_waiting_buffers[addr]))[in_port_rank] != NULL) {
147                (*(m_waiting_buffers[addr]))[in_port_rank]->reanalyzeMessages(addr);
148            }
149        }
150        delete m_waiting_buffers[addr];
151        m_waiting_buffers.erase(addr);
152    }
153}
154
155void
156AbstractController::wakeUpAllBuffers()
157{
158    //
159    // Wake up all possible buffers that could be waiting on any message.
160    //
161
162    std::vector<MsgVecType*> wokeUpMsgVecs;
163
164    if(m_waiting_buffers.size() > 0) {
165        for (WaitingBufType::iterator buf_iter = m_waiting_buffers.begin();
166             buf_iter != m_waiting_buffers.end();
167             ++buf_iter) {
168             for (MsgVecType::iterator vec_iter = buf_iter->second->begin();
169                  vec_iter != buf_iter->second->end();
170                  ++vec_iter) {
171                  if (*vec_iter != NULL) {
172                      (*vec_iter)->reanalyzeAllMessages();
173                  }
174             }
175             wokeUpMsgVecs.push_back(buf_iter->second);
176        }
177
178        for (std::vector<MsgVecType*>::iterator wb_iter = wokeUpMsgVecs.begin();
179             wb_iter != wokeUpMsgVecs.end();
180             ++wb_iter) {
181             delete (*wb_iter);
182        }
183
184        m_waiting_buffers.clear();
185    }
186}
187
188void
189AbstractController::blockOnQueue(Address addr, MessageBuffer* port)
190{
191    m_is_blocking = true;
192    m_block_map[addr] = port;
193}
194
195void
196AbstractController::unblock(Address addr)
197{
198    m_block_map.erase(addr);
199    if (m_block_map.size() == 0) {
200       m_is_blocking = false;
201    }
202}
203