[PATCH] D52937: [clangd] Add clangd.serverInfo command to inspect server state.

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 5 09:58:22 PDT 2018


sammccall created this revision.
sammccall added a reviewer: ioeric.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay, ilya-biryukov.

Initially just export the information that's easily available.
(I want to measure changes in dynamic index size, so this is good enough for now)


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D52937

Files:
  clangd/ClangdLSPServer.cpp
  clangd/Protocol.cpp
  clangd/Protocol.h


Index: clangd/Protocol.h
===================================================================
--- clangd/Protocol.h
+++ clangd/Protocol.h
@@ -661,6 +661,8 @@
 struct ExecuteCommandParams {
   // Command to apply fix-its. Uses WorkspaceEdit as argument.
   const static llvm::StringLiteral CLANGD_APPLY_FIX_COMMAND;
+  // Retrieve information about the server state. No arguments.
+  const static llvm::StringLiteral CLANGD_SERVER_INFO;
 
   /// The command identifier, e.g. CLANGD_APPLY_FIX_COMMAND
   std::string command;
Index: clangd/Protocol.cpp
===================================================================
--- clangd/Protocol.cpp
+++ clangd/Protocol.cpp
@@ -408,6 +408,8 @@
 
 const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND =
     "clangd.applyFix";
+const llvm::StringLiteral ExecuteCommandParams::CLANGD_SERVER_INFO =
+    "clangd.serverInfo";
 bool fromJSON(const json::Value &Params, ExecuteCommandParams &R) {
   json::ObjectMapper O(Params);
   if (!O || !O.map("command", R.command))
@@ -417,6 +419,8 @@
   if (R.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND) {
     return Args && Args->size() == 1 &&
            fromJSON(Args->front(), R.workspaceEdit);
+  } else if (R.command == ExecuteCommandParams::CLANGD_SERVER_INFO) {
+    return true; // No args.
   }
   return false; // Unrecognized command.
 }
Index: clangd/ClangdLSPServer.cpp
===================================================================
--- clangd/ClangdLSPServer.cpp
+++ clangd/ClangdLSPServer.cpp
@@ -12,6 +12,7 @@
 #include "JSONRPCDispatcher.h"
 #include "SourceCode.h"
 #include "URI.h"
+#include "clang/Basic/Version.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/FormatVariadic.h"
@@ -214,6 +215,28 @@
 
     reply("Fix applied.");
     ApplyEdit(*Params.workspaceEdit);
+  } else if (Params.command == ExecuteCommandParams::CLANGD_SERVER_INFO) {
+    json::Object Info{
+        {"version", getClangToolFullVersion("clangd")},
+    };
+    if (const auto *DynIndex = Server->dynamicIndex()) {
+      Info["dynamicIndex"] = json::Object{
+          {"memory", int64_t(DynIndex->estimateMemoryUsage())},
+      };
+    }
+    json::Object TrackedFiles;
+    auto MemUse = Server->getUsedBytesPerFile();
+    DenseMap<StringRef, size_t> MemUseMap = {MemUse.begin(), MemUse.end()};
+    for (const auto &File : DraftMgr.getActiveFiles()) {
+      json::Object FileInfo;
+      if (auto Data = DraftMgr.getDraft(File))
+        FileInfo["size"] = int64_t(Data->size());
+      if (auto Mem = MemUseMap.lookup(File))
+        FileInfo["memory"] = int64_t(Mem);
+      TrackedFiles[File] = std::move(FileInfo);
+    }
+    Info["files"] = std::move(TrackedFiles);
+    reply(std::move(Info));
   } else {
     // We should not get here because ExecuteCommandParams would not have
     // parsed in the first place and this handler should not be called. But if


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52937.168485.patch
Type: text/x-patch
Size: 2935 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181005/59a27630/attachment.bin>


More information about the cfe-commits mailing list