2 * Copyright (c) 2014 ARM Limited
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.
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.
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.
38 #ifndef __MEM_MEM_CHECKER_HH__
39 #define __MEM_MEM_CHECKER_HH__
46 #include <unordered_map>
49 #include "base/cprintf.hh"
50 #include "base/trace.hh"
51 #include "base/types.hh"
52 #include "debug/MemChecker.hh"
53 #include "params/MemChecker.hh"
54 #include "sim/core.hh"
55 #include "sim/sim_object.hh"
58 * MemChecker. Verifies that reads observe the values from permissible writes.
59 * As memory operations have a start and completion time, we consider them as
60 * transactions which have a start and end time. Because of this, the lifetimes
61 * of transactions of memory operations may be overlapping -- we assume that if
62 * there is overlap between writes, they could be reordered by the memory
63 * subsystem, and a read could any of these. For more detail, see comments of
66 * For simplicity, the permissible values a read can observe are only dependent
67 * on the particular location, and we do not consider the effect of multi-byte
68 * reads or writes. This precludes us from discovering single-copy atomicity
71 class MemChecker : public SimObject
75 * The Serial type is used to be able to uniquely identify a transaction as
76 * it passes through the system. It's value is independent of any other
79 typedef uint64_t Serial;
81 static const Serial SERIAL_INITIAL = 0; //!< Initial serial
84 * The initial tick the system starts with. Must not be larger than the
85 * minimum value that curTick() could return at any time in the system's
88 static const Tick TICK_INITIAL = 0;
91 * The maximum value that curTick() could ever return.
93 static const Tick TICK_FUTURE = MaxTick;
96 * Initial data value. No requirements.
98 static const uint8_t DATA_INITIAL = 0x00;
101 * The Transaction class captures the lifetimes of read and write
102 * operations, and the values they consumed or produced respectively.
108 Transaction(Serial _serial,
109 Tick _start, Tick _complete,
110 uint8_t _data = DATA_INITIAL)
112 start(_start), complete(_complete),
117 Serial serial; //!< Unique identifying serial
118 Tick start; //!< Start tick
119 Tick complete; //!< Completion tick
122 * Depending on the memory operation, the data value either represents:
123 * for writes, the value written upon start; for reads, the value read
129 * Orders Transactions for use with std::map.
131 bool operator<(const Transaction& rhs) const
132 { return serial < rhs.serial; }
136 * The WriteCluster class captures sets of writes where all writes are
137 * overlapping with at least one other write. Capturing writes in this way
138 * simplifies pruning of writes.
144 : start(TICK_FUTURE), complete(TICK_FUTURE),
145 completeMax(TICK_INITIAL), numIncomplete(0)
149 * Starts a write transaction.
151 * @param serial Unique identifier of the write.
152 * @param _start When the write was sent off to the memory subsystem.
153 * @param data The data that this write passed to the memory
156 void startWrite(Serial serial, Tick _start, uint8_t data);
159 * Completes a write transaction.
161 * @param serial Unique identifier of a write *previously started*.
162 * @param _complete When the write was sent off to the memory
165 void completeWrite(Serial serial, Tick _complete);
168 * Aborts a write transaction.
170 * @param serial Unique identifier of a write *previously started*.
172 void abortWrite(Serial serial);
175 * @return true if this cluster's write all completed, false otherwise.
177 bool isComplete() const { return complete != TICK_FUTURE; }
180 Tick start; //!< Start of earliest write in cluster
181 Tick complete; //!< Completion of last write in cluster
184 * Map of Serial --> Transaction of all writes in cluster; contains
185 * all, in-flight or already completed.
187 std::unordered_map<Serial, Transaction> writes;
191 size_t numIncomplete;
194 typedef std::list<Transaction> TransactionList;
195 typedef std::list<WriteCluster> WriteClusterList;
198 * The ByteTracker keeps track of transactions for the *same byte* -- all
199 * outstanding reads, the completed reads (and what they observed) and write
200 * clusters (see WriteCluster).
202 class ByteTracker : public Named
206 ByteTracker(Addr addr = 0, const MemChecker *parent = NULL)
207 : Named((parent != NULL ? parent->name() : "") +
208 csprintf(".ByteTracker@%#llx", addr))
210 // The initial transaction has start == complete == TICK_INITIAL,
211 // indicating that there has been no real write to this location;
212 // therefore, upon checking, we do not expect any particular value.
213 readObservations.emplace_back(
214 Transaction(SERIAL_INITIAL, TICK_INITIAL, TICK_INITIAL,
219 * Starts a read transaction.
221 * @param serial Unique identifier for the read.
222 * @param start When the read was sent off to the memory subsystem.
224 void startRead(Serial serial, Tick start);
227 * Given a start and end time (of any read transaction), this function
228 * iterates through all data that such a read is expected to see. The
229 * data parameter is the actual value that we observed, and the
230 * function immediately returns true when a match is found, false
233 * The set of expected data are:
235 * 1. The last value observed by a read with a completion time before
236 * this start time (if any).
238 * 2. The data produced by write transactions with a completion after
239 * the last observed read start time. Only data produced in the
240 * closest overlapping / earlier write cluster relative to this check
241 * request is considered, as writes in separate clusters are not
244 * @param start Start time of transaction to validate.
245 * @param complete End time of transaction to validate.
246 * @param data The value that we have actually seen.
248 * @return True if a match is found, false otherwise.
250 bool inExpectedData(Tick start, Tick complete, uint8_t data);
253 * Completes a read transaction that is still outstanding.
255 * @param serial Unique identifier of a read *previously started*.
256 * @param complete When the read got a response.
257 * @param data The data returned by the memory subsystem.
259 bool completeRead(Serial serial, Tick complete, uint8_t data);
262 * Starts a write transaction. Wrapper to startWrite of WriteCluster
265 * @param serial Unique identifier of the write.
266 * @param start When the write was sent off to the memory subsystem.
267 * @param data The data that this write passed to the memory
270 void startWrite(Serial serial, Tick start, uint8_t data);
273 * Completes a write transaction. Wrapper to startWrite of WriteCluster
276 * @param serial Unique identifier of a write *previously started*.
277 * @param complete When the write was sent off to the memory subsystem.
279 void completeWrite(Serial serial, Tick complete);
282 * Aborts a write transaction. Wrapper to abortWrite of WriteCluster
285 * @param serial Unique identifier of a write *previously started*.
287 void abortWrite(Serial serial);
290 * This function returns the expected data that inExpectedData iterated
291 * through in the last call. If inExpectedData last returned true, the
292 * set may be incomplete; if inExpectedData last returned false, the
293 * vector will contain the full set.
295 * @return Reference to internally maintained vector maintaining last
296 * expected data that inExpectedData iterated through.
298 const std::vector<uint8_t>& lastExpectedData() const
299 { return _lastExpectedData; }
304 * Convenience function to return the most recent incomplete write
305 * cluster. Instantiates new write cluster if the most recent one has
308 * @return The most recent incomplete write cluster.
310 WriteCluster* getIncompleteWriteCluster();
313 * Helper function to return an iterator to the entry of a container of
314 * Transaction compatible classes, before a certain tick.
316 * @param before Tick value which should be greater than the
317 * completion tick of the returned element.
319 * @return Iterator into container.
321 template <class TList>
322 typename TList::iterator lastCompletedTransaction(TList *l, Tick before)
326 // Scanning backwards increases the chances of getting a match
330 for (--it; it != l->begin() && it->complete >= before; --it);
336 * Prunes no longer needed transactions. We only keep up to the last /
337 * most recent of each, readObservations and writeClusters, before the
338 * first outstanding read.
340 * It depends on the contention / overlap between memory operations to
341 * the same location of a particular workload how large each of them
344 void pruneTransactions();
349 * Maintains a map of Serial -> Transaction for all outstanding reads.
351 * Use an ordered map here, as this makes pruneTransactions() more
352 * efficient (find first outstanding read).
354 std::map<Serial, Transaction> outstandingReads;
357 * List of completed reads, i.e. observations of reads.
359 TransactionList readObservations;
362 * List of write clusters for this address.
364 WriteClusterList writeClusters;
367 * See lastExpectedData().
369 std::vector<uint8_t> _lastExpectedData;
374 MemChecker(const MemCheckerParams &p)
376 nextSerial(SERIAL_INITIAL)
379 virtual ~MemChecker() {}
382 * Starts a read transaction.
384 * @param start Tick this read was sent to the memory subsystem.
385 * @param addr Address for read.
386 * @param size Size of data expected.
388 * @return Serial representing the unique identifier for this transaction.
390 Serial startRead(Tick start, Addr addr, size_t size);
393 * Starts a write transaction.
395 * @param start Tick when this write was sent to the memory subsystem.
396 * @param addr Address for write.
397 * @param size Size of data to be written.
398 * @param data Pointer to size bytes, containing data to be written.
400 * @return Serial representing the unique identifier for this transaction.
402 Serial startWrite(Tick start, Addr addr, size_t size, const uint8_t *data);
405 * Completes a previously started read transaction.
407 * @param serial A serial of a read that was previously started and
408 * matches the address of the previously started read.
409 * @param complete Tick we received the response from the memory subsystem.
410 * @param addr Address for read.
411 * @param size Size of data received.
412 * @param data Pointer to size bytes, containing data received.
414 * @return True if the data we received is in the expected set, false
417 bool completeRead(Serial serial, Tick complete,
418 Addr addr, size_t size, uint8_t *data);
421 * Completes a previously started write transaction.
423 * @param serial A serial of a write that was previously started and
424 * matches the address of the previously started write.
425 * @param complete Tick we received acknowledgment of completion from the
427 * @param addr Address for write.
428 * @param size The size of the data written.
430 void completeWrite(Serial serial, Tick complete, Addr addr, size_t size);
433 * Aborts a previously started write transaction.
435 * @param serial A serial of a write that was previously started and
436 * matches the address of the previously started write.
437 * @param addr Address for write.
438 * @param size The size of the data written.
440 void abortWrite(Serial serial, Addr addr, size_t size);
443 * Resets the entire checker. Note that if there are transactions
444 * in-flight, this will cause a warning to be issued if these are completed
445 * after the reset. This does not reset nextSerial to avoid such a race
446 * condition: where a transaction started before a reset with serial S,
447 * then reset() was called, followed by a start of a transaction with the
448 * same serial S and then receive a completion of the transaction before
449 * the reset with serial S.
452 { byte_trackers.clear(); }
455 * Resets an address-range. This may be useful in case other unmonitored
456 * parts of the system caused modification to this memory, but we cannot
457 * track their written values.
459 * @param addr Address base.
460 * @param size Size of range to be invalidated.
462 void reset(Addr addr, size_t size);
465 * In completeRead, if an error is encountered, this does not print nor
466 * cause an error, but instead should be handled by the caller. However, to
467 * record information about the cause of an error, completeRead creates an
468 * errorMessage. This function returns the last error that was detected in
471 * @return Reference to string of error message.
473 const std::string& getErrorMessage() const { return errorMessage; }
477 * Returns the instance of ByteTracker for the requested location.
479 ByteTracker* getByteTracker(Addr addr)
481 auto it = byte_trackers.find(addr);
482 if (it == byte_trackers.end()) {
483 it = byte_trackers.insert(
484 std::make_pair(addr, ByteTracker(addr, this))).first;
491 * Detailed error message of the last violation in completeRead.
493 std::string errorMessage;
496 * Next distinct serial to be assigned to the next transaction to be
502 * Maintain a map of address --> byte-tracker. Per-byte entries are
503 * initialized as needed.
505 * The required space for this obviously grows with the number of distinct
506 * addresses used for a particular workload. The used size is independent on
507 * the number of nodes in the system, those may affect the size of per-byte
508 * tracking information.
510 * Access via getByteTracker()!
512 std::unordered_map<Addr, ByteTracker> byte_trackers;
515 inline MemChecker::Serial
516 MemChecker::startRead(Tick start, Addr addr, size_t size)
519 "starting read: serial = %d, start = %d, addr = %#llx, "
520 "size = %d\n", nextSerial, start, addr , size);
522 for (size_t i = 0; i < size; ++i) {
523 getByteTracker(addr + i)->startRead(nextSerial, start);
529 inline MemChecker::Serial
530 MemChecker::startWrite(Tick start, Addr addr, size_t size, const uint8_t *data)
533 "starting write: serial = %d, start = %d, addr = %#llx, "
534 "size = %d\n", nextSerial, start, addr, size);
536 for (size_t i = 0; i < size; ++i) {
537 getByteTracker(addr + i)->startWrite(nextSerial, start, data[i]);
544 MemChecker::completeWrite(MemChecker::Serial serial, Tick complete,
545 Addr addr, size_t size)
548 "completing write: serial = %d, complete = %d, "
549 "addr = %#llx, size = %d\n", serial, complete, addr, size);
551 for (size_t i = 0; i < size; ++i) {
552 getByteTracker(addr + i)->completeWrite(serial, complete);
557 MemChecker::abortWrite(MemChecker::Serial serial, Addr addr, size_t size)
560 "aborting write: serial = %d, addr = %#llx, size = %d\n",
563 for (size_t i = 0; i < size; ++i) {
564 getByteTracker(addr + i)->abortWrite(serial);
568 #endif // __MEM_MEM_CHECKER_HH__