2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "base/loader/symtab.hh"
35 #include "base/timebuf.hh"
36 #include "cpu/checker/cpu.hh"
37 #include "cpu/exetrace.hh"
38 #include "cpu/o3/commit.hh"
39 #include "cpu/o3/thread_state.hh"
44 DefaultCommit<Impl>::TrapEvent::TrapEvent(DefaultCommit<Impl> *_commit,
46 : Event(&mainEventQueue, CPU_Tick_Pri), commit(_commit), tid(_tid)
48 this->setFlags(Event::AutoDelete);
53 DefaultCommit<Impl>::TrapEvent::process()
55 // This will get reset by commit if it was switched out at the
56 // time of this event processing.
57 commit->trapSquash[tid] = true;
62 DefaultCommit<Impl>::TrapEvent::description()
68 DefaultCommit<Impl>::DefaultCommit(Params *params)
70 iewToCommitDelay(params->iewToCommitDelay),
71 commitToIEWDelay(params->commitToIEWDelay),
72 renameToROBDelay(params->renameToROBDelay),
73 fetchToCommitDelay(params->commitToFetchDelay),
74 renameWidth(params->renameWidth),
75 iewWidth(params->executeWidth),
76 commitWidth(params->commitWidth),
77 numThreads(params->numberOfThreads),
79 trapLatency(params->trapLatency),
80 fetchTrapLatency(params->fetchTrapLatency)
83 _nextStatus = Inactive;
84 string policy = params->smtCommitPolicy;
86 //Convert string to lowercase
87 std::transform(policy.begin(), policy.end(), policy.begin(),
88 (int(*)(int)) tolower);
90 //Assign commit policy
91 if (policy == "aggressive"){
92 commitPolicy = Aggressive;
94 DPRINTF(Commit,"Commit Policy set to Aggressive.");
95 } else if (policy == "roundrobin"){
96 commitPolicy = RoundRobin;
98 //Set-Up Priority List
99 for (int tid=0; tid < numThreads; tid++) {
100 priority_list.push_back(tid);
103 DPRINTF(Commit,"Commit Policy set to Round Robin.");
104 } else if (policy == "oldestready"){
105 commitPolicy = OldestReady;
107 DPRINTF(Commit,"Commit Policy set to Oldest Ready.");
109 assert(0 && "Invalid SMT Commit Policy. Options Are: {Aggressive,"
110 "RoundRobin,OldestReady}");
113 for (int i=0; i < numThreads; i++) {
114 commitStatus[i] = Idle;
115 changedROBNumEntries[i] = false;
116 trapSquash[i] = false;
124 template <class Impl>
126 DefaultCommit<Impl>::name() const
128 return cpu->name() + ".commit";
131 template <class Impl>
133 DefaultCommit<Impl>::regStats()
135 using namespace Stats;
137 .name(name() + ".commitCommittedInsts")
138 .desc("The number of committed instructions")
139 .prereq(commitCommittedInsts);
141 .name(name() + ".commitSquashedInsts")
142 .desc("The number of squashed insts skipped by commit")
143 .prereq(commitSquashedInsts);
145 .name(name() + ".commitSquashEvents")
146 .desc("The number of times commit is told to squash")
147 .prereq(commitSquashEvents);
149 .name(name() + ".commitNonSpecStalls")
150 .desc("The number of times commit has been forced to stall to "
151 "communicate backwards")
152 .prereq(commitNonSpecStalls);
154 .name(name() + ".branchMispredicts")
155 .desc("The number of times a branch was mispredicted")
156 .prereq(branchMispredicts);
158 .init(0,commitWidth,1)
159 .name(name() + ".COM:committed_per_cycle")
160 .desc("Number of insts commited each cycle")
165 .init(cpu->number_of_threads)
166 .name(name() + ".COM:count")
167 .desc("Number of instructions committed")
172 .init(cpu->number_of_threads)
173 .name(name() + ".COM:swp_count")
174 .desc("Number of s/w prefetches committed")
179 .init(cpu->number_of_threads)
180 .name(name() + ".COM:refs")
181 .desc("Number of memory references committed")
186 .init(cpu->number_of_threads)
187 .name(name() + ".COM:loads")
188 .desc("Number of loads committed")
193 .init(cpu->number_of_threads)
194 .name(name() + ".COM:membars")
195 .desc("Number of memory barriers committed")
200 .init(cpu->number_of_threads)
201 .name(name() + ".COM:branches")
202 .desc("Number of branches committed")
207 // Commit-Eligible instructions...
209 // -> The number of instructions eligible to commit in those
210 // cycles where we reached our commit BW limit (less the number
211 // actually committed)
213 // -> The average value is computed over ALL CYCLES... not just
214 // the BW limited cycles
216 // -> The standard deviation is computed only over cycles where
217 // we reached the BW limit
220 .init(cpu->number_of_threads)
221 .name(name() + ".COM:bw_limited")
222 .desc("number of insts not committed due to BW limits")
226 commitEligibleSamples
227 .name(name() + ".COM:bw_lim_events")
228 .desc("number cycles where commit BW limit reached")
232 template <class Impl>
234 DefaultCommit<Impl>::setCPU(FullCPU *cpu_ptr)
236 DPRINTF(Commit, "Commit: Setting CPU pointer.\n");
239 // Commit must broadcast the number of free entries it has at the start of
240 // the simulation, so it starts as active.
241 cpu->activateStage(FullCPU::CommitIdx);
243 trapLatency = cpu->cycles(trapLatency);
244 fetchTrapLatency = cpu->cycles(fetchTrapLatency);
247 template <class Impl>
249 DefaultCommit<Impl>::setThreads(vector<Thread *> &threads)
254 template <class Impl>
256 DefaultCommit<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
258 DPRINTF(Commit, "Commit: Setting time buffer pointer.\n");
261 // Setup wire to send information back to IEW.
262 toIEW = timeBuffer->getWire(0);
264 // Setup wire to read data from IEW (for the ROB).
265 robInfoFromIEW = timeBuffer->getWire(-iewToCommitDelay);
268 template <class Impl>
270 DefaultCommit<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
272 DPRINTF(Commit, "Commit: Setting fetch queue pointer.\n");
275 // Setup wire to get instructions from rename (for the ROB).
276 fromFetch = fetchQueue->getWire(-fetchToCommitDelay);
279 template <class Impl>
281 DefaultCommit<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
283 DPRINTF(Commit, "Commit: Setting rename queue pointer.\n");
284 renameQueue = rq_ptr;
286 // Setup wire to get instructions from rename (for the ROB).
287 fromRename = renameQueue->getWire(-renameToROBDelay);
290 template <class Impl>
292 DefaultCommit<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
294 DPRINTF(Commit, "Commit: Setting IEW queue pointer.\n");
297 // Setup wire to get instructions from IEW.
298 fromIEW = iewQueue->getWire(-iewToCommitDelay);
301 template <class Impl>
303 DefaultCommit<Impl>::setFetchStage(Fetch *fetch_stage)
305 fetchStage = fetch_stage;
308 template <class Impl>
310 DefaultCommit<Impl>::setIEWStage(IEW *iew_stage)
312 iewStage = iew_stage;
317 DefaultCommit<Impl>::setActiveThreads(list<unsigned> *at_ptr)
319 DPRINTF(Commit, "Commit: Setting active threads list pointer.\n");
320 activeThreads = at_ptr;
323 template <class Impl>
325 DefaultCommit<Impl>::setRenameMap(RenameMap rm_ptr[])
327 DPRINTF(Commit, "Setting rename map pointers.\n");
329 for (int i=0; i < numThreads; i++) {
330 renameMap[i] = &rm_ptr[i];
334 template <class Impl>
336 DefaultCommit<Impl>::setROB(ROB *rob_ptr)
338 DPRINTF(Commit, "Commit: Setting ROB pointer.\n");
342 template <class Impl>
344 DefaultCommit<Impl>::initStage()
346 rob->setActiveThreads(activeThreads);
349 // Broadcast the number of free entries.
350 for (int i=0; i < numThreads; i++) {
351 toIEW->commitInfo[i].usedROB = true;
352 toIEW->commitInfo[i].freeROBEntries = rob->numFreeEntries(i);
355 cpu->activityThisCycle();
358 template <class Impl>
360 DefaultCommit<Impl>::switchOut()
362 switchPending = true;
365 template <class Impl>
367 DefaultCommit<Impl>::doSwitchOut()
370 switchPending = false;
374 template <class Impl>
376 DefaultCommit<Impl>::takeOverFrom()
380 _nextStatus = Inactive;
381 for (int i=0; i < numThreads; i++) {
382 commitStatus[i] = Idle;
383 changedROBNumEntries[i] = false;
384 trapSquash[i] = false;
391 template <class Impl>
393 DefaultCommit<Impl>::updateStatus()
395 // reset ROB changed variable
396 list<unsigned>::iterator threads = (*activeThreads).begin();
397 while (threads != (*activeThreads).end()) {
398 unsigned tid = *threads++;
399 changedROBNumEntries[tid] = false;
401 // Also check if any of the threads has a trap pending
402 if (commitStatus[tid] == TrapPending ||
403 commitStatus[tid] == FetchTrapPending) {
404 _nextStatus = Active;
408 if (_nextStatus == Inactive && _status == Active) {
409 DPRINTF(Activity, "Deactivating stage.\n");
410 cpu->deactivateStage(FullCPU::CommitIdx);
411 } else if (_nextStatus == Active && _status == Inactive) {
412 DPRINTF(Activity, "Activating stage.\n");
413 cpu->activateStage(FullCPU::CommitIdx);
416 _status = _nextStatus;
419 template <class Impl>
421 DefaultCommit<Impl>::setNextStatus()
425 list<unsigned>::iterator threads = (*activeThreads).begin();
427 while (threads != (*activeThreads).end()) {
428 unsigned tid = *threads++;
430 if (commitStatus[tid] == ROBSquashing) {
435 assert(squashes == squashCounter);
437 // If commit is currently squashing, then it will have activity for the
438 // next cycle. Set its next status as active.
440 _nextStatus = Active;
444 template <class Impl>
446 DefaultCommit<Impl>::changedROBEntries()
448 list<unsigned>::iterator threads = (*activeThreads).begin();
450 while (threads != (*activeThreads).end()) {
451 unsigned tid = *threads++;
453 if (changedROBNumEntries[tid]) {
461 template <class Impl>
463 DefaultCommit<Impl>::numROBFreeEntries(unsigned tid)
465 return rob->numFreeEntries(tid);
468 template <class Impl>
470 DefaultCommit<Impl>::generateTrapEvent(unsigned tid)
472 DPRINTF(Commit, "Generating trap event for [tid:%i]\n", tid);
474 TrapEvent *trap = new TrapEvent(this, tid);
476 trap->schedule(curTick + trapLatency);
478 thread[tid]->trapPending = true;
481 template <class Impl>
483 DefaultCommit<Impl>::generateXCEvent(unsigned tid)
485 DPRINTF(Commit, "Generating XC squash event for [tid:%i]\n", tid);
487 xcSquash[tid] = true;
490 template <class Impl>
492 DefaultCommit<Impl>::squashAll(unsigned tid)
494 // If we want to include the squashing instruction in the squash,
495 // then use one older sequence number.
496 // Hopefully this doesn't mess things up. Basically I want to squash
497 // all instructions of this thread.
498 InstSeqNum squashed_inst = rob->isEmpty() ?
499 0 : rob->readHeadInst(tid)->seqNum - 1;;
501 // All younger instructions will be squashed. Set the sequence
502 // number as the youngest instruction in the ROB (0 in this case.
503 // Hopefully nothing breaks.)
504 youngestSeqNum[tid] = 0;
506 rob->squash(squashed_inst, tid);
507 changedROBNumEntries[tid] = true;
509 // Send back the sequence number of the squashed instruction.
510 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
512 // Send back the squash signal to tell stages that they should
514 toIEW->commitInfo[tid].squash = true;
516 // Send back the rob squashing signal so other stages know that
517 // the ROB is in the process of squashing.
518 toIEW->commitInfo[tid].robSquashing = true;
520 toIEW->commitInfo[tid].branchMispredict = false;
522 toIEW->commitInfo[tid].nextPC = PC[tid];
525 template <class Impl>
527 DefaultCommit<Impl>::squashFromTrap(unsigned tid)
531 DPRINTF(Commit, "Squashing from trap, restarting at PC %#x\n", PC[tid]);
533 thread[tid]->trapPending = false;
534 thread[tid]->inSyscall = false;
536 trapSquash[tid] = false;
538 commitStatus[tid] = ROBSquashing;
539 cpu->activityThisCycle();
544 template <class Impl>
546 DefaultCommit<Impl>::squashFromXC(unsigned tid)
550 DPRINTF(Commit, "Squashing from XC, restarting at PC %#x\n", PC[tid]);
552 thread[tid]->inSyscall = false;
553 assert(!thread[tid]->trapPending);
555 commitStatus[tid] = ROBSquashing;
556 cpu->activityThisCycle();
558 xcSquash[tid] = false;
563 template <class Impl>
565 DefaultCommit<Impl>::tick()
567 wroteToTimeBuffer = false;
568 _nextStatus = Inactive;
570 if (switchPending && rob->isEmpty() && !iewStage->hasStoresToWB()) {
571 cpu->signalSwitched();
575 list<unsigned>::iterator threads = (*activeThreads).begin();
577 // Check if any of the threads are done squashing. Change the
578 // status if they are done.
579 while (threads != (*activeThreads).end()) {
580 unsigned tid = *threads++;
582 if (commitStatus[tid] == ROBSquashing) {
584 if (rob->isDoneSquashing(tid)) {
585 commitStatus[tid] = Running;
588 DPRINTF(Commit,"[tid:%u]: Still Squashing, cannot commit any"
589 "insts this cycle.\n", tid);
596 markCompletedInsts();
598 threads = (*activeThreads).begin();
600 while (threads != (*activeThreads).end()) {
601 unsigned tid = *threads++;
603 if (!rob->isEmpty(tid) && rob->readHeadInst(tid)->readyToCommit()) {
604 // The ROB has more instructions it can commit. Its next status
606 _nextStatus = Active;
608 DynInstPtr inst = rob->readHeadInst(tid);
610 DPRINTF(Commit,"[tid:%i]: Instruction [sn:%lli] PC %#x is head of"
611 " ROB and ready to commit\n",
612 tid, inst->seqNum, inst->readPC());
614 } else if (!rob->isEmpty(tid)) {
615 DynInstPtr inst = rob->readHeadInst(tid);
617 DPRINTF(Commit,"[tid:%i]: Can't commit, Instruction [sn:%lli] PC "
618 "%#x is head of ROB and not ready\n",
619 tid, inst->seqNum, inst->readPC());
622 DPRINTF(Commit, "[tid:%i]: ROB has %d insts & %d free entries.\n",
623 tid, rob->countInsts(tid), rob->numFreeEntries(tid));
627 if (wroteToTimeBuffer) {
628 DPRINTF(Activity, "Activity This Cycle.\n");
629 cpu->activityThisCycle();
635 template <class Impl>
637 DefaultCommit<Impl>::commit()
640 //////////////////////////////////////
641 // Check for interrupts
642 //////////////////////////////////////
645 // Process interrupts if interrupts are enabled, not in PAL mode,
646 // and no other traps or external squashes are currently pending.
647 // @todo: Allow other threads to handle interrupts.
648 if (cpu->checkInterrupts &&
649 cpu->check_interrupts() &&
650 !cpu->inPalMode(readPC()) &&
653 // Tell fetch that there is an interrupt pending. This will
654 // make fetch wait until it sees a non PAL-mode PC, at which
655 // point it stops fetching instructions.
656 toIEW->commitInfo[0].interruptPending = true;
658 // Wait until the ROB is empty and all stores have drained in
659 // order to enter the interrupt.
660 if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
661 // Not sure which thread should be the one to interrupt. For now
662 // always do thread 0.
663 assert(!thread[0]->inSyscall);
664 thread[0]->inSyscall = true;
666 // CPU will handle implementation of the interrupt.
667 cpu->processInterrupts();
669 // Now squash or record that I need to squash this cycle.
670 commitStatus[0] = TrapPending;
672 // Exit state update mode to avoid accidental updating.
673 thread[0]->inSyscall = false;
675 // Generate trap squash event.
676 generateTrapEvent(0);
678 toIEW->commitInfo[0].clearInterrupt = true;
680 DPRINTF(Commit, "Interrupt detected.\n");
682 DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
685 #endif // FULL_SYSTEM
687 ////////////////////////////////////
688 // Check for any possible squashes, handle them first
689 ////////////////////////////////////
691 list<unsigned>::iterator threads = (*activeThreads).begin();
693 while (threads != (*activeThreads).end()) {
694 unsigned tid = *threads++;
696 if (fromFetch->fetchFault && commitStatus[0] != TrapPending) {
697 // Record the fault. Wait until it's empty in the ROB.
698 // Then handle the trap. Ignore it if there's already a
699 // trap pending as fetch will be redirected.
700 fetchFault = fromFetch->fetchFault;
701 fetchFaultTick = curTick + fetchTrapLatency;
702 commitStatus[0] = FetchTrapPending;
703 DPRINTF(Commit, "Fault from fetch recorded. Will trap if the "
704 "ROB empties without squashing the fault.\n");
708 // Fetch may tell commit to clear the trap if it's been squashed.
709 if (fromFetch->clearFetchFault) {
710 DPRINTF(Commit, "Received clear fetch fault signal\n");
712 if (commitStatus[0] == FetchTrapPending) {
713 DPRINTF(Commit, "Clearing fault from fetch\n");
714 commitStatus[0] = Running;
718 // Not sure which one takes priority. I think if we have
719 // both, that's a bad sign.
720 if (trapSquash[tid] == true) {
721 assert(!xcSquash[tid]);
723 } else if (xcSquash[tid] == true) {
727 // Squashed sequence number must be older than youngest valid
728 // instruction in the ROB. This prevents squashes from younger
729 // instructions overriding squashes from older instructions.
730 if (fromIEW->squash[tid] &&
731 commitStatus[tid] != TrapPending &&
732 fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
734 DPRINTF(Commit, "[tid:%i]: Squashing due to PC %#x [sn:%i]\n",
736 fromIEW->mispredPC[tid],
737 fromIEW->squashedSeqNum[tid]);
739 DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
741 fromIEW->nextPC[tid]);
743 commitStatus[tid] = ROBSquashing;
747 // If we want to include the squashing instruction in the squash,
748 // then use one older sequence number.
749 InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
751 if (fromIEW->includeSquashInst[tid] == true)
754 // All younger instructions will be squashed. Set the sequence
755 // number as the youngest instruction in the ROB.
756 youngestSeqNum[tid] = squashed_inst;
758 rob->squash(squashed_inst, tid);
759 changedROBNumEntries[tid] = true;
761 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
763 toIEW->commitInfo[tid].squash = true;
765 // Send back the rob squashing signal so other stages know that
766 // the ROB is in the process of squashing.
767 toIEW->commitInfo[tid].robSquashing = true;
769 toIEW->commitInfo[tid].branchMispredict =
770 fromIEW->branchMispredict[tid];
772 toIEW->commitInfo[tid].branchTaken =
773 fromIEW->branchTaken[tid];
775 toIEW->commitInfo[tid].nextPC = fromIEW->nextPC[tid];
777 toIEW->commitInfo[tid].mispredPC = fromIEW->mispredPC[tid];
779 if (toIEW->commitInfo[tid].branchMispredict) {
788 if (squashCounter != numThreads) {
789 // If we're not currently squashing, then get instructions.
792 // Try to commit any instructions.
796 //Check for any activity
797 threads = (*activeThreads).begin();
799 while (threads != (*activeThreads).end()) {
800 unsigned tid = *threads++;
802 if (changedROBNumEntries[tid]) {
803 toIEW->commitInfo[tid].usedROB = true;
804 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
806 if (rob->isEmpty(tid)) {
807 toIEW->commitInfo[tid].emptyROB = true;
810 wroteToTimeBuffer = true;
811 changedROBNumEntries[tid] = false;
816 template <class Impl>
818 DefaultCommit<Impl>::commitInsts()
820 ////////////////////////////////////
822 // Note that commit will be handled prior to putting new
823 // instructions in the ROB so that the ROB only tries to commit
824 // instructions it has in this current cycle, and not instructions
825 // it is writing in during this cycle. Can't commit and squash
826 // things at the same time...
827 ////////////////////////////////////
829 DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
831 unsigned num_committed = 0;
833 DynInstPtr head_inst;
835 // Commit as many instructions as possible until the commit bandwidth
836 // limit is reached, or it becomes impossible to commit any more.
837 while (num_committed < commitWidth) {
838 int commit_thread = getCommittingThread();
840 if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
843 head_inst = rob->readHeadInst(commit_thread);
845 int tid = head_inst->threadNumber;
847 assert(tid == commit_thread);
849 DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
850 head_inst->seqNum, tid);
852 // If the head instruction is squashed, it is ready to retire
853 // (be removed from the ROB) at any time.
854 if (head_inst->isSquashed()) {
856 DPRINTF(Commit, "Retiring squashed instruction from "
859 rob->retireHead(commit_thread);
861 ++commitSquashedInsts;
863 // Record that the number of ROB entries has changed.
864 changedROBNumEntries[tid] = true;
866 PC[tid] = head_inst->readPC();
867 nextPC[tid] = head_inst->readNextPC();
869 // Increment the total number of non-speculative instructions
871 // Hack for now: it really shouldn't happen until after the
872 // commit is deemed to be successful, but this count is needed
874 thread[tid]->funcExeInst++;
876 // Try to commit the head instruction.
877 bool commit_success = commitHead(head_inst, num_committed);
879 if (commit_success) {
882 changedROBNumEntries[tid] = true;
884 // Set the doneSeqNum to the youngest committed instruction.
885 toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
887 ++commitCommittedInsts;
889 // To match the old model, don't count nops and instruction
890 // prefetches towards the total commit count.
891 if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
895 PC[tid] = nextPC[tid];
896 nextPC[tid] = nextPC[tid] + sizeof(TheISA::MachInst);
901 // Debug statement. Checks to make sure we're not
902 // currently updating state while handling PC events.
904 assert(!thread[tid]->inSyscall &&
905 !thread[tid]->trapPending);
907 cpu->system->pcEventQueue.service(
908 thread[tid]->getXCProxy());
910 } while (oldpc != PC[tid]);
912 DPRINTF(Commit, "PC skip function event, stopping commit\n");
917 DPRINTF(Commit, "Unable to commit head instruction PC:%#x "
918 "[tid:%i] [sn:%i].\n",
919 head_inst->readPC(), tid ,head_inst->seqNum);
925 DPRINTF(CommitRate, "%i\n", num_committed);
926 numCommittedDist.sample(num_committed);
928 if (num_committed == commitWidth) {
929 commitEligibleSamples++;
933 template <class Impl>
935 DefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
939 int tid = head_inst->threadNumber;
941 // If the instruction is not executed yet, then it will need extra
942 // handling. Signal backwards that it should be executed.
943 if (!head_inst->isExecuted()) {
944 // Keep this number correct. We have not yet actually executed
945 // and committed this instruction.
946 thread[tid]->funcExeInst--;
948 head_inst->reachedCommit = true;
950 if (head_inst->isNonSpeculative() ||
951 head_inst->isStoreConditional() ||
952 head_inst->isMemBarrier() ||
953 head_inst->isWriteBarrier()) {
955 DPRINTF(Commit, "Encountered a barrier or non-speculative "
956 "instruction [sn:%lli] at the head of the ROB, PC %#x.\n",
957 head_inst->seqNum, head_inst->readPC());
960 // Hack to make sure syscalls/memory barriers/quiesces
961 // aren't executed until all stores write back their data.
962 // This direct communication shouldn't be used for
963 // anything other than this.
964 if (inst_num > 0 || iewStage->hasStoresToWB())
966 if ((head_inst->isMemBarrier() || head_inst->isWriteBarrier() ||
967 head_inst->isQuiesce()) &&
968 iewStage->hasStoresToWB())
971 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
975 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
977 // Change the instruction so it won't try to commit again until
979 head_inst->clearCanCommit();
981 ++commitNonSpecStalls;
984 } else if (head_inst->isLoad()) {
985 DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %#x.\n",
986 head_inst->seqNum, head_inst->readPC());
988 // Send back the non-speculative instruction's sequence
989 // number. Tell the lsq to re-execute the load.
990 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
991 toIEW->commitInfo[tid].uncached = true;
992 toIEW->commitInfo[tid].uncachedLoad = head_inst;
994 head_inst->clearCanCommit();
998 panic("Trying to commit un-executed instruction "
999 "of unknown type!\n");
1003 if (head_inst->isThreadSync()) {
1004 // Not handled for now.
1005 panic("Thread sync instructions are not handled yet.\n");
1008 // Stores mark themselves as completed.
1009 if (!head_inst->isStore()) {
1010 head_inst->setCompleted();
1013 // Use checker prior to updating anything due to traps or PC
1016 cpu->checker->tick(head_inst);
1019 // Check if the instruction caused a fault. If so, trap.
1020 Fault inst_fault = head_inst->getFault();
1022 if (inst_fault != NoFault) {
1023 head_inst->setCompleted();
1025 DPRINTF(Commit, "Inst [sn:%lli] PC %#x has a fault\n",
1026 head_inst->seqNum, head_inst->readPC());
1028 if (iewStage->hasStoresToWB() || inst_num > 0) {
1029 DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
1033 if (cpu->checker && head_inst->isStore()) {
1034 cpu->checker->tick(head_inst);
1037 assert(!thread[tid]->inSyscall);
1039 // Mark that we're in state update mode so that the trap's
1040 // execution doesn't generate extra squashes.
1041 thread[tid]->inSyscall = true;
1043 // DTB will sometimes need the machine instruction for when
1044 // faults happen. So we will set it here, prior to the DTB
1045 // possibly needing it for its fault.
1046 thread[tid]->setInst(
1047 static_cast<TheISA::MachInst>(head_inst->staticInst->machInst));
1049 // Execute the trap. Although it's slightly unrealistic in
1050 // terms of timing (as it doesn't wait for the full timing of
1051 // the trap event to complete before updating state), it's
1052 // needed to update the state as soon as possible. This
1053 // prevents external agents from changing any specific state
1054 // that the trap need.
1055 cpu->trap(inst_fault, tid);
1057 // Exit state update mode to avoid accidental updating.
1058 thread[tid]->inSyscall = false;
1060 commitStatus[tid] = TrapPending;
1062 // Generate trap squash event.
1063 generateTrapEvent(tid);
1066 #else // !FULL_SYSTEM
1067 panic("fault (%d) detected @ PC %08p", inst_fault,
1069 #endif // FULL_SYSTEM
1072 updateComInstStats(head_inst);
1074 if (head_inst->traceData) {
1075 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1076 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1077 head_inst->traceData->finalize();
1078 head_inst->traceData = NULL;
1081 // Update the commit rename map
1082 for (int i = 0; i < head_inst->numDestRegs(); i++) {
1083 renameMap[tid]->setEntry(head_inst->destRegIdx(i),
1084 head_inst->renamedDestRegIdx(i));
1087 // Finally clear the head ROB entry.
1088 rob->retireHead(tid);
1090 // Return true to indicate that we have committed an instruction.
1094 template <class Impl>
1096 DefaultCommit<Impl>::getInsts()
1098 // Read any renamed instructions and place them into the ROB.
1099 int insts_to_process = min((int)renameWidth, fromRename->size);
1101 for (int inst_num = 0; inst_num < insts_to_process; ++inst_num)
1103 DynInstPtr inst = fromRename->insts[inst_num];
1104 int tid = inst->threadNumber;
1106 if (!inst->isSquashed() &&
1107 commitStatus[tid] != ROBSquashing) {
1108 changedROBNumEntries[tid] = true;
1110 DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ROB.\n",
1111 inst->readPC(), inst->seqNum, tid);
1113 rob->insertInst(inst);
1115 assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
1117 youngestSeqNum[tid] = inst->seqNum;
1119 DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
1120 "squashed, skipping.\n",
1121 inst->readPC(), inst->seqNum, tid);
1126 template <class Impl>
1128 DefaultCommit<Impl>::markCompletedInsts()
1130 // Grab completed insts out of the IEW instruction queue, and mark
1131 // instructions completed within the ROB.
1132 for (int inst_num = 0;
1133 inst_num < fromIEW->size && fromIEW->insts[inst_num];
1136 if (!fromIEW->insts[inst_num]->isSquashed()) {
1137 DPRINTF(Commit, "[tid:%i]: Marking PC %#x, [sn:%lli] ready "
1139 fromIEW->insts[inst_num]->threadNumber,
1140 fromIEW->insts[inst_num]->readPC(),
1141 fromIEW->insts[inst_num]->seqNum);
1143 // Mark the instruction as ready to commit.
1144 fromIEW->insts[inst_num]->setCanCommit();
1149 template <class Impl>
1151 DefaultCommit<Impl>::robDoneSquashing()
1153 list<unsigned>::iterator threads = (*activeThreads).begin();
1155 while (threads != (*activeThreads).end()) {
1156 unsigned tid = *threads++;
1158 if (!rob->isDoneSquashing(tid))
1165 template <class Impl>
1167 DefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
1169 unsigned thread = inst->threadNumber;
1172 // Pick off the software prefetches
1175 if (inst->isDataPrefetch()) {
1176 statComSwp[thread]++;
1178 statComInst[thread]++;
1181 statComInst[thread]++;
1185 // Control Instructions
1187 if (inst->isControl())
1188 statComBranches[thread]++;
1191 // Memory references
1193 if (inst->isMemRef()) {
1194 statComRefs[thread]++;
1196 if (inst->isLoad()) {
1197 statComLoads[thread]++;
1201 if (inst->isMemBarrier()) {
1202 statComMembars[thread]++;
1206 ////////////////////////////////////////
1208 // SMT COMMIT POLICY MAINTAINED HERE //
1210 ////////////////////////////////////////
1211 template <class Impl>
1213 DefaultCommit<Impl>::getCommittingThread()
1215 if (numThreads > 1) {
1216 switch (commitPolicy) {
1219 //If Policy is Aggressive, commit will call
1220 //this function multiple times per
1222 return oldestReady();
1225 return roundRobin();
1228 return oldestReady();
1234 int tid = (*activeThreads).front();
1236 if (commitStatus[tid] == Running ||
1237 commitStatus[tid] == Idle ||
1238 commitStatus[tid] == FetchTrapPending) {
1246 template<class Impl>
1248 DefaultCommit<Impl>::roundRobin()
1250 list<unsigned>::iterator pri_iter = priority_list.begin();
1251 list<unsigned>::iterator end = priority_list.end();
1253 while (pri_iter != end) {
1254 unsigned tid = *pri_iter;
1256 if (commitStatus[tid] == Running ||
1257 commitStatus[tid] == Idle) {
1259 if (rob->isHeadReady(tid)) {
1260 priority_list.erase(pri_iter);
1261 priority_list.push_back(tid);
1273 template<class Impl>
1275 DefaultCommit<Impl>::oldestReady()
1277 unsigned oldest = 0;
1280 list<unsigned>::iterator threads = (*activeThreads).begin();
1282 while (threads != (*activeThreads).end()) {
1283 unsigned tid = *threads++;
1285 if (!rob->isEmpty(tid) &&
1286 (commitStatus[tid] == Running ||
1287 commitStatus[tid] == Idle ||
1288 commitStatus[tid] == FetchTrapPending)) {
1290 if (rob->isHeadReady(tid)) {
1292 DynInstPtr head_inst = rob->readHeadInst(tid);
1297 } else if (head_inst->seqNum < oldest) {