Skip to content

Commit

Permalink
Merge pull request #42 from sarchlab/40-tlb-is-not-memory-efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
syifan authored Sep 9, 2023
2 parents 79021be + 83c89d4 commit a13f925
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions mem/vm/tlb/internal/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package internal

import (
"fmt"
"sort"

"github.com/google/btree"
"github.com/sarchlab/akita/v3/mem/vm"
)

Expand All @@ -20,7 +20,7 @@ type Set interface {
func NewSet(numWays int) Set {
s := &setImpl{}
s.blocks = make([]*block, numWays)
s.visitTree = btree.New(2)
s.visitList = make([]*block, 0, numWays)
s.vAddrWayIDMap = make(map[string]int)
for i := range s.blocks {
b := &block{}
Expand All @@ -37,14 +37,14 @@ type block struct {
lastVisit uint64
}

func (b *block) Less(anotherBlock btree.Item) bool {
return b.lastVisit < anotherBlock.(*block).lastVisit
func (b *block) Less(anotherBlock *block) bool {
return b.lastVisit < anotherBlock.lastVisit
}

type setImpl struct {
blocks []*block
vAddrWayIDMap map[string]int
visitTree *btree.BTree
visitList []*block
visitCount uint64
}

Expand Down Expand Up @@ -83,19 +83,34 @@ func (s *setImpl) Evict() (wayID int, ok bool) {
return 0, false
}

wayID = s.visitTree.DeleteMin().(*block).wayID
// wayID = s.visitTree.DeleteMin().(*block).wayID
leastVisited := s.visitList[0]
wayID = leastVisited.wayID
s.visitList = s.visitList[1:]
return wayID, true
}

func (s *setImpl) Visit(wayID int) {
block := s.blocks[wayID]
s.visitTree.Delete(block)

for i, b := range s.visitList {
if b.wayID == wayID {
s.visitList = append(s.visitList[:i], s.visitList[i+1:]...)
}
}

s.visitCount++
block.lastVisit = s.visitCount
s.visitTree.ReplaceOrInsert(block)

index := sort.Search(len(s.visitList), func(i int) bool {
return s.visitList[i].lastVisit > block.lastVisit
})

s.visitList = append(s.visitList, nil)
copy(s.visitList[index+1:], s.visitList[index:])
s.visitList[index] = block
}

func (s *setImpl) hasNothingToEvict() bool {
return s.visitTree.Len() == 0
return len(s.visitList) == 0
}

0 comments on commit a13f925

Please sign in to comment.