Skip to content

Commit 34593ae

Browse files
committed
Introduce clangd-server-monitor tool
Reviewed By: kadircet Differential Revision: https://reviews.llvm.org/D101516
1 parent 8fa56f7 commit 34593ae

File tree

6 files changed

+113
-3
lines changed

6 files changed

+113
-3
lines changed

clang-tools-extra/clangd/index/remote/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ if (CLANGD_ENABLE_REMOTE)
3838

3939
add_subdirectory(marshalling)
4040
add_subdirectory(server)
41+
add_subdirectory(monitor)
4142
else()
4243
# Provides a no-op implementation of clangdRemoteIndex.
4344
add_subdirectory(unimplemented)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
set(LLVM_LINK_COMPONENTS
2+
Support
3+
)
4+
add_clang_executable(clangd-index-server-monitor
5+
Monitor.cpp
6+
7+
DEPENDS
8+
RemoteIndexServiceProto
9+
)
10+
11+
target_link_libraries(clangd-index-server-monitor
12+
PRIVATE
13+
clangBasic
14+
clangdSupport
15+
16+
MonitoringServiceProto
17+
RemoteIndexServiceProto
18+
)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//===--- Monitor.cpp - Request server monitoring information through CLI --===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "MonitoringService.grpc.pb.h"
10+
#include "MonitoringService.pb.h"
11+
12+
#include "support/Logger.h"
13+
#include "clang/Basic/Version.h"
14+
#include "llvm/Support/CommandLine.h"
15+
#include "llvm/Support/Signals.h"
16+
17+
#include <chrono>
18+
#include <google/protobuf/util/json_util.h>
19+
#include <grpc++/grpc++.h>
20+
21+
namespace clang {
22+
namespace clangd {
23+
namespace remote {
24+
namespace {
25+
26+
static constexpr char Overview[] = R"(
27+
This tool requests monitoring information (uptime, index freshness) from the
28+
server and prints it to stdout.
29+
)";
30+
31+
llvm::cl::opt<std::string>
32+
ServerAddress("server-address", llvm::cl::Positional,
33+
llvm::cl::desc("Address of the invoked server."),
34+
llvm::cl::Required);
35+
36+
} // namespace
37+
} // namespace remote
38+
} // namespace clangd
39+
} // namespace clang
40+
41+
int main(int argc, char *argv[]) {
42+
using namespace clang::clangd::remote;
43+
llvm::cl::ParseCommandLineOptions(argc, argv, Overview);
44+
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
45+
46+
const auto Channel =
47+
grpc::CreateChannel(ServerAddress, grpc::InsecureChannelCredentials());
48+
const auto Stub = clang::clangd::remote::v1::Monitor::NewStub(Channel);
49+
grpc::ClientContext Context;
50+
Context.set_deadline(std::chrono::system_clock::now() +
51+
std::chrono::seconds(10));
52+
Context.AddMetadata("version", clang::getClangToolFullVersion("clangd"));
53+
const clang::clangd::remote::v1::MonitoringInfoRequest Request;
54+
clang::clangd::remote::v1::MonitoringInfoReply Response;
55+
const auto Status = Stub->MonitoringInfo(&Context, Request, &Response);
56+
if (!Status.ok()) {
57+
clang::clangd::elog("Can not request monitoring information ({0}): {1}\n",
58+
Status.error_code(), Status.error_message());
59+
return -1;
60+
}
61+
std::string Output;
62+
google::protobuf::util::JsonPrintOptions Options;
63+
Options.add_whitespace = true;
64+
Options.always_print_primitive_fields = true;
65+
Options.preserve_proto_field_names = true;
66+
const auto JsonStatus =
67+
google::protobuf::util::MessageToJsonString(Response, &Output, Options);
68+
if (!JsonStatus.ok()) {
69+
clang::clangd::elog("Can not convert response ({0}) to JSON ({1}): {2}\n",
70+
Response.DebugString(), JsonStatus.error_code(),
71+
JsonStatus.error_message().as_string());
72+
return -1;
73+
}
74+
llvm::outs() << Output;
75+
}

clang-tools-extra/clangd/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if(CLANGD_BUILD_XPC)
2222
endif()
2323

2424
if(CLANGD_ENABLE_REMOTE)
25-
list(APPEND CLANGD_TEST_DEPS clangd-index-server)
25+
list(APPEND CLANGD_TEST_DEPS clangd-index-server clangd-index-server-monitor)
2626
endif()
2727

2828
foreach(dep FileCheck count not llvm-config)

clang-tools-extra/clangd/test/remote-index/pipeline.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
# RUN: %python %S/pipeline_helper.py --input-file-name=%s --project-root=%S --index-file=%t.idx | FileCheck %s
44
# REQUIRES: clangd-remote-index
55

6+
# CHECK: "uptime_seconds": "{{.*}}",
7+
# CHECK-NEXT: "index_age_seconds": "{{.*}}",
8+
# CHECK-NEXT: "index_commit_hash": "{{.*}}",
9+
# CHECK-NEXT: "index_link": "{{.*}}"
10+
611
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
712
# CHECK: "id": 0,
813
# CHECK-NEXT: "jsonrpc": "2.0",

clang-tools-extra/clangd/test/remote-index/pipeline_helper.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import threading
1818

1919

20-
def kill_server_after_delay(server_process):
20+
def kill_process_after_delay(server_process):
2121
time.sleep(10)
2222
if server_process.poll() is None:
2323
server_process.kill()
@@ -48,7 +48,7 @@ def main():
4848
# This will kill index_server_process if it hangs without printing init
4949
# message.
5050
shutdown_thread = threading.Thread(
51-
target=kill_server_after_delay, args=(index_server_process,))
51+
target=kill_process_after_delay, args=(index_server_process,))
5252
shutdown_thread.daemon = True
5353
shutdown_thread.start()
5454

@@ -67,6 +67,13 @@ def main():
6767
print('Server initialization failed. Shutting down.', file=sys.stderr)
6868
sys.exit(1)
6969

70+
print('Running clangd-index-server-monitor...', file=sys.stderr)
71+
index_server_monitor_process = subprocess.Popen([
72+
'clangd-index-server-monitor', server_address,
73+
], stderr=subprocess.PIPE)
74+
75+
index_server_monitor_process.wait()
76+
7077
in_file = open(args.input_file_name)
7178

7279
print('Staring clangd...', file=sys.stderr)
@@ -84,6 +91,10 @@ def main():
8491
args.server_log.write(line)
8592
args.server_log.flush()
8693

94+
for line in index_server_monitor_process.stderr:
95+
args.server_log.write(line)
96+
args.server_log.flush()
97+
8798

8899
if __name__ == '__main__':
89100
main()

0 commit comments

Comments
 (0)