gem5  v21.1.0.2
mem_ctrl.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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_ctrl.hh"
39 
40 #include "mem/qos/policy.hh"
41 #include "mem/qos/q_policy.hh"
43 #include "sim/core.hh"
44 
45 namespace gem5
46 {
47 
48 namespace memory
49 {
50 
52 namespace qos
53 {
54 
55 MemCtrl::MemCtrl(const QoSMemCtrlParams &p)
56  : ClockedObject(p),
57  policy(p.qos_policy),
58  turnPolicy(p.qos_turnaround_policy),
59  queuePolicy(QueuePolicy::create(p)),
60  _numPriorities(p.qos_priorities),
61  qosPriorityEscalation(p.qos_priority_escalation),
62  qosSyncroScheduler(p.qos_syncro_scheduler),
63  totalReadQueueSize(0), totalWriteQueueSize(0),
64  busState(READ), busStateNext(READ),
65  stats(*this),
66  _system(p.system)
67 {
68  // Set the priority policy
69  if (policy) {
70  policy->setMemCtrl(this);
71  }
72 
73  // Set the queue priority policy
74  if (queuePolicy) {
75  queuePolicy->setMemCtrl(this);
76  }
77 
78  // Set the bus turnaround policy
79  if (turnPolicy) {
80  turnPolicy->setMemCtrl(this);
81  }
82 
86 }
87 
89 {}
90 
91 void
93  Addr addr, uint64_t entries)
94 {
95  // If needed, initialize all counters and statistics
96  // for this requestor
97  addRequestor(id);
98 
99  DPRINTF(QOS,
100  "qos::MemCtrl::logRequest REQUESTOR %s [id %d] address %#x"
101  " prio %d this requestor q packets %d"
102  " - queue size %d - requested entries %d\n",
103  requestors[id], id, addr, _qos, packetPriorities[id][_qos],
104  (dir == READ) ? readQueueSizes[_qos]: writeQueueSizes[_qos],
105  entries);
106 
107  if (dir == READ) {
108  readQueueSizes[_qos] += entries;
109  totalReadQueueSize += entries;
110  } else if (dir == WRITE) {
111  writeQueueSizes[_qos] += entries;
112  totalWriteQueueSize += entries;
113  }
114 
115  packetPriorities[id][_qos] += entries;
116  for (auto j = 0; j < entries; ++j) {
117  requestTimes[id][addr].push_back(curTick());
118  }
119 
120  // Record statistics
121  stats.avgPriority[id].sample(_qos);
122 
123  // Compute avg priority distance
124 
125  for (uint8_t i = 0; i < packetPriorities[id].size(); ++i) {
126  uint8_t distance =
127  (abs(int(_qos) - int(i))) * packetPriorities[id][i];
128 
129  if (distance > 0) {
130  stats.avgPriorityDistance[id].sample(distance);
131  DPRINTF(QOS,
132  "qos::MemCtrl::logRequest REQUESTOR %s [id %d]"
133  " registering priority distance %d for priority %d"
134  " (packets %d)\n",
135  requestors[id], id, distance, i,
136  packetPriorities[id][i]);
137  }
138  }
139 
140  DPRINTF(QOS,
141  "qos::MemCtrl::logRequest REQUESTOR %s [id %d] prio %d "
142  "this requestor q packets %d - new queue size %d\n",
143  requestors[id], id, _qos, packetPriorities[id][_qos],
144  (dir == READ) ? readQueueSizes[_qos]: writeQueueSizes[_qos]);
145 
146 }
147 
148 void
150  Addr addr, uint64_t entries, double delay)
151 {
152  panic_if(!hasRequestor(id),
153  "Logging response with invalid requestor\n");
154 
155  DPRINTF(QOS,
156  "qos::MemCtrl::logResponse REQUESTOR %s [id %d] address %#x prio"
157  " %d this requestor q packets %d"
158  " - queue size %d - requested entries %d\n",
159  requestors[id], id, addr, _qos, packetPriorities[id][_qos],
160  (dir == READ) ? readQueueSizes[_qos]: writeQueueSizes[_qos],
161  entries);
162 
163  if (dir == READ) {
164  readQueueSizes[_qos] -= entries;
165  totalReadQueueSize -= entries;
166  } else if (dir == WRITE) {
167  writeQueueSizes[_qos] -= entries;
168  totalWriteQueueSize -= entries;
169  }
170 
171  panic_if(packetPriorities[id][_qos] == 0,
172  "qos::MemCtrl::logResponse requestor %s negative packets "
173  "for priority %d", requestors[id], _qos);
174 
175  packetPriorities[id][_qos] -= entries;
176 
177  for (auto j = 0; j < entries; ++j) {
178  auto it = requestTimes[id].find(addr);
179  panic_if(it == requestTimes[id].end(),
180  "qos::MemCtrl::logResponse requestor %s unmatched response "
181  "for address %#x received", requestors[id], addr);
182 
183  // Load request time
184  uint64_t requestTime = it->second.front();
185 
186  // Remove request entry
187  it->second.pop_front();
188 
189  // Remove whole address entry if last one
190  if (it->second.empty()) {
191  requestTimes[id].erase(it);
192  }
193  // Compute latency
194  double latency = (double) (curTick() + delay - requestTime)
196 
197  if (latency > 0) {
198  // Record per-priority latency stats
199  if (stats.priorityMaxLatency[_qos].value() < latency) {
200  stats.priorityMaxLatency[_qos] = latency;
201  }
202 
203  if (stats.priorityMinLatency[_qos].value() > latency
204  || stats.priorityMinLatency[_qos].value() == 0) {
205  stats.priorityMinLatency[_qos] = latency;
206  }
207  }
208  }
209 
210  DPRINTF(QOS,
211  "qos::MemCtrl::logResponse REQUESTOR %s [id %d] prio %d "
212  "this requestor q packets %d - new queue size %d\n",
213  requestors[id], id, _qos, packetPriorities[id][_qos],
214  (dir == READ) ? readQueueSizes[_qos]: writeQueueSizes[_qos]);
215 }
216 
217 uint8_t
219 {
220  if (policy) {
221  return policy->schedule(id, data);
222  } else {
223  DPRINTF(QOS,
224  "qos::MemCtrl::schedule requestor id [%d] "
225  "data received [%d], but QoS scheduler not initialized\n",
226  id,data);
227  return 0;
228  }
229 }
230 
231 uint8_t
233 {
234  assert(pkt->req);
235 
236  if (policy) {
237  return schedule(pkt->req->requestorId(), pkt->getSize());
238  } else {
239  DPRINTF(QOS, "qos::MemCtrl::schedule Packet received [Qv %d], "
240  "but QoS scheduler not initialized\n",
241  pkt->qosValue());
242  return pkt->qosValue();
243  }
244 }
245 
248 {
249  auto bus_state = getBusState();
250 
251  if (turnPolicy) {
252  DPRINTF(QOS,
253  "qos::MemCtrl::selectNextBusState running policy %s\n",
254  turnPolicy->name());
255 
256  bus_state = turnPolicy->selectBusState();
257  } else {
258  DPRINTF(QOS,
259  "qos::MemCtrl::selectNextBusState running "
260  "default bus direction selection policy\n");
261 
262  if ((!getTotalReadQueueSize() && bus_state == MemCtrl::READ) ||
263  (!getTotalWriteQueueSize() && bus_state == MemCtrl::WRITE)) {
264  // READ/WRITE turnaround
265  bus_state = (bus_state == MemCtrl::READ) ? MemCtrl::WRITE :
267 
268  }
269  }
270 
271  return bus_state;
272 }
273 
274 void
276 {
277  if (!hasRequestor(id)) {
278  requestors.emplace(id, _system->getRequestorName(id));
279  packetPriorities[id].resize(numPriorities(), 0);
280 
281  DPRINTF(QOS,
282  "qos::MemCtrl::addRequestor registering"
283  " Requestor %s [id %d]\n",
284  requestors[id], id);
285  }
286 }
287 
289  : statistics::Group(&mc),
290  memCtrl(mc),
291 
292  ADD_STAT(avgPriority, statistics::units::Count::get(),
293  "Average QoS priority value for accepted requests"),
294  ADD_STAT(avgPriorityDistance, statistics::units::Count::get(),
295  "Average QoS priority distance between assigned and queued "
296  "values"),
297 
298  ADD_STAT(priorityMinLatency, statistics::units::Second::get(),
299  "per QoS priority minimum request to response latency"),
300  ADD_STAT(priorityMaxLatency, statistics::units::Second::get(),
301  "per QoS priority maximum request to response latency"),
302  ADD_STAT(numReadWriteTurnArounds, statistics::units::Count::get(),
303  "Number of turnarounds from READ to WRITE"),
304  ADD_STAT(numWriteReadTurnArounds, statistics::units::Count::get(),
305  "Number of turnarounds from WRITE to READ"),
306  ADD_STAT(numStayReadState, statistics::units::Count::get(),
307  "Number of times bus staying in READ state"),
308  ADD_STAT(numStayWriteState, statistics::units::Count::get(),
309  "Number of times bus staying in WRITE state")
310 {
311 }
312 
313 void
315 {
317 
318  using namespace statistics;
319 
320  System *system = memCtrl._system;
321  const auto max_requestors = system->maxRequestors();
322  const auto num_priorities = memCtrl.numPriorities();
323 
324  // Initializes per requestor statistics
325  avgPriority
326  .init(max_requestors)
327  .flags(nozero | nonan)
328  .precision(2)
329  ;
330 
331  avgPriorityDistance
332  .init(max_requestors)
333  .flags(nozero | nonan)
334  ;
335 
336  priorityMinLatency
337  .init(num_priorities)
338  .precision(12)
339  ;
340 
341  priorityMaxLatency
342  .init(num_priorities)
343  .precision(12)
344  ;
345 
346  for (int i = 0; i < max_requestors; i++) {
347  const std::string name = system->getRequestorName(i);
348  avgPriority.subname(i, name);
349  avgPriorityDistance.subname(i, name);
350  }
351 
352  for (int j = 0; j < num_priorities; ++j) {
353  priorityMinLatency.subname(j, std::to_string(j));
354  priorityMaxLatency.subname(j, std::to_string(j));
355  }
356 }
357 
358 void
360 {
361  if (busStateNext != busState) {
362  if (busState == READ) {
364  } else if (busState == WRITE) {
366  }
367  } else {
368  if (busState == READ) {
370  } else if (busState == WRITE) {
372  }
373  }
374 }
375 
376 } // namespace qos
377 } // namespace memory
378 } // namespace gem5
gem5::curTick
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
gem5::memory::qos::MemCtrl::totalReadQueueSize
uint64_t totalReadQueueSize
Total read request packets queue length in #packets.
Definition: mem_ctrl.hh:131
gem5::memory::qos::MemCtrl::queuePolicy
const std::unique_ptr< QueuePolicy > queuePolicy
QoS Queue Policy: selects packet among same-priority queue.
Definition: mem_ctrl.hh:94
gem5::memory::qos::MemCtrl::MemCtrlStats::numWriteReadTurnArounds
statistics::Scalar numWriteReadTurnArounds
Count the number of turnarounds WRITE to READ.
Definition: mem_ctrl.hh:168
gem5::memory::qos::MemCtrl::logResponse
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
data
const char data[]
Definition: circlebuf.test.cc:48
memory
Definition: mem.h:38
turnaround_policy.hh
gem5::memory::qos::MemCtrl::requestTimes
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
gem5::memory::qos::MemCtrl::_numPriorities
const uint8_t _numPriorities
Number of configured QoS priorities.
Definition: mem_ctrl.hh:97
gem5::memory::qos::MemCtrl::MemCtrlStats::avgPriorityDistance
statistics::VectorStandardDeviation avgPriorityDistance
per-requestor average QoS distance between assigned and queued values
Definition: mem_ctrl.hh:159
gem5::memory::qos::MemCtrl::addRequestor
void addRequestor(const RequestorID id)
Initializes dynamically counters and statistics for a given Requestor.
Definition: mem_ctrl.cc:275
gem5::Packet::req
RequestPtr req
A pointer to the original request.
Definition: packet.hh:366
gem5::memory::qos::MemCtrl::READ
@ READ
Definition: mem_ctrl.hh:84
gem5::memory::qos::QueuePolicy
QoS Queue Policy.
Definition: q_policy.hh:66
gem5::memory::qos::MemCtrl::_system
System * _system
Pointer to the System object.
Definition: mem_ctrl.hh:176
gem5::statistics::nozero
const FlagsType nozero
Don't print if this is zero.
Definition: info.hh:68
sc_dt::to_string
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:91
gem5::Packet::qosValue
uint8_t qosValue() const
QoS Value getter Returns 0 if QoS value was never set (constructor default).
Definition: packet.hh:756
gem5::memory::qos::MemCtrl::MemCtrlStats::numReadWriteTurnArounds
statistics::Scalar numReadWriteTurnArounds
Count the number of turnarounds READ to WRITE.
Definition: mem_ctrl.hh:166
gem5::memory::qos::MemCtrl::readQueueSizes
std::vector< uint64_t > readQueueSizes
Read request packets queue length in #packets, per QoS priority.
Definition: mem_ctrl.hh:125
gem5::memory::qos::MemCtrl::MemCtrlStats::numStayWriteState
statistics::Scalar numStayWriteState
Count the number of times bus staying in WRITE state.
Definition: mem_ctrl.hh:172
gem5::X86ISA::system
Bitfield< 15 > system
Definition: misc.hh:1003
gem5::memory::qos::MemCtrl::MemCtrl
MemCtrl(const QoSMemCtrlParams &)
QoS Memory base class.
Definition: mem_ctrl.cc:55
gem5::memory::GEM5_DEPRECATED_NAMESPACE
GEM5_DEPRECATED_NAMESPACE(QoS, qos)
gem5::statistics::nonan
const FlagsType nonan
Don't print if this is NAN.
Definition: info.hh:70
q_policy.hh
gem5::memory::qos::MemCtrl::writeQueueSizes
std::vector< uint64_t > writeQueueSizes
Write request packets queue length in #packets, per QoS priority.
Definition: mem_ctrl.hh:128
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::memory::qos::MemCtrl::BusState
BusState
Bus Direction.
Definition: mem_ctrl.hh:84
gem5::memory::qos::MemCtrl::getTotalWriteQueueSize
uint64_t getTotalWriteQueueSize() const
Gets the total combined WRITE queues size.
Definition: mem_ctrl.hh:351
gem5::memory::qos::MemCtrl::busStateNext
BusState busStateNext
bus state for next request event triggered
Definition: mem_ctrl.hh:143
gem5::memory::qos::MemCtrl::busState
BusState busState
Bus state used to control the read/write switching and drive the scheduling of the next request.
Definition: mem_ctrl.hh:140
gem5::memory::qos::MemCtrl::hasRequestor
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
gem5::memory::qos::MemCtrl::~MemCtrl
virtual ~MemCtrl()
Definition: mem_ctrl.cc:88
gem5::statistics::VectorBase::value
void value(VCounter &vec) const
Definition: statistics.hh:964
gem5::memory::qos::MemCtrl::turnPolicy
const std::unique_ptr< TurnaroundPolicy > turnPolicy
QoS Bus Turnaround Policy: selects the bus direction (READ/WRITE)
Definition: mem_ctrl.hh:91
gem5::ArmISA::j
Bitfield< 24 > j
Definition: misc_types.hh:57
gem5::memory::qos::MemCtrl::schedule
uint8_t schedule(RequestorID id, uint64_t data)
Definition: mem_ctrl.cc:218
gem5::System::maxRequestors
RequestorID maxRequestors()
Get the number of requestors registered in the system.
Definition: system.hh:518
gem5::System
Definition: system.hh:77
gem5::Named::name
virtual std::string name() const
Definition: named.hh:47
gem5::memory::qos::MemCtrl::selectNextBusState
BusState selectNextBusState()
Returns next bus direction (READ or WRITE) based on configured policy.
Definition: mem_ctrl.cc:247
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
ADD_STAT
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:75
gem5::Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:283
gem5::MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
gem5::System::getRequestorName
std::string getRequestorName(RequestorID requestor_id)
Get the name of an object for a given request id.
Definition: system.cc:637
gem5::memory::qos::MemCtrl::stats
gem5::memory::qos::MemCtrl::MemCtrlStats stats
gem5::memory::qos::MemCtrl::system
System * system() const
read the system pointer
Definition: mem_ctrl.hh:371
gem5::memory::qos::MemCtrl::totalWriteQueueSize
uint64_t totalWriteQueueSize
Total write request packets queue length in #packets.
Definition: mem_ctrl.hh:134
policy.hh
gem5::memory::qos::MemCtrl::MemCtrlStats::avgPriority
statistics::VectorStandardDeviation avgPriority
per-requestor average QoS priority
Definition: mem_ctrl.hh:154
core.hh
gem5::Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
gem5::statistics::Group::regStats
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:69
gem5::ClockedObject
The ClockedObject class extends the SimObject with a clock and accessor functions to relate ticks to ...
Definition: clocked_object.hh:234
mem_ctrl.hh
gem5::memory::qos::MemCtrl::MemCtrlStats::regStats
void regStats() override
Callback to set stat parameters.
Definition: mem_ctrl.cc:314
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:203
gem5::memory::qos::MemCtrl::serviceTick
std::vector< Tick > serviceTick
Vector of QoS priorities/last service time.
Definition: mem_ctrl.hh:122
gem5::memory::qos::MemCtrl::getTotalReadQueueSize
uint64_t getTotalReadQueueSize() const
Gets the total combined READ queues size.
Definition: mem_ctrl.hh:344
gem5::memory::qos::MemCtrl::policy
const std::unique_ptr< Policy > policy
QoS Policy, assigns QoS priority to the incoming packets.
Definition: mem_ctrl.hh:88
gem5::memory::qos::MemCtrl::getBusState
BusState getBusState() const
Gets the current bus state.
Definition: mem_ctrl.hh:297
gem5::memory::qos::MemCtrl::packetPriorities
std::unordered_map< RequestorID, std::vector< uint64_t > > packetPriorities
Hash of requestors - number of packets queued per priority.
Definition: mem_ctrl.hh:112
gem5::memory::qos::MemCtrl::numPriorities
uint8_t numPriorities() const
Gets the total number of priority levels in the QoS memory controller.
Definition: mem_ctrl.hh:367
gem5::memory::qos::MemCtrl::MemCtrlStats::priorityMinLatency
statistics::Vector priorityMinLatency
per-priority minimum latency
Definition: mem_ctrl.hh:162
gem5::statistics::Group
Statistics container.
Definition: group.hh:93
gem5::memory::qos::MemCtrl::recordTurnaroundStats
void recordTurnaroundStats()
Record statistics on turnarounds based on busStateNext and busState values.
Definition: mem_ctrl.cc:359
gem5::memory::qos::MemCtrl::WRITE
@ WRITE
Definition: mem_ctrl.hh:84
gem5::ArmISA::id
Bitfield< 33 > id
Definition: misc_types.hh:250
gem5::memory::qos::MemCtrl::MemCtrlStats::MemCtrlStats
MemCtrlStats(MemCtrl &mc)
Definition: mem_ctrl.cc:288
gem5::memory::qos::MemCtrl
The qos::MemCtrl is a base class for Memory objects which support QoS - it provides access to a set o...
Definition: mem_ctrl.hh:80
gem5::RequestorID
uint16_t RequestorID
Definition: request.hh:95
gem5::memory::qos::MemCtrl::requestors
std::unordered_map< RequestorID, const std::string > requestors
Hash of requestor ID - requestor name.
Definition: mem_ctrl.hh:109
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::memory::qos::MemCtrl::logRequest
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
gem5::memory::qos::MemCtrl::MemCtrlStats::numStayReadState
statistics::Scalar numStayReadState
Count the number of times bus staying in READ state.
Definition: mem_ctrl.hh:170
gem5::Packet::getSize
unsigned getSize() const
Definition: packet.hh:791
gem5::memory::qos::MemCtrl::MemCtrlStats::priorityMaxLatency
statistics::Vector priorityMaxLatency
per-priority maximum latency
Definition: mem_ctrl.hh:164
gem5::sim_clock::as_float::s
double s
These variables equal the number of ticks in the unit of time they're named after in a double.
Definition: core.cc:53
gem5::X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:84

Generated on Tue Sep 21 2021 12:25:32 for gem5 by doxygen 1.8.17