Skip to content

Commit c497eeb

Browse files
[CAS] Add OnDiskGraphDB and OnDiskKeyValueDB
Add OnDiskGraphDB and OnDiskKeyValueDB that can be used to implement ObjectStore and ActionCache respectively. Those are on-disk persistent storage that build upon OnDiskTrieHashMap and implements key functions that are required by LLVMCAS interfaces. This abstraction layer defines how the objects are hashed and stored on disk. OnDiskKeyValueDB is a basic OnDiskTrieHashMap while OnDiskGraphDB also defines: * How objects of various size are store on disk and are referenced by the trie nodes. * How to store the references from one stored object to another object that is referenced. In addition to basic APIs for ObjectStore and ActionCache, other advances database configuration features can be implemented in this layer without exposing to the users of the LLVMCAS interface. For example, OnDiskGraphDB has a faulty in function to fetch data from an upstream OnDiskGraphDB if the data is missing. Reviewers: Pull Request: #114102
1 parent f43721a commit c497eeb

File tree

11 files changed

+2884
-1
lines changed

11 files changed

+2884
-1
lines changed

llvm/include/llvm/CAS/OnDiskGraphDB.h

Lines changed: 446 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//===- OnDiskKeyValueDB.h ---------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CAS_ONDISKKEYVALUEDB_H
10+
#define LLVM_CAS_ONDISKKEYVALUEDB_H
11+
12+
#include "llvm/CAS/OnDiskTrieRawHashMap.h"
13+
14+
namespace llvm::cas::ondisk {
15+
16+
/// An on-disk key-value data store with the following properties:
17+
/// * Keys are fixed length binary hashes with expected normal distribution.
18+
/// * Values are buffers of the same size, specified at creation time.
19+
/// * The value of a key cannot be changed once it is set.
20+
/// * The value buffers returned from a key lookup have 8-byte alignment.
21+
class OnDiskKeyValueDB {
22+
public:
23+
/// Associate a value with a key.
24+
///
25+
/// \param Key the hash bytes for the key
26+
/// \param Value the value bytes, same size as \p ValueSize parameter of
27+
/// \p open call.
28+
///
29+
/// \returns the value associated with the \p Key. It may be different than
30+
/// \p Value if another value is already associated with this key.
31+
Expected<ArrayRef<char>> put(ArrayRef<uint8_t> Key, ArrayRef<char> Value);
32+
33+
/// \returns the value associated with the \p Key, or \p std::nullopt if the
34+
/// key does not exist.
35+
Expected<std::optional<ArrayRef<char>>> get(ArrayRef<uint8_t> Key);
36+
37+
/// \returns Total size of stored data.
38+
size_t getStorageSize() const {
39+
return Cache.size();
40+
}
41+
42+
/// \returns The precentage of space utilization of hard space limits.
43+
///
44+
/// Return value is an integer between 0 and 100 for percentage.
45+
unsigned getHardStorageLimitUtilization() const {
46+
return Cache.size() * 100ULL / Cache.capacity();
47+
}
48+
49+
/// Open the on-disk store from a directory.
50+
///
51+
/// \param Path directory for the on-disk store. The directory will be created
52+
/// if it doesn't exist.
53+
/// \param HashName Identifier name for the hashing algorithm that is going to
54+
/// be used.
55+
/// \param KeySize Size for the key hash bytes.
56+
/// \param ValueName Identifier name for the values.
57+
/// \param ValueSize Size for the value bytes.
58+
static Expected<std::unique_ptr<OnDiskKeyValueDB>>
59+
open(StringRef Path, StringRef HashName, unsigned KeySize,
60+
StringRef ValueName, size_t ValueSize);
61+
62+
using CheckValueT = function_ref<Error(FileOffset Offset, ArrayRef<char>)>;
63+
Error validate(CheckValueT CheckValue) const;
64+
65+
private:
66+
OnDiskKeyValueDB(size_t ValueSize, OnDiskTrieRawHashMap Cache)
67+
: ValueSize(ValueSize), Cache(std::move(Cache)) {}
68+
69+
const size_t ValueSize;
70+
OnDiskTrieRawHashMap Cache;
71+
};
72+
73+
} // namespace llvm::cas::ondisk
74+
75+
#endif // LLVM_CAS_ONDISKKEYVALUEDB_H

