Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Mar 12, 2024
1 parent 13ea74d commit 881e40a
Show file tree
Hide file tree
Showing 20 changed files with 335 additions and 23 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if git.found()
endif

config_h.set_quoted('NIXD_VERSION', meson.project_version())
config_h.set_quoted('NIXD_LIBEXEC', get_option('prefix') / get_option('libexecdir'))

configure_file(
output: 'nixd-config.h',
Expand Down
31 changes: 25 additions & 6 deletions nixd/librpc/include/nixd/rpc/Protocol.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "Protocol.h"
#include <bc/Write.h>

#include <cstdint>
#include <cstring>
Expand Down Expand Up @@ -30,6 +30,22 @@ enum class RPCKind : uint8_t {
ExprValue,
};

template <class T> struct Message {
RPCKind Kind;
T Params;
};

template <class T> void writeBytecode(std::ostream &OS, const Message<T> &Msg) {
using bc::writeBytecode;
writeBytecode(OS, Msg.Kind);
writeBytecode(OS, Msg.Params);
}

template <class T> void readBytecode(std::string_view &Data, Message<T> &Msg) {
readBytecode(Data, Msg.Kind);
readBytecode(Data, Msg.Params);
}

struct RegisterBCParams {
std::string Shm;
std::size_t Size;
Expand All @@ -45,10 +61,10 @@ struct ExprValueParams {
};

void writeBytecode(std::ostream &OS, const ExprValueParams &Params);
void readBytecode(std::string_view &Data, const ExprValueParams &Params);
void readBytecode(std::string_view &Data, ExprValueParams &Params);

struct ExprValueResponse {
enum class Kind {
enum ResultKinds {
/// \brief The expr is not found in the registered bytecodes.
NotFound,

Expand All @@ -60,18 +76,21 @@ struct ExprValueResponse {

/// \brief The value is available.
OK,
};
} ResultKind;
/// \brief The value ID, for future reference.
///
/// We may want to query the value of the same expr multiple times, with more
/// detailed information.
std::uintptr_t ValueID;

/// \brief Opaque data, the value of the expr.
enum class ValueKind {
enum ValueKinds {
Int,
Float,
};
} ValueKind;
};

void writeBytecode(std::ostream &OS, const ExprValueResponse &Params);
void readBytecode(std::string_view &Data, ExprValueResponse &Params);

} // namespace nixd::rpc
24 changes: 21 additions & 3 deletions nixd/librpc/src/Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
#include <bc/Read.h>
#include <bc/Write.h>

#include <cstring>
#include <sstream>

namespace nixd::rpc {

using bc::readBytecode;
Expand All @@ -23,4 +20,25 @@ void readBytecode(std::string_view &Data, RegisterBCParams &Params) {
readBytecode(Data, Params.BCID);
}

void writeBytecode(std::ostream &OS, const ExprValueParams &Params) {
writeBytecode(OS, Params.ExprID);
writeBytecode(OS, Params.BCID);
}

void readBytecode(std::string_view &Data, ExprValueParams &Params) {
readBytecode(Data, Params.ExprID);
readBytecode(Data, Params.BCID);
}

void writeBytecode(std::ostream &OS, const ExprValueResponse &Params) {
writeBytecode(OS, Params.ValueKind);
writeBytecode(OS, Params.ValueID);
writeBytecode(OS, Params.ResultKind);
}
void readBytecode(std::string_view &Data, ExprValueResponse &Params) {
readBytecode(Data, Params.ValueKind);
readBytecode(Data, Params.ValueID);
readBytecode(Data, Params.ResultKind);
}

} // namespace nixd::rpc
5 changes: 4 additions & 1 deletion nixd/libutil/include/nixd/util/AutoHUPPID.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//
#include <csignal>

#include <sched.h>
Expand All @@ -10,7 +11,9 @@ class AutoHUPPID {
public:
AutoHUPPID(pid_t Pid) noexcept : Pid(Pid) {}

~AutoHUPPID() { kill(Pid, SIGHUP); }
~AutoHUPPID() { kill(Pid, SIGKILL); }

operator pid_t() const { return Pid; }
};

} // namespace nixd::util
1 change: 1 addition & 0 deletions nixd/tools/meson.build
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
subdir('nixd')
subdir('nix-node-eval')
3 changes: 3 additions & 0 deletions nixd/tools/nix-node-eval/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# nix-node-eval

Collect per-node eval information, by using official evaluator.
7 changes: 7 additions & 0 deletions nixd/tools/nix-node-eval/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
nix_node_eval = executable('nix-node-eval',
'src/EvalProvider.cpp',
'src/Main.cpp',
install: true,
install_dir: get_option('libexecdir'),
dependencies: [ libnixdrpc, nixt ]
)
37 changes: 37 additions & 0 deletions nixd/tools/nix-node-eval/src/EvalProvider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "EvalProvider.h"

#include "nixd/rpc/Protocol.h"

#include <bc/Read.h>
#include <bc/Write.h>

namespace nixd {

using bc::readBytecode;
using rpc::readBytecode;

void EvalProvider::handleInbound(const std::vector<char> &Buf) {
std::ostringstream OS;
rpc::RPCKind Kind;
std::string_view Data(Buf.data(), Buf.size());
readBytecode(Data, Kind);
switch (Kind) {
case rpc::RPCKind::RegisterBC: {
rpc::RegisterBCParams Params;
readBytecode(Data, Params);
onRegisterBC(Params);
break;
}
case rpc::RPCKind::UnregisterBC:
case rpc::RPCKind::Log:
case rpc::RPCKind::ExprValue: {
rpc::ExprValueParams Params;
readBytecode(Data, Params);
rpc::ExprValueResponse Response = onExprValue(Params);
writeBytecode(OS, Response);
break;
}
}
}

} // namespace nixd
39 changes: 39 additions & 0 deletions nixd/tools/nix-node-eval/src/EvalProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "nixd/rpc/Protocol.h"
#include "nixd/rpc/Transport.h"

#include <nixt/Deserialize.h>
#include <nixt/PtrPool.h>

#include <nix/eval.hh>
#include <nix/nixexpr.hh>
#include <nix/store-api.hh>

// libc
#include <fcntl.h>
#include <sys/mman.h>

namespace nixd {

class EvalProvider : public rpc::Transport {

nixt::PtrPool<nix::Expr> Pool;
nixt::ValueMap VMap;
nixt::EnvMap EMap;
std::unique_ptr<nix::EvalState> State;

void handleInbound(const std::vector<char> &Buf) override;

public:
EvalProvider(int InboundFD, int OutboundFD)
: rpc::Transport(InboundFD, OutboundFD), State(nullptr) {}

void onRegisterBC(const rpc::RegisterBCParams &Params) {}

rpc::ExprValueResponse onExprValue(const rpc::ExprValueParams &Params) {
return {};
}
};

} // namespace nixd
12 changes: 12 additions & 0 deletions nixd/tools/nix-node-eval/src/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#include "EvalProvider.h"

#include <nixt/InitEval.h>

#include <unistd.h>

int main() {
nixt::initEval();
nixd::EvalProvider Provider(STDIN_FILENO, STDOUT_FILENO);
return Provider.run();
}
3 changes: 2 additions & 1 deletion nixd/tools/nixd/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ nixd_next = executable('nixd',
'src/CodeAction.cpp',
'src/Convert.cpp',
'src/Diagnostics.cpp',
'src/EvalClient.cpp',
'src/Hover.cpp',
'src/LifeTime.cpp',
'src/Main.cpp',
'src/Support.cpp',
'src/TextDocumentSync.cpp',
install: true,
dependencies: [ nixd_lsp_server, nixf, llvm ]
dependencies: [ nixd_lsp_server, nixf, llvm, libnixdrpc, libnixdutil ]
)


Expand Down
1 change: 1 addition & 0 deletions nixd/tools/nixd/src/CodeAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_codeAction

#include "Controller.h"
#include "Convert.h"

namespace nixd {

Expand Down
3 changes: 2 additions & 1 deletion nixd/tools/nixd/src/Controller.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "Convert.h"
#include "EvalClient.h"
#include "NixTU.h"

#include "lspserver/DraftStore.h"
Expand All @@ -9,6 +9,7 @@
namespace nixd {

class Controller : public lspserver::LSPServer {
std::unique_ptr<EvalClient> Eval;
lspserver::DraftStore Store;

llvm::unique_function<void(const lspserver::PublishDiagnosticsParams &)>
Expand Down
1 change: 1 addition & 0 deletions nixd/tools/nixd/src/Diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_publishDiagnostics

#include "Controller.h"
#include "Convert.h"

namespace nixd {

Expand Down
46 changes: 46 additions & 0 deletions nixd/tools/nixd/src/EvalClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "nixd-config.h"

#include "EvalClient.h"

#include "nixd/rpc/Protocol.h"
#include "nixd/util/ForkPiped.h"

#include <bc/Read.h>
#include <bc/Write.h>

namespace nixd {

using namespace rpc;
using namespace nixd::util;

std::unique_ptr<EvalClient> EvalClient::create(int &Fail) {
int In;
int Out;
int Err;

pid_t Child = forkPiped(In, Out, Err);
if (Child == 0) {
execl(NIXD_LIBEXEC "/nix-node-eval", "nix-node-eval", nullptr);
exit(-1);
} else if (Child < 0) {
// Error.
Fail = Child;
return nullptr;
}

Fail = 0;
// Parent process.
auto Proc = std::make_unique<PipedProc>(Child, In, Out, Err);
return std::make_unique<EvalClient>(Out, In, std::move(Proc));
}

void EvalClient::registerBC(const RegisterBCParams &Params) {
sendPacket(Message{RPCKind::RegisterBC, Params});
}

ExprValueResponse EvalClient::exprValue(const ExprValueParams &Params) {
sendPacket(Message{RPCKind::ExprValue, Params});
return recvPacket<ExprValueResponse>();
}

} // namespace nixd
35 changes: 35 additions & 0 deletions nixd/tools/nixd/src/EvalClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "nixd/rpc/Protocol.h"
#include "nixd/rpc/Transport.h"
#include "nixd/util/PipedProc.h"

#include <memory>

namespace nixd {

class EvalClient : public rpc::Transport {
// Owned process of the evaluator
std::unique_ptr<util::PipedProc> Proc;
void handleInbound(const std::vector<char> &Buf) override{};

public:
EvalClient(int InboundFD, int OutboundFD,
std::unique_ptr<util::PipedProc> Proc)
: rpc::Transport(InboundFD, OutboundFD), Proc(std::move(Proc)) {}

virtual ~EvalClient() = default;

/// Lanch nix-node-eval, with properly handled file descriptors.
/// System-wide errno will be written into "Fail" variable and thus cannot be
/// discarded.
static std::unique_ptr<EvalClient> create(int &Fail);

void registerBC(const rpc::RegisterBCParams &Params);

rpc::ExprValueResponse exprValue(const rpc::ExprValueParams &Params);

util::PipedProc *proc() { return Proc.get(); }
};

} // namespace nixd
Loading

0 comments on commit 881e40a

Please sign in to comment.