Skip to content

Commit

Permalink
Merged master:974ddb54c9ad into amd-gfx:50f1f527b5ee
Browse files Browse the repository at this point in the history
Change-Id: Icb43b1f3cc1a665f1934808d330f9daec620e1d3
  • Loading branch information
piotrAMD committed Oct 14, 2020
2 parents 50f1f52 + 974ddb5 commit 7d8613e
Show file tree
Hide file tree
Showing 106 changed files with 5,525 additions and 1,305 deletions.
5 changes: 2 additions & 3 deletions clang-tools-extra/clang-tidy/utils/IncludeInserter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ namespace utils {
/// public:
/// void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
/// Preprocessor *ModuleExpanderPP) override {
/// Inserter.registerPreprocessor();
/// Inserter.registerPreprocessor(PP);
/// }
///
/// void registerMatchers(ast_matchers::MatchFinder* Finder) override { ... }
///
/// void check(
/// const ast_matchers::MatchFinder::MatchResult& Result) override {
/// ...
/// Inserter.createMainFileIncludeInsertion("path/to/Header.h",
/// /*IsAngled=*/false);
/// Inserter.createMainFileIncludeInsertion("path/to/Header.h");
/// ...
/// }
///
Expand Down
73 changes: 52 additions & 21 deletions clang-tools-extra/clangd/ClangdLSPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "ClangdLSPServer.h"
#include "ClangdServer.h"
#include "CodeComplete.h"
#include "Diagnostics.h"
#include "DraftStore.h"
Expand All @@ -18,6 +19,7 @@
#include "URI.h"
#include "refactor/Tweak.h"
#include "support/Context.h"
#include "support/MemoryTree.h"
#include "support/Trace.h"
#include "clang/Basic/Version.h"
#include "clang/Tooling/Core/Replacement.h"
Expand All @@ -26,13 +28,15 @@
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SHA1.h"
#include "llvm/Support/ScopedPrinter.h"
#include <chrono>
#include <cstddef>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -144,7 +148,6 @@ llvm::Error validateEdits(const DraftStore &DraftMgr, const FileEdits &FE) {
return error("Files must be saved first: {0} (and {1} others)",
LastInvalidFile, InvalidFileCount - 1);
}

} // namespace

// MessageHandler dispatches incoming LSP messages.
Expand All @@ -163,14 +166,16 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
log("<-- {0}", Method);
if (Method == "exit")
return false;
if (!Server.Server)
if (!Server.Server) {
elog("Notification {0} before initialization", Method);
else if (Method == "$/cancelRequest")
} else if (Method == "$/cancelRequest") {
onCancel(std::move(Params));
else if (auto Handler = Notifications.lookup(Method))
} else if (auto Handler = Notifications.lookup(Method)) {
Handler(std::move(Params));
else
Server.maybeExportMemoryProfile();
} else {
log("unhandled notification {0}", Method);
}
return true;
}

Expand Down Expand Up @@ -1049,22 +1054,21 @@ void ClangdLSPServer::onCompletion(const CompletionParams &Params,
vlog("ignored auto-triggered completion, preceding char did not match");
return Reply(CompletionList());
}
Server->codeComplete(Params.textDocument.uri.file(), Params.position,
Opts.CodeComplete,
[Reply = std::move(Reply),
this](llvm::Expected<CodeCompleteResult> List) mutable {
if (!List)
return Reply(List.takeError());
CompletionList LSPList;
LSPList.isIncomplete = List->HasMore;
for (const auto &R : List->Completions) {
CompletionItem C = R.render(Opts.CodeComplete);
C.kind = adjustKindToCapability(
C.kind, SupportedCompletionItemKinds);
LSPList.items.push_back(std::move(C));
}
return Reply(std::move(LSPList));
});
Server->codeComplete(
Params.textDocument.uri.file(), Params.position, Opts.CodeComplete,
[Reply = std::move(Reply),
this](llvm::Expected<CodeCompleteResult> List) mutable {
if (!List)
return Reply(List.takeError());
CompletionList LSPList;
LSPList.isIncomplete = List->HasMore;
for (const auto &R : List->Completions) {
CompletionItem C = R.render(Opts.CodeComplete);
C.kind = adjustKindToCapability(C.kind, SupportedCompletionItemKinds);
LSPList.items.push_back(std::move(C));
}
return Reply(std::move(LSPList));
});
}

