[clang] [llvm] [llvm][clang] Trace VFS calls (PR #88326)

2024-04-12 Thread Jan Svoboda via cfe-commits

https://github.com/jansvoboda11 updated 
https://github.com/llvm/llvm-project/pull/88326

>From b395e907fcb3895941b58e0d00ef69f14b07c4ae Mon Sep 17 00:00:00 2001
From: Jan Svoboda 
Date: Fri, 12 Apr 2024 10:47:13 -0700
Subject: [PATCH] [llvm][support] Implement tracing virtual file system

---
 .../DependencyScanningFilesystemTest.cpp  | 26 +-
 llvm/include/llvm/Support/VirtualFileSystem.h | 48 ++
 llvm/lib/Support/VirtualFileSystem.cpp| 13 +
 .../Support/VirtualFileSystemTest.cpp | 50 +++
 4 files changed, 113 insertions(+), 24 deletions(-)

diff --git 
a/clang/unittests/Tooling/DependencyScanning/DependencyScanningFilesystemTest.cpp
 
b/clang/unittests/Tooling/DependencyScanning/DependencyScanningFilesystemTest.cpp
index 697b7d70ff035a..c1a06c1f5ea20e 100644
--- 
a/clang/unittests/Tooling/DependencyScanning/DependencyScanningFilesystemTest.cpp
+++ 
b/clang/unittests/Tooling/DependencyScanning/DependencyScanningFilesystemTest.cpp
@@ -13,33 +13,11 @@
 
 using namespace clang::tooling::dependencies;
 
-namespace {
-struct InstrumentingFilesystem
-: llvm::RTTIExtends {
-  unsigned NumStatusCalls = 0;
-  unsigned NumGetRealPathCalls = 0;
-
-  using llvm::RTTIExtends::RTTIExtends;
-
-  llvm::ErrorOr status(const llvm::Twine ) override {
-++NumStatusCalls;
-return ProxyFileSystem::status(Path);
-  }
-
-  std::error_code getRealPath(const llvm::Twine ,
-  llvm::SmallVectorImpl ) override {
-++NumGetRealPathCalls;
-return ProxyFileSystem::getRealPath(Path, Output);
-  }
-};
-} // namespace
-
 TEST(DependencyScanningWorkerFilesystem, CacheStatusFailures) {
   auto InMemoryFS = llvm::makeIntrusiveRefCnt();
 
   auto InstrumentingFS =
-  llvm::makeIntrusiveRefCnt(InMemoryFS);
+  llvm::makeIntrusiveRefCnt(InMemoryFS);
 
   DependencyScanningFilesystemSharedCache SharedCache;
   DependencyScanningWorkerFilesystem DepFS(SharedCache, InstrumentingFS);
@@ -65,7 +43,7 @@ TEST(DependencyScanningFilesystem, CacheGetRealPath) {
   InMemoryFS->addFile("/bar", 0, llvm::MemoryBuffer::getMemBuffer(""));
 
   auto InstrumentingFS =
-  llvm::makeIntrusiveRefCnt(InMemoryFS);
+  llvm::makeIntrusiveRefCnt(InMemoryFS);
 
   DependencyScanningFilesystemSharedCache SharedCache;
   DependencyScanningWorkerFilesystem DepFS(SharedCache, InstrumentingFS);
diff --git a/llvm/include/llvm/Support/VirtualFileSystem.h 
b/llvm/include/llvm/Support/VirtualFileSystem.h
index 4b1ca0c3d262b6..1d851d4d9aea51 100644
--- a/llvm/include/llvm/Support/VirtualFileSystem.h
+++ b/llvm/include/llvm/Support/VirtualFileSystem.h
@@ -1125,6 +1125,54 @@ class YAMLVFSWriter {
   void write(llvm::raw_ostream );
 };
 
+/// File system that tracks the number of calls to the underlying file system.
+/// This is particularly useful when wrapped around \c RealFileSystem to add
+/// lightweight tracking of expensive syscalls.
+class TracingFileSystem
+: public llvm::RTTIExtends {
+public:
+  static const char ID;
+
+  std::size_t NumStatusCalls = 0;
+  std::size_t NumOpenFileForReadCalls = 0;
+  std::size_t NumDirBeginCalls = 0;
+  std::size_t NumGetRealPathCalls = 0;
+  std::size_t NumIsLocalCalls = 0;
+
+  TracingFileSystem(llvm::IntrusiveRefCntPtr FS)
+  : RTTIExtends(std::move(FS)) {}
+
+  ErrorOr status(const Twine ) override {
+++NumStatusCalls;
+return ProxyFileSystem::status(Path);
+  }
+
+  ErrorOr> openFileForRead(const Twine ) override {
+++NumOpenFileForReadCalls;
+return ProxyFileSystem::openFileForRead(Path);
+  }
+
+  directory_iterator dir_begin(const Twine , std::error_code ) override 
{
+++NumDirBeginCalls;
+return ProxyFileSystem::dir_begin(Dir, EC);
+  }
+
+  std::error_code getRealPath(const Twine ,
+  SmallVectorImpl ) override {
+++NumGetRealPathCalls;
+return ProxyFileSystem::getRealPath(Path, Output);
+  }
+
+  std::error_code isLocal(const Twine , bool ) override {
+++NumIsLocalCalls;
+return ProxyFileSystem::isLocal(Path, Result);
+  }
+
+protected:
+  void printImpl(raw_ostream , PrintType Type,
+ unsigned IndentLevel) const override;
+};
+
 } // namespace vfs
 } // namespace llvm
 
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp 
b/llvm/lib/Support/VirtualFileSystem.cpp
index 32b480028e71b4..fe8924585e1ceb 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -2877,8 +2877,21 @@ recursive_directory_iterator::increment(std::error_code 
) {
   return *this;
 }
 
+void TracingFileSystem::printImpl(raw_ostream , PrintType Type,
+  unsigned IndentLevel) const {
+  printIndent(OS, IndentLevel);
+  OS << "TracingFileSystem\n";
+  if (Type == PrintType::Summary)
+return;
+
+  if (Type == PrintType::Contents)
+Type = PrintType::Summary;
+  getUnderlyingFS().print(OS, Type, IndentLevel + 1);
+}
+
 const char FileSystem::ID = 0;
 const char 

[clang] [llvm] [llvm][clang] Trace VFS calls (PR #88326)

2024-04-10 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff e72c949c15208ba3dd53a9cebfee02734965a678 
540321e84dbd3c5687cfcc60e9deec79d790896e -- 
clang/include/clang/Driver/Compilation.h 
clang/include/clang/Frontend/CompilerInstance.h 
clang/include/clang/Frontend/CompilerInvocation.h 
clang/include/clang/Frontend/FrontendOptions.h clang/lib/Driver/Driver.cpp 
clang/lib/Driver/ToolChains/Clang.cpp clang/lib/Frontend/CompilerInstance.cpp 
clang/lib/Frontend/CompilerInvocation.cpp 
clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp 
clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp 
clang/tools/clang-scan-deps/ClangScanDeps.cpp clang/tools/driver/cc1_main.cpp 
llvm/include/llvm/Support/VirtualFileSystem.h 
llvm/lib/Support/VirtualFileSystem.cpp 
llvm/unittests/Support/VirtualFileSystemTest.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/include/clang/Frontend/CompilerInstance.h 
b/clang/include/clang/Frontend/CompilerInstance.h
index 2135002366..7f645ee9e9 100644
--- a/clang/include/clang/Frontend/CompilerInstance.h
+++ b/clang/include/clang/Frontend/CompilerInstance.h
@@ -95,8 +95,8 @@ class CompilerInstance : public ModuleLoader {
 public:
   /// The instrumenting file system.
   IntrusiveRefCntPtr IVFS;
-private:
 
+private:
   /// The file manager.
   IntrusiveRefCntPtr FileMgr;
 

``




https://github.com/llvm/llvm-project/pull/88326
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [llvm][clang] Trace VFS calls (PR #88326)

2024-04-10 Thread Jan Svoboda via cfe-commits

https://github.com/jansvoboda11 created 
https://github.com/llvm/llvm-project/pull/88326

None

>From 540321e84dbd3c5687cfcc60e9deec79d790896e Mon Sep 17 00:00:00 2001
From: Jan Svoboda 
Date: Wed, 10 Apr 2024 16:03:19 -0700
Subject: [PATCH] [llvm][clang] Trace VFS calls

---
 clang/include/clang/Driver/Compilation.h  | 11 +
 clang/include/clang/Driver/Options.td |  7 
 .../include/clang/Frontend/CompilerInstance.h |  8 
 .../clang/Frontend/CompilerInvocation.h   | 10 +++--
 .../include/clang/Frontend/FrontendOptions.h  |  3 ++
 clang/lib/Driver/Driver.cpp   | 34 ++-
 clang/lib/Driver/ToolChains/Clang.cpp |  3 ++
 clang/lib/Frontend/CompilerInstance.cpp   |  2 +-
 clang/lib/Frontend/CompilerInvocation.cpp | 19 ++---
 .../DependencyScanningWorker.cpp  |  1 +
 .../DependencyScanning/ModuleDepCollector.cpp |  7 
 clang/test/ClangScanDeps/modules-inferred.m   |  2 +-
 clang/tools/clang-scan-deps/ClangScanDeps.cpp | 25 ++-
 clang/tools/clang-scan-deps/Opts.td   |  1 +
 clang/tools/driver/cc1_main.cpp   | 13 ++
 llvm/include/llvm/Support/VirtualFileSystem.h | 29 +
 llvm/lib/Support/VirtualFileSystem.cpp| 42 +++
 .../Support/VirtualFileSystemTest.cpp | 37 
 18 files changed, 241 insertions(+), 13 deletions(-)

diff --git a/clang/include/clang/Driver/Compilation.h 
b/clang/include/clang/Driver/Compilation.h
index 36ae85c4245143..baf55d6b0f6061 100644
--- a/clang/include/clang/Driver/Compilation.h
+++ b/clang/include/clang/Driver/Compilation.h
@@ -115,6 +115,9 @@ class Compilation {
   /// -ftime-trace result files.
   ArgStringMap TimeTraceFiles;
 
+  /// -fvfs-trace result files.
+  ArgStringMap VFSTraceFiles;
+
   /// Optional redirection for stdin, stdout, stderr.
   std::vector> Redirects;
 
@@ -280,6 +283,14 @@ class Compilation {
 TimeTraceFiles[JA] = Name;
   }
 
+  const char *getVFSTraceFile(const JobAction *JA) const {
+return VFSTraceFiles.lookup(JA);
+  }
+  void addVFSTraceFile(const char *Name, const JobAction *JA) {
+assert(!VFSTraceFiles.contains(JA));
+VFSTraceFiles[JA] = Name;
+  }
+
   /// CleanupFile - Delete a given file.
   ///
   /// \param IssueErrors - Report failures as errors.
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 0a74e6c75f95bb..630c1c763b5180 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3904,6 +3904,13 @@ def ftime_trace_EQ : Joined<["-"], "ftime-trace=">, 
Group,
   HelpText<"Similar to -ftime-trace. Specify the JSON file or a directory 
which will contain the JSON file">,
   Visibility<[ClangOption, CC1Option, CLOption, DXCOption]>,
   MarshallingInfoString>;
+def fvfs_trace : Flag<["-"], "fvfs-trace">, Group,
+  HelpText<"Turn of virtual file system profiler. Generates text file based on 
output filename.">,
+  Visibility<[ClangOption, CLOption, DXCOption]>;
+def fvfs_trace_EQ : Joined<["-"], "fvfs-trace=">, Group,
+  HelpText<"Similar to -fvfs-trace. Specify the text file or a directory that 
will contain the text file">,
+  Visibility<[ClangOption, CC1Option, CLOption, DXCOption]>,
+  MarshallingInfoString>;
 def fproc_stat_report : Joined<["-"], "fproc-stat-report">, Group,
   HelpText<"Print subprocess statistics">;
 def fproc_stat_report_EQ : Joined<["-"], "fproc-stat-report=">, Group,
diff --git a/clang/include/clang/Frontend/CompilerInstance.h 
b/clang/include/clang/Frontend/CompilerInstance.h
index 3464654284f199..2135002366a33a 100644
--- a/clang/include/clang/Frontend/CompilerInstance.h
+++ b/clang/include/clang/Frontend/CompilerInstance.h
@@ -35,6 +35,9 @@ namespace llvm {
 class raw_fd_ostream;
 class Timer;
 class TimerGroup;
+namespace vfs {
+struct InstrumentingFileSystem;
+}
 }
 
 namespace clang {
@@ -89,6 +92,11 @@ class CompilerInstance : public ModuleLoader {
   /// Auxiliary Target info.
   IntrusiveRefCntPtr AuxTarget;
 
+public:
+  /// The instrumenting file system.
+  IntrusiveRefCntPtr IVFS;
+private:
+
   /// The file manager.
   IntrusiveRefCntPtr FileMgr;
 
diff --git a/clang/include/clang/Frontend/CompilerInvocation.h 
b/clang/include/clang/Frontend/CompilerInvocation.h
index 1a2a39411e58d8..b50e7c7da636ec 100644
--- a/clang/include/clang/Frontend/CompilerInvocation.h
+++ b/clang/include/clang/Frontend/CompilerInvocation.h
@@ -39,6 +39,7 @@ class ArgList;
 namespace vfs {
 
 class FileSystem;
+struct InstrumentingFileSystem;
 
 } // namespace vfs
 
@@ -390,13 +391,14 @@ class CowCompilerInvocation : public 
CompilerInvocationBase {
   /// @}
 };
 
-IntrusiveRefCntPtr
-createVFSFromCompilerInvocation(const CompilerInvocation ,
-DiagnosticsEngine );
+IntrusiveRefCntPtr createVFSFromCompilerInvocation(
+const CompilerInvocation , DiagnosticsEngine ,
+IntrusiveRefCntPtr *TracingFS = {});
 
 IntrusiveRefCntPtr