trace_cpu.cc revision 11249:0733a1c08600
1/*
2 * Copyright (c) 2013 - 2015 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 * Authors: Radhika Jagtap
38 *          Andreas Hansson
39 *          Thomas Grass
40 */
41
42#include "cpu/trace/trace_cpu.hh"
43
44#include "sim/sim_exit.hh"
45
46// Declare and initialize the static counter for number of trace CPUs.
47int TraceCPU::numTraceCPUs = 0;
48
49TraceCPU::TraceCPU(TraceCPUParams *params)
50    :   BaseCPU(params),
51        icachePort(this),
52        dcachePort(this),
53        instMasterID(params->system->getMasterId(name() + ".inst")),
54        dataMasterID(params->system->getMasterId(name() + ".data")),
55        instTraceFile(params->instTraceFile),
56        dataTraceFile(params->dataTraceFile),
57        icacheGen(*this, ".iside", icachePort, instMasterID, instTraceFile),
58        dcacheGen(*this, ".dside", dcachePort, dataMasterID, dataTraceFile,
59                    params->sizeROB, params->sizeStoreBuffer,
60                    params->sizeLoadBuffer),
61        icacheNextEvent(this),
62        dcacheNextEvent(this),
63        oneTraceComplete(false),
64        firstFetchTick(0),
65        execCompleteEvent(nullptr)
66{
67    // Increment static counter for number of Trace CPUs.
68    ++TraceCPU::numTraceCPUs;
69
70    // Check that the python parameters for sizes of ROB, store buffer and load
71    // buffer do not overflow the corresponding C++ variables.
72    fatal_if(params->sizeROB > UINT16_MAX, "ROB size set to %d exceeds the "
73                "max. value of %d.\n", params->sizeROB, UINT16_MAX);
74    fatal_if(params->sizeStoreBuffer > UINT16_MAX, "ROB size set to %d "
75                "exceeds the max. value of %d.\n", params->sizeROB,
76                UINT16_MAX);
77    fatal_if(params->sizeLoadBuffer > UINT16_MAX, "Load buffer size set to"
78                " %d exceeds the max. value of %d.\n",
79                params->sizeLoadBuffer, UINT16_MAX);
80}
81
82TraceCPU::~TraceCPU()
83{
84
85}
86
87TraceCPU*
88TraceCPUParams::create()
89{
90    return new TraceCPU(this);
91}
92
93void
94TraceCPU::takeOverFrom(BaseCPU *oldCPU)
95{
96    // Unbind the ports of the old CPU and bind the ports of the TraceCPU.
97    assert(!getInstPort().isConnected());
98    assert(oldCPU->getInstPort().isConnected());
99    BaseSlavePort &inst_peer_port = oldCPU->getInstPort().getSlavePort();
100    oldCPU->getInstPort().unbind();
101    getInstPort().bind(inst_peer_port);
102
103    assert(!getDataPort().isConnected());
104    assert(oldCPU->getDataPort().isConnected());
105    BaseSlavePort &data_peer_port = oldCPU->getDataPort().getSlavePort();
106    oldCPU->getDataPort().unbind();
107    getDataPort().bind(data_peer_port);
108}
109
110void
111TraceCPU::init()
112{
113    DPRINTF(TraceCPUInst, "Instruction fetch request trace file is \"%s\"."
114            "\n", instTraceFile);
115    DPRINTF(TraceCPUData, "Data memory request trace file is \"%s\".\n",
116            dataTraceFile);
117
118    BaseCPU::init();
119
120    // Get the send tick of the first instruction read request and schedule
121    // icacheNextEvent at that tick.
122    Tick first_icache_tick = icacheGen.init();
123    schedule(icacheNextEvent, first_icache_tick);
124
125    // Get the send tick of the first data read/write request and schedule
126    // dcacheNextEvent at that tick.
127    Tick first_dcache_tick = dcacheGen.init();
128    schedule(dcacheNextEvent, first_dcache_tick);
129
130    // The static counter for number of Trace CPUs is correctly set at this
131    // point so create an event and pass it.
132    execCompleteEvent = new CountedExitEvent("end of all traces reached.",
133                                                numTraceCPUs);
134    // Save the first fetch request tick to dump it as tickOffset
135    firstFetchTick = first_icache_tick;
136}
137
138void
139TraceCPU::schedIcacheNext()
140{
141    DPRINTF(TraceCPUInst, "IcacheGen event.\n");
142
143    // Try to send the current packet or a retry packet if there is one
144    bool sched_next = icacheGen.tryNext();
145    // If packet sent successfully, schedule next event
146    if (sched_next) {
147        DPRINTF(TraceCPUInst, "Scheduling next icacheGen event "
148                "at %d.\n", curTick() + icacheGen.tickDelta());
149        schedule(icacheNextEvent, curTick() + icacheGen.tickDelta());
150        ++numSchedIcacheEvent;
151    } else {
152        // check if traceComplete. If not, do nothing because sending failed
153        // and next event will be scheduled via RecvRetry()
154        if (icacheGen.isTraceComplete()) {
155            // If this is the first trace to complete, set the variable. If it
156            // is already set then both traces are complete to exit sim.
157            checkAndSchedExitEvent();
158        }
159    }
160    return;
161}
162
163void
164TraceCPU::schedDcacheNext()
165{
166    DPRINTF(TraceCPUData, "DcacheGen event.\n");
167
168    dcacheGen.execute();
169    if (dcacheGen.isExecComplete()) {
170        checkAndSchedExitEvent();
171    }
172}
173
174void
175TraceCPU::checkAndSchedExitEvent()
176{
177    if (!oneTraceComplete) {
178        oneTraceComplete = true;
179    } else {
180        // Schedule event to indicate execution is complete as both
181        // instruction and data access traces have been played back.
182        inform("%s: Execution complete.\n", name());
183
184        // Record stats which are computed at the end of simulation
185        tickOffset = firstFetchTick;
186        numCycles = (clockEdge() - firstFetchTick) / clockPeriod();
187        numOps = dcacheGen.getMicroOpCount();
188        schedule(*execCompleteEvent, curTick());
189    }
190}
191
192void
193TraceCPU::regStats()
194{
195
196    BaseCPU::regStats();
197
198    numSchedDcacheEvent
199    .name(name() + ".numSchedDcacheEvent")
200    .desc("Number of events scheduled to trigger data request generator")
201    ;
202
203    numSchedIcacheEvent
204    .name(name() + ".numSchedIcacheEvent")
205    .desc("Number of events scheduled to trigger instruction request generator")
206    ;
207
208    numOps
209    .name(name() + ".numOps")
210    .desc("Number of micro-ops simulated by the Trace CPU")
211    ;
212
213    cpi
214    .name(name() + ".cpi")
215    .desc("Cycles per micro-op used as a proxy for CPI")
216    .precision(6)
217    ;
218    cpi = numCycles/numOps;
219
220    tickOffset
221    .name(name() + ".tickOffset")
222    .desc("The first execution tick for the root node of elastic traces")
223    ;
224
225    icacheGen.regStats();
226    dcacheGen.regStats();
227}
228
229void
230TraceCPU::ElasticDataGen::regStats()
231{
232    using namespace Stats;
233
234    maxDependents
235    .name(name() + ".maxDependents")
236    .desc("Max number of dependents observed on a node")
237    ;
238
239    maxReadyListSize
240    .name(name() + ".maxReadyListSize")
241    .desc("Max size of the ready list observed")
242    ;
243
244    numSendAttempted
245    .name(name() + ".numSendAttempted")
246    .desc("Number of first attempts to send a request")
247    ;
248
249    numSendSucceeded
250    .name(name() + ".numSendSucceeded")
251    .desc("Number of successful first attempts")
252    ;
253
254    numSendFailed
255    .name(name() + ".numSendFailed")
256    .desc("Number of failed first attempts")
257    ;
258
259    numRetrySucceeded
260    .name(name() + ".numRetrySucceeded")
261    .desc("Number of successful retries")
262    ;
263
264    numSplitReqs
265    .name(name() + ".numSplitReqs")
266    .desc("Number of split requests")
267    ;
268
269    numSOLoads
270    .name(name() + ".numSOLoads")
271    .desc("Number of strictly ordered loads")
272    ;
273
274    numSOStores
275    .name(name() + ".numSOStores")
276    .desc("Number of strictly ordered stores")
277    ;
278
279    dataLastTick
280    .name(name() + ".dataLastTick")
281    .desc("Last tick simulated from the elastic data trace")
282    ;
283}
284
285Tick
286TraceCPU::ElasticDataGen::init()
287{
288    DPRINTF(TraceCPUData, "Initializing data memory request generator "
289            "DcacheGen: elastic issue with retry.\n");
290
291    if (!readNextWindow())
292        panic("Trace has %d elements. It must have at least %d elements.\n",
293              depGraph.size(), 2 * windowSize);
294    DPRINTF(TraceCPUData, "After 1st read, depGraph size:%d.\n",
295            depGraph.size());
296
297    if (!readNextWindow())
298        panic("Trace has %d elements. It must have at least %d elements.\n",
299              depGraph.size(), 2 * windowSize);
300    DPRINTF(TraceCPUData, "After 2st read, depGraph size:%d.\n",
301            depGraph.size());
302
303    // Print readyList
304    if (DTRACE(TraceCPUData)) {
305        printReadyList();
306    }
307    auto free_itr = readyList.begin();
308    DPRINTF(TraceCPUData, "Execute tick of the first dependency free node %lli"
309            " is %d.\n", free_itr->seqNum, free_itr->execTick);
310    // Return the execute tick of the earliest ready node so that an event
311    // can be scheduled to call execute()
312    return (free_itr->execTick);
313}
314
315void
316TraceCPU::ElasticDataGen::exit()
317{
318    trace.reset();
319}
320
321bool
322TraceCPU::ElasticDataGen::readNextWindow()
323{
324
325    // Read and add next window
326    DPRINTF(TraceCPUData, "Reading next window from file.\n");
327
328    if (traceComplete) {
329        // We are at the end of the file, thus we have no more records.
330        // Return false.
331        return false;
332    }
333
334    DPRINTF(TraceCPUData, "Start read: Size of depGraph is %d.\n",
335            depGraph.size());
336
337    uint32_t num_read = 0;
338    while (num_read != windowSize) {
339
340        // Create a new graph node
341        GraphNode* new_node = new GraphNode;
342
343        // Read the next line to get the next record. If that fails then end of
344        // trace has been reached and traceComplete needs to be set in addition
345        // to returning false.
346        if (!trace.read(new_node)) {
347            DPRINTF(TraceCPUData, "\tTrace complete!\n");
348            traceComplete = true;
349            return false;
350        }
351
352        // Annotate the ROB dependencies of the new node onto the parent nodes.
353        addDepsOnParent(new_node, new_node->robDep, new_node->numRobDep);
354        // Annotate the register dependencies of the new node onto the parent
355        // nodes.
356        addDepsOnParent(new_node, new_node->regDep, new_node->numRegDep);
357
358        num_read++;
359        // Add to map
360        depGraph[new_node->seqNum] = new_node;
361        if (new_node->numRobDep == 0 && new_node->numRegDep == 0) {
362            // Source dependencies are already complete, check if resources
363            // are available and issue. The execution time is approximated
364            // to current time plus the computational delay.
365            checkAndIssue(new_node);
366        }
367    }
368
369    DPRINTF(TraceCPUData, "End read: Size of depGraph is %d.\n",
370            depGraph.size());
371    return true;
372}
373
374template<typename T> void
375TraceCPU::ElasticDataGen::addDepsOnParent(GraphNode *new_node,
376                                            T& dep_array, uint8_t& num_dep)
377{
378    for (auto& a_dep : dep_array) {
379        // The convention is to set the dependencies starting with the first
380        // index in the ROB and register dependency arrays. Thus, when we reach
381        // a dependency equal to the initialisation value of zero, we know have
382        // iterated over all dependencies and can break.
383        if (a_dep == 0)
384            break;
385        // We look up the valid dependency, i.e. the parent of this node
386        auto parent_itr = depGraph.find(a_dep);
387        if (parent_itr != depGraph.end()) {
388            // If the parent is found, it is yet to be executed. Append a
389            // pointer to the new node to the dependents list of the parent
390            // node.
391            parent_itr->second->dependents.push_back(new_node);
392            auto num_depts = parent_itr->second->dependents.size();
393            maxDependents = std::max<double>(num_depts, maxDependents.value());
394        } else {
395            // The dependency is not found in the graph. So consider
396            // the execution of the parent is complete, i.e. remove this
397            // dependency.
398            a_dep = 0;
399            num_dep--;
400        }
401    }
402}
403
404void
405TraceCPU::ElasticDataGen::execute()
406{
407    DPRINTF(TraceCPUData, "Execute start occupancy:\n");
408    DPRINTFR(TraceCPUData, "\tdepGraph = %d, readyList = %d, "
409            "depFreeQueue = %d ,", depGraph.size(), readyList.size(),
410            depFreeQueue.size());
411    hwResource.printOccupancy();
412
413    // Read next window to make sure that dependents of all dep-free nodes
414    // are in the depGraph
415    if (nextRead) {
416        readNextWindow();
417        nextRead = false;
418    }
419
420    // First attempt to issue the pending dependency-free nodes held
421    // in depFreeQueue. If resources have become available for a node,
422    // then issue it, i.e. add the node to readyList.
423    while (!depFreeQueue.empty()) {
424        if (checkAndIssue(depFreeQueue.front(), false)) {
425            DPRINTF(TraceCPUData, "Removing from depFreeQueue: seq. num "
426                "%lli.\n", (depFreeQueue.front())->seqNum);
427            depFreeQueue.pop();
428        } else {
429            break;
430        }
431    }
432    // Proceed to execute from readyList
433    auto graph_itr = depGraph.begin();
434    auto free_itr = readyList.begin();
435    // Iterate through readyList until the next free node has its execute
436    // tick later than curTick or the end of readyList is reached
437    while (free_itr->execTick <= curTick() && free_itr != readyList.end()) {
438
439        // Get pointer to the node to be executed
440        graph_itr = depGraph.find(free_itr->seqNum);
441        assert(graph_itr != depGraph.end());
442        GraphNode* node_ptr = graph_itr->second;
443
444        // If there is a retryPkt send that else execute the load
445        if (retryPkt) {
446            // The retryPkt must be the request that was created by the
447            // first node in the readyList.
448            if (retryPkt->req->getReqInstSeqNum() != node_ptr->seqNum) {
449                panic("Retry packet's seqence number does not match "
450                      "the first node in the readyList.\n");
451            }
452            if (port.sendTimingReq(retryPkt)) {
453                ++numRetrySucceeded;
454                retryPkt = nullptr;
455            }
456        } else if (node_ptr->isLoad || node_ptr->isStore) {
457            // If there is no retryPkt, attempt to send a memory request in
458            // case of a load or store node. If the send fails, executeMemReq()
459            // returns a packet pointer, which we save in retryPkt. In case of
460            // a comp node we don't do anything and simply continue as if the
461            // execution of the comp node succedded.
462            retryPkt = executeMemReq(node_ptr);
463        }
464        // If the retryPkt or a new load/store node failed, we exit from here
465        // as a retry from cache will bring the control to execute(). The
466        // first node in readyList then, will be the failed node.
467        if (retryPkt) {
468            break;
469        }
470
471        // Proceed to remove dependencies for the successfully executed node.
472        // If it is a load which is not strictly ordered and we sent a
473        // request for it successfully, we do not yet mark any register
474        // dependencies complete. But as per dependency modelling we need
475        // to mark ROB dependencies of load and non load/store nodes which
476        // are based on successful sending of the load as complete.
477        if (node_ptr->isLoad && !node_ptr->isStrictlyOrdered()) {
478            // If execute succeeded mark its dependents as complete
479            DPRINTF(TraceCPUData, "Node seq. num %lli sent. Waking up "
480                    "dependents..\n", node_ptr->seqNum);
481
482            auto child_itr = (node_ptr->dependents).begin();
483            while (child_itr != (node_ptr->dependents).end()) {
484                // ROB dependency of a store on a load must not be removed
485                // after load is sent but after response is received
486                if (!(*child_itr)->isStore &&
487                    (*child_itr)->removeRobDep(node_ptr->seqNum)) {
488
489                    // Check if the child node has become dependency free
490                    if ((*child_itr)->numRobDep == 0 &&
491                        (*child_itr)->numRegDep == 0) {
492
493                        // Source dependencies are complete, check if
494                        // resources are available and issue
495                        checkAndIssue(*child_itr);
496                    }
497                    // Remove this child for the sent load and point to new
498                    // location of the element following the erased element
499                    child_itr = node_ptr->dependents.erase(child_itr);
500                } else {
501                    // This child is not dependency-free, point to the next
502                    // child
503                    child_itr++;
504                }
505            }
506        } else {
507            // If it is a strictly ordered load mark its dependents as complete
508            // as we do not send a request for this case. If it is a store or a
509            // comp node we also mark all its dependents complete.
510            DPRINTF(TraceCPUData, "Node seq. num %lli done. Waking"
511                    " up dependents..\n", node_ptr->seqNum);
512
513            for (auto child : node_ptr->dependents) {
514                // If the child node is dependency free removeDepOnInst()
515                // returns true.
516                if (child->removeDepOnInst(node_ptr->seqNum)) {
517                    // Source dependencies are complete, check if resources
518                    // are available and issue
519                    checkAndIssue(child);
520                }
521            }
522        }
523
524        // After executing the node, remove from readyList and delete node.
525        readyList.erase(free_itr);
526        // If it is a cacheable load which was sent, don't delete
527        // just yet.  Delete it in completeMemAccess() after the
528        // response is received. If it is an strictly ordered
529        // load, it was not sent and all dependencies were simply
530        // marked complete. Thus it is safe to delete it. For
531        // stores and non load/store nodes all dependencies were
532        // marked complete so it is safe to delete it.
533        if (!node_ptr->isLoad || node_ptr->isStrictlyOrdered()) {
534            // Release all resources occupied by the completed node
535            hwResource.release(node_ptr);
536            // clear the dynamically allocated set of dependents
537            (node_ptr->dependents).clear();
538            // delete node
539            delete node_ptr;
540            // remove from graph
541            depGraph.erase(graph_itr);
542        }
543        // Point to first node to continue to next iteration of while loop
544        free_itr = readyList.begin();
545    } // end of while loop
546
547    // Print readyList, sizes of queues and resource status after updating
548    if (DTRACE(TraceCPUData)) {
549        printReadyList();
550        DPRINTF(TraceCPUData, "Execute end occupancy:\n");
551        DPRINTFR(TraceCPUData, "\tdepGraph = %d, readyList = %d, "
552                "depFreeQueue = %d ,", depGraph.size(), readyList.size(),
553                depFreeQueue.size());
554        hwResource.printOccupancy();
555    }
556
557    if (retryPkt) {
558        DPRINTF(TraceCPUData, "Not scheduling an event as expecting a retry"
559                "event from the cache for seq. num %lli.\n",
560                retryPkt->req->getReqInstSeqNum());
561        return;
562    }
563    // If the size of the dependency graph is less than the dependency window
564    // then read from the trace file to populate the graph next time we are in
565    // execute.
566    if (depGraph.size() < windowSize && !traceComplete)
567        nextRead = true;
568
569    // If cache is not blocked, schedule an event for the first execTick in
570    // readyList else retry from cache will schedule the event. If the ready
571    // list is empty then check if the next pending node has resources
572    // available to issue. If yes, then schedule an event for the next cycle.
573    if (!readyList.empty()) {
574        Tick next_event_tick = std::max(readyList.begin()->execTick,
575                                        curTick());
576        DPRINTF(TraceCPUData, "Attempting to schedule @%lli.\n",
577                next_event_tick);
578        owner.schedDcacheNextEvent(next_event_tick);
579    } else if (readyList.empty() && !depFreeQueue.empty() &&
580                hwResource.isAvailable(depFreeQueue.front())) {
581        DPRINTF(TraceCPUData, "Attempting to schedule @%lli.\n",
582                owner.clockEdge(Cycles(1)));
583        owner.schedDcacheNextEvent(owner.clockEdge(Cycles(1)));
584    }
585
586    // If trace is completely read, readyList is empty and depGraph is empty,
587    // set execComplete to true
588    if (depGraph.empty() && readyList.empty() && traceComplete &&
589        !hwResource.awaitingResponse()) {
590        DPRINTF(TraceCPUData, "\tExecution Complete!\n");
591        execComplete = true;
592        dataLastTick = curTick();
593    }
594}
595
596PacketPtr
597TraceCPU::ElasticDataGen::executeMemReq(GraphNode* node_ptr)
598{
599
600    DPRINTF(TraceCPUData, "Executing memory request %lli (addr %d, pc %#x, "
601            "size %d, flags %d).\n", node_ptr->seqNum, node_ptr->addr,
602            node_ptr->pc, node_ptr->size, node_ptr->flags);
603
604    // If the request is strictly ordered, do not send it. Just return nullptr
605    // as if it was succesfully sent.
606    if (node_ptr->isStrictlyOrdered()) {
607        node_ptr->isLoad ? ++numSOLoads : ++numSOStores;
608        DPRINTF(TraceCPUData, "Skipping strictly ordered request %lli.\n",
609                node_ptr->seqNum);
610        return nullptr;
611    }
612
613    // Check if the request spans two cache lines as this condition triggers
614    // an assert fail in the L1 cache. If it does then truncate the size to
615    // access only until the end of that line and ignore the remainder. The
616    // stat counting this is useful to keep a check on how frequently this
617    // happens. If required the code could be revised to mimick splitting such
618    // a request into two.
619    unsigned blk_size = owner.cacheLineSize();
620    Addr blk_offset = (node_ptr->addr & (Addr)(blk_size - 1));
621    if (!(blk_offset + node_ptr->size <= blk_size)) {
622        node_ptr->size = blk_size - blk_offset;
623        ++numSplitReqs;
624    }
625
626    // Create a request and the packet containing request
627    Request* req = new Request(node_ptr->addr, node_ptr->size, node_ptr->flags,
628                               masterID, node_ptr->seqNum,
629                               ContextID(0), ThreadID(0));
630    req->setPC(node_ptr->pc);
631    PacketPtr pkt;
632    uint8_t* pkt_data = new uint8_t[req->getSize()];
633    if (node_ptr->isLoad) {
634        pkt = Packet::createRead(req);
635    } else {
636        pkt = Packet::createWrite(req);
637        memset(pkt_data, 0xA, req->getSize());
638    }
639    pkt->dataDynamic(pkt_data);
640
641    // Call MasterPort method to send a timing request for this packet
642    bool success = port.sendTimingReq(pkt);
643    ++numSendAttempted;
644
645    if (!success) {
646        // If it fails, return the packet to retry when a retry is signalled by
647        // the cache
648        ++numSendFailed;
649        DPRINTF(TraceCPUData, "Send failed. Saving packet for retry.\n");
650        return pkt;
651    } else {
652        // It is succeeds, return nullptr
653        ++numSendSucceeded;
654        return nullptr;
655    }
656}
657
658bool
659TraceCPU::ElasticDataGen::checkAndIssue(const GraphNode* node_ptr, bool first)
660{
661    // Assert the node is dependency-free
662    assert(node_ptr->numRobDep == 0 && node_ptr->numRegDep == 0);
663
664    // If this is the first attempt, print a debug message to indicate this.
665    if (first) {
666        DPRINTFR(TraceCPUData, "\t\tseq. num %lli(%s) with rob num %lli is now"
667            " dependency free.\n", node_ptr->seqNum,
668            node_ptr->isLoad ? "L" : (node_ptr->isStore ? "S" : "C"),
669            node_ptr->robNum);
670    }
671
672    // Check if resources are available to issue the specific node
673    if (hwResource.isAvailable(node_ptr)) {
674        // If resources are free only then add to readyList
675        DPRINTFR(TraceCPUData, "\t\tResources available for seq. num %lli. Adding"
676            " to readyList, occupying resources.\n", node_ptr->seqNum);
677        // Compute the execute tick by adding the compute delay for the node
678        // and add the ready node to the ready list
679        addToSortedReadyList(node_ptr->seqNum,
680                                owner.clockEdge() + node_ptr->compDelay);
681        // Account for the resources taken up by this issued node.
682        hwResource.occupy(node_ptr);
683        return true;
684
685    } else {
686        if (first) {
687            // Although dependencies are complete, resources are not available.
688            DPRINTFR(TraceCPUData, "\t\tResources unavailable for seq. num %lli."
689                " Adding to depFreeQueue.\n", node_ptr->seqNum);
690            depFreeQueue.push(node_ptr);
691        } else {
692            DPRINTFR(TraceCPUData, "\t\tResources unavailable for seq. num %lli. "
693                "Still pending issue.\n", node_ptr->seqNum);
694        }
695        return false;
696    }
697}
698
699void
700TraceCPU::ElasticDataGen::completeMemAccess(PacketPtr pkt)
701{
702    // Release the resources for this completed node.
703    if (pkt->isWrite()) {
704        // Consider store complete.
705        hwResource.releaseStoreBuffer();
706        // If it is a store response then do nothing since we do not model
707        // dependencies on store completion in the trace. But if we were
708        // blocking execution due to store buffer fullness, we need to schedule
709        // an event and attempt to progress.
710    } else {
711        // If it is a load response then release the dependents waiting on it.
712        // Get pointer to the completed load
713        auto graph_itr = depGraph.find(pkt->req->getReqInstSeqNum());
714        assert(graph_itr != depGraph.end());
715        GraphNode* node_ptr = graph_itr->second;
716
717        // Release resources occupied by the load
718        hwResource.release(node_ptr);
719
720        DPRINTF(TraceCPUData, "Load seq. num %lli response received. Waking up"
721                " dependents..\n", node_ptr->seqNum);
722
723        for (auto child : node_ptr->dependents) {
724            if (child->removeDepOnInst(node_ptr->seqNum)) {
725                checkAndIssue(child);
726            }
727        }
728
729        // clear the dynamically allocated set of dependents
730        (node_ptr->dependents).clear();
731        // delete node
732        delete node_ptr;
733        // remove from graph
734        depGraph.erase(graph_itr);
735    }
736
737    if (DTRACE(TraceCPUData)) {
738        printReadyList();
739    }
740
741    // If the size of the dependency graph is less than the dependency window
742    // then read from the trace file to populate the graph next time we are in
743    // execute.
744    if (depGraph.size() < windowSize && !traceComplete)
745        nextRead = true;
746
747    // If not waiting for retry, attempt to schedule next event
748    if (!retryPkt) {
749        // We might have new dep-free nodes in the list which will have execute
750        // tick greater than or equal to curTick. But a new dep-free node might
751        // have its execute tick earlier. Therefore, attempt to reschedule. It
752        // could happen that the readyList is empty and we got here via a
753        // last remaining response. So, either the trace is complete or there
754        // are pending nodes in the depFreeQueue. The checking is done in the
755        // execute() control flow, so schedule an event to go via that flow.
756        Tick next_event_tick = readyList.empty() ? owner.clockEdge(Cycles(1)) :
757            std::max(readyList.begin()->execTick, owner.clockEdge(Cycles(1)));
758        DPRINTF(TraceCPUData, "Attempting to schedule @%lli.\n",
759                next_event_tick);
760        owner.schedDcacheNextEvent(next_event_tick);
761    }
762}
763
764void
765TraceCPU::ElasticDataGen::addToSortedReadyList(NodeSeqNum seq_num,
766                                                    Tick exec_tick)
767{
768    ReadyNode ready_node;
769    ready_node.seqNum = seq_num;
770    ready_node.execTick = exec_tick;
771
772    // Iterator to readyList
773    auto itr = readyList.begin();
774
775    // If the readyList is empty, simply insert the new node at the beginning
776    // and return
777    if (itr == readyList.end()) {
778        readyList.insert(itr, ready_node);
779        maxReadyListSize = std::max<double>(readyList.size(),
780                                              maxReadyListSize.value());
781        return;
782    }
783
784    // If the new node has its execution tick equal to the first node in the
785    // list then go to the next node. If the first node in the list failed
786    // to execute, its position as the first is thus maintained.
787    if (retryPkt)
788        if (retryPkt->req->getReqInstSeqNum() == itr->seqNum)
789            itr++;
790
791    // Increment the iterator and compare the node pointed to by it to the new
792    // node till the position to insert the new node is found.
793    bool found = false;
794    while (!found && itr != readyList.end()) {
795        // If the execution tick of the new node is less than the node then
796        // this is the position to insert
797        if (exec_tick < itr->execTick)
798            found = true;
799        // If the execution tick of the new node is equal to the node then
800        // sort in ascending order of sequence numbers
801        else if (exec_tick == itr->execTick) {
802            // If the sequence number of the new node is less than the node
803            // then this is the position to insert
804            if (seq_num < itr->seqNum)
805                found = true;
806            // Else go to next node
807            else
808                itr++;
809        }
810        // If the execution tick of the new node is greater than the node then
811        // go to the next node
812        else
813            itr++;
814    }
815    readyList.insert(itr, ready_node);
816    // Update the stat for max size reached of the readyList
817    maxReadyListSize = std::max<double>(readyList.size(),
818                                          maxReadyListSize.value());
819}
820
821void
822TraceCPU::ElasticDataGen::printReadyList() {
823
824    auto itr = readyList.begin();
825    if (itr == readyList.end()) {
826        DPRINTF(TraceCPUData, "readyList is empty.\n");
827        return;
828    }
829    DPRINTF(TraceCPUData, "Printing readyList:\n");
830    while (itr != readyList.end()) {
831        auto graph_itr = depGraph.find(itr->seqNum);
832        GraphNode* node_ptr M5_VAR_USED = graph_itr->second;
833        DPRINTFR(TraceCPUData, "\t%lld(%s), %lld\n", itr->seqNum,
834            node_ptr->isLoad ? "L" : (node_ptr->isStore ? "S" : "C"),
835            itr->execTick);
836        itr++;
837    }
838}
839
840TraceCPU::ElasticDataGen::HardwareResource::HardwareResource(
841    uint16_t max_rob, uint16_t max_stores, uint16_t max_loads)
842  : sizeROB(max_rob),
843    sizeStoreBuffer(max_stores),
844    sizeLoadBuffer(max_loads),
845    oldestInFlightRobNum(UINT64_MAX),
846    numInFlightLoads(0),
847    numInFlightStores(0)
848{}
849
850void
851TraceCPU::ElasticDataGen::HardwareResource::occupy(const GraphNode* new_node)
852{
853    // Occupy ROB entry for the issued node
854    // Merely maintain the oldest node, i.e. numerically least robNum by saving
855    // it in the variable oldestInFLightRobNum.
856    inFlightNodes[new_node->seqNum] = new_node->robNum;
857    oldestInFlightRobNum = inFlightNodes.begin()->second;
858
859    // Occupy Load/Store Buffer entry for the issued node if applicable
860    if (new_node->isLoad) {
861        ++numInFlightLoads;
862    } else if (new_node->isStore) {
863        ++numInFlightStores;
864    } // else if it is a non load/store node, no buffer entry is occupied
865
866    printOccupancy();
867}
868
869void
870TraceCPU::ElasticDataGen::HardwareResource::release(const GraphNode* done_node)
871{
872    assert(!inFlightNodes.empty());
873    DPRINTFR(TraceCPUData, "\tClearing done seq. num %d from inFlightNodes..\n",
874        done_node->seqNum);
875
876    assert(inFlightNodes.find(done_node->seqNum) != inFlightNodes.end());
877    inFlightNodes.erase(done_node->seqNum);
878
879    if (inFlightNodes.empty()) {
880        // If we delete the only in-flight node and then the
881        // oldestInFlightRobNum is set to it's initialized (max) value.
882        oldestInFlightRobNum = UINT64_MAX;
883    } else {
884        // Set the oldest in-flight node rob number equal to the first node in
885        // the inFlightNodes since that will have the numerically least value.
886        oldestInFlightRobNum = inFlightNodes.begin()->second;
887    }
888
889    DPRINTFR(TraceCPUData, "\tCleared. inFlightNodes.size() = %d, "
890        "oldestInFlightRobNum = %d\n", inFlightNodes.size(),
891        oldestInFlightRobNum);
892
893    // A store is considered complete when a request is sent, thus ROB entry is
894    // freed. But it occupies an entry in the Store Buffer until its response
895    // is received. A load is considered complete when a response is received,
896    // thus both ROB and Load Buffer entries can be released.
897    if (done_node->isLoad) {
898        assert(numInFlightLoads != 0);
899        --numInFlightLoads;
900    }
901    // For normal writes, we send the requests out and clear a store buffer
902    // entry on response. For writes which are strictly ordered, for e.g.
903    // writes to device registers, we do that within release() which is called
904    // when node is executed and taken off from readyList.
905    if (done_node->isStore && done_node->isStrictlyOrdered()) {
906        releaseStoreBuffer();
907    }
908}
909
910void
911TraceCPU::ElasticDataGen::HardwareResource::releaseStoreBuffer()
912{
913    assert(numInFlightStores != 0);
914    --numInFlightStores;
915}
916
917bool
918TraceCPU::ElasticDataGen::HardwareResource::isAvailable(
919    const GraphNode* new_node) const
920{
921    uint16_t num_in_flight_nodes;
922    if (inFlightNodes.empty()) {
923        num_in_flight_nodes = 0;
924        DPRINTFR(TraceCPUData, "\t\tChecking resources to issue seq. num %lli:"
925            " #in-flight nodes = 0", new_node->seqNum);
926    } else if (new_node->robNum > oldestInFlightRobNum) {
927        // This is the intuitive case where new dep-free node is younger
928        // instruction than the oldest instruction in-flight. Thus we make sure
929        // in_flight_nodes does not overflow.
930        num_in_flight_nodes = new_node->robNum - oldestInFlightRobNum;
931        DPRINTFR(TraceCPUData, "\t\tChecking resources to issue seq. num %lli:"
932            " #in-flight nodes = %d - %d =  %d", new_node->seqNum,
933             new_node->robNum, oldestInFlightRobNum, num_in_flight_nodes);
934    } else {
935        // This is the case where an instruction older than the oldest in-
936        // flight instruction becomes dep-free. Thus we must have already
937        // accounted for the entry in ROB for this new dep-free node.
938        // Immediately after this check returns true, oldestInFlightRobNum will
939        // be updated in occupy(). We simply let this node issue now.
940        num_in_flight_nodes = 0;
941        DPRINTFR(TraceCPUData, "\t\tChecking resources to issue seq. num %lli:"
942            " new oldestInFlightRobNum = %d, #in-flight nodes ignored",
943            new_node->seqNum, new_node->robNum);
944    }
945    DPRINTFR(TraceCPUData, ", LQ = %d/%d, SQ  = %d/%d.\n",
946        numInFlightLoads, sizeLoadBuffer,
947        numInFlightStores, sizeStoreBuffer);
948    // Check if resources are available to issue the specific node
949    if (num_in_flight_nodes >= sizeROB) {
950        return false;
951    }
952    if (new_node->isLoad && numInFlightLoads >= sizeLoadBuffer) {
953        return false;
954    }
955    if (new_node->isStore && numInFlightStores >= sizeStoreBuffer) {
956        return false;
957    }
958    return true;
959}
960
961bool
962TraceCPU::ElasticDataGen::HardwareResource::awaitingResponse() const {
963    // Return true if there is at least one read or write request in flight
964    return (numInFlightStores != 0 || numInFlightLoads != 0);
965}
966
967void
968TraceCPU::ElasticDataGen::HardwareResource::printOccupancy() {
969    DPRINTFR(TraceCPUData, "oldestInFlightRobNum = %d, "
970            "LQ = %d/%d, SQ  = %d/%d.\n",
971            oldestInFlightRobNum,
972            numInFlightLoads, sizeLoadBuffer,
973            numInFlightStores, sizeStoreBuffer);
974}
975
976void
977TraceCPU::FixedRetryGen::regStats()
978{
979    using namespace Stats;
980
981    numSendAttempted
982    .name(name() + ".numSendAttempted")
983    .desc("Number of first attempts to send a request")
984    ;
985
986    numSendSucceeded
987    .name(name() + ".numSendSucceeded")
988    .desc("Number of successful first attempts")
989    ;
990
991    numSendFailed
992    .name(name() + ".numSendFailed")
993    .desc("Number of failed first attempts")
994    ;
995
996    numRetrySucceeded
997    .name(name() + ".numRetrySucceeded")
998    .desc("Number of successful retries")
999    ;
1000
1001    instLastTick
1002    .name(name() + ".instLastTick")
1003    .desc("Last tick simulated from the fixed inst trace")
1004    ;
1005}
1006
1007Tick
1008TraceCPU::FixedRetryGen::init()
1009{
1010    DPRINTF(TraceCPUInst, "Initializing instruction fetch request generator"
1011            " IcacheGen: fixed issue with retry.\n");
1012
1013    if (nextExecute()) {
1014        DPRINTF(TraceCPUInst, "\tFirst tick = %d.\n", currElement.tick);
1015        return currElement.tick;
1016    } else {
1017        panic("Read of first message in the trace failed.\n");
1018        return MaxTick;
1019    }
1020}
1021
1022bool
1023TraceCPU::FixedRetryGen::tryNext()
1024{
1025    // If there is a retry packet, try to send it
1026    if (retryPkt) {
1027
1028        DPRINTF(TraceCPUInst, "Trying to send retry packet.\n");
1029
1030        if (!port.sendTimingReq(retryPkt)) {
1031            // Still blocked! This should never occur.
1032            DPRINTF(TraceCPUInst, "Retry packet sending failed.\n");
1033            return false;
1034        }
1035        ++numRetrySucceeded;
1036    } else {
1037
1038        DPRINTF(TraceCPUInst, "Trying to send packet for currElement.\n");
1039
1040        // try sending current element
1041        assert(currElement.isValid());
1042
1043        ++numSendAttempted;
1044
1045        if (!send(currElement.addr, currElement.blocksize,
1046                    currElement.cmd, currElement.flags, currElement.pc)) {
1047            DPRINTF(TraceCPUInst, "currElement sending failed.\n");
1048            ++numSendFailed;
1049            // return false to indicate not to schedule next event
1050            return false;
1051        } else {
1052            ++numSendSucceeded;
1053        }
1054    }
1055    // If packet was sent successfully, either retryPkt or currElement, return
1056    // true to indicate to schedule event at current Tick plus delta. If packet
1057    // was sent successfully and there is no next packet to send, return false.
1058    DPRINTF(TraceCPUInst, "Packet sent successfully, trying to read next "
1059        "element.\n");
1060    retryPkt = nullptr;
1061    // Read next element into currElement, currElement gets cleared so save the
1062    // tick to calculate delta
1063    Tick last_tick = currElement.tick;
1064    if (nextExecute()) {
1065        assert(currElement.tick >= last_tick);
1066        delta = currElement.tick - last_tick;
1067    }
1068    return !traceComplete;
1069}
1070
1071void
1072TraceCPU::FixedRetryGen::exit()
1073{
1074    trace.reset();
1075}
1076
1077bool
1078TraceCPU::FixedRetryGen::nextExecute()
1079{
1080    if (traceComplete)
1081        // We are at the end of the file, thus we have no more messages.
1082        // Return false.
1083        return false;
1084
1085
1086    //Reset the currElement to the default values
1087    currElement.clear();
1088
1089    // Read the next line to get the next message. If that fails then end of
1090    // trace has been reached and traceComplete needs to be set in addition
1091    // to returning false. If successful then next message is in currElement.
1092    if (!trace.read(&currElement)) {
1093        traceComplete = true;
1094        instLastTick = curTick();
1095        return false;
1096    }
1097
1098    DPRINTF(TraceCPUInst, "inst fetch: %c addr %d pc %#x size %d tick %d\n",
1099            currElement.cmd.isRead() ? 'r' : 'w',
1100            currElement.addr,
1101            currElement.pc,
1102            currElement.blocksize,
1103            currElement.tick);
1104
1105    return true;
1106}
1107
1108bool
1109TraceCPU::FixedRetryGen::send(Addr addr, unsigned size, const MemCmd& cmd,
1110              Request::FlagsType flags, Addr pc)
1111{
1112
1113    // Create new request
1114    Request* req = new Request(addr, size, flags, masterID);
1115    req->setPC(pc);
1116
1117    // If this is not done it triggers assert in L1 cache for invalid contextId
1118    req->setThreadContext(ContextID(0), ThreadID(0));
1119
1120    // Embed it in a packet
1121    PacketPtr pkt = new Packet(req, cmd);
1122
1123    uint8_t* pkt_data = new uint8_t[req->getSize()];
1124    pkt->dataDynamic(pkt_data);
1125
1126    if (cmd.isWrite()) {
1127        memset(pkt_data, 0xA, req->getSize());
1128    }
1129
1130    // Call MasterPort method to send a timing request for this packet
1131    bool success = port.sendTimingReq(pkt);
1132    if (!success) {
1133        // If it fails, save the packet to retry when a retry is signalled by
1134        // the cache
1135        retryPkt = pkt;
1136    }
1137    return success;
1138}
1139
1140void
1141TraceCPU::icacheRetryRecvd()
1142{
1143    // Schedule an event to go through the control flow in the same tick as
1144    // retry is received
1145    DPRINTF(TraceCPUInst, "Icache retry received. Scheduling next IcacheGen"
1146            " event @%lli.\n", curTick());
1147    schedule(icacheNextEvent, curTick());
1148}
1149
1150void
1151TraceCPU::dcacheRetryRecvd()
1152{
1153    // Schedule an event to go through the execute flow in the same tick as
1154    // retry is received
1155    DPRINTF(TraceCPUData, "Dcache retry received. Scheduling next DcacheGen"
1156            " event @%lli.\n", curTick());
1157    schedule(dcacheNextEvent, curTick());
1158}
1159
1160void
1161TraceCPU::schedDcacheNextEvent(Tick when)
1162{
1163    if (!dcacheNextEvent.scheduled()) {
1164        DPRINTF(TraceCPUData, "Scheduling next DcacheGen event at %lli.\n",
1165                when);
1166        schedule(dcacheNextEvent, when);
1167        ++numSchedDcacheEvent;
1168    } else if (when < dcacheNextEvent.when()) {
1169        DPRINTF(TraceCPUData, "Re-scheduling next dcache event from %lli"
1170                " to %lli.\n", dcacheNextEvent.when(), when);
1171        reschedule(dcacheNextEvent, when);
1172    }
1173
1174}
1175
1176bool
1177TraceCPU::IcachePort::recvTimingResp(PacketPtr pkt)
1178{
1179    // All responses on the instruction fetch side are ignored. Simply delete
1180    // the request and packet to free allocated memory
1181    delete pkt->req;
1182    delete pkt;
1183
1184    return true;
1185}
1186
1187void
1188TraceCPU::IcachePort::recvReqRetry()
1189{
1190    owner->icacheRetryRecvd();
1191}
1192
1193void
1194TraceCPU::dcacheRecvTimingResp(PacketPtr pkt)
1195{
1196    DPRINTF(TraceCPUData, "Received timing response from Dcache.\n");
1197    dcacheGen.completeMemAccess(pkt);
1198}
1199
1200bool
1201TraceCPU::DcachePort::recvTimingResp(PacketPtr pkt)
1202{
1203    // Handle the responses for data memory requests which is done inside the
1204    // elastic data generator
1205    owner->dcacheRecvTimingResp(pkt);
1206    // After processing the response delete the request and packet to free
1207    // memory
1208    delete pkt->req;
1209    delete pkt;
1210
1211    return true;
1212}
1213
1214void
1215TraceCPU::DcachePort::recvReqRetry()
1216{
1217    owner->dcacheRetryRecvd();
1218}
1219
1220TraceCPU::ElasticDataGen::InputStream::InputStream(const std::string& filename)
1221    : trace(filename),
1222      microOpCount(0)
1223{
1224    // Create a protobuf message for the header and read it from the stream
1225    ProtoMessage::InstDepRecordHeader header_msg;
1226    if (!trace.read(header_msg)) {
1227        panic("Failed to read packet header from %s\n", filename);
1228
1229        if (header_msg.tick_freq() != SimClock::Frequency) {
1230            panic("Trace %s was recorded with a different tick frequency %d\n",
1231                  header_msg.tick_freq());
1232        }
1233    } else {
1234        // Assign window size equal to the field in the trace that was recorded
1235        // when the data dependency trace was captured in the o3cpu model
1236        windowSize = header_msg.window_size();
1237    }
1238}
1239
1240void
1241TraceCPU::ElasticDataGen::InputStream::reset()
1242{
1243    trace.reset();
1244}
1245
1246bool
1247TraceCPU::ElasticDataGen::InputStream::read(GraphNode* element)
1248{
1249    ProtoMessage::InstDepRecord pkt_msg;
1250    if (trace.read(pkt_msg)) {
1251        // Required fields
1252        element->seqNum = pkt_msg.seq_num();
1253        element->isLoad = pkt_msg.load();
1254        element->isStore = pkt_msg.store();
1255        element->compDelay = pkt_msg.comp_delay();
1256
1257        // Repeated field robDepList
1258        element->clearRobDep();
1259        assert((pkt_msg.rob_dep()).size() <= element->maxRobDep);
1260        for (int i = 0; i < (pkt_msg.rob_dep()).size(); i++) {
1261            element->robDep[element->numRobDep] = pkt_msg.rob_dep(i);
1262            element->numRobDep += 1;
1263        }
1264
1265        // Repeated field
1266        element->clearRegDep();
1267        assert((pkt_msg.reg_dep()).size() <= TheISA::MaxInstSrcRegs);
1268        for (int i = 0; i < (pkt_msg.reg_dep()).size(); i++) {
1269            // There is a possibility that an instruction has both, a register
1270            // and order dependency on an instruction. In such a case, the
1271            // register dependency is omitted
1272            bool duplicate = false;
1273            for (int j = 0; j < element->numRobDep; j++) {
1274                duplicate |= (pkt_msg.reg_dep(i) == element->robDep[j]);
1275            }
1276            if (!duplicate) {
1277                element->regDep[element->numRegDep] = pkt_msg.reg_dep(i);
1278                element->numRegDep += 1;
1279            }
1280        }
1281
1282        // Optional fields
1283        if (pkt_msg.has_addr())
1284            element->addr = pkt_msg.addr();
1285        else
1286            element->addr = 0;
1287
1288        if (pkt_msg.has_size())
1289            element->size = pkt_msg.size();
1290        else
1291            element->size = 0;
1292
1293        if (pkt_msg.has_flags())
1294            element->flags = pkt_msg.flags();
1295        else
1296            element->flags = 0;
1297
1298        if (pkt_msg.has_pc())
1299            element->pc = pkt_msg.pc();
1300        else
1301            element->pc = 0;
1302
1303        // ROB occupancy number
1304        ++microOpCount;
1305        if (pkt_msg.has_weight()) {
1306            microOpCount += pkt_msg.weight();
1307        }
1308        element->robNum = microOpCount;
1309        return true;
1310    }
1311
1312    // We have reached the end of the file
1313    return false;
1314}
1315
1316bool
1317TraceCPU::ElasticDataGen::GraphNode::removeRegDep(NodeSeqNum reg_dep)
1318{
1319    for (auto& own_reg_dep : regDep) {
1320        if (own_reg_dep == reg_dep) {
1321            // If register dependency is found, make it zero and return true
1322            own_reg_dep = 0;
1323            --numRegDep;
1324            assert(numRegDep >= 0);
1325            DPRINTFR(TraceCPUData, "\tFor %lli: Marking register dependency %lli "
1326                    "done.\n", seqNum, reg_dep);
1327            return true;
1328        }
1329    }
1330
1331    // Return false if the dependency is not found
1332    return false;
1333}
1334
1335bool
1336TraceCPU::ElasticDataGen::GraphNode::removeRobDep(NodeSeqNum rob_dep)
1337{
1338    for (auto& own_rob_dep : robDep) {
1339        if (own_rob_dep == rob_dep) {
1340            // If the rob dependency is found, make it zero and return true
1341            own_rob_dep = 0;
1342            --numRobDep;
1343            assert(numRobDep >= 0);
1344            DPRINTFR(TraceCPUData, "\tFor %lli: Marking ROB dependency %lli "
1345                "done.\n", seqNum, rob_dep);
1346            return true;
1347        }
1348    }
1349    return false;
1350}
1351
1352void
1353TraceCPU::ElasticDataGen::GraphNode::clearRegDep() {
1354    for (auto& own_reg_dep : regDep) {
1355        own_reg_dep = 0;
1356    }
1357    numRegDep = 0;
1358}
1359
1360void
1361TraceCPU::ElasticDataGen::GraphNode::clearRobDep() {
1362    for (auto& own_rob_dep : robDep) {
1363        own_rob_dep = 0;
1364    }
1365    numRobDep = 0;
1366}
1367
1368bool
1369TraceCPU::ElasticDataGen::GraphNode::removeDepOnInst(NodeSeqNum done_seq_num)
1370{
1371    // If it is an rob dependency then remove it
1372    if (!removeRobDep(done_seq_num)) {
1373        // If it is not an rob dependency then it must be a register dependency
1374        // If the register dependency is not found, it violates an assumption
1375        // and must be caught by assert.
1376        bool regdep_found M5_VAR_USED = removeRegDep(done_seq_num);
1377        assert(regdep_found);
1378    }
1379    // Return true if the node is dependency free
1380    return (numRobDep == 0 && numRegDep == 0);
1381}
1382
1383void
1384TraceCPU::ElasticDataGen::GraphNode::writeElementAsTrace() const
1385{
1386    DPRINTFR(TraceCPUData, "%lli", seqNum);
1387    DPRINTFR(TraceCPUData, ",%s", (isLoad ? "True" : "False"));
1388    DPRINTFR(TraceCPUData, ",%s", (isStore ? "True" : "False"));
1389    if (isLoad || isStore) {
1390        DPRINTFR(TraceCPUData, ",%i", addr);
1391        DPRINTFR(TraceCPUData, ",%i", size);
1392        DPRINTFR(TraceCPUData, ",%i", flags);
1393    }
1394    DPRINTFR(TraceCPUData, ",%lli", compDelay);
1395    int i = 0;
1396    DPRINTFR(TraceCPUData, "robDep:");
1397    while (robDep[i] != 0) {
1398        DPRINTFR(TraceCPUData, ",%lli", robDep[i]);
1399        i++;
1400    }
1401    i = 0;
1402    DPRINTFR(TraceCPUData, "regDep:");
1403    while (regDep[i] != 0) {
1404        DPRINTFR(TraceCPUData, ",%lli", regDep[i]);
1405        i++;
1406    }
1407    auto child_itr = dependents.begin();
1408    DPRINTFR(TraceCPUData, "dependents:");
1409    while (child_itr != dependents.end()) {
1410        DPRINTFR(TraceCPUData, ":%lli", (*child_itr)->seqNum);
1411        child_itr++;
1412    }
1413
1414    DPRINTFR(TraceCPUData, "\n");
1415}
1416
1417TraceCPU::FixedRetryGen::InputStream::InputStream(const std::string& filename)
1418    : trace(filename)
1419{
1420    // Create a protobuf message for the header and read it from the stream
1421    ProtoMessage::PacketHeader header_msg;
1422    if (!trace.read(header_msg)) {
1423        panic("Failed to read packet header from %s\n", filename);
1424
1425        if (header_msg.tick_freq() != SimClock::Frequency) {
1426            panic("Trace %s was recorded with a different tick frequency %d\n",
1427                  header_msg.tick_freq());
1428        }
1429    }
1430}
1431
1432void
1433TraceCPU::FixedRetryGen::InputStream::reset()
1434{
1435    trace.reset();
1436}
1437
1438bool
1439TraceCPU::FixedRetryGen::InputStream::read(TraceElement* element)
1440{
1441    ProtoMessage::Packet pkt_msg;
1442    if (trace.read(pkt_msg)) {
1443        element->cmd = pkt_msg.cmd();
1444        element->addr = pkt_msg.addr();
1445        element->blocksize = pkt_msg.size();
1446        element->tick = pkt_msg.tick();
1447        element->flags = pkt_msg.has_flags() ? pkt_msg.flags() : 0;
1448        element->pc = pkt_msg.has_pc() ? pkt_msg.pc() : 0;
1449        return true;
1450    }
1451
1452    // We have reached the end of the file
1453    return false;
1454}
1455