Commit bec70717 authored by phlo's avatar phlo

removed Simulator's static interface

parent c886a154
......@@ -202,7 +202,7 @@ int simulate (const char * name, const int argc, const char ** argv)
// run program with given seed
// TODO: MMap
Trace::ptr trace = Simulator::simulate(programs, {}, bound);
Trace::ptr trace = Simulator().simulate(programs, {}, bound);
// print the result
std::cout << trace->print();
......@@ -265,7 +265,7 @@ int replay (const char * name, const int argc, const char ** argv)
std::make_unique<Trace>(create_from_file<Trace>(trace_path));
// run given trace
trace = Simulator::replay(*trace, bound);
trace = Simulator().replay(*trace, bound);
// print the result
std::cout << trace->print();
......
......@@ -26,18 +26,33 @@ inline void erase (C & container, T & val)
// constructors
//------------------------------------------------------------------------------
Simulator::Simulator (const Program::List::ptr & p,
const std::shared_ptr<MMap> & mmap,
const size_t b) :
random(seed),
programs(p),
trace(std::make_unique<Trace>(p, mmap)),
step(0),
bound(b ? b : static_cast<size_t>(-1)),
state(p->size(), State::running),
active(p->size())
{
for (thread = 0; thread < programs->size(); thread++)
Simulator::Simulator () : random(seed) {}
//------------------------------------------------------------------------------
// member functions
//------------------------------------------------------------------------------
// Simulator::init -------------------------------------------------------------
void Simulator::init (const Program::List::ptr & p,
const std::shared_ptr<MMap> & m,
const size_t b)
{
const size_t num_threads = p->size();
programs = p.get();
trace = std::make_unique<Trace>(p, m);
bound = b ? b : static_cast<size_t>(-1);
step = 0;
// initialize state vector
state.assign(num_threads, State::running);
// activate threads
active.resize(num_threads);
std::iota(active.begin(), active.end(), 0);
for (thread = 0; thread < num_threads; thread++)
{
// initialize register states
pc(0);
......@@ -52,14 +67,10 @@ Simulator::Simulator (const Program::List::ptr & p,
threads_per_checkpoint[c].insert(thread);
}
// activate threads
std::iota(active.begin(), active.end(), 0);
// reset waiting map
waiting_for_checkpoint.clear();
}
//------------------------------------------------------------------------------
// member functions
//------------------------------------------------------------------------------
// Simulator::pc ---------------------------------------------------------------
word_t Simulator::pc () const
......@@ -408,6 +419,8 @@ void Simulator::execute (const Instruction::Exit & e)
Trace::ptr Simulator::run (std::function<void()> scheduler)
{
assert(trace);
bool done = active.empty();
while (!done && step <= bound)
......@@ -441,54 +454,48 @@ Trace::ptr Simulator::run (std::function<void()> scheduler)
}
}
return move(trace);
return std::move(trace);
}
// Simulator::simulate ---------------------------------------------------------
Trace::ptr Simulator::simulate (const Program::List::ptr & programs,
const std::shared_ptr<MMap> & mmap,
const size_t bound)
Trace::ptr Simulator::simulate (const Program::List::ptr & p,
const std::shared_ptr<MMap> & m,
const size_t b)
{
Simulator s (programs, mmap, bound);
init(p, m, b);
// random scheduler
return s.run([&s] {
s.thread = s.active[s.random() % s.active.size()];
return run([this] {
thread = active[random() % active.size()];
assert(s.state[s.thread] == State::running);
assert(state[thread] == State::running);
// rule 2, 5, 7: store buffer may be flushed at any time or must be empty
if (s.sb_full())
if (sb_full())
{
const Instruction & op = (*s.programs)[s.thread][s.pc()];
const Instruction & op = (*programs)[thread][pc()];
if (s.random() % 2 || op.requires_flush())
s.state[s.thread] = State::flushing;
if (random() % 2 || op.requires_flush())
state[thread] = State::flushing;
}
});
}
// Simulator::replay -----------------------------------------------------------
Trace::ptr Simulator::replay (const Trace & trace, const size_t bound)
Trace::ptr Simulator::replay (const Trace & t, const size_t b)
{
Simulator s (trace.programs,
{},
bound && bound < trace.length ? bound : trace.length - 1);
// copy memory map
if (trace.mmap)
s.trace->mmap = trace.mmap;
init(t.programs, t.mmap, b && b < t.length ? bound : t.length - 1);
// replay scheduler
Trace::iterator it = trace.begin();
Trace::iterator it = t.begin();
return s.run([&s, &it] {
return run([this, &it] {
if (it->flush)
s.state[it->thread] = State::flushing;
state[it->thread] = State::flushing;
s.thread = it++->thread;
thread = it++->thread;
});
}
......
......@@ -7,7 +7,6 @@
#include "instruction.hh"
// TODO: forward-declare
#include "mmap.hh"
#include "program.hh"
#include "trace.hh"
......@@ -17,6 +16,8 @@ namespace ConcuBinE {
// forward declarations
//==============================================================================
class MMap;
//==============================================================================
// Simulator class
//==============================================================================
......@@ -44,24 +45,25 @@ struct Simulator
//----------------------------------------------------------------------------
// Mersenne Twister pseudo-random number generator
//
std::mt19937_64 random;
// list of programs
//
Program::List::ptr programs;
const Program::List * programs;
// generated trace
//
Trace::ptr trace;
// current step
//
size_t step;
// bound
//
size_t bound;
// current step
//
size_t step;
// current thread
//
word_t thread;
......@@ -92,14 +94,18 @@ struct Simulator
// constructors
//----------------------------------------------------------------------------
Simulator (const Program::List::ptr & programs,
const std::shared_ptr<MMap> & mmap = {},
size_t bound = 0);
Simulator ();
//----------------------------------------------------------------------------
// private functions
//----------------------------------------------------------------------------
// (re)initialize
//
void init (const Program::List::ptr & programs,
const std::shared_ptr<MMap> & mmap,
size_t bound);
// program counter
//
word_t pc () const;
......@@ -189,15 +195,15 @@ struct Simulator
// public functions
//----------------------------------------------------------------------------
// runs the simulator using a random trace
// simulate given programs using a random scheduler
//
static Trace::ptr simulate (const Program::List::ptr & programs,
const std::shared_ptr<MMap> & mmap,
size_t bound = 0);
Trace::ptr simulate (const Program::List::ptr & programs,
const std::shared_ptr<MMap> & mmap = {},
size_t bound = 0);
// replay the given trace (trace must match simulator configuration)
// replay given trace
//
static Trace::ptr replay (const Trace & trace, size_t bound = 0);
Trace::ptr replay (const Trace & trace, size_t bound = 0);
};
//==============================================================================
......
......@@ -50,9 +50,7 @@ TEST_F(Boolector, solve_check)
// std::cout << trace->print();
Simulator simulator (programs);
Trace::ptr replay (simulator.replay(*trace));
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......@@ -77,9 +75,7 @@ TEST_F(Boolector, solve_cas)
// std::cout << trace->print();
Simulator simulator (programs);
Trace::ptr replay (simulator.replay(*trace));
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......@@ -110,9 +106,7 @@ TEST_F(Boolector, solve_indirect_uninitialized)
// std::cout << trace->print();
Simulator simulator (programs);
Trace::ptr replay (simulator.replay(*trace));
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......
......@@ -62,9 +62,7 @@ TEST_F(BtorMC, solve_check)
// std::cout << trace->print();
Simulator simulator {programs};
Trace::ptr replay {simulator.replay(*trace)};
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......@@ -89,9 +87,7 @@ TEST_F(BtorMC, solve_cas)
// std::cout << trace->print();
Simulator simulator {programs};
Trace::ptr replay {simulator.replay(*trace)};
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......@@ -122,9 +118,7 @@ TEST_F(BtorMC, solve_indirect_uninitialized)
// std::cout << trace->print();
Simulator simulator (programs);
Trace::ptr replay (simulator.replay(*trace));
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......
......@@ -50,9 +50,7 @@ TEST_F(CVC4, solve_check)
// std::cout << trace->print();
Simulator simulator (programs);
Trace::ptr replay (simulator.replay(*trace));
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......@@ -75,9 +73,7 @@ TEST_F(CVC4, solve_cas)
// std::cout << trace->print();
Simulator simulator (programs);
Trace::ptr replay (simulator.replay(*trace));
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......@@ -108,9 +104,7 @@ TEST_F(CVC4, solve_indirect_uninitialized)
// std::cout << trace->print();
Simulator simulator (programs);
Trace::ptr replay (simulator.replay(*trace));
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......
This diff is collapsed.
......@@ -3,6 +3,7 @@
#include "trace.hh"
#include "instruction.hh"
#include "mmap.hh"
#include "parser.hh"
namespace ConcuBinE::test {
......
......@@ -49,9 +49,7 @@ TEST_F(Z3, solve_check)
// std::cout << trace->print();
Simulator simulator (programs);
Trace::ptr replay (simulator.replay(*trace));
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......@@ -77,9 +75,7 @@ TEST_F(Z3, solve_cas)
// std::cout << trace->print();
Simulator simulator (programs);
Trace::ptr replay (simulator.replay(*trace));
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......@@ -110,9 +106,7 @@ TEST_F(Z3, solve_indirect_uninitialized)
// std::cout << trace->print();
Simulator simulator (programs);
Trace::ptr replay (simulator.replay(*trace));
Trace::ptr replay = Simulator().replay(*trace);
// std::cout << replay->print();
......
......@@ -4,6 +4,7 @@
#include <sstream>
#include "instruction.hh"
#include "mmap.hh"
#include "parser.hh"
namespace ConcuBinE {
......
......@@ -5,11 +5,16 @@
#include <unordered_map>
#include <vector>
#include "mmap.hh"
#include "program.hh"
namespace ConcuBinE {
//==============================================================================
// forward declarations
//==============================================================================
class MMap;
//==============================================================================
// Trace class
//==============================================================================
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment