Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

テストを追加 #54

Open
wants to merge 21 commits into
base: feature/experiment
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions include/common/configure.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ class StatisticsConfigure {
CostMeasuring(RHS.CostMeasuring.load(std::memory_order_relaxed)),
TimeMeasuring(RHS.TimeMeasuring.load(std::memory_order_relaxed)),
DumpFlag(RHS.DumpFlag.load(std::memory_order_relaxed)),
RestoreFlag(RHS.RestoreFlag.load(std::memory_order_relaxed)) {}
RestoreFlag(RHS.RestoreFlag.load(std::memory_order_relaxed)) ,
ImageDir(RHS.getImageDir()){}

void setInstructionCounting(bool IsCount) noexcept {
InstrCounting.store(IsCount, std::memory_order_relaxed);
Expand Down Expand Up @@ -176,15 +177,33 @@ class StatisticsConfigure {
uint64_t getCostLimit() const noexcept {
return CostLimit.load(std::memory_order_relaxed);
}


void setDispatchLimit(uint64_t Count) noexcept {
DispatchLimit.store(Count, std::memory_order_relaxed);
}

uint64_t getDispatchLimit() const noexcept {
return DispatchLimit.load(std::memory_order_relaxed);
}

void setDumpFlag(bool flag) noexcept {
DumpFlag.store(flag, std::memory_order_relaxed);
}

bool getDumpFlag() const noexcept {
return DumpFlag.load(std::memory_order_relaxed);
}

void setImageDir(std::string dir) noexcept {
std::unique_lock Lock(Mutex);
ImageDir = dir;
}

std::string getImageDir() const noexcept {
std::shared_lock Lock(Mutex);
return ImageDir;
}

void setRestoreFlag(bool flag) noexcept {
RestoreFlag.store(flag, std::memory_order_relaxed);
}
Expand All @@ -196,7 +215,7 @@ class StatisticsConfigure {
void setDebugMode(bool flag) noexcept {
DebugMode.store(flag, std::memory_order_relaxed);
}

bool getDebugMode() const noexcept {
return DebugMode.load(std::memory_order_relaxed);
}
Expand All @@ -209,6 +228,10 @@ class StatisticsConfigure {
std::atomic<bool> RestoreFlag = false;
std::atomic<bool> DebugMode = false;
std::atomic<uint64_t> CostLimit = UINT64_C(-1);
std::atomic<uint64_t> DispatchLimit = UINT64_C(-1);

mutable std::shared_mutex Mutex;
std::string ImageDir = "";
};

class Configure {
Expand Down
9 changes: 7 additions & 2 deletions include/common/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ namespace Statistics {
class Statistics {
public:
Statistics(const uint64_t Lim = UINT64_MAX)
: CostTab(UINT16_MAX + 1, 1ULL), InstrCnt(0), CostLimit(Lim), CostSum(0) {
: CostTab(UINT16_MAX + 1, 1ULL), InstrCnt(0), CostLimit(Lim), DispatchLimit(Lim), CostSum(0) {
}
Statistics(Span<const uint64_t> Tab, const uint64_t Lim = UINT64_MAX)
: CostTab(Tab.begin(), Tab.end()), InstrCnt(0), CostLimit(Lim),
: CostTab(Tab.begin(), Tab.end()), InstrCnt(0), CostLimit(Lim), DispatchLimit(Lim),
CostSum(0) {
if (CostTab.size() < UINT16_MAX + 1) {
CostTab.resize(UINT16_MAX + 1, 0ULL);
Expand Down Expand Up @@ -81,6 +81,10 @@ class Statistics {
void setCostLimit(uint64_t Lim) { CostLimit = Lim; }
uint64_t getCostLimit() const { return CostLimit; }

/// Getter and setter of dispatch limit.
void setDispatchLimit(uint64_t Lim) { DispatchLimit = Lim; }
uint64_t getDispatchLimit() const { return DispatchLimit; }

/// Add cost and return false if exceeded limit.
bool addCost(uint64_t Cost) {
const auto Limit = CostLimit;
Expand Down Expand Up @@ -198,6 +202,7 @@ class Statistics {
std::vector<uint64_t> CostTab;
std::atomic_uint64_t InstrCnt;
uint64_t CostLimit;
uint64_t DispatchLimit;
std::atomic_uint64_t CostSum;
Timer::Timer TimeRecorder;
bool DumpFlag;
Expand Down
9 changes: 9 additions & 0 deletions include/driver/tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ struct DriverToolOptions {
PO::Description("Forcibly run WASM in interpreter mode."sv)),
DumpFlag(PO::Description("Unsnapshot statement to img files."sv)),
RestoreFlag(PO::Description("Restore statement by img files."sv)),
ImageDir(PO::Description("Restore statement by img files."sv), PO::MetaVar("IMAGE_DIR"sv)),
TimeLim(
PO::Description(
"Limitation of maximum time(in milliseconds) for execution, default value is 0 for no limitations"sv),
PO::MetaVar("TIMEOUT"sv), PO::DefaultValue<uint64_t>(0)),
DispatchLim(
PO::Description(
"Limitation of maximum dispatch(execution count) for execution, default value is 0 for no limitations"sv),
PO::MetaVar("DISPATCH_LIMIT"sv), PO::DefaultValue<uint64_t>(0)),
GasLim(
PO::Description(
"Limitation of execution gas. Upper bound can be specified as --gas-limit `GAS_LIMIT`."sv),
Expand Down Expand Up @@ -111,8 +116,10 @@ struct DriverToolOptions {
PO::Option<PO::Toggle> ConfForceInterpreter;
PO::Option<PO::Toggle> DumpFlag;
PO::Option<PO::Toggle> RestoreFlag;
PO::List<std::string> ImageDir;
PO::Option<PO::Toggle> DebugMode;
PO::Option<uint64_t> TimeLim;
PO::Option<uint64_t> DispatchLim;
PO::List<int> GasLim;
PO::List<int> MemLim;
PO::List<std::string> ForbiddenPlugins;
Expand Down Expand Up @@ -143,8 +150,10 @@ struct DriverToolOptions {
.add_option("enable-all"sv, PropAll)
.add_option("no-snapshot"sv, DumpFlag)
.add_option("restore"sv, RestoreFlag)
.add_option("image-dir"sv, ImageDir)
.add_option("debug-mode"sv, DebugMode)
.add_option("time-limit"sv, TimeLim)
.add_option("dispatch-limit"sv, DispatchLim)
.add_option("gas-limit"sv, GasLim)
.add_option("memory-page-limit"sv, MemLim)
.add_option("forbidden-plugin"sv, ForbiddenPlugins);
Expand Down
1 change: 1 addition & 0 deletions include/executor/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Executor {
}
if (Stat) {
Stat->setCostLimit(Conf.getStatisticsConfigure().getCostLimit());
Stat->setDispatchLimit(Conf.getStatisticsConfigure().getDispatchLimit());
}
}
~Executor() noexcept {
Expand Down
50 changes: 32 additions & 18 deletions include/executor/migrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Migrator {
/// TODO: ModuleInstanceがnullだったときの名前。重複しないようにする
const std::string NULL_MOD_NAME = "null";
const std::string TYPE_TABLE = "type_table";
const std::string TYPE_TABLEMAP_FUNC = "type_tablemap_func";
const std::string TYPE_TABLEMAP_OFFSET = "type_tablemap_offset";
const std::string TYPE_TABLEMAP_FUNC = "tablemap_func";
const std::string TYPE_TABLEMAP_OFFSET = "tablemap_offset";
struct CtrlInfo {
// AST::InstrView::iterator Iter;
uint32_t BeginAddrOfs;
Expand All @@ -47,7 +47,7 @@ class Migrator {
/// ================

// void Prepare(const Runtime::Instance::ModuleInstance* ModInst) {
void Prepare(const Runtime::Instance::ModuleInstance* ModInst) {
void Prepare(const Runtime::Instance::ModuleInstance* ModInst, std::string dirname) {
for (uint32_t I = 0; I < ModInst->getFuncNum(); ++I) {
Runtime::Instance::FunctionInstance* FuncInst = ModInst->getFunc(I).value();
AST::InstrView Instr = FuncInst->getInstrs();
Expand All @@ -61,6 +61,19 @@ class Migrator {
// 昇順ソート
std::sort(ik.AddrVec.begin(), ik.AddrVec.end());

if (dirname.size() > 0) {
// dirnameの終端文字は/
if (dirname.back() != '/') {
dirname.push_back('/');
}

// ディレクトリがなければ作成する
if (!std::filesystem::is_directory(dirname)) {
std::filesystem::create_directory(dirname);
}
}
ImageDir = dirname;

BaseModName = ModInst->getModuleName();
}

Expand Down Expand Up @@ -157,11 +170,11 @@ class Migrator {
/// ================
void debugFrame(uint32_t FrameIdx, uint32_t EnterFuncIdx, uint32_t Locals, uint32_t Arity, uint32_t VPos) {
std::string DebugPrefix = "[DEBUG]";
std::cerr << DebugPrefix << "Frame Idx: " << FrameIdx << std::endl;
std::cerr << DebugPrefix << "EnterFuncIdx: " << EnterFuncIdx << std::endl;
std::cerr << DebugPrefix << "Locals: " << Locals << std::endl;
std::cerr << DebugPrefix << "Arity: " << Arity << std::endl;
std::cerr << DebugPrefix << "VPos: " << VPos << std::endl;
std::cerr << DebugPrefix << "Frame Idx : " << FrameIdx << std::endl;
std::cerr << DebugPrefix << "EnterFuncIdx : " << EnterFuncIdx << std::endl;
std::cerr << DebugPrefix << "Locals : " << Locals << std::endl;
std::cerr << DebugPrefix << "Arity : " << Arity << std::endl;
std::cerr << DebugPrefix << "VPos : " << VPos << std::endl;
std::cerr << std::endl;
}

Expand Down Expand Up @@ -225,16 +238,16 @@ class Migrator {
/// Dump functions
/// ================
void dumpMemory(const Runtime::Instance::ModuleInstance* ModInst) {
ModInst->dumpMemInst();
ModInst->dumpMemInst(ImageDir);
}

void dumpGlobal(const Runtime::Instance::ModuleInstance* ModInst) {
ModInst->dumpGlobInst();
ModInst->dumpGlobInst(ImageDir);
}


Expect<void> dumpProgramCounter(const Runtime::Instance::ModuleInstance* ModInst, AST::InstrView::iterator Iter) {
std::ofstream ofs("program_counter.img", std::ios::trunc | std::ios::binary);
std::ofstream ofs(ImageDir + "program_counter.img", std::ios::trunc | std::ios::binary);
if (!ofs) {
return Unexpect(ErrCode::Value::IllegalPath);
}
Expand All @@ -251,7 +264,7 @@ class Migrator {
std::vector<Runtime::StackManager::Frame> FrameStack = StackMgr.getFrameStack();
std::vector<ValVariant> ValueStack = StackMgr.getValueStack();
std::vector<std::vector<uint8_t>> TypeStacks(FrameStack.size());
std::ofstream frame_fout("frame.img", std::ios::trunc | std::ios::binary);
std::ofstream frame_fout(ImageDir + "frame.img", std::ios::trunc | std::ios::binary);

// header file. frame stackのサイズを記録
uint32_t LenFrame = FrameStack.size()-1;
Expand Down Expand Up @@ -286,7 +299,7 @@ class Migrator {
// フレームスタックを上から見ていく。上からstack1, stack2...とする
StackIdx = 1;
for (size_t I = FrameStack.size()-1; I > 0; --I, ++StackIdx) {
std::ofstream ofs("stack" + std::to_string(StackIdx) + ".img", std::ios::trunc | std::ios::binary);
std::ofstream ofs(ImageDir + "stack" + std::to_string(StackIdx) + ".img", std::ios::trunc | std::ios::binary);
Runtime::StackManager::Frame f = FrameStack[I];

// ModuleInstance
Expand Down Expand Up @@ -355,11 +368,11 @@ class Migrator {
/// Restore functions
/// ================
void restoreMemory(const Runtime::Instance::ModuleInstance* ModInst) {
ModInst->restoreMemInst();
ModInst->restoreMemInst(ImageDir);
}

void restoreGlobal(const Runtime::Instance::ModuleInstance* ModInst) {
ModInst->restoreGlobInst();
ModInst->restoreGlobInst(ImageDir);
}

Expect<AST::InstrView::iterator> _restoreIter(const Runtime::Instance::ModuleInstance* ModInst, uint32_t FuncIdx, uint32_t Offset) {
Expand Down Expand Up @@ -417,7 +430,7 @@ class Migrator {
}

Expect<AST::InstrView::iterator> restoreProgramCounter(const Runtime::Instance::ModuleInstance* ModInst) {
std::ifstream ifs("program_counter.img", std::ios::binary);
std::ifstream ifs(ImageDir + "program_counter.img", std::ios::binary);

uint32_t FuncIdx, Offset;
ifs.read(reinterpret_cast<char *>(&FuncIdx), sizeof(uint32_t));
Expand All @@ -433,14 +446,14 @@ class Migrator {
const Runtime::Instance::ModuleInstance *Module = StackMgr.getModule();

uint32_t LenFrame;
std::ifstream ifs("frame.img", std::ios::binary);
std::ifstream ifs(ImageDir + "frame.img", std::ios::binary);
ifs.read(reinterpret_cast<char *>(&LenFrame), sizeof(uint32_t));
ifs.close();

// LenFrame-1から始まるのは、Stack{LenFrame}.imgがダミーフレームだから
AST::InstrView::iterator PC = StackMgr.popFrame();
for (size_t I = LenFrame; I > 0; --I) {
ifs.open("stack" + std::to_string(I) + ".img", std::ios::binary);
ifs.open(ImageDir + "stack" + std::to_string(I) + ".img", std::ios::binary);

// 関数インデックスのロード
uint32_t EnterFuncIdx;
Expand Down Expand Up @@ -509,6 +522,7 @@ class Migrator {
/// \name Module name mapping.
std::map<std::string, const Runtime::Instance::ModuleInstance *, std::less<>> NamedMod;
IteratorKeys ik;
std::string ImageDir;
};

} // namespace Runtime
Expand Down
20 changes: 10 additions & 10 deletions include/runtime/instance/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,37 +181,37 @@ class ModuleInstance {
}

// Migration function
void dumpMemInst() const noexcept {
void dumpMemInst(const std::string ImageDir) const noexcept {
// std::unique_lock Lock(Mutex);
// MemoryInstanceの保存
for (uint32_t I = 0; I < getMemoryNum(); ++I) {
auto Res = getMemory(I);
MemoryInstance* MemInst = Res.value();
if (I == 0) {
MemInst->dump("");
MemInst->dump(ImageDir);
}
else {
MemInst->dump(std::to_string(I));
MemInst->dump(ImageDir + std::to_string(I));
}
}
}

void restoreMemInst() const noexcept {
void restoreMemInst(const std::string ImageDir) const noexcept {
for (uint32_t I = 0; I < getMemoryNum(); ++I) {
auto Res = getMemory(I);
MemoryInstance* MemInst = Res.value();
if (I == 0) {
MemInst->restore("");
MemInst->restore(ImageDir);
}
else {
MemInst->restore(std::to_string(I));
MemInst->restore(ImageDir + std::to_string(I));
}

}
}

Expect<void> dumpGlobInst() const noexcept {
std::ofstream ofs("global.img", std::ios::trunc | std::ios::binary);
Expect<void> dumpGlobInst(const std::string ImageDir) const noexcept {
std::ofstream ofs(ImageDir + "global.img", std::ios::trunc | std::ios::binary);
if (!ofs) {
return Unexpect(ErrCode::Value::IllegalPath);
}
Expand All @@ -224,8 +224,8 @@ class ModuleInstance {
return {};
}

Expect<void> restoreGlobInst() const noexcept {
std::ifstream ifs("global.img", std::ios::binary);
Expect<void> restoreGlobInst(const std::string ImageDir) const noexcept {
std::ifstream ifs(ImageDir + "global.img", std::ios::binary);
if (!ifs) {
return Unexpect(ErrCode::Value::IllegalPath);
}
Expand Down
10 changes: 10 additions & 0 deletions lib/driver/runtimeTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ int Tool(struct DriverToolOptions &Opt) noexcept {
Timeout = std::chrono::system_clock::now() +
std::chrono::milliseconds(Opt.TimeLim.value());
}
if (Opt.DispatchLim.value() > 0) {
std::cerr << "Opt.DispatchLim.value(): " << Opt.DispatchLim.value() << std::endl;
Conf.getStatisticsConfigure().setInstructionCounting(true);
Conf.getStatisticsConfigure().setDispatchLimit(
static_cast<uint32_t>(Opt.DispatchLim.value()));
}
if (Opt.GasLim.value().size() > 0) {
Conf.getStatisticsConfigure().setCostMeasuring(true);
Conf.getStatisticsConfigure().setCostLimit(
Expand Down Expand Up @@ -114,6 +120,10 @@ int Tool(struct DriverToolOptions &Opt) noexcept {
for (const auto &Name : Opt.ForbiddenPlugins.value()) {
Conf.addForbiddenPlugins(Name);
}
for (const auto &ImageDir : Opt.ImageDir.value()) {
Conf.getStatisticsConfigure().setImageDir(ImageDir);
break;
}

Conf.addHostRegistration(HostRegistration::Wasi);
const auto InputPath =
Expand Down
Loading