6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
9
+ #include " Index.pb.h"
9
10
#include " index/Index.h"
10
11
#include " index/Serialization.h"
12
+ #include " index/Symbol.h"
11
13
#include " index/remote/marshalling/Marshalling.h"
12
14
#include " support/Logger.h"
15
+ #include " support/Trace.h"
13
16
#include " llvm/ADT/StringRef.h"
14
17
#include " llvm/Support/CommandLine.h"
15
18
#include " llvm/Support/Path.h"
@@ -36,6 +39,16 @@ llvm::cl::opt<std::string> IndexPath(llvm::cl::desc("<INDEX FILE>"),
36
39
llvm::cl::opt<std::string> IndexRoot (llvm::cl::desc(" <PROJECT ROOT>" ),
37
40
llvm::cl::Positional, llvm::cl::Required);
38
41
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
+
39
52
llvm::cl::opt<std::string> ServerAddress (
40
53
" server-address" , llvm::cl::init(" 0.0.0.0:50051" ),
41
54
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 {
60
73
grpc::Status Lookup (grpc::ServerContext *Context,
61
74
const LookupRequest *Request,
62
75
grpc::ServerWriter<LookupReply> *Reply) override {
76
+ trace::Span Tracer (LookupRequest::descriptor ()->name ());
63
77
auto Req = ProtobufMarshaller->fromProtobuf (Request);
64
78
if (!Req) {
65
79
elog (" Can not parse LookupRequest from protobuf: {0}" , Req.takeError ());
66
80
return grpc::Status::CANCELLED;
67
81
}
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;
71
88
return ;
89
+ }
72
90
LookupReply NextMessage;
73
- *NextMessage.mutable_stream_result () = *SerializedSymbol ;
91
+ *NextMessage.mutable_stream_result () = *SerializedItem ;
74
92
Reply->Write (NextMessage);
93
+ ++Sent;
75
94
});
76
95
LookupReply LastMessage;
77
96
LastMessage.set_final_result (true );
78
97
Reply->Write (LastMessage);
98
+ SPAN_ATTACH (Tracer, " Sent" , Sent);
99
+ SPAN_ATTACH (Tracer, " Failed to send" , FailedToSend);
79
100
return grpc::Status::OK;
80
101
}
81
102
82
103
grpc::Status FuzzyFind (grpc::ServerContext *Context,
83
104
const FuzzyFindRequest *Request,
84
105
grpc::ServerWriter<FuzzyFindReply> *Reply) override {
106
+ trace::Span Tracer (FuzzyFindRequest::descriptor ()->name ());
85
107
auto Req = ProtobufMarshaller->fromProtobuf (Request);
86
108
if (!Req) {
87
109
elog (" Can not parse FuzzyFindRequest from protobuf: {0}" ,
88
110
Req.takeError ());
89
111
return grpc::Status::CANCELLED;
90
112
}
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;
94
119
return ;
120
+ }
95
121
FuzzyFindReply NextMessage;
96
- *NextMessage.mutable_stream_result () = *SerializedSymbol ;
122
+ *NextMessage.mutable_stream_result () = *SerializedItem ;
97
123
Reply->Write (NextMessage);
124
+ ++Sent;
98
125
});
99
126
FuzzyFindReply LastMessage;
100
127
LastMessage.set_final_result (HasMore);
101
128
Reply->Write (LastMessage);
129
+ SPAN_ATTACH (Tracer, " Sent" , Sent);
130
+ SPAN_ATTACH (Tracer, " Failed to send" , FailedToSend);
102
131
return grpc::Status::OK;
103
132
}
104
133
105
134
grpc::Status Refs (grpc::ServerContext *Context, const RefsRequest *Request,
106
135
grpc::ServerWriter<RefsReply> *Reply) override {
136
+ trace::Span Tracer (RefsRequest::descriptor ()->name ());
107
137
auto Req = ProtobufMarshaller->fromProtobuf (Request);
108
138
if (!Req) {
109
139
elog (" Can not parse RefsRequest from protobuf: {0}" , Req.takeError ());
110
140
return grpc::Status::CANCELLED;
111
141
}
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;
115
148
return ;
149
+ }
116
150
RefsReply NextMessage;
117
- *NextMessage.mutable_stream_result () = *SerializedRef ;
151
+ *NextMessage.mutable_stream_result () = *SerializedItem ;
118
152
Reply->Write (NextMessage);
153
+ ++Sent;
119
154
});
120
155
RefsReply LastMessage;
121
156
LastMessage.set_final_result (HasMore);
122
157
Reply->Write (LastMessage);
158
+ SPAN_ATTACH (Tracer, " Sent" , Sent);
159
+ SPAN_ATTACH (Tracer, " Failed to send" , FailedToSend);
123
160
return grpc::Status::OK;
124
161
}
125
162
@@ -146,20 +183,44 @@ void runServer(std::unique_ptr<clangd::SymbolIndex> Index,
146
183
} // namespace clangd
147
184
} // namespace clang
148
185
186
+ using clang::clangd::elog;
187
+
149
188
int main (int argc, char *argv[]) {
150
189
using namespace clang ::clangd::remote;
151
190
llvm::cl::ParseCommandLineOptions (argc, argv, Overview);
152
191
llvm::sys::PrintStackTraceOnErrorSignal (argv[0 ]);
153
192
154
193
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." ) ;
156
195
return -1 ;
157
196
}
158
197
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
+
159
220
std::unique_ptr<clang::clangd::SymbolIndex> Index = openIndex (IndexPath);
160
221
161
222
if (!Index) {
162
- llvm::errs () << " Failed to open the index.\n " ;
223
+ elog ( " Failed to open the index." ) ;
163
224
return -1 ;
164
225
}
165
226
0 commit comments