Skip to content

Commit

Permalink
Merge pull request #163 from comparch-security/update-flush-mirage
Browse files Browse the repository at this point in the history
update with changes form internal
  • Loading branch information
wsong83 authored Nov 25, 2024
2 parents 885f703 + 117f7e6 commit aadcb62
Show file tree
Hide file tree
Showing 11 changed files with 5,646 additions and 1,694 deletions.
8 changes: 4 additions & 4 deletions cache/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ class CacheBase : public CacheMonitorSupport
virtual bool hit(uint64_t addr,
uint32_t *ai, // index of the hitting cache array in "arrays"
uint32_t *s, uint32_t *w,
uint16_t prio = 0, // transaction priority
bool check_and_set = false // whether to check and set the priority if hit
uint16_t prio, // transaction priority
bool check_and_set // whether to check and set the priority if hit
) = 0;

__always_inline bool hit(uint64_t addr) {
Expand Down Expand Up @@ -340,8 +340,8 @@ class CacheSkewed : public CacheBase
if constexpr (EnMon || !C_VOID<DLY>) monitors->hook_write(addr, ai, s, w, hit, meta, data, delay);
}

virtual void hook_manage(uint64_t addr, uint32_t ai, uint32_t s, uint32_t w, bool hit, bool evict, bool writeback, const CMMetadataBase * meta, const CMDataBase *data, uint64_t *delay) override {
if(ai < P && hit && evict) replacer[ai].invalid(s, w);
virtual void hook_manage(uint64_t addr, uint32_t ai, uint32_t s, uint32_t w, bool hit, uint32_t evict, bool writeback, const CMMetadataBase * meta, const CMDataBase *data, uint64_t *delay) override {
if(ai < P && hit && evict) replacer[ai].invalid(s, w, evict == 2);
if constexpr (EnMon || !C_VOID<DLY>) monitors->hook_manage(addr, ai, s, w, hit, evict, writeback, meta, data, delay);
}

Expand Down
12 changes: 6 additions & 6 deletions cache/coherence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class OuterCohPortT : public OPUC<Policy, EnMT, Extra...>
break;
}
} else {
hit = cache->hit(addr, &ai, &s, &w);
hit = cache->hit(addr, &ai, &s, &w, 0, false);
if(hit) std::tie(meta, data) = cache->access_line(ai, s, w);
}

Expand All @@ -198,12 +198,12 @@ class OuterCohPortT : public OPUC<Policy, EnMT, Extra...>
if constexpr (EnMT) {assert(meta->match(addr)); meta_outer->lock(); }
if((writeback = Policy::probe_need_writeback(outer_cmd, meta))) { if(data_outer) data_outer->copy(data); } // writeback if dirty
Policy::meta_after_probe(outer_cmd, meta, meta_outer, coh_id, writeback); // alway update meta
cache->hook_manage(addr, ai, s, w, hit, coh::is_evict(outer_cmd), writeback, meta, data, delay);
cache->hook_manage(addr, ai, s, w, hit, (coh::is_evict(outer_cmd) ? 1 : 0), writeback, meta, data, delay);
if constexpr (EnMT) { meta_outer->unlock(); meta->unlock(); cache->reset_mt_state(ai, s, XactPrio::probe); }
} else {
if constexpr (EnMT) meta_outer->lock();
Policy::meta_after_probe(outer_cmd, meta, meta_outer, coh_id, writeback); // alway update meta
cache->hook_manage(addr, ai, s, w, hit, coh::is_evict(outer_cmd), writeback, meta, data, delay);
cache->hook_manage(addr, ai, s, w, hit, (coh::is_evict(outer_cmd) ? 1 : 0), writeback, meta, data, delay);
if constexpr (EnMT) meta_outer->unlock();
}
return std::make_pair(hit, writeback);
Expand Down Expand Up @@ -264,7 +264,7 @@ class InnerCohPortUncached : public InnerCohPortBase
auto writeback = Policy::writeback_need_writeback(meta);
if(writeback.first) outer->writeback_req(addr, meta, data, writeback.second, delay); // writeback if dirty
Policy::meta_after_evict(meta);
cache->hook_manage(addr, ai, s, w, true, true, writeback.first, meta, data, delay);
cache->hook_manage(addr, ai, s, w, true, 1, writeback.first, meta, data, delay);
}

virtual std::tuple<bool, CMMetadataBase *, CMDataBase *, uint32_t, uint32_t, uint32_t>
Expand Down Expand Up @@ -301,7 +301,7 @@ class InnerCohPortUncached : public InnerCohPortBase
break;
}
} else {
hit = cache->hit(addr, &ai, &s, &w);
hit = cache->hit(addr, &ai, &s, &w, 0, false);
if(!hit && do_replace) cache->replace(addr, &ai, &s, &w, prio);
if(hit || do_replace) std::tie(meta, data) = cache->access_line(ai, s, w);
}
Expand Down Expand Up @@ -359,7 +359,7 @@ class InnerCohPortUncached : public InnerCohPortBase
if(writeback.first) outer->writeback_req(addr, meta, data, writeback.second, delay); // writeback if dirty

