mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 10:56:30 +00:00
run in thread
This commit is contained in:
parent
5591b628db
commit
a3827243ff
@ -7,17 +7,16 @@
|
||||
|
||||
using namespace sliger::rpc::action;
|
||||
|
||||
auto sliger::rpc::action::RunDebug::perform_action(
|
||||
std::unique_ptr<sliger::rpc::BufferedWriter> writer,
|
||||
auto Status::perform_action(std::unique_ptr<sliger::rpc::BufferedWriter> writer,
|
||||
vm_provider::VmProvider& vm) -> void
|
||||
{
|
||||
auto program = this->instructions;
|
||||
vm.load_and_run(program);
|
||||
writer->write("{ \"ok\": true }");
|
||||
bool running = not vm.done();
|
||||
|
||||
writer->write(std::format("{{ \"ok\": true, \"running\": {} }}", running));
|
||||
writer->flush();
|
||||
};
|
||||
|
||||
auto sliger::rpc::action::FlameGraph::perform_action(
|
||||
auto FlameGraph::perform_action(
|
||||
std::unique_ptr<sliger::rpc::BufferedWriter> writer,
|
||||
vm_provider::VmProvider& vm) -> void
|
||||
{
|
||||
@ -31,7 +30,7 @@ auto sliger::rpc::action::FlameGraph::perform_action(
|
||||
writer->flush();
|
||||
};
|
||||
|
||||
auto sliger::rpc::action::CodeCoverage::perform_action(
|
||||
auto CodeCoverage::perform_action(
|
||||
std::unique_ptr<sliger::rpc::BufferedWriter> writer,
|
||||
vm_provider::VmProvider& vm) -> void
|
||||
{
|
||||
@ -45,8 +44,18 @@ auto sliger::rpc::action::CodeCoverage::perform_action(
|
||||
writer->flush();
|
||||
};
|
||||
|
||||
auto sliger::rpc::action::action_from_json(const sliger::json::Value& value)
|
||||
-> std::unique_ptr<Action>
|
||||
auto RunDebug::perform_action(
|
||||
std::unique_ptr<sliger::rpc::BufferedWriter> writer,
|
||||
vm_provider::VmProvider& vm) -> void
|
||||
{
|
||||
auto program = this->instructions;
|
||||
vm.load_and_run(program);
|
||||
writer->write("{ \"ok\": true }");
|
||||
writer->flush();
|
||||
};
|
||||
|
||||
auto sliger::rpc::action::action_from_json(
|
||||
const sliger::json::Value& value) -> std::unique_ptr<Action>
|
||||
{
|
||||
auto& obj = value.as<sliger::json::Object>();
|
||||
auto type = obj.fields.at("type")->as<sliger::json::String>();
|
||||
|
@ -6,11 +6,17 @@ namespace sliger::rpc::action {
|
||||
|
||||
struct Action {
|
||||
virtual auto perform_action(std::unique_ptr<BufferedWriter> writer,
|
||||
vm_provider::VmProvider& vm_provider) -> void
|
||||
= 0;
|
||||
vm_provider::VmProvider& vm_provider) -> void = 0;
|
||||
virtual ~Action() = default;
|
||||
};
|
||||
|
||||
class Status : public Action {
|
||||
public:
|
||||
Status() { }
|
||||
auto perform_action(std::unique_ptr<BufferedWriter> writer,
|
||||
vm_provider::VmProvider& vm_provider) -> void;
|
||||
};
|
||||
|
||||
class FlameGraph : public Action {
|
||||
public:
|
||||
FlameGraph() { }
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include "rpc_server.hpp"
|
||||
#include "vm.hpp"
|
||||
#include "vm_provider.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <format>
|
||||
@ -68,13 +67,13 @@ int main(int argc, char** argv)
|
||||
return execute_file_and_exit(argv[2], print_debug);
|
||||
}
|
||||
|
||||
auto state = sliger::rpc::vm_provider::VmProvider();
|
||||
auto vm_provider = sliger::rpc::vm_provider::VmProvider();
|
||||
|
||||
auto rpc = sliger::rpc::RpcServer(
|
||||
[&](std::unique_ptr<sliger::json::Value> req,
|
||||
std::unique_ptr<sliger::rpc::BufferedWriter> writer) {
|
||||
auto action = sliger::rpc::action::action_from_json(*req);
|
||||
action->perform_action(std::move(writer), state);
|
||||
action->perform_action(std::move(writer), vm_provider);
|
||||
});
|
||||
|
||||
std::cout << "binding on 127.0.0.1:13370\n";
|
||||
|
@ -157,6 +157,8 @@ public:
|
||||
void run_n_instructions(size_t amount);
|
||||
void run_instruction();
|
||||
|
||||
inline auto done() const -> bool { return this->pc >= this->program_size; }
|
||||
|
||||
inline auto flame_graph_json() const -> std::string
|
||||
{
|
||||
return json::to_json(this->flame_graph);
|
||||
@ -215,11 +217,6 @@ private:
|
||||
return this->program[this->pc];
|
||||
}
|
||||
|
||||
inline auto done() const -> bool
|
||||
{
|
||||
return not this->halt and this->pc >= this->program_size;
|
||||
}
|
||||
|
||||
inline auto fn_stack_at(size_t idx) -> Value&
|
||||
{
|
||||
return this->stack.at(this->bp + idx);
|
||||
@ -254,8 +251,6 @@ private:
|
||||
{ 2, stderr },
|
||||
};
|
||||
|
||||
bool halt = false;
|
||||
|
||||
FlameGraphBuilder flame_graph;
|
||||
CodeCoverageBuilder code_coverage;
|
||||
};
|
||||
|
@ -1,22 +1,32 @@
|
||||
#include "vm_provider.hpp"
|
||||
#include "vm.hpp"
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
using namespace sliger::rpc::vm_provider;
|
||||
|
||||
auto VmProvider::load_and_run(std::vector<uint32_t> instructions) -> void
|
||||
{
|
||||
auto vm = VM(instructions,
|
||||
std::lock_guard lock(this->mutex);
|
||||
|
||||
this->vm = VM(instructions,
|
||||
{
|
||||
.flame_graph = true,
|
||||
.code_coverage = true,
|
||||
.print_debug = false,
|
||||
});
|
||||
vm.run_until_done();
|
||||
this->vm = vm;
|
||||
|
||||
this->running_thread = std::thread([&]() {
|
||||
while (not this->done()) {
|
||||
this->run_timeslot();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
auto VmProvider::flame_graph_json() -> std::optional<std::string>
|
||||
{
|
||||
std::lock_guard lock(this->mutex);
|
||||
|
||||
if (this->vm) {
|
||||
return this->vm->flame_graph_json();
|
||||
} else {
|
||||
@ -26,9 +36,28 @@ auto VmProvider::flame_graph_json() -> std::optional<std::string>
|
||||
|
||||
auto VmProvider::code_coverage_json() -> std::optional<std::string>
|
||||
{
|
||||
std::lock_guard lock(this->mutex);
|
||||
|
||||
if (this->vm) {
|
||||
return this->vm->code_coverage_json();
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
void VmProvider::run_timeslot()
|
||||
{
|
||||
std::lock_guard lock(this->mutex);
|
||||
|
||||
if (!this->vm.has_value())
|
||||
return;
|
||||
|
||||
this->vm->run_n_instructions(100);
|
||||
}
|
||||
|
||||
auto VmProvider::done() -> bool
|
||||
{
|
||||
std::lock_guard lock(this->mutex);
|
||||
|
||||
return not this->vm.has_value() or this->vm->done();
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "vm.hpp"
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
namespace sliger::rpc::vm_provider {
|
||||
class VmProvider {
|
||||
@ -11,8 +13,15 @@ public:
|
||||
auto flame_graph_json() -> std::optional<std::string>;
|
||||
auto code_coverage_json() -> std::optional<std::string>;
|
||||
|
||||
auto done() -> bool;
|
||||
|
||||
private:
|
||||
void run_timeslot();
|
||||
|
||||
std::mutex mutex;
|
||||
|
||||
std::optional<VM> vm;
|
||||
std::optional<std::thread> running_thread;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user