Elaborate access statistics
MODIFY
src/cacheconfigwidget.cpp
src/cacheconfigwidget.cpp
@@ -63,6 +63,7 @@ void CacheConfigWidget::setCache(CacheSim* cache) {
});
connect(m_cache, &CacheSim::configurationChanged, this, &CacheConfigWidget::configChanged);
+ connect(m_cache, &CacheSim::hitrateChanged, this, &CacheConfigWidget::updateHitrate);
setupPresets();
m_ui->replacementPolicy->setCurrentIndex(0);
@@ -178,11 +179,13 @@ void CacheConfigWidget::configChanged() {
updateIndexingText();
m_ui->size->setText(QString::number(m_cache->getCacheSize().bits));
- setHitRate(m_cache->getHitRate());
+ updateHitrate();
}
-void CacheConfigWidget::setHitRate(double hitrate) {
- m_ui->hitrate->setText(QString::number(hitrate));
+void CacheConfigWidget::updateHitrate() {
+ m_ui->hitrate->setText(QString::number(m_cache->getHitRate(), 'G', 4));
+ m_ui->hits->setText(QString::number(m_cache->getHits()));
+ m_ui->misses->setText(QString::number(m_cache->getMisses()));
}
void CacheConfigWidget::showSizeBreakdown() {

MODIFY
src/cacheconfigwidget.h
src/cacheconfigwidget.h
@@ -19,7 +19,7 @@ public:
void setCache(CacheSim* cache);
public slots:
- void setHitRate(double hitrate);
+ void updateHitrate();
void configChanged();
private:

MODIFY
src/cacheconfigwidget.ui
src/cacheconfigwidget.ui
<rect>
<x>0</x>
<y>0</y>
- <width>779</width>
- <height>274</height>
+ <width>645</width>
+ <height>271</height>
</rect>
</property>
<property name="windowTitle">
<number>0</number>
</property>
<item row="0" column="0">
- <layout class="QHBoxLayout" name="horizontalLayout">
+ <layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Statistics:</string>
</property>
<layout class="QGridLayout" name="gridLayout_5">
+ <item row="2" column="0">
+ <widget class="QPushButton" name="randomwrite">
+ <property name="text">
+ <string>Random write</string>
+ </property>
+ </widget>
+ </item>
<item row="3" column="0">
<widget class="QPushButton" name="undo">
<property name="text">
</widget>
</item>
<item row="0" column="0">
- <layout class="QGridLayout" name="gridLayout_4">
- <item row="0" column="1">
- <widget class="QLineEdit" name="hitrate"/>
- </item>
- <item row="1" column="0">
+ <layout class="QVBoxLayout" name="verticalLayout_3">
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_7">
+ <item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Size (bits):</string>
</property>
</widget>
</item>
- <item row="0" column="0">
+ <item>
+ <widget class="QLineEdit" name="size">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="minimumSize">
+ <size>
+ <width>0</width>
+ <height>0</height>
+ </size>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QToolButton" name="sizeBreakdownButton">
+ <property name="text">
+ <string>...</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_6">
+ <item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Hit rate:</string>
</property>
</widget>
</item>
- <item row="2" column="0">
+ <item>
+ <widget class="QLineEdit" name="hitrate">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="minimumSize">
+ <size>
+ <width>0</width>
+ <height>0</height>
+ </size>
+ </property>
+ </widget>
+ </item>
+ <item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>Writebacks:</string>
</property>
</widget>
</item>
- <item row="2" column="1">
- <widget class="QLineEdit" name="writebacks"/>
- </item>
- <item row="1" column="1">
- <layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
- <widget class="QLineEdit" name="size"/>
+ <widget class="QLineEdit" name="writebacks">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="minimumSize">
+ <size>
+ <width>0</width>
+ <height>0</height>
+ </size>
+ </property>
+ </widget>
+ </item>
+ </layout>
</item>
<item>
- <widget class="QToolButton" name="sizeBreakdownButton">
+ <layout class="QHBoxLayout" name="horizontalLayout_9">
+ <item>
+ <widget class="QLabel" name="label_11">
<property name="text">
- <string>...</string>
+ <string>Hits:</string>
</property>
</widget>
</item>
- </layout>
- </item>
- </layout>
+ <item>
+ <widget class="QLineEdit" name="hits">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="minimumSize">
+ <size>
+ <width>0</width>
+ <height>0</height>
+ </size>
+ </property>
+ </widget>
</item>
- <item row="2" column="0">
- <widget class="QPushButton" name="randomwrite">
+ <item>
+ <widget class="QLabel" name="label_12">
<property name="text">
- <string>Random write</string>
+ <string>Misses:</string>
</property>
</widget>
</item>
- <item row="1" column="0">
- <widget class="QPushButton" name="randomread">
- <property name="text">
- <string>Random read</string>
+ <item>
+ <widget class="QLineEdit" name="misses">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="minimumSize">
+ <size>
+ <width>0</width>
+ <height>0</height>
+ </size>
</property>
</widget>
</item>
+ </layout>
+ </item>
+ </layout>
+ </item>
<item row="5" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
</property>
</spacer>
</item>
+ <item row="1" column="0">
+ <widget class="QPushButton" name="randomread">
+ <property name="text">
+ <string>Random read</string>
+ </property>
+ </widget>
+ </item>
<item row="4" column="0">
<widget class="QPushButton" name="autoAccess">
<property name="text">

MODIFY
src/cachesim.cpp
src/cachesim.cpp
@@ -115,15 +115,31 @@ void CacheSim::evictAndUpdate(CacheTransaction& transaction) {
transaction.tagChanged = true;
}
-void CacheSim::updateHitRate() {
- auto oldHitRate = m_hitrate;
+unsigned CacheSim::getHits() const {
if (m_accessTrace.size() == 0) {
- m_hitrate = 0;
+ return 0;
} else {
auto& trace = m_accessTrace.rbegin()->second;
- m_hitrate = static_cast<double>(trace.hits) / (trace.hits + trace.misses);
+ return trace.hits;
+ }
+}
+
+unsigned CacheSim::getMisses() const {
+ if (m_accessTrace.size() == 0) {
+ return 0;
+ } else {
+ auto& trace = m_accessTrace.rbegin()->second;
+ return trace.misses;
+ }
+}
+
+double CacheSim::getHitRate() const {
+ if (m_accessTrace.size() == 0) {
+ return 0;
+ } else {
+ auto& trace = m_accessTrace.rbegin()->second;
+ return static_cast<double>(trace.hits) / (trace.hits + trace.misses);
}
- emit hitRateChanged(m_hitrate);
}
void CacheSim::analyzeCacheAccess(CacheTransaction& transaction) {
@@ -149,7 +165,7 @@ void CacheSim::analyzeCacheAccess(CacheTransaction& transaction) {
m_accessTrace[m_accessTrace.size()] =
CacheAccessTrace(m_accessTrace[m_accessTrace.size() - 1], transaction.isHit);
}
- updateHitRate();
+ emit hitrateChanged();
}
void CacheSim::access(uint32_t address, AccessType type) {
@@ -216,7 +232,6 @@ void CacheSim::updateConfiguration() {
// Cache configuration changes shall enforce a full reset of the computing system
m_cacheLines.clear();
m_accessTrace.clear();
- m_hitrate = 0;
// Recalculate masks
int bitoffset = 2; // 2^2 = 4-byte offset (32-bit words in cache)
@@ -229,7 +244,6 @@ void CacheSim::updateConfiguration() {
m_tagMask = generateBitmask(32 - bitoffset) << bitoffset;
- emit hitRateChanged(m_hitrate);
emit configurationChanged();
}

MODIFY
src/cachesim.h
src/cachesim.h
@@ -84,7 +84,9 @@ public:
ReplPolicy getReplacementPolicy() const { return m_replPolicy; }
WritePolicy getWritePolicy() const { return m_wrPolicy; }
- double getHitRate() const { return m_hitrate; }
+ double getHitRate() const;
+ unsigned getHits() const;
+ unsigned getMisses() const;
CacheSize getCacheSize() const;
int getBlockBits() const { return m_blocks; }
@@ -114,10 +116,9 @@ public slots:
signals:
void configurationChanged();
void dataChanged(const CacheTransaction& transaction);
- void hitRateChanged(double hitrate);
+ void hitrateChanged();
private:
- void updateHitRate();
void evictAndUpdate(CacheTransaction& transaction);
void analyzeCacheAccess(CacheTransaction& transaction);
void updateConfiguration();
@@ -134,8 +135,6 @@ private:
int m_lines = 2; // Some power of 2
int m_ways = 2; // Some power of 2
- double m_hitrate = 0; // Most recent hitrate
-
std::map<unsigned, CacheLine> m_cacheLines;
void updateCacheLineLRU(CacheLine& line, unsigned lruIdx);

MODIFY
src/cachewidget.cpp
src/cachewidget.cpp
@@ -16,7 +16,6 @@ CacheWidget::CacheWidget(QWidget* parent) : QWidget(parent), m_ui(new Ui::CacheW
auto* cacheSim = new CacheSim(this);
m_ui->cacheConfig->setCache(cacheSim);
- connect(cacheSim, &CacheSim::hitRateChanged, m_ui->cacheConfig, &CacheConfigWidget::setHitRate);
auto* cacheGraphic = new CacheGraphic(*cacheSim);
m_ui->cacheView->setScene(scene);
scene->addItem(cacheGraphic);

Elaborate access statistics