Policy::meta_after_flush(cmd, meta, cache);
cache->hook_manage(addr, ai, s, w, hit, coh::is_evict(cmd), writeback.first, meta, data, delay);
cache->hook_manage(addr, ai, s, w, hit, (coh::is_evict(cmd) ? 2 : 0), writeback.first, meta, data, delay); // identify flush to hook_manager

if constexpr (EnMT) { meta->unlock(); cache->reset_mt_state(ai, s, XactPrio::flush); }
}
Expand Down
80 changes: 78 additions & 2 deletions cache/dynamic_random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "cache/coherence.hpp"
#include "util/monitor.hpp"
#include <numeric>

#define MAGIC_ID_REMAP 2024091300ul

Expand Down Expand Up @@ -140,7 +141,7 @@ class InnerCohPortRemapT : public InnerCohPortT<InnerCohPortUncached, Policy, En
uint64_t m_addr = m_meta->addr(new_idx);
if (m_meta->is_valid()) {
if (static_cast<MT *>(m_meta)->is_relocated()) this->evict(m_meta, m_data, new_ai, new_idx, new_way, nullptr);
else cache->hook_manage(m_addr, new_ai, new_idx, new_way, true, true, false, m_meta, m_data, nullptr);
else cache->hook_manage(m_addr, new_ai, new_idx, new_way, true, 1, false, m_meta, m_data, nullptr);
}
static_cast<CT *>(cache)->swap(m_addr, c_addr, m_meta, c_meta, m_data, c_data);
cache->hook_read(c_addr, new_ai, new_idx, new_way, false, false, m_meta, m_data, nullptr);
Expand All @@ -156,7 +157,7 @@ class InnerCohPortRemapT : public InnerCohPortT<InnerCohPortUncached, Policy, En
auto c_data = data ? cache->data_copy_buffer() : nullptr;
static_cast<CT *>(cache)->relocate(c_addr, meta, c_meta, data, c_data);
static_cast<MT *>(meta)->to_relocated();
cache->hook_manage(c_addr, ai, idx, way, true, true, false, c_meta, c_data, nullptr);
cache->hook_manage(c_addr, ai, idx, way, true, 1, false, c_meta, c_data, nullptr);

while(c_meta->is_valid()){
relocation(c_meta, c_data, c_addr);
Expand Down Expand Up @@ -209,4 +210,79 @@ class SimpleEVRemapper : public RemapperBase
}
};

template<int IW>
class ZSEVRemapper : public RemapperBase
{
protected:
static constexpr uint32_t nset = 1ul << IW;
const double factor;
const double threshold;
const uint64_t access_period, evict_period;
std::array<uint64_t, nset> evicts{};
std::array<double, nset> set_evict_history{};

bool Z_Score_detect(){
double qrm = sqrt(std::accumulate(evicts.begin(), evicts.end(), 0.0,
[](double q, const uint64_t& d){return q + d * d;})
/ (nset-1.0)
);
double mu = std::accumulate(evicts.begin(), evicts.end(), 0.0) / (double)(nset);
for(size_t i=0; i<nset; i++) {
double delta = qrm == 0.0 ? 0.0 : (evicts[i] - mu) * evicts[i] / qrm;
set_evict_history[i] = (1 - factor) * set_evict_history[i] + factor * (evicts[i] > mu ? delta : -delta);
}

for(size_t i=0; i<nset; i++){
if(set_evict_history[i] >= threshold) {
// std::cerr << "found set " << i << std::endl;
return true;
}
}
return false;
}

public:
ZSEVRemapper(const double factor, uint64_t access_period, uint64_t evict_period, double th, bool remap_enable = true)
: RemapperBase(remap_enable), factor(factor), threshold(th),
access_period(access_period), evict_period(evict_period){}

virtual void read(uint64_t cache_id, uint64_t addr, int32_t ai, int32_t s, int32_t w, bool hit, const CMMetadataBase *meta, const CMDataBase *data) override {
if(!active) return;
cnt_access++;
if(!hit) cnt_miss++;
if(access_period != 0 && (cnt_access % access_period) == 0) {
if(Z_Score_detect()) {
remap = true;
}
evicts.fill(0);
}
}
virtual void write(uint64_t cache_id, uint64_t addr, int32_t ai, int32_t s, int32_t w, bool hit, const CMMetadataBase *meta, const CMDataBase *data) override {
if(!active) return;
cnt_access++; cnt_write++;
if(!hit) { cnt_miss++; cnt_write_miss++;}
if(access_period != 0 && (cnt_access % access_period) == 0) {
if(Z_Score_detect()) {
remap = true;
}
evicts.fill(0);
}
}
virtual void invalid(uint64_t cache_id, uint64_t addr, int32_t ai, int32_t s, int32_t w, const CMMetadataBase *meta, const CMDataBase *data) override {
if(!active) return;
cnt_invalid++;
evicts[s]++;
if(evict_period != 0 && (cnt_invalid % evict_period) == 0) {
remap = true;
// std::cerr << " remapped by eviction limit @" << cnt_invalid << std::endl;
}
}

virtual void reset() override {
evicts.fill(0);
set_evict_history.fill(0.0);
RemapperBase::reset();
}
};

