clang-tools  7.0.0
TUScheduler.h
Go to the documentation of this file.
1 //===--- TUScheduler.h -------------------------------------------*-C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_TUSCHEDULER_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_TUSCHEDULER_H
12 
13 #include "ClangdUnit.h"
14 #include "Function.h"
15 #include "Threading.h"
16 #include "llvm/ADT/StringMap.h"
17 
18 namespace clang {
19 namespace clangd {
20 
21 /// Returns a number of a default async threads to use for TUScheduler.
22 /// Returned value is always >= 1 (i.e. will not cause requests to be processed
23 /// synchronously).
25 
26 struct InputsAndAST {
29 };
30 
32  llvm::StringRef Contents;
33  const tooling::CompileCommand &Command;
35 };
36 
37 /// Determines whether diagnostics should be generated for a file snapshot.
38 enum class WantDiagnostics {
39  Yes, /// Diagnostics must be generated for this snapshot.
40  No, /// Diagnostics must not be generated for this snapshot.
41  Auto, /// Diagnostics must be generated for this snapshot or a subsequent one,
42  /// within a bounded amount of time.
43 };
44 
45 /// Configuration of the AST retention policy. This only covers retention of
46 /// *idle* ASTs. If queue has operations requiring the AST, they might be
47 /// kept in memory.
49  /// Maximum number of ASTs to be retained in memory when there are no pending
50  /// requests for them.
51  unsigned MaxRetainedASTs = 3;
52 };
53 
54 /// Handles running tasks for ClangdServer and managing the resources (e.g.,
55 /// preambles and ASTs) for opened files.
56 /// TUScheduler is not thread-safe, only one thread should be providing updates
57 /// and scheduling tasks.
58 /// Callbacks are run on a threadpool and it's appropriate to do slow work in
59 /// them. Each task has a name, used for tracing (should be UpperCamelCase).
60 /// FIXME(sammccall): pull out a scheduler options struct.
61 class TUScheduler {
62 public:
63  TUScheduler(unsigned AsyncThreadsCount, bool StorePreamblesInMemory,
64  PreambleParsedCallback PreambleCallback,
65  std::chrono::steady_clock::duration UpdateDebounce,
66  ASTRetentionPolicy RetentionPolicy);
67  ~TUScheduler();
68 
69  /// Returns estimated memory usage for each of the currently open files.
70  /// The order of results is unspecified.
71  std::vector<std::pair<Path, std::size_t>> getUsedBytesPerFile() const;
72 
73  /// Returns a list of files with ASTs currently stored in memory. This method
74  /// is not very reliable and is only used for test. E.g., the results will not
75  /// contain files that currently run something over their AST.
76  std::vector<Path> getFilesWithCachedAST() const;
77 
78  /// Schedule an update for \p File. Adds \p File to a list of tracked files if
79  /// \p File was not part of it before.
80  /// FIXME(ibiryukov): remove the callback from this function.
82  llvm::unique_function<void(std::vector<Diag>)> OnUpdated);
83 
84  /// Remove \p File from the list of tracked files and schedule removal of its
85  /// resources.
86  void remove(PathRef File);
87 
88  /// Schedule an async read of the AST. \p Action will be called when AST is
89  /// ready. The AST passed to \p Action refers to the version of \p File
90  /// tracked at the time of the call, even if new updates are received before
91  /// \p Action is executed.
92  /// If an error occurs during processing, it is forwarded to the \p Action
93  /// callback.
94  void runWithAST(llvm::StringRef Name, PathRef File,
96 
97  /// Schedule an async read of the Preamble.
98  /// The preamble may be stale, generated from an older version of the file.
99  /// Reading from locations in the preamble may cause the files to be re-read.
100  /// This gives callers two options:
101  /// - validate that the preamble is still valid, and only use it in this case
102  /// - accept that preamble contents may be outdated, and try to avoid reading
103  /// source code from headers.
104  /// If there's no preamble yet (because the file was just opened), we'll wait
105  /// for it to build. The preamble may still be null if it fails to build or is
106  /// empty.
107  /// If an error occurs during processing, it is forwarded to the \p Action
108  /// callback.
109  void runWithPreamble(llvm::StringRef Name, PathRef File,
111 
112  /// Wait until there are no scheduled or running tasks.
113  /// Mostly useful for synchronizing tests.
114  bool blockUntilIdle(Deadline D) const;
115 
116 private:
117  /// This class stores per-file data in the Files map.
118  struct FileData;
119 
120 public:
121  /// Responsible for retaining and rebuilding idle ASTs. An implementation is
122  /// an LRU cache.
123  class ASTCache;
124 
125 private:
126  const bool StorePreamblesInMemory;
127  const std::shared_ptr<PCHContainerOperations> PCHOps;
128  const PreambleParsedCallback PreambleCallback;
129  Semaphore Barrier;
130  llvm::StringMap<std::unique_ptr<FileData>> Files;
131  std::unique_ptr<ASTCache> IdleASTs;
132  // None when running tasks synchronously and non-None when running tasks
133  // asynchronously.
134  llvm::Optional<AsyncTaskRunner> PreambleTasks;
135  llvm::Optional<AsyncTaskRunner> WorkerThreads;
136  std::chrono::steady_clock::duration UpdateDebounce;
137 };
138 } // namespace clangd
139 } // namespace clang
140 
141 #endif
llvm::StringRef Name
WantDiagnostics
Determines whether diagnostics should be generated for a file snapshot.
Definition: TUScheduler.h:38
Diagnostics must be generated for this snapshot.
const tooling::CompileCommand & Command
Definition: TUScheduler.h:33
std::function< void(PathRef Path, ASTContext &, std::shared_ptr< clang::Preprocessor >)> PreambleParsedCallback
Definition: ClangdUnit.h:131
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:24
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:28
Limits the number of threads that can acquire the lock at the same time.
Definition: Threading.h:40
Configuration of the AST retention policy.
Definition: TUScheduler.h:48
llvm::unique_function< void()> Action
const ParseInputs & Inputs
Definition: TUScheduler.h:27
Stores and provides access to parsed AST.
Definition: ClangdUnit.h:66
unsigned getDefaultAsyncThreadsCount()
Returns a number of a default async threads to use for TUScheduler.
Information required to run clang, e.g. to parse AST or do code completion.
Definition: ClangdUnit.h:59
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
A point in time we can wait for.
Definition: Threading.h:56
Handles running tasks for ClangdServer and managing the resources (e.g., preambles and ASTs) for open...
Definition: TUScheduler.h:61
const PreambleData * Preamble
Definition: TUScheduler.h:34
An LRU cache of idle ASTs.
Definition: TUScheduler.cpp:70
Diagnostics must not be generated for this snapshot.