gem5  v22.1.0.0
mem_sink.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include "mem/qos/mem_sink.hh"
39 
40 #include "base/logging.hh"
41 #include "base/trace.hh"
42 #include "debug/Drain.hh"
43 #include "debug/QOS.hh"
44 #include "mem/qos/q_policy.hh"
45 #include "params/QoSMemSinkInterface.hh"
46 
47 namespace gem5
48 {
49 
50 namespace memory
51 {
52 
54 namespace qos
55 {
56 
57 MemSinkCtrl::MemSinkCtrl(const QoSMemSinkCtrlParams &p)
58  : MemCtrl(p), requestLatency(p.request_latency),
59  responseLatency(p.response_latency),
60  memoryPacketSize(p.memory_packet_size),
61  readBufferSize(p.read_buffer_size),
62  writeBufferSize(p.write_buffer_size), port(name() + ".port", *this),
63  interface(p.interface),
64  retryRdReq(false), retryWrReq(false), nextRequest(0), nextReqEvent(this),
65  stats(this)
66 {
67  // Resize read and write queue to allocate space
68  // for configured QoS priorities
69  readQueue.resize(numPriorities());
70  writeQueue.resize(numPriorities());
71 
72  interface->setMemCtrl(this);
73 }
74 
76 {}
77 
78 void
80 {
81  MemCtrl::init();
82 
83  // Allow unconnected memories as this is used in several ruby
84  // systems at the moment
85  if (port.isConnected()) {
87  }
88 }
89 
90 bool
91 MemSinkCtrl::readQueueFull(const uint64_t packets) const
92 {
93  return (totalReadQueueSize + packets > readBufferSize);
94 }
95 
96 bool
97 MemSinkCtrl::writeQueueFull(const uint64_t packets) const
98 {
99  return (totalWriteQueueSize + packets > writeBufferSize);
100 }
101 
102 Tick
104 {
105  panic_if(pkt->cacheResponding(),
106  "%s Should not see packets where cache is responding\n",
107  __func__);
108 
109  interface->access(pkt);
110  return responseLatency;
111 }
112 
113 void
115 {
116  pkt->pushLabel(name());
117 
119 
120  pkt->popLabel();
121 }
122 
123 Port &
124 MemSinkCtrl::getPort(const std::string &interface, PortID idx)
125 {
126  if (interface != "port") {
127  return MemCtrl::getPort(interface, idx);
128  } else {
129  return port;
130  }
131 }
132 
133 bool
135 {
136  // Request accepted
137  bool req_accepted = true;
138 
139  panic_if(!(pkt->isRead() || pkt->isWrite()),
140  "%s. Should only see "
141  "read and writes at memory controller\n",
142  __func__);
143 
144  panic_if(pkt->cacheResponding(),
145  "%s. Should not see packets where cache is responding\n",
146  __func__);
147 
148  DPRINTF(QOS,
149  "%s: REQUESTOR %s request %s addr %lld size %d\n",
150  __func__,
151  _system->getRequestorName(pkt->req->requestorId()),
152  pkt->cmdString(), pkt->getAddr(), pkt->getSize());
153 
154  uint64_t required_entries = divCeil(pkt->getSize(), memoryPacketSize);
155 
156  assert(required_entries);
157 
158  // Schedule packet
159  uint8_t pkt_priority = qosSchedule({&readQueue, &writeQueue},
160  memoryPacketSize, pkt);
161 
162  if (pkt->isRead()) {
163  if (readQueueFull(required_entries)) {
164  DPRINTF(QOS,
165  "%s Read queue full, not accepting\n", __func__);
166  // Remember that we have to retry this port
167  retryRdReq = true;
169  req_accepted = false;
170  } else {
171  // Enqueue the incoming packet into corresponding
172  // QoS priority queue
173  readQueue.at(pkt_priority).push_back(pkt);
174  queuePolicy->enqueuePacket(pkt);
175  }
176  } else {
177  if (writeQueueFull(required_entries)) {
178  DPRINTF(QOS,
179  "%s Write queue full, not accepting\n", __func__);
180  // Remember that we have to retry this port
181  retryWrReq = true;
183  req_accepted = false;
184  } else {
185  // Enqueue the incoming packet into corresponding QoS
186  // priority queue
187  writeQueue.at(pkt_priority).push_back(pkt);
188  queuePolicy->enqueuePacket(pkt);
189  }
190  }
191 
192  if (req_accepted) {
193  // The packet is accepted - log it
194  logRequest(pkt->isRead()? READ : WRITE,
195  pkt->req->requestorId(),
196  pkt->qosValue(),
197  pkt->getAddr(),
198  required_entries);
199  }
200 
201  // Check if we have to process next request event
202  if (!nextReqEvent.scheduled()) {
203  DPRINTF(QOS,
204  "%s scheduling next request at "
205  "time %d (next is %d)\n", __func__,
206  std::max(curTick(), nextRequest), nextRequest);
207  schedule(nextReqEvent, std::max(curTick(), nextRequest));
208  }
209  return req_accepted;
210 }
211 
212 void
214 {
215  PacketPtr pkt = nullptr;
216 
217  // Evaluate bus direction
219 
220  // Record turnaround stats and update current state direction
222 
223  // Set current bus state
225 
226  // Access current direction buffer
227  std::vector<PacketQueue>* queue_ptr = (busState == READ ? &readQueue :
228  &writeQueue);
229 
230  DPRINTF(QOS,
231  "%s DUMPING %s queues status\n", __func__,
232  (busState == WRITE ? "WRITE" : "READ"));
233 
234  if (debug::QOS) {
235  for (uint8_t i = 0; i < numPriorities(); ++i) {
236  std::string plist = "";
237  for (auto& e : (busState == WRITE ? writeQueue[i]: readQueue[i])) {
238  plist += (std::to_string(e->req->requestorId())) + " ";
239  }
240  DPRINTF(QOS,
241  "%s priority Queue [%i] contains %i elements, "
242  "packets are: [%s]\n", __func__, i,
244  readQueueSizes[i],
245  plist);
246  }
247  }
248 
249  uint8_t curr_prio = numPriorities();
250 
251  for (auto queue = (*queue_ptr).rbegin();
252  queue != (*queue_ptr).rend(); ++queue) {
253 
254  curr_prio--;
255 
256  DPRINTF(QOS,
257  "%s checking %s queue [%d] priority [%d packets]\n",
258  __func__, (busState == READ? "READ" : "WRITE"),
259  curr_prio, queue->size());
260 
261  if (!queue->empty()) {
262  // Call the queue policy to select packet from priority queue
263  auto p_it = queuePolicy->selectPacket(&(*queue));
264  pkt = *p_it;
265  queue->erase(p_it);
266 
267  DPRINTF(QOS,
268  "%s scheduling packet address %d for requestor %s from "
269  "priority queue %d\n", __func__, pkt->getAddr(),
270  _system->getRequestorName(pkt->req->requestorId()),
271  curr_prio);
272  break;
273  }
274  }
275 
276  assert(pkt);
277 
278  // Setup next request service time - do it here as retry request
279  // hands over control to the port
281 
282  uint64_t removed_entries = divCeil(pkt->getSize(), memoryPacketSize);
283 
284  DPRINTF(QOS,
285  "%s scheduled packet address %d for requestor %s size is %d, "
286  "corresponds to %d memory packets\n", __func__, pkt->getAddr(),
287  _system->getRequestorName(pkt->req->requestorId()),
288  pkt->getSize(), removed_entries);
289 
290  // Schedule response
291  panic_if(!pkt->needsResponse(),
292  "%s response not required\n", __func__);
293 
294  // Do the actual memory access which also turns the packet
295  // into a response
296  interface->access(pkt);
297 
298  // Log the response
299  logResponse(pkt->isRead()? READ : WRITE,
300  pkt->req->requestorId(),
301  pkt->qosValue(),
302  pkt->getAddr(),
303  removed_entries, responseLatency);
304 
305  // Schedule the response
307  DPRINTF(QOS,
308  "%s response scheduled at time %d\n",
309  __func__, curTick() + responseLatency);
310 
311  // Finally - handle retry requests - this handles control
312  // to the port, so do it last
313  if (busState == READ && retryRdReq) {
314  retryRdReq = false;
315  port.sendRetryReq();
316  } else if (busState == WRITE && retryWrReq) {
317  retryWrReq = false;
318  port.sendRetryReq();
319  }
320 
321  // Check if we have to schedule another request event
323  !nextReqEvent.scheduled()) {
324 
326  DPRINTF(QOS,
327  "%s scheduling next request event at tick %d\n",
328  __func__, curTick() + requestLatency);
329  }
330 }
331 
334 {
336  DPRINTF(Drain,
337  "%s queues have requests, waiting to drain\n",
338  __func__);
339  return DrainState::Draining;
340  } else {
341  return DrainState::Drained;
342  }
343 }
344 
346  : statistics::Group(parent),
347  ADD_STAT(numReadRetries, statistics::units::Count::get(),
348  "Number of read retries"),
349  ADD_STAT(numWriteRetries, statistics::units::Count::get(),
350  "Number of write retries")
351 {
352 }
353 
355  MemSinkCtrl& m)
356  : QueuedResponsePort(n, &m, queue, true),
357  mem(m), queue(mem, *this, true)
358 {}
359 
362 {
363  AddrRangeList ranges;
364  ranges.push_back(mem.interface->getAddrRange());
365  return ranges;
366 }
367 
368 Tick
370 {
371  return mem.recvAtomic(pkt);
372 }
373 
374 void
376 {
377  pkt->pushLabel(mem.name());
378 
379  if (!queue.trySatisfyFunctional(pkt)) {
380  // Default implementation of SimpleTimingPort::recvFunctional()
381  // calls recvAtomic() and throws away the latency; we can save a
382  // little here by just not calculating the latency.
383  mem.recvFunctional(pkt);
384  }
385 
386  pkt->popLabel();
387 }
388 
389 bool
391 {
392  return mem.recvTimingReq(pkt);
393 }
394 
395 MemSinkInterface::MemSinkInterface(const QoSMemSinkInterfaceParams &_p)
396  : AbstractMemory(_p)
397 {
398 }
399 
400 } // namespace qos
401 } // namespace memory
402 } // namespace gem5
#define DPRINTF(x,...)
Definition: trace.hh:186
virtual std::string name() const
Definition: named.hh:47
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:294
bool isRead() const
Definition: packet.hh:592
Addr getAddr() const
Definition: packet.hh:805
void pushLabel(const std::string &lbl)
Push label for PrintReq (safe to call unconditionally).
Definition: packet.hh:1448
bool needsResponse() const
Definition: packet.hh:607
uint8_t qosValue() const
QoS Value getter Returns 0 if QoS value was never set (constructor default).
Definition: packet.hh:767
bool isWrite() const
Definition: packet.hh:593
const std::string & cmdString() const
Return the string name of the cmd field (for debugging and tracing).
Definition: packet.hh:587
RequestPtr req
A pointer to the original request.
Definition: packet.hh:376
unsigned getSize() const
Definition: packet.hh:815
void popLabel()
Pop label for PrintReq (safe to call unconditionally).
Definition: packet.hh:1458
bool cacheResponding() const
Definition: packet.hh:657
Ports are used to interface objects to each other.
Definition: port.hh:62
bool isConnected() const
Is this port currently connected to a peer?
Definition: port.hh:133
A queued port is a port that has an infinite queue for outgoing packets and thus decouples the module...
Definition: qport.hh:62
void schedTimingResp(PacketPtr pkt, Tick when)
Schedule the sending of a timing response.
Definition: qport.hh:93
void sendRangeChange() const
Called by the owner to send a range change.
Definition: port.hh:296
void sendRetryReq()
Send a retry to the request port that previously attempted a sendTimingReq to this response port and ...
Definition: port.hh:401
std::string getRequestorName(RequestorID requestor_id)
Get the name of an object for a given request id.
Definition: system.cc:526
An abstract memory represents a contiguous block of physical memory, with an associated address range...
void access(PacketPtr pkt)
Perform an untimed memory access and update all the state (e.g.
void functionalAccess(PacketPtr pkt)
Perform an untimed memory read or write without changing anything but the memory itself.
The qos::MemCtrl is a base class for Memory objects which support QoS - it provides access to a set o...
Definition: mem_ctrl.hh:81
void logResponse(BusState dir, RequestorID id, uint8_t _qos, Addr addr, uint64_t entries, double delay)
Called upon receiving a response, updates statistics and updates queues status.
Definition: mem_ctrl.cc:149
std::vector< uint64_t > writeQueueSizes
Write request packets queue length in #packets, per QoS priority.
Definition: mem_ctrl.hh:128
uint8_t qosSchedule(std::initializer_list< Queues * > queues_ptr, uint64_t queue_entry_size, const PacketPtr pkt)
Assign priority to a packet by executing the configured QoS policy.
Definition: mem_ctrl.hh:496
uint64_t totalWriteQueueSize
Total write request packets queue length in #packets.
Definition: mem_ctrl.hh:134
void setCurrentBusState()
Set current bus direction (READ or WRITE) from next selected one.
Definition: mem_ctrl.hh:240
std::vector< uint64_t > readQueueSizes
Read request packets queue length in #packets, per QoS priority.
Definition: mem_ctrl.hh:125
const std::unique_ptr< QueuePolicy > queuePolicy
QoS Queue Policy: selects packet among same-priority queue.
Definition: mem_ctrl.hh:94
void recordTurnaroundStats()
Record statistics on turnarounds based on busStateNext and busState values.
Definition: mem_ctrl.cc:359
BusState selectNextBusState()
Returns next bus direction (READ or WRITE) based on configured policy.
Definition: mem_ctrl.cc:247
uint8_t numPriorities() const
Gets the total number of priority levels in the QoS memory controller.
Definition: mem_ctrl.hh:367
BusState busState
Bus state used to control the read/write switching and drive the scheduling of the next request.
Definition: mem_ctrl.hh:140
System * _system
Pointer to the System object.
Definition: mem_ctrl.hh:176
uint64_t totalReadQueueSize
Total read request packets queue length in #packets.
Definition: mem_ctrl.hh:131
BusState busStateNext
bus state for next request event triggered
Definition: mem_ctrl.hh:143
uint8_t schedule(RequestorID id, uint64_t data)
Definition: mem_ctrl.cc:218
void logRequest(BusState dir, RequestorID id, uint8_t _qos, Addr addr, uint64_t entries)
Called upon receiving a request or updates statistics and updates queues status.
Definition: mem_ctrl.cc:92
void recvFunctional(PacketPtr pkt)
Receive a Packet in Functional mode.
Definition: mem_sink.cc:375
MemoryPort(const std::string &, MemSinkCtrl &)
Constructor.
Definition: mem_sink.cc:354
AddrRangeList getAddrRanges() const
Gets the configured address ranges for this port.
Definition: mem_sink.cc:361
Tick recvAtomic(PacketPtr pkt)
Receive a Packet in Atomic mode.
Definition: mem_sink.cc:369
bool recvTimingReq(PacketPtr pkt)
Receive a Packet in Timing mode.
Definition: mem_sink.cc:390
MemoryPort port
Memory response port.
Definition: mem_sink.hh:182
Tick recvAtomic(PacketPtr pkt)
Receive a Packet in Atomic mode.
Definition: mem_sink.cc:103
const Tick responseLatency
Memory response latency (ticks)
Definition: mem_sink.hh:170
Port & getPort(const std::string &if_name, PortID=InvalidPortID) override
Getter method to access this memory's response port.
Definition: mem_sink.cc:124
const uint64_t writeBufferSize
Write request packets queue buffer size in #packets.
Definition: mem_sink.hh:179
DrainState drain() override
Checks and return the Drain state of this SimObject.
Definition: mem_sink.cc:333
bool readQueueFull(const uint64_t packets) const
Check if the read queue has room for more entries.
Definition: mem_sink.cc:91
bool recvTimingReq(PacketPtr pkt)
Receive a Packet in Timing mode.
Definition: mem_sink.cc:134
const uint64_t readBufferSize
Read request packets queue buffer size in #packets.
Definition: mem_sink.hh:176
bool retryWrReq
Write request pending.
Definition: mem_sink.hh:193
EventWrapper< MemSinkCtrl, &MemSinkCtrl::processNextReqEvent > nextReqEvent
Event wrapper to schedule next request handler function.
Definition: mem_sink.hh:228
std::vector< PacketQueue > readQueue
QoS-aware (per priority) incoming read requests packets queue.
Definition: mem_sink.hh:212
bool retryRdReq
Read request pending.
Definition: mem_sink.hh:190
void processNextReqEvent()
Processes the next Request event according to configured request latency.
Definition: mem_sink.cc:213
bool writeQueueFull(const uint64_t packets) const
Check if the write queue has room for more entries.
Definition: mem_sink.cc:97
const Tick requestLatency
Memory between requests latency (ticks)
Definition: mem_sink.hh:167
const uint64_t memoryPacketSize
Memory packet size in bytes.
Definition: mem_sink.hh:173
void init() override
Initializes this object.
Definition: mem_sink.cc:79
MemSinkCtrl(const QoSMemSinkCtrlParams &)
QoS Memory Sink Constructor.
Definition: mem_sink.cc:57
void recvFunctional(PacketPtr pkt)
Receive a Packet in Functional mode.
Definition: mem_sink.cc:114
Tick nextRequest
Next request service time.
Definition: mem_sink.hh:196
std::vector< PacketQueue > writeQueue
QoS-aware (per priority) incoming read requests packets queue.
Definition: mem_sink.hh:217
MemSinkInterface *const interface
Create pointer to interface of actual media.
Definition: mem_sink.hh:187
MemSinkInterface(const QoSMemSinkInterfaceParams &_p)
Definition: mem_sink.cc:395
void setMemCtrl(MemSinkCtrl *_ctrl)
Setting a pointer to the interface.
Definition: mem_sink.hh:276
Statistics container.
Definition: group.hh:94
STL vector class.
Definition: stl.hh:37
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:75
static constexpr T divCeil(const T &a, const U &b)
Definition: intmath.hh:110
DrainState
Object drain/handover states.
Definition: drain.hh:75
@ Draining
Draining buffers pending serialization/handover.
@ Drained
Buffers drained, ready for serialization/handover.
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:204
virtual Port & getPort(const std::string &if_name, PortID idx=InvalidPortID)
Get a port with a given name and index.
Definition: sim_object.cc:126
virtual void init()
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition: sim_object.cc:76
Bitfield< 31 > n
Definition: misc_types.hh:462
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 9 > e
Definition: misc_types.hh:65
Bitfield< 54 > p
Definition: pagetable.hh:70
GEM5_DEPRECATED_NAMESPACE(QoS, qos)
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:245
uint64_t Tick
Tick count type.
Definition: types.hh:58
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:60
statistics::Scalar numReadRetries
Count the number of read retries.
Definition: mem_sink.hh:203
MemSinkCtrlStats(statistics::Group *parent)
Definition: mem_sink.cc:345
statistics::Scalar numWriteRetries
Count the number of write retries.
Definition: mem_sink.hh:206
Definition: mem.h:38
bool_vector8 mem[]
Definition: reset_stim.h:43
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:37 for gem5 by doxygen 1.9.1