void ClangdLSPServer::onSignatureHelp(const TextDocumentPositionParams &Params,
Expand Down Expand Up @@ -1235,6 +1239,25 @@ void ClangdLSPServer::publishDiagnostics(
notify("textDocument/publishDiagnostics", Params);
}

void ClangdLSPServer::maybeExportMemoryProfile() {
if (!trace::enabled())
return;
// Profiling might be expensive, so we throttle it to happen once every 5
// minutes.
static constexpr auto ProfileInterval = std::chrono::minutes(5);
auto Now = std::chrono::steady_clock::now();
if (Now < NextProfileTime)
return;

static constexpr trace::Metric MemoryUsage(
"memory_usage", trace::Metric::Value, "component_name");
trace::Span Tracer("ProfileBrief");
MemoryTree MT;
profile(MT);
record(MT, "clangd_lsp_server", MemoryUsage);
NextProfileTime = Now + ProfileInterval;
}

// FIXME: This function needs to be properly tested.
void ClangdLSPServer::onChangeConfiguration(
const DidChangeConfigurationParams &Params) {
Expand Down Expand Up @@ -1405,6 +1428,9 @@ ClangdLSPServer::ClangdLSPServer(class Transport &Transp,
if (Opts.FoldingRanges)
MsgHandler->bind("textDocument/foldingRange", &ClangdLSPServer::onFoldingRange);
// clang-format on

// Delay first profile until we've finished warming up.
NextProfileTime = std::chrono::steady_clock::now() + std::chrono::minutes(1);
}

ClangdLSPServer::~ClangdLSPServer() {
Expand All @@ -1425,6 +1451,11 @@ bool ClangdLSPServer::run() {
return CleanExit && ShutdownRequestReceived;
}

void ClangdLSPServer::profile(MemoryTree &MT) const {
if (Server)
Server->profile(MT.child("clangd_server"));
}

std::vector<Fix> ClangdLSPServer::getFixes(llvm::StringRef File,
const clangd::Diagnostic &D) {
std::lock_guard<std::mutex> Lock(FixItsMutex);
Expand Down
13 changes: 13 additions & 0 deletions clang-tools-extra/clangd/ClangdLSPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
#include "Protocol.h"
#include "Transport.h"
#include "support/Context.h"
#include "support/MemoryTree.h"
#include "support/Path.h"
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/JSON.h"
#include <chrono>
#include <memory>

namespace clang {
Expand Down Expand Up @@ -67,6 +69,9 @@ class ClangdLSPServer : private ClangdServer::Callbacks {
/// \return Whether we shut down cleanly with a 'shutdown' -> 'exit' sequence.
bool run();

/// Profiles resource-usage.
void profile(MemoryTree &MT) const;

private:
// Implement ClangdServer::Callbacks.
void onDiagnosticsReady(PathRef File, llvm::StringRef Version,
Expand Down Expand Up @@ -160,6 +165,14 @@ class ClangdLSPServer : private ClangdServer::Callbacks {
/// Sends a "publishDiagnostics" notification to the LSP client.
void publishDiagnostics(const PublishDiagnosticsParams &);

/// Runs profiling and exports memory usage metrics if tracing is enabled and
/// profiling hasn't happened recently.
void maybeExportMemoryProfile();

/// Timepoint until which profiling is off. It is used to throttle profiling
/// requests.
std::chrono::steady_clock::time_point NextProfileTime;

/// Since initialization of CDBs and ClangdServer is done lazily, the
/// following context captures the one used while creating ClangdLSPServer and
/// passes it to above mentioned object instances to make sure they share the
Expand Down
8 changes: 8 additions & 0 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "refactor/Tweak.h"
#include "support/Logger.h"
#include "support/Markup.h"
#include "support/MemoryTree.h"
#include "support/ThreadsafeFS.h"
#include "support/Trace.h"
#include "clang/Format/Format.h"
Expand Down Expand Up @@ -826,5 +827,12 @@ ClangdServer::blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds) {
BackgroundIdx->blockUntilIdleForTest(TimeoutSeconds));
}

void ClangdServer::profile(MemoryTree &MT) const {
if (DynamicIdx)
DynamicIdx->profile(MT.child("dynamic_index"));
if (BackgroundIdx)
BackgroundIdx->profile(MT.child("background_index"));
WorkScheduler.profile(MT.child("tuscheduler"));
}
} // namespace clangd
} // namespace clang
4 changes: 4 additions & 0 deletions clang-tools-extra/clangd/ClangdServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "refactor/Tweak.h"
#include "support/Cancellation.h"
#include "support/Function.h"
#include "support/MemoryTree.h"
#include "support/ThreadsafeFS.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "clang/Tooling/Core/Replacement.h"
Expand Down Expand Up @@ -337,6 +338,9 @@ class ClangdServer {
LLVM_NODISCARD bool
blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds = 10);

/// Builds a nested representation of memory used by components.
void profile(MemoryTree &MT) const;

private:
void formatCode(PathRef File, llvm::StringRef Code,
ArrayRef<tooling::Range> Ranges,
Expand Down
67 changes: 24 additions & 43 deletions clang-tools-extra/clangd/Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,10 @@ bool fromJSON(const llvm::json::Value &Params, DidSaveTextDocumentParams &R,
bool fromJSON(const llvm::json::Value &Params, DidChangeTextDocumentParams &R,
llvm::json::Path P) {
llvm::json::ObjectMapper O(Params, P);
if (!O)
return false;
O.map("forceRebuild", R.forceRebuild); // Optional clangd extension.
return O.map("textDocument", R.textDocument) &&
return O && O.map("textDocument", R.textDocument) &&
O.map("contentChanges", R.contentChanges) &&
O.map("wantDiagnostics", R.wantDiagnostics);
O.map("wantDiagnostics", R.wantDiagnostics) &&
O.mapOptional("forceRebuild", R.forceRebuild);
}

bool fromJSON(const llvm::json::Value &E, FileChangeType &Out,
Expand Down Expand Up @@ -578,12 +576,10 @@ llvm::json::Value toJSON(const Diagnostic &D) {
bool fromJSON(const llvm::json::Value &Params, Diagnostic &R,
llvm::json::Path P) {
llvm::json::ObjectMapper O(Params, P);
if (!O || !O.map("range", R.range) || !O.map("message", R.message))
return false;
O.map("severity", R.severity);
O.map("category", R.category);
O.map("code", R.code);
O.map("source", R.source);
return O && O.map("range", R.range) && O.map("message", R.message) &&
O.mapOptional("severity", R.severity) &&
O.mapOptional("category", R.category) &&
O.mapOptional("code", R.code) && O.mapOptional("source", R.source);
return true;
}

Expand Down Expand Up @@ -800,10 +796,8 @@ llvm::json::Value toJSON(const ApplyWorkspaceEditParams &Params) {
bool fromJSON(const llvm::json::Value &Response, ApplyWorkspaceEditResponse &R,
llvm::json::Path P) {
llvm::json::ObjectMapper O(Response, P);
if (!O || !O.map("applied", R.applied))
return false;
O.map("failureReason", R.failureReason);
return true;
return O && O.map("applied", R.applied) &&
O.map("failureReason", R.failureReason);
}

bool fromJSON(const llvm::json::Value &Params, TextDocumentPositionParams &R,
Expand All @@ -816,16 +810,11 @@ bool fromJSON(const llvm::json::Value &Params, TextDocumentPositionParams &R,
bool fromJSON(const llvm::json::Value &Params, CompletionContext &R,
llvm::json::Path P) {
llvm::json::ObjectMapper O(Params, P);
if (!O)
return false;

int TriggerKind;
if (!O.map("triggerKind", TriggerKind))
if (!O || !O.map("triggerKind", TriggerKind) ||
!O.mapOptional("triggerCharacter", R.triggerCharacter))
return false;
R.triggerKind = static_cast<CompletionTriggerKind>(TriggerKind);

if (auto *TC = Params.getAsObject()->get("triggerCharacter"))
return fromJSON(*TC, R.triggerCharacter, P.field("triggerCharacter"));
return true;
}

Expand Down Expand Up @@ -1126,8 +1115,8 @@ bool fromJSON(const llvm::json::Value &Params, ConfigurationSettings &S,
llvm::json::ObjectMapper O(Params, P);
if (!O)
return true; // 'any' type in LSP.
O.map("compilationDatabaseChanges", S.compilationDatabaseChanges);
return true;
return O.mapOptional("compilationDatabaseChanges",
S.compilationDatabaseChanges);
}

bool fromJSON(const llvm::json::Value &Params, InitializationOptions &Opts,
Expand All @@ -1136,11 +1125,10 @@ bool fromJSON(const llvm::json::Value &Params, InitializationOptions &Opts,
if (!O)
return true; // 'any' type in LSP.

fromJSON(Params, Opts.ConfigSettings, P);
O.map("compilationDatabasePath", Opts.compilationDatabasePath);
O.map("fallbackFlags", Opts.fallbackFlags);
O.map("clangdFileStatus", Opts.FileStatus);
return true;
return fromJSON(Params, Opts.ConfigSettings, P) &&
O.map("compilationDatabasePath", Opts.compilationDatabasePath) &&
O.mapOptional("fallbackFlags", Opts.fallbackFlags) &&
O.mapOptional("clangdFileStatus", Opts.FileStatus);
}

bool fromJSON(const llvm::json::Value &E, TypeHierarchyDirection &Out,
Expand Down Expand Up @@ -1193,20 +1181,13 @@ bool fromJSON(const llvm::json::Value &Params, TypeHierarchyItem &I,
llvm::json::ObjectMapper O(Params, P);

// Required fields.
if (!(O && O.map("name", I.name) && O.map("kind", I.kind) &&
O.map("uri", I.uri) && O.map("range", I.range) &&
O.map("selectionRange", I.selectionRange))) {
return false;
}

// Optional fields.
O.map("detail", I.detail);
O.map("deprecated", I.deprecated);
O.map("parents", I.parents);
O.map("children", I.children);
O.map("data", I.data);

return true;
return O && O.map("name", I.name) && O.map("kind", I.kind) &&
O.map("uri", I.uri) && O.map("range", I.range) &&
O.map("selectionRange", I.selectionRange) &&
O.mapOptional("detail", I.detail) &&
O.mapOptional("deprecated", I.deprecated) &&
O.mapOptional("parents", I.parents) &&
O.mapOptional("children", I.children) && O.mapOptional("data", I.data);
}

bool fromJSON(const llvm::json::Value &Params,
Expand Down
14 changes: 12 additions & 2 deletions clang-tools-extra/clangd/TUScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "support/Cancellation.h"
#include "support/Context.h"
#include "support/Logger.h"
#include "support/MemoryTree.h"
#include "support/Path.h"
#include "support/Threading.h"
#include "support/Trace.h"
Expand Down Expand Up @@ -932,9 +933,9 @@ TUScheduler::FileStats ASTWorker::stats() const {
// Note that we don't report the size of ASTs currently used for processing
// the in-flight requests. We used this information for debugging purposes
// only, so this should be fine.
Result.UsedBytes = IdleASTs.getUsedBytes(this);
Result.UsedBytesAST = IdleASTs.getUsedBytes(this);
if (auto Preamble = getPossiblyStalePreamble())
Result.UsedBytes += Preamble->Preamble.getSize();
Result.UsedBytesPreamble = Preamble->Preamble.getSize();
return Result;
}

Expand Down Expand Up @@ -1429,5 +1430,14 @@ DebouncePolicy DebouncePolicy::fixed(clock::duration T) {
return P;
}

void TUScheduler::profile(MemoryTree &MT) const {
for (const auto &Elem : fileStats()) {
MT.detail(Elem.first())
.child("preamble")
.addUsage(Opts.StorePreamblesInMemory ? Elem.second.UsedBytesPreamble
: 0);
MT.detail(Elem.first()).child("ast").addUsage(Elem.second.UsedBytesAST);
}
}
} // namespace clangd
} // namespace clang
Loading

0 comments on commit 7d8613e

Please sign in to comment.