Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
316 changes: 316 additions & 0 deletions llvm/include/llvm/CAS/OnDiskTrieRawHashMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
/// \file
/// This file declares interface for OnDiskTrieRawHashMap, a thread-safe and
/// (mostly) lock-free hash map stored as trie and backed by persistent files on
/// disk.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_CAS_ONDISKHASHMAPPEDTRIE_H
#define LLVM_CAS_ONDISKHASHMAPPEDTRIE_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include <optional>

namespace llvm {

class raw_ostream;

namespace cas {

/// FileOffset is a wrapper around `uint64_t` to represent the offset of data
/// from the beginning of the file.
class FileOffset {
public:
uint64_t get() const { return Offset; }

explicit operator bool() const { return Offset; }

FileOffset() = default;
explicit FileOffset(uint64_t Offset) : Offset(Offset) {}

private:
uint64_t Offset = 0;
};

/// OnDiskTrieRawHashMap is a persistent trie data structure used as hash maps.
/// The keys are fixed length, and are expected to be binary hashes with a
/// normal distribution.
///
/// - Thread-safety is achieved through the use of atomics within a shared
/// memory mapping. Atomic access does not work on networked filesystems.
/// - Filesystem locks are used, but only sparingly:
/// - during initialization, for creating / opening an existing store;
/// - for the lifetime of the instance, a shared/reader lock is held
/// - during destruction, if there are no concurrent readers, to shrink the
/// files to their minimum size.
/// - Path is used as a directory:
/// - "index" stores the root trie and subtries.
/// - "data" stores (most of) the entries, like a bump-ptr-allocator.
/// - Large entries are stored externally in a file named by the key.
/// - Code is system-dependent and binary format itself is not portable. These
/// are not artifacts that can/should be moved between different systems; they
/// are only appropriate for local storage.
class OnDiskTrieRawHashMap {
public:
LLVM_DUMP_METHOD void dump() const;
void
print(raw_ostream &OS,
function_ref<void(ArrayRef<char>)> PrintRecordData = nullptr) const;

public:
/// Const value proxy to access the records stored in TrieRawHashMap.
struct ConstValueProxy {
ConstValueProxy() = default;
ConstValueProxy(ArrayRef<uint8_t> Hash, ArrayRef<char> Data)
: Hash(Hash), Data(Data) {}
ConstValueProxy(ArrayRef<uint8_t> Hash, StringRef Data)
: Hash(Hash), Data(Data.begin(), Data.size()) {}

ArrayRef<uint8_t> Hash;
ArrayRef<char> Data;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity — why is this using a signed char as storage?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What suggestions you have? It is quite natural as Data can be get as StringRef which is const char*, but I guess you are asking why Hash and Data are different types? I don't really have preference either way.

};

/// Value proxy to access the records stored in TrieRawHashMap.
struct ValueProxy {
operator ConstValueProxy() const { return ConstValueProxy(Hash, Data); }

ValueProxy() = default;
ValueProxy(ArrayRef<uint8_t> Hash, MutableArrayRef<char> Data)
: Hash(Hash), Data(Data) {}

ArrayRef<uint8_t> Hash;
MutableArrayRef<char> Data;
};

/// Validate the trie data structure.
///
/// Callback receives the file offset to the data entry and the data stored.
Error validate(
function_ref<Error(FileOffset, ConstValueProxy)> RecordVerifier) const;

/// Check the valid range of file offset for OnDiskTrieRawHashMap.
static bool validOffset(FileOffset Offset) {
return Offset.get() < (1LL << 48);
}

public:
/// Template class to implement a `pointer` type into the trie data structure.
///
/// It provides pointer-like operation, e.g., dereference to get underlying
/// data. It also reserves the top 16 bits of the pointer value, which can be
/// used to pack additional information if needed.
template <class ProxyT> class PointerImpl {
public:
FileOffset getOffset() const {
return FileOffset(OffsetLow32 | (uint64_t)OffsetHigh16 << 32);
}

explicit operator bool() const { return IsValue; }

const ProxyT &operator*() const {
assert(IsValue);
return Value;
}
const ProxyT *operator->() const {
assert(IsValue);
return &Value;
}

PointerImpl() = default;

protected:
PointerImpl(ProxyT Value, FileOffset Offset, bool IsValue = true)
: Value(Value), OffsetLow32((uint64_t)Offset.get()),
OffsetHigh16((uint64_t)Offset.get() >> 32), IsValue(IsValue) {
if (IsValue)
assert(validOffset(Offset));
}

ProxyT Value;
uint32_t OffsetLow32 = 0;
uint16_t OffsetHigh16 = 0;

// True if points to a value (not a "nullptr"). Use an extra field because
// 0 can be a valid offset.
bool IsValue = false;
};

class pointer;
class const_pointer : public PointerImpl<ConstValueProxy> {
public:
const_pointer() = default;

private:
friend class pointer;
friend class OnDiskTrieRawHashMap;
using const_pointer::PointerImpl::PointerImpl;
};

class pointer : public PointerImpl<ValueProxy> {
public:
operator const_pointer() const {
return const_pointer(Value, getOffset(), IsValue);
}

pointer() = default;

private:
friend class OnDiskTrieRawHashMap;
using pointer::PointerImpl::PointerImpl;
};

/// Find the value from hash.
///
/// \returns pointer to the value if exists, otherwise returns a non-value
/// pointer that evaluates to `false` when convert to boolean.
const_pointer find(ArrayRef<uint8_t> Hash) const;

/// Helper function to recover a pointer into the trie from file offset.
Expected<const_pointer> recoverFromFileOffset(FileOffset Offset) const;

using LazyInsertOnConstructCB =
function_ref<void(FileOffset TentativeOffset, ValueProxy TentativeValue)>;
using LazyInsertOnLeakCB =
function_ref<void(FileOffset TentativeOffset, ValueProxy TentativeValue,
FileOffset FinalOffset, ValueProxy FinalValue)>;

/// Insert lazily.
///
/// \p OnConstruct is called when ready to insert a value, after allocating
/// space for the data. It is called at most once.
///
/// \p OnLeak is called only if \p OnConstruct has been called and a race
/// occurred before insertion, causing the tentative offset and data to be
/// abandoned. This allows clients to clean up other results or update any
/// references.
///
/// NOTE: Does *not* guarantee that \p OnConstruct is only called on success.
/// The in-memory \a TrieRawHashMap uses LazyAtomicPointer to synchronize
/// simultaneous writes, but that seems dangerous to use in a memory-mapped
/// file in case a process crashes in the busy state.
Expected<pointer> insertLazy(ArrayRef<uint8_t> Hash,
LazyInsertOnConstructCB OnConstruct = nullptr,
LazyInsertOnLeakCB OnLeak = nullptr);

Expected<pointer> insert(const ConstValueProxy &Value) {
return insertLazy(Value.Hash, [&](FileOffset, ValueProxy Allocated) {
assert(Allocated.Hash == Value.Hash);
assert(Allocated.Data.size() == Value.Data.size());
llvm::copy(Value.Data, Allocated.Data.begin());
});
}

size_t size() const;
size_t capacity() const;

/// Gets or creates a file at \p Path with a hash-mapped trie named \p
/// TrieName. The hash size is \p NumHashBits (in bits) and the records store
/// data of size \p DataSize (in bytes).
///
/// \p MaxFileSize controls the maximum file size to support, limiting the
/// size of the \a mapped_file_region. \p NewFileInitialSize is the starting
/// size if a new file is created.
///
/// \p NewTableNumRootBits and \p NewTableNumSubtrieBits are hints to
/// configure the trie, if it doesn't already exist.
///
/// \pre NumHashBits is a multiple of 8 (byte-aligned).
static Expected<OnDiskTrieRawHashMap>
create(const Twine &Path, const Twine &TrieName, size_t NumHashBits,
uint64_t DataSize, uint64_t MaxFileSize,
std::optional<uint64_t> NewFileInitialSize,
std::optional<size_t> NewTableNumRootBits = std::nullopt,
std::optional<size_t> NewTableNumSubtrieBits = std::nullopt);

OnDiskTrieRawHashMap(OnDiskTrieRawHashMap &&RHS);
OnDiskTrieRawHashMap &operator=(OnDiskTrieRawHashMap &&RHS);
~OnDiskTrieRawHashMap();

private:
struct ImplType;
explicit OnDiskTrieRawHashMap(std::unique_ptr<ImplType> Impl);
std::unique_ptr<ImplType> Impl;
};

/// Sink for data. Stores variable length data with 8-byte alignment. Does not
/// track size of data, which is assumed to known from context, or embedded.
/// Uses 0-padding but does not guarantee 0-termination.
class OnDiskDataAllocator {
public:
using ValueProxy = MutableArrayRef<char>;

/// An iterator-like return value for data insertion. Maybe it should be
/// called \c iterator, but it has no increment.
class pointer {
public:
FileOffset getOffset() const { return Offset; }
explicit operator bool() const { return bool(getOffset()); }
const ValueProxy &operator*() const {
assert(Offset && "Null dereference");
return Value;
}
const ValueProxy *operator->() const {
assert(Offset && "Null dereference");
return &Value;
}

pointer() = default;

private:
friend class OnDiskDataAllocator;
pointer(FileOffset Offset, ValueProxy Value)
: Offset(Offset), Value(Value) {}
FileOffset Offset;
ValueProxy Value;
};

/// Look up the data stored at the given offset.
const char *beginData(FileOffset Offset) const;

/// Allocate at least \p Size with 8-byte alignment.
Expected<pointer> allocate(size_t Size);

/// \returns the buffer that was allocated at \p create time, with size
/// \p UserHeaderSize.
MutableArrayRef<uint8_t> getUserHeader();

size_t size() const;
size_t capacity() const;

static Expected<OnDiskDataAllocator>
create(const Twine &Path, const Twine &TableName, uint64_t MaxFileSize,
std::optional<uint64_t> NewFileInitialSize,
uint32_t UserHeaderSize = 0,
function_ref<void(void *)> UserHeaderInit = nullptr);

OnDiskDataAllocator(OnDiskDataAllocator &&RHS);
OnDiskDataAllocator &operator=(OnDiskDataAllocator &&RHS);

// No copy. Just call \a create() again.
OnDiskDataAllocator(const OnDiskDataAllocator &) = delete;
OnDiskDataAllocator &operator=(const OnDiskDataAllocator &) = delete;

~OnDiskDataAllocator();

private:
struct ImplType;
explicit OnDiskDataAllocator(std::unique_ptr<ImplType> Impl);
std::unique_ptr<ImplType> Impl;
};

} // namespace cas
} // namespace llvm

#endif // LLVM_CAS_ONDISKHASHMAPPEDTRIE_H
1 change: 1 addition & 0 deletions llvm/lib/CAS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_llvm_component_library(LLVMCAS
MappedFileRegionArena.cpp
ObjectStore.cpp
OnDiskCommon.cpp
OnDiskTrieRawHashMap.cpp

ADDITIONAL_HEADER_DIRS
${LLVM_MAIN_INCLUDE_DIR}/llvm/CAS
Expand Down
Loading