Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/test/CAS/daemon-cas-recovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/// Construct a malformed CAS to recovery from.
// RUN: echo "abc" | llvm-cas --cas %t/cas --make-blob --data -
// RUN: rm %t/cas/v1.1/v9.data
// RUN: rm %t/cas/v1.1/v10.data
// RUN: not llvm-cas --cas %t/cas --validate --check-hash

// RUN: env LLVM_CACHE_CAS_PATH=%t/cas LLVM_CAS_FORCE_VALIDATION=1 %clang-cache \
Expand Down
6 changes: 3 additions & 3 deletions clang/test/CAS/depscan-cas-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
// RUN: -cc1-args -cc1 -triple x86_64-apple-macosx11.0.0 -emit-obj %s -o %t/t.o -fcas-path %t/cas
// RUN: FileCheck %s --input-file %t/cas/v1.log

// CHECK: [[PID1:[0-9]*]] {{[0-9]*}}: mmap '{{.*}}v9.index'
// CHECK: [[PID1:[0-9]*]] {{[0-9]*}}: mmap '{{.*}}v{{[0-9]+}}.index'
// CHECK: [[PID1]] {{[0-9]*}}: create subtrie

// Even a minimal compilation involves at least 9 records for the cache key.
// CHECK-COUNT-9: [[PID1]] {{[0-9]*}}: create record

// CHECK: [[PID2:[0-9]*]] {{[0-9]*}}: mmap '{{.*}}v9.index'
// CHECK: [[PID2]] {{[0-9]*}}: close mmap '{{.*}}v9.index'
// CHECK: [[PID2:[0-9]*]] {{[0-9]*}}: mmap '{{.*}}v{{[0-9]+}}.index'
// CHECK: [[PID2]] {{[0-9]*}}: close mmap '{{.*}}v{{[0-9]+}}.index'
2 changes: 1 addition & 1 deletion clang/test/CAS/validate-once.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: rm -rf %t

// RUN: llvm-cas --cas %t/cas --ingest %s
// RUN: mv %t/cas/v1.1/v9.data %t/cas/v1.1/v9.data.bak
// RUN: mv %t/cas/v1.1/v10.data %t/cas/v1.1/v10.data.bak

// RUN: %clang -cc1depscand -execute %{clang-daemon-dir}/%basename_t -cas-args -fcas-path %t/cas -- \
// RUN: %clang -target x86_64-apple-macos11 -I %S/Inputs \
Expand Down
1 change: 1 addition & 0 deletions clang/tools/driver/cc1depscanProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Expected<ScanDaemon> ScanDaemon::launchDaemon(StringRef BasePath,
static constexpr const char *PassThroughEnv[] = {
"LLVM_CAS_LOG",
"LLVM_CAS_DISABLE_VALIDATION",
"LLVM_CAS_MAX_MAPPING_SIZE",
};
SmallVector<const char *> EnvP;
for (const char *Name : PassThroughEnv)
Expand Down
41 changes: 24 additions & 17 deletions llvm/include/llvm/CAS/MappedFileRegionBumpPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
/// \file
/// This file declares interface for MappedFileRegionBumpPtr, a bump pointer
/// allocator, backed by a memory-mapped file.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H
#define LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H

#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/FileSystem.h"
#include <atomic>
Expand All @@ -18,7 +23,7 @@ namespace llvm::cas {

namespace ondisk {
class OnDiskCASLogger;
}
} // namespace ondisk

/// Allocator for an owned mapped file region that supports thread-safe and
/// process-safe bump pointer allocation.
Expand All @@ -31,33 +36,37 @@ class OnDiskCASLogger;
/// Process-safe. Uses file locks when resizing the file during initialization
/// and destruction.
///
/// Thread-safe, assuming all threads use the same instance to talk to a given
/// file/mapping. Unsafe to have multiple instances talking to the same file
/// in the same process since file locks will misbehave. Clients should
/// coordinate (somehow).
///
/// \note Currently we allocate the whole file without sparseness on Windows.
/// Thread-safe. Requires OS support thread-safe file lock.
///
/// Provides 8-byte alignment for all allocations.
class MappedFileRegionBumpPtr {
public:
using RegionT = sys::fs::mapped_file_region;

/// Header for MappedFileRegionBumpPtr. It can be configured to be located
/// at any location within the file and the allocation will be appended after
/// the header.
struct Header {
std::atomic<uint64_t> BumpPtr;
std::atomic<uint64_t> AllocatedSize;
};

/// Create a \c MappedFileRegionBumpPtr.
///
/// \param Path the path to open the mapped region.
/// \param Capacity the maximum size for the mapped file region.
/// \param BumpPtrOffset the offset at which to store the bump pointer.
/// \param HeaderOffset the offset at which to store the header. This is so
/// that information can be stored before the header, like a file magic.
/// \param NewFileConstructor is for constructing new files. It has exclusive
/// access to the file. Must call \c initializeBumpPtr.
static Expected<MappedFileRegionBumpPtr>
create(const Twine &Path, uint64_t Capacity, int64_t BumpPtrOffset,
create(const Twine &Path, uint64_t Capacity, uint64_t HeaderOffset,
std::shared_ptr<ondisk::OnDiskCASLogger> Logger,
function_ref<Error(MappedFileRegionBumpPtr &)> NewFileConstructor);

/// Finish initializing the bump pointer. Must be called by
/// \c NewFileConstructor.
void initializeBumpPtr(int64_t BumpPtrOffset);
/// Finish initializing the header. Must be called by \c NewFileConstructor.
/// \c HeaderOffset passed should match the value passed to \c create.
void initializeHeader(uint64_t HeaderOffset);

/// Minimum alignment for allocations, currently hardcoded to 8B.
static constexpr Align getAlign() {
Expand Down Expand Up @@ -108,14 +117,12 @@ class MappedFileRegionBumpPtr {
}

private:
struct Header {
std::atomic<int64_t> BumpPtr;
std::atomic<int64_t> AllocatedSize;
};
RegionT Region;
Header *H = nullptr;
std::string Path;
// File descriptor for the main storage file.
std::optional<int> FD;
// File descriptor for the file used as reader/writer lock.
std::optional<int> SharedLockFD;
std::shared_ptr<ondisk::OnDiskCASLogger> Logger = nullptr;
};
Expand Down
Loading