gem5  v22.1.0.0
mem_ctrl.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 #ifndef __MEM_QOS_MEM_CTRL_HH__
39 #define __MEM_QOS_MEM_CTRL_HH__
40 
41 #include <cstdint>
42 #include <deque>
43 #include <memory>
44 #include <unordered_map>
45 #include <utility>
46 #include <vector>
47 
48 #include "base/compiler.hh"
49 #include "base/logging.hh"
50 #include "base/statistics.hh"
51 #include "base/trace.hh"
52 #include "base/types.hh"
53 #include "debug/QOS.hh"
54 #include "mem/packet.hh"
55 #include "mem/request.hh"
56 #include "params/QoSMemCtrl.hh"
57 #include "sim/clocked_object.hh"
58 #include "sim/cur_tick.hh"
59 #include "sim/system.hh"
60 
61 namespace gem5
62 {
63 
64 namespace memory
65 {
66 
68 namespace qos
69 {
70 
71 class Policy;
72 class QueuePolicy;
73 class TurnaroundPolicy;
74 
80 class MemCtrl : public ClockedObject
81 {
82  public:
84  enum BusState { READ, WRITE };
85 
86  protected:
88  const std::unique_ptr<Policy> policy;
89 
91  const std::unique_ptr<TurnaroundPolicy> turnPolicy;
92 
94  const std::unique_ptr<QueuePolicy> queuePolicy;
95 
97  const uint8_t _numPriorities;
98 
101 
106  const bool qosSyncroScheduler;
107 
109  std::unordered_map<RequestorID, const std::string> requestors;
110 
112  std::unordered_map<RequestorID, std::vector<uint64_t> > packetPriorities;
113 
115  std::unordered_map<RequestorID,
116  std::unordered_map<uint64_t, std::deque<uint64_t>> > requestTimes;
117 
123 
126 
129 
132 
135 
141 
144 
146  {
147  MemCtrlStats(MemCtrl &mc);
148 
149  void regStats() override;
150 
151  const MemCtrl &memCtrl;
152 
160 
173  } stats;
174 
177 
184  void addRequestor(const RequestorID id);
185 
196  void logRequest(BusState dir, RequestorID id, uint8_t _qos,
197  Addr addr, uint64_t entries);
198 
210  void logResponse(BusState dir, RequestorID id, uint8_t _qos,
211  Addr addr, uint64_t entries, double delay);
212 
222  template<typename Queues>
223  uint8_t qosSchedule(std::initializer_list<Queues*> queues_ptr,
224  uint64_t queue_entry_size, const PacketPtr pkt);
225 
226  using SimObject::schedule;
227  uint8_t schedule(RequestorID id, uint64_t data);
228  uint8_t schedule(const PacketPtr pkt);
229 
235 
241 
246  void recordTurnaroundStats();
247 
258  template<typename Queues>
259  void escalate(std::initializer_list<Queues*> queues,
260  uint64_t queue_entry_size,
261  RequestorID id, uint8_t tgt_prio);
262 
278  template<typename Queues>
279  void escalateQueues(Queues& queues, uint64_t queue_entry_size,
280  RequestorID id, uint8_t curr_prio, uint8_t tgt_prio);
281 
282  public:
288  MemCtrl(const QoSMemCtrlParams &);
289 
290  virtual ~MemCtrl();
291 
297  BusState getBusState() const { return busState; }
298 
305 
316  bool hasRequestor(RequestorID id) const
317  {
318  return requestors.find(id) != requestors.end();
319  }
320 
327  uint64_t getReadQueueSize(const uint8_t prio) const
328  { return readQueueSizes[prio]; }
329 
336  uint64_t getWriteQueueSize(const uint8_t prio) const
337  { return writeQueueSizes[prio]; }
338 
344  uint64_t getTotalReadQueueSize() const { return totalReadQueueSize; }
345 
351  uint64_t getTotalWriteQueueSize() const { return totalWriteQueueSize; }
352 
359  Tick getServiceTick(const uint8_t prio) const { return serviceTick[prio]; }
360 
367  uint8_t numPriorities() const { return _numPriorities; }
368 
371  System* system() const { return _system; }
372 };
373 
374 template<typename Queues>
375 void
376 MemCtrl::escalateQueues(Queues& queues, uint64_t queue_entry_size,
377  RequestorID id, uint8_t curr_prio, uint8_t tgt_prio)
378 {
379  auto it = queues[curr_prio].begin();
380  while (it != queues[curr_prio].end()) {
381  // No packets left to move
382  if (packetPriorities[id][curr_prio] == 0)
383  break;
384 
385  auto pkt = *it;
386 
387  DPRINTF(QOS,
388  "qos::MemCtrl::escalateQueues checking priority %d packet "
389  "id %d address %d\n", curr_prio,
390  pkt->requestorId(), pkt->getAddr());
391 
392  // Found a packet to move
393  if (pkt->requestorId() == id) {
394 
395  uint64_t moved_entries = divCeil(pkt->getSize(),
396  queue_entry_size);
397 
398  DPRINTF(QOS,
399  "qos::MemCtrl::escalateQueues Requestor %s [id %d] moving "
400  "packet addr %d size %d (p size %d) from priority %d "
401  "to priority %d - "
402  "this requestor packets %d (entries to move %d)\n",
403  requestors[id], id, pkt->getAddr(),
404  pkt->getSize(),
405  queue_entry_size, curr_prio, tgt_prio,
406  packetPriorities[id][curr_prio], moved_entries);
407 
408 
409  if (pkt->isRead()) {
410  panic_if(readQueueSizes[curr_prio] < moved_entries,
411  "qos::MemCtrl::escalateQueues requestor %s negative "
412  "READ packets for priority %d",
413  requestors[id], tgt_prio);
414  readQueueSizes[curr_prio] -= moved_entries;
415  readQueueSizes[tgt_prio] += moved_entries;
416  } else if (pkt->isWrite()) {
417  panic_if(writeQueueSizes[curr_prio] < moved_entries,
418  "qos::MemCtrl::escalateQueues requestor %s negative "
419  "WRITE packets for priority %d",
420  requestors[id], tgt_prio);
421  writeQueueSizes[curr_prio] -= moved_entries;
422  writeQueueSizes[tgt_prio] += moved_entries;
423  }
424 
425  // Change QoS priority and move packet
426  pkt->qosValue(tgt_prio);
427  queues[tgt_prio].push_back(pkt);
428 
429  // Erase element from source packet queue, this will
430  // increment the iterator
431  it = queues[curr_prio].erase(it);
432  panic_if(packetPriorities[id][curr_prio] < moved_entries,
433  "qos::MemCtrl::escalateQueues requestor %s negative "
434  "packets for priority %d",
435  requestors[id], tgt_prio);
436 
437  packetPriorities[id][curr_prio] -= moved_entries;
438  packetPriorities[id][tgt_prio] += moved_entries;
439  } else {
440  // Increment iterator to next location in the queue
441  it++;
442  }
443  }
444 }
445 
446 template<typename Queues>
447 void
448 MemCtrl::escalate(std::initializer_list<Queues*> queues,
449  uint64_t queue_entry_size,
450  RequestorID id, uint8_t tgt_prio)
451 {
452  // If needed, initialize all counters and statistics
453  // for this requestor
454  addRequestor(id);
455 
456  DPRINTF(QOS,
457  "qos::MemCtrl::escalate Requestor %s [id %d] to priority "
458  "%d (currently %d packets)\n",requestors[id], id, tgt_prio,
459  packetPriorities[id][tgt_prio]);
460 
461  for (uint8_t curr_prio = 0; curr_prio < numPriorities(); ++curr_prio) {
462  // Skip target priority
463  if (curr_prio == tgt_prio)
464  continue;
465 
466  // Process other priority packet
467  while (packetPriorities[id][curr_prio] > 0) {
468  DPRINTF(QOS,
469  "qos::MemCtrl::escalate MID %d checking priority %d "
470  "(packets %d)- current packets in prio %d: %d\n"
471  "\t(source read %d source write %d target read %d, "
472  "target write %d)\n",
473  id, curr_prio, packetPriorities[id][curr_prio],
474  tgt_prio, packetPriorities[id][tgt_prio],
475  readQueueSizes[curr_prio],
476  writeQueueSizes[curr_prio], readQueueSizes[tgt_prio],
477  writeQueueSizes[tgt_prio]);
478 
479  // Check both read and write queue
480  for (auto q : queues) {
481  escalateQueues(*q, queue_entry_size, id,
482  curr_prio, tgt_prio);
483  }
484  }
485  }
486 
487  DPRINTF(QOS,
488  "qos::MemCtrl::escalate Completed requestor %s [id %d] to "
489  "priority %d (now %d packets)\n\t(total read %d, total write %d)"
490  "\n", requestors[id], id, tgt_prio, packetPriorities[id][tgt_prio],
491  readQueueSizes[tgt_prio], writeQueueSizes[tgt_prio]);
492 }
493 
494 template<typename Queues>
495 uint8_t
496 MemCtrl::qosSchedule(std::initializer_list<Queues*> queues,
497  const uint64_t queue_entry_size,
498  const PacketPtr pkt)
499 {
500  // Schedule packet.
501  uint8_t pkt_priority = schedule(pkt);
502 
503  assert(pkt_priority < numPriorities());
504 
505  pkt->qosValue(pkt_priority);
506 
507  if (qosSyncroScheduler) {
508  // Call the scheduling function on all other requestors.
509  for (const auto& requestor : requestors) {
510 
511  if (requestor.first == pkt->requestorId())
512  continue;
513 
514  uint8_t prio = schedule(requestor.first, 0);
515 
516  if (qosPriorityEscalation) {
517  DPRINTF(QOS,
518  "qos::MemCtrl::qosSchedule: (syncro) escalating "
519  "REQUESTOR %s to assigned priority %d\n",
520  _system->getRequestorName(requestor.first),
521  prio);
522  escalate(queues, queue_entry_size, requestor.first, prio);
523  }
524  }
525  }
526 
527  if (qosPriorityEscalation) {
528  DPRINTF(QOS,
529  "qos::MemCtrl::qosSchedule: escalating "
530  "REQUESTOR %s to assigned priority %d\n",
532  pkt_priority);
533  escalate(queues, queue_entry_size, pkt->requestorId(), pkt_priority);
534  }
535 
536  // Update last service tick for selected priority
537  serviceTick[pkt_priority] = curTick();
538 
539  return pkt_priority;
540 }
541 
542 } // namespace qos
543 } // namespace memory
544 } // namespace gem5
545 
546 #endif /* __MEM_QOS_MEM_CTRL_HH__ */
#define DPRINTF(x,...)
Definition: trace.hh:186
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
const char data[]
The ClockedObject class extends the SimObject with a clock and accessor functions to relate ticks to ...
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:294
RequestorID requestorId() const
Definition: packet.hh:778
uint8_t qosValue() const
QoS Value getter Returns 0 if QoS value was never set (constructor default).
Definition: packet.hh:767
std::string getRequestorName(RequestorID requestor_id)
Get the name of an object for a given request id.
Definition: system.cc:526
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
BusState getBusState() const
Gets the current bus state.
Definition: mem_ctrl.hh:297
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
uint64_t getReadQueueSize(const uint8_t prio) const
Gets a READ queue size.
Definition: mem_ctrl.hh:327
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
BusState
Bus Direction.
Definition: mem_ctrl.hh:84
void escalateQueues(Queues &queues, uint64_t queue_entry_size, RequestorID id, uint8_t curr_prio, uint8_t tgt_prio)
Escalates/demotes priority of all packets belonging to the passed requestor to given priority value i...
Definition: mem_ctrl.hh:376
const bool qosPriorityEscalation
Enables QoS priority escalation.
Definition: mem_ctrl.hh:100
std::vector< Tick > serviceTick
Vector of QoS priorities/last service time.
Definition: mem_ctrl.hh:122
void setCurrentBusState()
Set current bus direction (READ or WRITE) from next selected one.
Definition: mem_ctrl.hh:240
std::unordered_map< RequestorID, std::vector< uint64_t > > packetPriorities
Hash of requestors - number of packets queued per priority.
Definition: mem_ctrl.hh:112
std::vector< uint64_t > readQueueSizes
Read request packets queue length in #packets, per QoS priority.
Definition: mem_ctrl.hh:125
const std::unique_ptr< Policy > policy
QoS Policy, assigns QoS priority to the incoming packets.
Definition: mem_ctrl.hh:88
const std::unique_ptr< QueuePolicy > queuePolicy
QoS Queue Policy: selects packet among same-priority queue.
Definition: mem_ctrl.hh:94
BusState getBusStateNext() const
Gets the next bus state.
Definition: mem_ctrl.hh:304
Tick getServiceTick(const uint8_t prio) const
Gets the last service tick related to a QoS Priority.
Definition: mem_ctrl.hh:359
MemCtrl(const QoSMemCtrlParams &)
QoS Memory base class.
Definition: mem_ctrl.cc:55
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
const std::unique_ptr< TurnaroundPolicy > turnPolicy
QoS Bus Turnaround Policy: selects the bus direction (READ/WRITE)
Definition: mem_ctrl.hh:91
const bool qosSyncroScheduler
Enables QoS synchronized scheduling invokes the QoS scheduler on all requestors, at every packet arri...
Definition: mem_ctrl.hh:106
bool hasRequestor(RequestorID id) const
hasRequestor returns true if the selected requestor(ID) has been registered in the memory controller,...
Definition: mem_ctrl.hh:316
System * system() const
read the system pointer
Definition: mem_ctrl.hh:371
uint8_t numPriorities() const
Gets the total number of priority levels in the QoS memory controller.
Definition: mem_ctrl.hh:367
uint64_t getTotalWriteQueueSize() const
Gets the total combined WRITE queues size.
Definition: mem_ctrl.hh:351
const uint8_t _numPriorities
Number of configured QoS priorities.
Definition: mem_ctrl.hh:97
void addRequestor(const RequestorID id)
Initializes dynamically counters and statistics for a given Requestor.
Definition: mem_ctrl.cc:275
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
void escalate(std::initializer_list< Queues * > queues, uint64_t queue_entry_size, RequestorID id, uint8_t tgt_prio)
Escalates/demotes priority of all packets belonging to the passed requestor to given priority value.
Definition: mem_ctrl.hh:448
uint64_t totalReadQueueSize
Total read request packets queue length in #packets.
Definition: mem_ctrl.hh:131
std::unordered_map< RequestorID, std::unordered_map< uint64_t, std::deque< uint64_t > > > requestTimes
Hash of requestors - address of request - queue of times of request.
Definition: mem_ctrl.hh:116
uint64_t getWriteQueueSize(const uint8_t prio) const
Gets a WRITE queue size.
Definition: mem_ctrl.hh:336
gem5::memory::qos::MemCtrl::MemCtrlStats stats
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
uint64_t getTotalReadQueueSize() const
Gets the total combined READ queues size.
Definition: mem_ctrl.hh:344
std::unordered_map< RequestorID, const std::string > requestors
Hash of requestor ID - requestor name.
Definition: mem_ctrl.hh:109
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
Statistics container.
Definition: group.hh:94
This is a simple scalar statistic, like a counter.
Definition: statistics.hh:1931
This is a vector of StandardDeviation stats.
Definition: statistics.hh:2293
A vector of scalar stats.
Definition: statistics.hh:2007
ClockedObject declaration and implementation.
static constexpr T divCeil(const T &a, const U &b)
Definition: intmath.hh:110
void schedule(Event &event, Tick when)
Definition: eventq.hh:1019
#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
Bitfield< 27 > q
Definition: misc_types.hh:55
Bitfield< 33 > id
Definition: misc_types.hh:257
Bitfield< 3 > addr
Definition: types.hh:84
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
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
uint64_t Tick
Tick count type.
Definition: types.hh:58
uint16_t RequestorID
Definition: request.hh:95
Declaration of the Packet class.
Declaration of a request, the overall memory request consisting of the parts of the request that are ...
Declaration of Statistics objects.
statistics::VectorStandardDeviation avgPriorityDistance
per-requestor average QoS distance between assigned and queued values
Definition: mem_ctrl.hh:159
statistics::VectorStandardDeviation avgPriority
per-requestor average QoS priority
Definition: mem_ctrl.hh:154
statistics::Vector priorityMaxLatency
per-priority maximum latency
Definition: mem_ctrl.hh:164
statistics::Vector priorityMinLatency
per-priority minimum latency
Definition: mem_ctrl.hh:162
statistics::Scalar numStayReadState
Count the number of times bus staying in READ state.
Definition: mem_ctrl.hh:170
statistics::Scalar numReadWriteTurnArounds
Count the number of turnarounds READ to WRITE.
Definition: mem_ctrl.hh:166
void regStats() override
Callback to set stat parameters.
Definition: mem_ctrl.cc:314
statistics::Scalar numStayWriteState
Count the number of times bus staying in WRITE state.
Definition: mem_ctrl.hh:172
statistics::Scalar numWriteReadTurnArounds
Count the number of turnarounds WRITE to READ.
Definition: mem_ctrl.hh:168
Definition: mem.h:38

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