mirror-llvm-project/clang/lib/Serialization/InMemoryModuleCache.cpp
Qiongsi Wu a730dda7d5
[clang][DependencyScanning] Logging Dependency Scanning Events (#195896)
This PR sets up an `AtomicLineLogger` (added by
https://github.com/llvm/llvm-project/pull/205395) to log the sequence of
dependency scanning events. The logging is triggered by a new `LogPath`
option in `DependencyScanningServiceOptions`. This PR adds an option
`-log-path=` to `clang-scan-deps` to trigger the log.

We are logging 16 events during dependency scanning. 

| Event | Logged In | When |
|-------|-----------|------|
| `starting scanning command: <args>` | `DependencyScanningWorker.cpp` |
Start of each TU scan |
| `finished scanning command: <args>` | `DependencyScanningWorker.cpp` |
End of each TU scan |
| `init_compiler_instance_with_context: <args>` |
`DependencyScanningTool.cpp` | CompilerInstanceWithContext created for
by-name scanning |
| `start scan_by_name: <module name>` | `DependencyScanningTool.cpp` |
Start of a by-name module query |
| `finish scan_by_name: <module name>` | `DependencyScanningTool.cpp` |
End of a by-name module query |
| `module_compile_thread: parent=<TID> pcm_compile: <pcm path>` |
`CompilerInstance::compileModule` | A new thread is created to compile a
pcm |
| `timestamp_read: <pcm path>` |
`InProcessModuleCache::getModuleTimestamp` | Validation timestamp read |
| `timestamp_write: <pcm path>` |
`InProcessModuleCache::updateModuleTimestamp` | Validation timestamp
update |
| `pcm_write: <pcm path>` | `InProcessModuleCache::write` | PCM write to
disk attempted |
| `pcm_read_disk: <pcm path>` | `InProcessModuleCache::read` | PCM read
from disk attempted |
| `pcm_read_cached: <pcm path>` | `InMemoryModuleCache::lookupPCM` | PCM
looked up in in-memory cache |
| `pcm_add: <pcm path>` | `InMemoryModuleCache::addPCM` | PCM added to
cache (not validated) |
| `pcm_add_built: <pcm path>` | `InMemoryModuleCache::addBuiltPCM` | PCM
added to cache (final) |
| `pcm_finalized: <pcm path>` | `InMemoryModuleCache::finalizePCM` | PCM
marked as validated/final |
| `pcm_dropped: <pcm path>` | `InMemoryModuleCache::tryToDropPCM` | PCM
dropped (validation failed) |
| `pcm_not_dropped: <pcm path>` | `InMemoryModuleCache::tryToDropPCM` |
PCM not dropped (validation failed, but already final) |

In addition to checking the logger's functionality,
`clang/test/ClangScanDeps/logging-simple.c` checks against the change
committed by https://github.com/llvm/llvm-project/pull/177062. Without
https://github.com/llvm/llvm-project/pull/177062,
`clang/test/ClangScanDeps/logging-simple.c` fails.

This implementation adds a single instance of `AtomicLineLogger` to the
`DependencyScanningService`. Most of the plumbing goes into the
`ModuleCache` and its derived classes.

Assisted-by: claude-opus-4.6

rdar://39907408
2026-07-16 10:41:22 -07:00

97 lines
3.1 KiB
C++

//===- InMemoryModuleCache.cpp - Cache for loaded memory buffers ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/Serialization/InMemoryModuleCache.h"
#include "clang/Basic/AtomicLineLogger.h"
#include "llvm/Support/MemoryBuffer.h"
using namespace clang;
InMemoryModuleCache::State
InMemoryModuleCache::getPCMState(llvm::StringRef Filename) const {
auto I = PCMs.find(Filename);
if (I == PCMs.end())
return Unknown;
if (I->second.IsFinal)
return Final;
return I->second.Buffer ? Tentative : ToBuild;
}
llvm::MemoryBuffer &
InMemoryModuleCache::addPCM(llvm::StringRef Filename,
std::unique_ptr<llvm::MemoryBuffer> Buffer,
off_t Size, time_t ModTime) {
Logger.log() << "pcm_add: " << Filename;
auto Insertion = PCMs.insert(
std::make_pair(Filename, PCM(std::move(Buffer), Size, ModTime)));
assert(Insertion.second && "Already has a PCM");
return *Insertion.first->second.Buffer;
}
llvm::MemoryBuffer &
InMemoryModuleCache::addBuiltPCM(llvm::StringRef Filename,
std::unique_ptr<llvm::MemoryBuffer> Buffer,
off_t Size, time_t ModTime) {
Logger.log() << "pcm_add_built: " << Filename;
auto &PCM = PCMs[Filename];
assert(!PCM.IsFinal && "Trying to override finalized PCM?");
assert(!PCM.Buffer && "Trying to override tentative PCM?");
PCM.Buffer = std::move(Buffer);
PCM.Size = Size;
PCM.ModTime = ModTime;
PCM.IsFinal = true;
return *PCM.Buffer;
}
llvm::MemoryBuffer *InMemoryModuleCache::lookupPCM(llvm::StringRef Filename,
off_t &Size,
time_t &ModTime) const {
Logger.log() << "pcm_read_cached: " << Filename;
auto I = PCMs.find(Filename);
if (I == PCMs.end())
return nullptr;
Size = I->second.Size;
ModTime = I->second.ModTime;
return I->second.Buffer.get();
}
bool InMemoryModuleCache::isPCMFinal(llvm::StringRef Filename) const {
return getPCMState(Filename) == Final;
}
bool InMemoryModuleCache::shouldBuildPCM(llvm::StringRef Filename) const {
return getPCMState(Filename) == ToBuild;
}
bool InMemoryModuleCache::tryToDropPCM(llvm::StringRef Filename) {
auto I = PCMs.find(Filename);
assert(I != PCMs.end() && "PCM to remove is unknown...");
auto &PCM = I->second;
assert(PCM.Buffer && "PCM to remove is scheduled to be built...");
if (PCM.IsFinal) {
Logger.log() << "pcm_not_dropped: " << Filename;
return true;
}
Logger.log() << "pcm_dropped: " << Filename;
PCM.Buffer.reset();
return false;
}
void InMemoryModuleCache::finalizePCM(llvm::StringRef Filename) {
Logger.log() << "pcm_finalized: " << Filename;
auto I = PCMs.find(Filename);
assert(I != PCMs.end() && "PCM to finalize is unknown...");
auto &PCM = I->second;
assert(PCM.Buffer && "Trying to finalize a dropped PCM...");
PCM.IsFinal = true;
}