vm provider; code coverage + flame graph actions

This commit is contained in:
Theis Pieter Hollebeek 2024-12-09 13:03:21 +01:00
parent 8deda48ca6
commit 5b4057160d
5 changed files with 119 additions and 3 deletions

36
runtime/actions.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "actions.hpp"
#include "vm_provider.hpp"
auto sliger::rpc::action::RunDebug::perform_action(
std::unique_ptr<sliger::rpc::BufferedWriter> writer,
vm_provider::VmProvider vm) -> void
{
auto program = this->instructions;
vm.load_and_run(program);
};
auto sliger::rpc::action::FlameGraph::perform_action(
std::unique_ptr<sliger::rpc::BufferedWriter> writer,
vm_provider::VmProvider vm) -> void
{
auto json = vm.flame_graph_json();
if (json) {
writer->write(json.value());
} else {
writer->write("null");
}
writer->flush();
};
auto sliger::rpc::action::CodeCoverage::perform_action(
std::unique_ptr<sliger::rpc::BufferedWriter> writer,
vm_provider::VmProvider vm) -> void
{
auto json = vm.code_coverage_json();
if (json) {
writer->write(json.value());
} else {
writer->write("null");
}
writer->flush();
};

View File

@ -1,22 +1,38 @@
#include "rpc_server.hpp"
#include "vm_provider.hpp"
#include <memory>
namespace sliger::rpc::action {
struct Action {
virtual auto perform_action(
std::unique_ptr<sliger::rpc::BufferedWriter> writer) -> void = 0;
std::unique_ptr<sliger::rpc::BufferedWriter> writer,
sliger::rpc::vm_provider::VmProvider vm_provider) -> void = 0;
virtual ~Action() = default;
};
class FlameGraph : public Action {
public:
FlameGraph() { }
auto perform_action(std::unique_ptr<sliger::rpc::BufferedWriter> writer,
sliger::rpc::vm_provider::VmProvider vm_provider) -> void;
};
class CodeCoverage : public Action {
public:
CodeCoverage() { }
auto perform_action(std::unique_ptr<sliger::rpc::BufferedWriter> writer,
sliger::rpc::vm_provider::VmProvider vm_provider) -> void;
};
class RunDebug : public Action {
public:
RunDebug(std::vector<uint32_t> instructions)
: instructions(instructions)
{
}
auto perform_action(
std::unique_ptr<sliger::rpc::BufferedWriter> writer) -> void;
auto perform_action(std::unique_ptr<sliger::rpc::BufferedWriter> writer,
sliger::rpc::vm_provider::VmProvider vm_provider) -> void;
private:
std::vector<uint32_t> instructions;
@ -28,6 +44,16 @@ static auto action_from_json(
auto& obj = value->as<sliger::json::Object>();
auto type = obj.fields.at("type")->as<sliger::json::String>();
if (type.value == "flamegraph") {
auto action = FlameGraph();
return std::make_unique<FlameGraph>(action);
}
if (type.value == "code-coverage") {
auto action = CodeCoverage();
return std::make_unique<CodeCoverage>(action);
}
if (type.value == "run-debug") {
sliger::json::ArrayValues values = std::move(
obj.fields.at("program")->as<sliger::json::Array>().values);

View File

@ -1,9 +1,12 @@
#include "actions.hpp"
#include "json.hpp"
#include "rpc_server.hpp"
#include "vm_provider.hpp"
int main()
{
auto state = sliger::rpc::vm_provider::VmProvider();
auto rpc = sliger::rpc::RpcServer(
[&](std::unique_ptr<sliger::json::Value> req,
std::unique_ptr<sliger::rpc::BufferedWriter> writer) {

33
runtime/vm_provider.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "vm_provider.hpp"
#include "vm.hpp"
using namespace sliger::rpc::vm_provider;
auto VmProvider::load_and_run(std::vector<uint32_t> instructions) -> void
{
auto vm = VM(instructions,
{
.flame_graph = true,
.code_coverage = true,
});
vm.run_until_done();
this->vm = vm;
}
auto VmProvider::flame_graph_json() -> std::optional<std::string>
{
if (this->vm) {
return this->vm->flame_graph_json();
} else {
return {};
}
}
auto VmProvider::code_coverage_json() -> std::optional<std::string>
{
if (this->vm) {
return this->vm->code_coverage_json();
} else {
return {};
}
}

18
runtime/vm_provider.hpp Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include "vm.hpp"
namespace sliger::rpc::vm_provider {
class VmProvider {
public:
VmProvider() { }
auto load_and_run(std::vector<uint32_t> instructions) -> void;
auto flame_graph_json() -> std::optional<std::string>;
auto code_coverage_json() -> std::optional<std::string>;
private:
std::optional<VM> vm;
};
}