Skip to content

Commit 731043c

Browse files
committed
[clangd] Add more logs and attach tracers to remote index server routines
Reviewers: kadircet Reviewed By: kadircet Subscribers: sammccall, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D84499
1 parent 34ddf0b commit 731043c

File tree

2 files changed

+83
-15
lines changed

2 files changed

+83
-15
lines changed

clang-tools-extra/clangd/index/remote/Client.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ class IndexClient : public clangd::SymbolIndex {
3737
bool FinalResult = false;
3838
trace::Span Tracer(RequestT::descriptor()->name());
3939
const auto RPCRequest = ProtobufMarshaller->toProtobuf(Request);
40+
SPAN_ATTACH(Tracer, "Request", RPCRequest.DebugString());
4041
grpc::ClientContext Context;
4142
std::chrono::system_clock::time_point Deadline =
4243
std::chrono::system_clock::now() + DeadlineWaitingTime;
4344
Context.set_deadline(Deadline);
4445
auto Reader = (Stub.get()->*RPCCall)(&Context, RPCRequest);
4546
ReplyT Reply;
47+
unsigned Successful = 0;
48+
unsigned FailedToParse = 0;
4649
while (Reader->Read(&Reply)) {
4750
if (!Reply.has_stream_result()) {
4851
FinalResult = Reply.final_result();
@@ -51,11 +54,15 @@ class IndexClient : public clangd::SymbolIndex {
5154
auto Response = ProtobufMarshaller->fromProtobuf(Reply.stream_result());
5255
if (!Response) {
5356
elog("Received invalid {0}", ReplyT::descriptor()->name());
57+
++FailedToParse;
5458
continue;
5559
}
5660
Callback(*Response);
61+
++Successful;
5762
}
58-
SPAN_ATTACH(Tracer, "status", Reader->Finish().ok());
63+
SPAN_ATTACH(Tracer, "Status", Reader->Finish().ok());
64+
SPAN_ATTACH(Tracer, "Successful", Successful);
65+
SPAN_ATTACH(Tracer, "Failed to parse", FailedToParse);
5966
return FinalResult;
6067
}
6168

clang-tools-extra/clangd/index/remote/server/Server.cpp

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "Index.pb.h"
910
#include "index/Index.h"
1011
#include "index/Serialization.h"
12+
#include "index/Symbol.h"
1113
#include "index/remote/marshalling/Marshalling.h"
1214
#include "support/Logger.h"
15+
#include "support/Trace.h"
1316
#include "llvm/ADT/StringRef.h"
1417
#include "llvm/Support/CommandLine.h"
1518
#include "llvm/Support/Path.h"
@@ -36,6 +39,16 @@ llvm::cl::opt<std::string> IndexPath(llvm::cl::desc("<INDEX FILE>"),
3639
llvm::cl::opt<std::string> IndexRoot(llvm::cl::desc("<PROJECT ROOT>"),
3740
llvm::cl::Positional, llvm::cl::Required);
3841

42+
llvm::cl::opt<std::string> TraceFile(
43+
"trace-file",
44+
llvm::cl::desc("Path to the file where tracer logs will be stored"));
45+
46+
llvm::cl::opt<bool> PrettyPrint{
47+
"pretty",
48+
llvm::cl::desc("Pretty-print JSON output in the trace"),
49+
llvm::cl::init(false),
50+
};
51+
3952
llvm::cl::opt<std::string> ServerAddress(
4053
"server-address", llvm::cl::init("0.0.0.0:50051"),
4154
llvm::cl::desc("Address of the invoked server. Defaults to 0.0.0.0:50051"));
@@ -60,66 +73,90 @@ class RemoteIndexServer final : public SymbolIndex::Service {
6073
grpc::Status Lookup(grpc::ServerContext *Context,
6174
const LookupRequest *Request,
6275
grpc::ServerWriter<LookupReply> *Reply) override {
76+
trace::Span Tracer(LookupRequest::descriptor()->name());
6377
auto Req = ProtobufMarshaller->fromProtobuf(Request);
6478
if (!Req) {
6579
elog("Can not parse LookupRequest from protobuf: {0}", Req.takeError());
6680
return grpc::Status::CANCELLED;
6781
}
68-
Index->lookup(*Req, [&](const clangd::Symbol &Sym) {
69-
auto SerializedSymbol = ProtobufMarshaller->toProtobuf(Sym);
70-
if (!SerializedSymbol)
82+
unsigned Sent = 0;
83+
unsigned FailedToSend = 0;
84+
Index->lookup(*Req, [&](const auto &Item) {
85+
auto SerializedItem = ProtobufMarshaller->toProtobuf(Item);
86+
if (!SerializedItem) {
87+
++FailedToSend;
7188
return;
89+
}
7290
LookupReply NextMessage;
73-
*NextMessage.mutable_stream_result() = *SerializedSymbol;
91+
*NextMessage.mutable_stream_result() = *SerializedItem;
7492
Reply->Write(NextMessage);
93+
++Sent;
7594
});
7695
LookupReply LastMessage;
7796
LastMessage.set_final_result(true);
7897
Reply->Write(LastMessage);
98+
SPAN_ATTACH(Tracer, "Sent", Sent);
99+
SPAN_ATTACH(Tracer, "Failed to send", FailedToSend);
79100
return grpc::Status::OK;
80101
}
81102

82103
grpc::Status FuzzyFind(grpc::ServerContext *Context,
83104
const FuzzyFindRequest *Request,
84105
grpc::ServerWriter<FuzzyFindReply> *Reply) override {
106+
trace::Span Tracer(FuzzyFindRequest::descriptor()->name());
85107
auto Req = ProtobufMarshaller->fromProtobuf(Request);
86108
if (!Req) {
87109
elog("Can not parse FuzzyFindRequest from protobuf: {0}",
88110
Req.takeError());
89111
return grpc::Status::CANCELLED;
90112
}
91-
bool HasMore = Index->fuzzyFind(*Req, [&](const clangd::Symbol &Sym) {
92-
auto SerializedSymbol = ProtobufMarshaller->toProtobuf(Sym);
93-
if (!SerializedSymbol)
113+
unsigned Sent = 0;
114+
unsigned FailedToSend = 0;
115+
bool HasMore = Index->fuzzyFind(*Req, [&](const auto &Item) {
116+
auto SerializedItem = ProtobufMarshaller->toProtobuf(Item);
117+
if (!SerializedItem) {
118+
++FailedToSend;
94119
return;
120+
}
95121
FuzzyFindReply NextMessage;
96-
*NextMessage.mutable_stream_result() = *SerializedSymbol;
122+
*NextMessage.mutable_stream_result() = *SerializedItem;
97123
Reply->Write(NextMessage);
124+
++Sent;
98125
});
99126
FuzzyFindReply LastMessage;
100127
LastMessage.set_final_result(HasMore);
101128
Reply->Write(LastMessage);
129+
SPAN_ATTACH(Tracer, "Sent", Sent);
130+
SPAN_ATTACH(Tracer, "Failed to send", FailedToSend);
102131
return grpc::Status::OK;
103132
}
104133

105134
grpc::Status Refs(grpc::ServerContext *Context, const RefsRequest *Request,
106135
grpc::ServerWriter<RefsReply> *Reply) override {
136+
trace::Span Tracer(RefsRequest::descriptor()->name());
107137
auto Req = ProtobufMarshaller->fromProtobuf(Request);
108138
if (!Req) {
109139
elog("Can not parse RefsRequest from protobuf: {0}", Req.takeError());
110140
return grpc::Status::CANCELLED;
111141
}
112-
bool HasMore = Index->refs(*Req, [&](const clangd::Ref &Reference) {
113-
auto SerializedRef = ProtobufMarshaller->toProtobuf(Reference);
114-
if (!SerializedRef)
142+
unsigned Sent = 0;
143+
unsigned FailedToSend = 0;
144+
bool HasMore = Index->refs(*Req, [&](const auto &Item) {
145+
auto SerializedItem = ProtobufMarshaller->toProtobuf(Item);
146+
if (!SerializedItem) {
147+
++FailedToSend;
115148
return;
149+
}
116150
RefsReply NextMessage;
117-
*NextMessage.mutable_stream_result() = *SerializedRef;
151+
*NextMessage.mutable_stream_result() = *SerializedItem;
118152
Reply->Write(NextMessage);
153+
++Sent;
119154
});
120155
RefsReply LastMessage;
121156
LastMessage.set_final_result(HasMore);
122157
Reply->Write(LastMessage);
158+
SPAN_ATTACH(Tracer, "Sent", Sent);
159+
SPAN_ATTACH(Tracer, "Failed to send", FailedToSend);
123160
return grpc::Status::OK;
124161
}
125162

@@ -146,20 +183,44 @@ void runServer(std::unique_ptr<clangd::SymbolIndex> Index,
146183
} // namespace clangd
147184
} // namespace clang
148185

186+
using clang::clangd::elog;
187+
149188
int main(int argc, char *argv[]) {
150189
using namespace clang::clangd::remote;
151190
llvm::cl::ParseCommandLineOptions(argc, argv, Overview);
152191
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
153192

154193
if (!llvm::sys::path::is_absolute(IndexRoot)) {
155-
llvm::errs() << "Index root should be an absolute path.\n";
194+
elog("Index root should be an absolute path.");
156195
return -1;
157196
}
158197

198+
llvm::Optional<llvm::raw_fd_ostream> TracerStream;
199+
std::unique_ptr<clang::clangd::trace::EventTracer> Tracer;
200+
if (!TraceFile.empty()) {
201+
std::error_code EC;
202+
TracerStream.emplace(TraceFile, EC,
203+
llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write);
204+
if (EC) {
205+
TracerStream.reset();
206+
elog("Error while opening trace file {0}: {1}", TraceFile, EC.message());
207+
} else {
208+
// FIXME(kirillbobyrev): Also create metrics tracer to track latency and
209+
// accumulate other request statistics.
210+
Tracer = clang::clangd::trace::createJSONTracer(*TracerStream,
211+
/*PrettyPrint=*/false);
212+
clang::clangd::vlog("Successfully created a tracer.");
213+
}
214+
}
215+
216+
llvm::Optional<clang::clangd::trace::Session> TracingSession;
217+
if (Tracer)
218+
TracingSession.emplace(*Tracer);
219+
159220
std::unique_ptr<clang::clangd::SymbolIndex> Index = openIndex(IndexPath);
160221

161222
if (!Index) {
162-
llvm::errs() << "Failed to open the index.\n";
223+
elog("Failed to open the index.");
163224
return -1;
164225
}
165226

0 commit comments

Comments
 (0)