llvm/lib/CAS/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ add_llvm_component_library(LLVMCAS
88
ObjectStore.cpp
99
OnDiskCommon.cpp
1010
OnDiskDataAllocator.cpp
11+
OnDiskGraphDB.cpp
12+
OnDiskKeyValueDB.cpp
1113
OnDiskTrieRawHashMap.cpp
1214

1315
ADDITIONAL_HEADER_DIRS

llvm/lib/CAS/OnDiskCommon.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "OnDiskCommon.h"
10-
#include "llvm/Config/config.h"
1110
#include "llvm/Support/Error.h"
1211
#include "llvm/Support/FileSystem.h"
12+
#include "llvm/Support/Process.h"
1313
#include <thread>
1414

1515
#if __has_include(<sys/file.h>)
@@ -27,6 +27,38 @@
2727

2828
using namespace llvm;
2929

30+
static uint64_t OnDiskCASMaxMappingSize = 0;
31+
32+
Expected<std::optional<uint64_t>> cas::ondisk::getOverriddenMaxMappingSize() {
33+
static std::once_flag Flag;
34+
Error Err = Error::success();
35+
std::call_once(Flag, [&Err] {
36+
ErrorAsOutParameter EAO(&Err);
37+
constexpr const char *EnvVar = "LLVM_CAS_MAX_MAPPING_SIZE";
38+
auto Value = sys::Process::GetEnv(EnvVar);
39+
if (!Value)
40+
return;
41+
42+
uint64_t Size;
43+
if (StringRef(*Value).getAsInteger(/*auto*/ 0, Size))
44+
Err = createStringError(inconvertibleErrorCode(),
45+
"invalid value for %s: expected integer", EnvVar);
46+
OnDiskCASMaxMappingSize = Size;
47+
});
48+
49+
if (Err)
50+
return std::move(Err);
51+
52+
if (OnDiskCASMaxMappingSize == 0)
53+
return std::nullopt;
54+
55+
return OnDiskCASMaxMappingSize;
56+
}
57+
58+
void cas::ondisk::setMaxMappingSize(uint64_t Size) {
59+
OnDiskCASMaxMappingSize = Size;
60+
}
61+
3062
std::error_code cas::ondisk::lockFileThreadSafe(int FD,
3163
sys::fs::LockKind Kind) {
3264
#if HAVE_FLOCK

llvm/lib/CAS/OnDiskCommon.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,25 @@
1212
#include "llvm/Support/Error.h"
1313
#include "llvm/Support/FileSystem.h"
1414
#include <chrono>
15+
#include <optional>
1516

1617
namespace llvm::cas::ondisk {
1718

19+
/// The prefix for all the ondisk database file. It includes a version that
20+
/// needs to be bumped when compatibility breaking changes are introduced.
21+
constexpr StringLiteral FilePrefix = "cas.v1.";
22+
23+
/// Retrieves an overridden maximum mapping size for CAS files, if any,
24+
/// speicified by LLVM_CAS_MAX_MAPPING_SIZE in the environment or set by
25+
/// `setMaxMappingSize()`. If the value from environment is unreadable, returns
26+
/// an error.
27+
Expected<std::optional<uint64_t>> getOverriddenMaxMappingSize();
28+
29+
/// Set MaxMappingSize for ondisk CAS. This function is not thread-safe and
30+
/// should be set before creaing any ondisk CAS and does not affect CAS already
31+
/// created. Set value 0 to use default size.
32+
void setMaxMappingSize(uint64_t Size);
33+
1834
/// Thread-safe alternative to \c sys::fs::lockFile. This does not support all
1935
/// the platforms that \c sys::fs::lockFile does, so keep it in the CAS library
2036
/// for now.

0 commit comments

Comments
 (0)