Skip to content

Commit

Permalink
Merge pull request #96 from comparch-security/refactor-monitor
Browse files Browse the repository at this point in the history
remove the unnecessary object of monitor
  • Loading branch information
wsong83 authored Jun 27, 2024
2 parents 2310ac7 + cf43c99 commit a8db5b2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ CACHE_HEADERS = $(wildcard cache/*.hpp)

CRYPTO_LIB = cryptopp/libcryptopp.a
CACHE_OBJS = cache/metadata.o
UTIL_OBJS = util/random.o util/query.o util/monitor.o
UTIL_OBJS = util/random.o util/query.o

all: libflexicas.a

Expand Down
57 changes: 52 additions & 5 deletions util/monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#include <set>
#include <string>
#include <thread>
#include <boost/format.hpp>
#include "util/delay.hpp"
#include "util/concept_macro.hpp"
#include "util/print.hpp"
#include "cache/metadata.hpp"
#include "util/random.hpp"

class CMDataBase;
class CMMetadataBase;
Expand Down Expand Up @@ -174,17 +177,56 @@ class SimpleTracer : public MonitorBase
bool active;
bool compact_data;

virtual void print(std::string& msg);
virtual void print(std::string& msg) { std::cout << msg << std::endl; }

public:
SimpleTracer(bool cd = false): active(true), compact_data(cd) {}
virtual ~SimpleTracer() {}

virtual bool attach(uint64_t cache_id) { return true; }

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);
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);
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);
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) {
std::string msg; msg.reserve(100);
msg += (boost::format("%-10s read %016x %02d %04d %02d %1x") % UniqueID::name(cache_id) % addr % ai % s % w % hit).str();

if(meta)
msg.append(" [").append(meta->to_string()).append("]");
else if(data)
msg.append(" ");

if(data)
msg.append(" ").append(compact_data ? (boost::format("%016x") % (data->read(0))).str() : data->to_string());

print(msg);
}
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) {
std::string msg; msg.reserve(100);
msg += (boost::format("%-10s write %016x %02d %04d %02d %1x") % UniqueID::name(cache_id) % addr % ai % s % w % hit).str();

if(meta)
msg.append(" [").append(meta->to_string()).append("]");
else if(data)
msg.append(" ");

if(data)
msg.append(" ").append(compact_data ? (boost::format("%016x") % (data->read(0))).str() : data->to_string());

print(msg);
}
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) {
std::string msg; msg.reserve(100);
msg += (boost::format("%-10s evict %016x %02d %04d %02d ") % UniqueID::name(cache_id) % addr % ai % s % w).str() ;

if(meta)
msg.append(" [").append(meta->to_string()).append("]");
else if(data)
msg.append(" ");

if(data)
msg.append(" ").append(compact_data ? (boost::format("%016x") % (data->read(0))).str() : data->to_string());

print(msg);
}

virtual void start() { active = true; }
virtual void stop() { active = false; }
Expand All @@ -198,7 +240,12 @@ class SimpleTracerMT : public SimpleTracer
{
PrintPool pool;
std::thread print_thread;
virtual void print(std::string& msg) { pool.add(msg); }
virtual void print(std::string& msg) {
std::hash<std::thread::id> hasher;
uint16_t id = hasher(std::this_thread::get_id());
std::string msg_ext = (boost::format("thread %04x: %s") % id % msg).str();
pool.add(msg_ext);
}

public:
SimpleTracerMT(bool cd = false): SimpleTracer(cd), pool(256) {
Expand Down
12 changes: 5 additions & 7 deletions util/multithread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <condition_variable>
#include <memory>
#include <chrono>
#include <iostream>

template<typename T>
class AtomicVar {
Expand Down Expand Up @@ -38,15 +39,12 @@ class AtomicVar {
return rv;
}

__always_inline void wait() {
std::unique_lock lk(mtx);
cv.wait(lk);
}

__always_inline void wait_timeout() {
__always_inline void wait(bool report = false) {
using namespace std::chrono_literals;
std::unique_lock lk(mtx);
cv.wait_for(lk, 10us);
auto result = cv.wait_for(lk, 100us);
if(report && std::cv_status::timeout == result)
std::cerr << "cv [" << std::hex << this << "] waits timeout once ..." << std::endl;
}
};

Expand Down
4 changes: 3 additions & 1 deletion util/print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <iostream>
#include "util/multithread.hpp"


// can be implemented using std::osyncstream after C++20
class PrintPool {
const int pool_size;
AtomicVar<int> pw, pr;
Expand Down Expand Up @@ -42,7 +44,7 @@ class PrintPool {
void print() { // start print the pool
auto index = pr.read();
while(!finish.read()) {
if(!valid[index].read()) { valid[index].wait_timeout(); continue; }
if(!valid[index].read()) { valid[index].wait(); continue; }
std::cout << pool[index] << std::endl;
valid[index].write(false, true);
index = (index + 1) % pool_size;
Expand Down

0 comments on commit a8db5b2

Please sign in to comment.