Skip to content

Commit

Permalink
Coding style update
Browse files Browse the repository at this point in the history
Refer to author's comment.
  • Loading branch information
tinhanho committed Jan 2, 2024
1 parent 101dc8c commit 48c4062
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 26 deletions.
83 changes: 64 additions & 19 deletions src/cachesim/cachegraphic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,45 @@ void CacheGraphic::updateLineReplFields(unsigned lineIdx) {
return;
}

if (m_cacheTextItems.at(0).at(0).lru == nullptr) {
if (m_cacheTextItems.at(0).at(0).lru == nullptr && m_cache.getReplacementPolicy() == ReplPolicy::LRU) {
// The current cache configuration does not have any replacement field
return;
}

for (const auto &way : m_cacheTextItems[lineIdx]) {
// If LRU was just initialized, the actual (software) LRU value may be very
// large. Mask to the number of actual LRU bits.
unsigned lruVal = cacheLine->at(way.first).lru;
lruVal &= vsrtl::generateBitmask(m_cache.getWaysBits());
const QString lruText = QString::number(lruVal);
way.second.lru->setText(lruText);

// LRU text might have changed; update LRU field position to center in
// column
const qreal y = lineIdx * m_lineHeight + way.first * m_setHeight;
const qreal x =
m_widthBeforeLRU + m_lruWidth / 2 - m_fm.horizontalAdvance(lruText) / 2;

way.second.lru->setPos(x, y);
if (m_cacheTextItems.at(0).at(0).lfu == nullptr && m_cache.getReplacementPolicy() == ReplPolicy::LFU){
return;
}
if(m_cache.getReplacementPolicy() == ReplPolicy::LRU){
for (const auto &way : m_cacheTextItems[lineIdx]) {
// If LRU was just initialized, the actual (software) LRU value may be very
// large. Mask to the number of actual LRU bits.
unsigned lruVal = cacheLine->at(way.first).lru;
lruVal &= vsrtl::generateBitmask(m_cache.getWaysBits());
const QString lruText = QString::number(lruVal);
way.second.lru->setText(lruText);

// LRU text might have changed; update LRU field position to center in
// column
const qreal y = lineIdx * m_lineHeight + way.first * m_setHeight;
const qreal x =
m_widthBeforeLRU + m_lruWidth / 2 - m_fm.horizontalAdvance(lruText) / 2;
way.second.lru->setPos(x, y);
}
}
if(m_cache.getReplacementPolicy() == ReplPolicy::LFU){
for (const auto &way : m_cacheTextItems[lineIdx]) {
// If LRU was just initialized, the actual (software) LRU value may be very
// large. Mask to the number of actual LRU bits.
int lfuVal = cacheLine->at(way.first).lfu;
const QString lfuText = QString::number(lfuVal);
way.second.lfu->setText(lfuText);

// LRU text might have changed; update LRU field position to center in
// column
const qreal y = lineIdx * m_lineHeight + way.first * m_setHeight;
const qreal x =
m_widthBeforeLFU + m_lfuWidth / 2 - m_fm.horizontalAdvance(lfuText) / 2;
way.second.lfu->setPos(x, y);
}
}
}

Expand Down Expand Up @@ -409,6 +428,7 @@ void CacheGraphic::cacheInvalidated() {
m_blockWidth = m_fm.horizontalAdvance(" " + addressString() + " ");
m_bitWidth = m_fm.horizontalAdvance("00");
m_lruWidth = m_fm.horizontalAdvance(QString::number(m_cache.getWays()) + " ");
m_lfuWidth = m_fm.horizontalAdvance(QString::number(m_cache.getWays()) + " ");
m_cacheHeight = m_lineHeight * m_cache.getLines();
m_tagWidth = m_blockWidth;

Expand Down Expand Up @@ -436,9 +456,10 @@ void CacheGraphic::cacheInvalidated() {
}

m_widthBeforeLRU = width;
m_widthBeforeLFU = width;

if (m_cache.getReplacementPolicy() == ReplPolicy::LRU &&
m_cache.getWays() > 1) {
if (m_cache.getReplacementPolicy() == ReplPolicy::LRU
&& m_cache.getWays() > 1) {
// Draw LRU bit column
new QGraphicsLineItem(width + m_lruWidth, 0, width + m_lruWidth,
m_cacheHeight, this);
Expand All @@ -451,6 +472,22 @@ void CacheGraphic::cacheInvalidated() {
width += m_lruWidth;
}

if (m_cache.getReplacementPolicy() == ReplPolicy::LFU
&& m_cache.getWays() > 1) {
// Draw LFU bit column
new QGraphicsLineItem(width + m_lfuWidth, 0, width + m_lfuWidth,
m_cacheHeight, this);
const QString LFUBitText = "LFU";
auto *textItem = drawText(LFUBitText,
width + m_lfuWidth / 2 -
m_fm.horizontalAdvance(LFUBitText) / 2,
-m_fm.height());
textItem->setToolTip("Least Frequently Used bits");
width += m_lfuWidth;
}



m_widthBeforeTag = width;

// Draw tag column
Expand Down Expand Up @@ -705,6 +742,14 @@ void CacheGraphic::initializeControlBits() {
m_fm.horizontalAdvance(lruText) / 2;
line[setIdx].lru = drawText(lruText, x, y);
}
if (m_cache.getReplacementPolicy() == ReplPolicy::LFU &&
m_cache.getWays() > 1) {
const QString lfuText = QString::number(0);
x = m_widthBeforeLFU + m_lfuWidth / 2 -
m_fm.horizontalAdvance(lfuText) / 2;
// Create LFU field
line[setIdx].lfu = drawText("0", x, y);
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/cachesim/cachegraphic.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public slots:
std::map<unsigned, std::unique_ptr<QGraphicsSimpleTextItem>> blocks;
std::unique_ptr<QGraphicsSimpleTextItem> tag = nullptr;
QGraphicsSimpleTextItem *lru = nullptr;
QGraphicsSimpleTextItem *lfu = nullptr;
QGraphicsSimpleTextItem *valid = nullptr;
QGraphicsSimpleTextItem *dirty = nullptr;
std::map<unsigned, std::unique_ptr<QGraphicsRectItem>> dirtyBlocks;
Expand Down Expand Up @@ -113,9 +114,10 @@ public slots:
qreal m_widthBeforeBlocks = 0;
qreal m_widthBeforeTag = 0;
qreal m_widthBeforeLRU = 0;
qreal m_widthBeforeLFU = 0;
qreal m_widthBeforeDirty = 0;
qreal m_lruWidth = 0;

qreal m_lfuWidth = 0;
static constexpr qreal z_grid = 0;
static constexpr qreal z_wires = -1;

Expand Down
53 changes: 50 additions & 3 deletions src/cachesim/cachesim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ void CacheSim::updateCacheLineReplFields(CacheLine &line, unsigned wayIdx) {
// Upgrade @p lruIdx to the most recently used
line[wayIdx].lru = 0;
}

if (getReplacementPolicy() == ReplPolicy::LFU) {
line[wayIdx].lfu += 1;
}
}

void CacheSim::revertCacheLineReplFields(CacheLine &line,
Expand Down Expand Up @@ -95,6 +99,13 @@ CacheSim::CacheSize CacheSim::getCacheSize() const {
size.bits += componentBits;
}

if (m_replPolicy == ReplPolicy::LFU) {
// LFU bits
componentBits = getWaysBits() * entries;
size.components.push_back("LFU bits: " + QString::number(componentBits));
size.bits += componentBits;
}

// Tag bits
componentBits = vsrtl::bitcount(m_tagMask) * entries;
size.components.push_back("Tag bits: " + QString::number(componentBits));
Expand Down Expand Up @@ -122,10 +133,10 @@ CacheSim::locateEvictionWay(const CacheTransaction &transaction) {
ew.second = &cacheLine[ew.first];
}
else if (m_replPolicy == ReplPolicy::FIFO){
ew.first = CacheSim::counter;
ew.first = m_fifoIndexCounter;
ew.second = &cacheLine[ew.first];
CacheSim::counter += 1;
CacheSim::counter %= getWays();
m_fifoIndexCounter += 1;
m_fifoIndexCounter %= getWays();
}
else if (m_replPolicy == ReplPolicy::LRU) {
if (getWays() == 1) {
Expand Down Expand Up @@ -156,6 +167,42 @@ CacheSim::locateEvictionWay(const CacheTransaction &transaction) {
}
}
}
}
else if (m_replPolicy == ReplPolicy::LFU) {
if (getWays() == 1) {
// Nothing to do if we are in LRU and only have 1 set.
ew.first = 0;
ew.second = &cacheLine[ew.first];
} else {
// Lazily initialize all ways in the cacheline before starting to iterate.
for (int i = 0; i < getWays(); ++i)
cacheLine[i];

// If there is an invalid cache line, select that.
auto it =
std::find_if(cacheLine.begin(), cacheLine.end(),
[=](const auto &way) { return !way.second.valid; });
if (it != cacheLine.end()) {
ew.first = it->first;
ew.second = &it->second;
}
if (ew.second == nullptr) {
// Else, Find LFU way.
int save_num = 0;
int tmp = 1000; // let it big enough. 1000 for test.
int loop_counter = 0;
for (auto &way : cacheLine) {
if (static_cast<long>(way.second.lfu) < tmp) {
tmp = way.second.lfu;
save_num = loop_counter;
}
loop_counter+=1;
}
ew.first = save_num;
ew.second = &cacheLine[ew.first];
}
}

}

Q_ASSERT(ew.first != s_invalidIndex && "Unable to locate way for eviction");
Expand Down
8 changes: 5 additions & 3 deletions src/cachesim/cachesim.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CacheSim;

enum WriteAllocPolicy { WriteAllocate, NoWriteAllocate };
enum WritePolicy { WriteThrough, WriteBack };
enum ReplPolicy { Random, LRU, FIFO};
enum ReplPolicy { Random, LRU, FIFO, LFU};

struct CachePreset {
QString name;
Expand Down Expand Up @@ -91,7 +91,7 @@ class CacheSim : public CacheInterface {
Q_OBJECT
public:
static constexpr unsigned s_invalidIndex = static_cast<unsigned>(-1);
int counter = 0;
unsigned m_fifoIndexCounter = 0;
struct CacheSize {
unsigned bits = 0;
std::vector<QString> components;
Expand All @@ -106,6 +106,7 @@ class CacheSim : public CacheInterface {
// LRU algorithm relies on invalid cache ways to have an initial high value.
// -1 ensures maximum value for all way sizes.
unsigned lru = -1;
int lfu = 0;
};

struct CacheIndex {
Expand Down Expand Up @@ -329,7 +330,8 @@ public slots:
};

const static std::map<ReplPolicy, QString> s_cacheReplPolicyStrings{
{ReplPolicy::Random, "Random"}, {ReplPolicy::LRU, "LRU"}, {ReplPolicy::FIFO, "FIFO"}};
{ReplPolicy::Random, "Random"}, {ReplPolicy::LRU, "LRU"}, {ReplPolicy::FIFO, "FIFO"},
{ReplPolicy::LFU, "LFU"}};
const static std::map<WriteAllocPolicy, QString> s_cacheWriteAllocateStrings{
{WriteAllocPolicy::WriteAllocate, "Write allocate"},
{WriteAllocPolicy::NoWriteAllocate, "No write allocate"}};
Expand Down

0 comments on commit 48c4062

Please sign in to comment.