#endif
12 changes: 6 additions & 6 deletions cache/exclusive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ class CacheSkewedExclusive : public CacheSkewed<IW, NW, P, MT, DT, IDX, RPC, DLY
}
}

virtual void hook_manage(uint64_t addr, uint32_t ai, uint32_t s, uint32_t w, bool hit, bool evict, bool writeback, const CMMetadataBase *meta, const CMDataBase *data, uint64_t *delay) override {
virtual void hook_manage(uint64_t addr, uint32_t ai, uint32_t s, uint32_t w, bool hit, uint32_t evict, bool writeback, const CMMetadataBase *meta, const CMDataBase *data, uint64_t *delay) override {
if(ai < P){
if(hit && evict) {
if(w >= NW) ext_replacer[ai].invalid(s, w-NW);
else replacer[ai].invalid(s, w);
else replacer[ai].invalid(s, w, evict == 2);
}
if constexpr (EnMon || !C_VOID<DLY>) monitors->hook_manage(addr, ai, s, w, hit, evict, writeback, meta, data, delay);
} else {
Expand Down Expand Up @@ -313,7 +313,7 @@ class ExclusiveInnerCohPortUncachedBroadcast : public InnerCohPortUncached<Polic
if(writeback.first) outer->writeback_req(addr, meta, data, writeback.second, delay); // writeback if dirty

Policy::meta_after_flush(cmd, meta, cache);
cache->hook_manage(addr, ai, s, w, hit, coh::is_evict(cmd), writeback.first, meta, data, delay);
cache->hook_manage(addr, ai, s, w, hit, (coh::is_evict(cmd) ? 2 : 0), writeback.first, meta, data, delay);

if(!hit) {
cache->meta_return_buffer(meta);
Expand Down Expand Up @@ -495,7 +495,7 @@ class ExclusiveInnerCohPortUncachedDirectory : public InnerCohPortUncached<Polic
if(writeback.first) outer->writeback_req(addr, meta, data, writeback.second, delay); // writeback if dirty

Policy::meta_after_flush(cmd, meta, cache);
cache->hook_manage(addr, ai, s, w, hit, coh::is_evict(cmd), writeback.first, meta, data, delay);
cache->hook_manage(addr, ai, s, w, hit, (coh::is_evict(cmd) ? 2 : 0), writeback.first, meta, data, delay);

if(meta->is_extend()) cache->data_return_buffer(data);
}
Expand Down Expand Up @@ -540,7 +540,7 @@ class ExclusiveOuterCohPortBroadcastT : public OPUC<Policy, EnMT>
}

Policy::meta_after_probe(outer_cmd, meta, meta_outer, coh_id, writeback);
cache->hook_manage(addr, ai, s, w, hit, coh::is_evict(outer_cmd), writeback, meta, data, delay);
cache->hook_manage(addr, ai, s, w, hit, (coh::is_evict(outer_cmd) ? 1 : 0), writeback, meta, data, delay);

if(!hit) {
cache->meta_return_buffer(meta);
Expand Down Expand Up @@ -590,7 +590,7 @@ class ExclusiveOuterCohPortDirectoryT : public OPUC<Policy, EnMT>
}

Policy::meta_after_probe(outer_cmd, meta, meta_outer, coh_id, writeback);
cache->hook_manage(addr, ai, s, w, hit, coh::is_evict(outer_cmd), writeback, meta, data, delay);
cache->hook_manage(addr, ai, s, w, hit, (coh::is_evict(outer_cmd) ? 1 : 0), writeback, meta, data, delay);

if(hit && meta->is_extend()) {
cache->data_return_buffer(data);
Expand Down
2 changes: 1 addition & 1 deletion cache/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class SimpleMemoryModel : public InnerCohPortUncached<policy_memory, EnMT>, publ
}

private:
virtual void hook_manage(uint64_t, uint32_t, uint32_t, uint32_t, bool, bool, bool, const CMMetadataBase *, const CMDataBase *, uint64_t *) override {}
virtual void hook_manage(uint64_t, uint32_t, uint32_t, uint32_t, bool, uint32_t, bool, const CMMetadataBase *, const CMDataBase *, uint64_t *) override {}
virtual void query_loc_resp(uint64_t, std::list<LocInfo> *) override {}
virtual std::pair<bool,bool> probe_req(uint64_t, CMMetadataBase *, CMDataBase *, coh_cmd_t, uint64_t *) override { return std::make_pair(false,false); }
};
Expand Down
Loading

0 comments on commit aadcb62

Please sign in to comment.