Skip to content

Commit 5f3599f

Browse files
[CAS] Improve MappedFileRegionBumpPtr
Improve MappedFileRegionBumpPtr so it can handle being opened with different capacities. Mismatching different capacities and header offsets will result in error on creation.
1 parent ea351fe commit 5f3599f

File tree

14 files changed

+446
-111
lines changed

14 files changed

+446
-111
lines changed

clang/test/CAS/daemon-cas-recovery.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

1010
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas LLVM_CAS_FORCE_VALIDATION=1 %clang-cache \

clang/test/CAS/depscan-cas-log.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
// RUN: -cc1-args -cc1 -triple x86_64-apple-macosx11.0.0 -emit-obj %s -o %t/t.o -fcas-path %t/cas
1111
// RUN: FileCheck %s --input-file %t/cas/v1.log
1212

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

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

19-
// CHECK: [[PID2:[0-9]*]] {{[0-9]*}}: mmap '{{.*}}v9.index'
20-
// CHECK: [[PID2]] {{[0-9]*}}: close mmap '{{.*}}v9.index'
19+
// CHECK: [[PID2:[0-9]*]] {{[0-9]*}}: mmap '{{.*}}v{{[0-9]+}}.index'
20+
// CHECK: [[PID2]] {{[0-9]*}}: close mmap '{{.*}}v{{[0-9]+}}.index'

clang/test/CAS/validate-once.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: rm -rf %t
22

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

66
// RUN: %clang -cc1depscand -execute %{clang-daemon-dir}/%basename_t -cas-args -fcas-path %t/cas -- \
77
// RUN: %clang -target x86_64-apple-macos11 -I %S/Inputs \

llvm/include/llvm/CAS/MappedFileRegionBumpPtr.h

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8+
//
9+
/// \file
10+
/// This file declares interface for MappedFileRegionBumpPtr, a bump pointer
11+
/// allocator, backed by a memory-mapped file.
12+
///
13+
//===----------------------------------------------------------------------===//
814

915
#ifndef LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H
1016
#define LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H
1117

12-
#include "llvm/Config/llvm-config.h"
1318
#include "llvm/Support/Alignment.h"
1419
#include "llvm/Support/FileSystem.h"
1520
#include <atomic>
@@ -18,7 +23,7 @@ namespace llvm::cas {
1823

1924
namespace ondisk {
2025
class OnDiskCASLogger;
21-
}
26+
} // namespace ondisk
2227

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

46+
/// Header for MappedFileRegionBumpPtr. It can be configured to be located
47+
/// at any location within the file and the allocation will be appended after
48+
/// the header.
49+
struct Header {
50+
std::atomic<uint64_t> BumpPtr;
51+
std::atomic<uint64_t> AllocatedSize;
52+
};
53+
4654
/// Create a \c MappedFileRegionBumpPtr.
4755
///
4856
/// \param Path the path to open the mapped region.
4957
/// \param Capacity the maximum size for the mapped file region.
50-
/// \param BumpPtrOffset the offset at which to store the bump pointer.
58+
/// \param HeaderOffset the offset at which to store the header. This is so
59+
/// that information can be stored before the header, like a file magic.
5160
/// \param NewFileConstructor is for constructing new files. It has exclusive
5261
/// access to the file. Must call \c initializeBumpPtr.
5362
static Expected<MappedFileRegionBumpPtr>
54-
create(const Twine &Path, uint64_t Capacity, int64_t BumpPtrOffset,
63+
create(const Twine &Path, uint64_t Capacity, uint64_t HeaderOffset,
5564
std::shared_ptr<ondisk::OnDiskCASLogger> Logger,
5665
function_ref<Error(MappedFileRegionBumpPtr &)> NewFileConstructor);
5766

58-
/// Finish initializing the bump pointer. Must be called by
59-
/// \c NewFileConstructor.
60-
void initializeBumpPtr(int64_t BumpPtrOffset);
67+
/// Finish initializing the header. Must be called by \c NewFileConstructor.
68+
void initializeHeader(uint64_t HeaderOffset);
6169

6270
/// Minimum alignment for allocations, currently hardcoded to 8B.
6371
static constexpr Align getAlign() {
@@ -108,14 +116,12 @@ class MappedFileRegionBumpPtr {
108116
}
109117

110118
private:
111-
struct Header {
112-
std::atomic<int64_t> BumpPtr;
113-
std::atomic<int64_t> AllocatedSize;
114-
};
115119
RegionT Region;
116120
Header *H = nullptr;
117121
std::string Path;
122+
// File descriptor for the main storage file.
118123
std::optional<int> FD;
124+
// File descriptor for the file used as reader/writer lock.
119125
std::optional<int> SharedLockFD;
120126
std::shared_ptr<ondisk::OnDiskCASLogger> Logger = nullptr;
121127
};

0 commit comments

Comments
 (0)