feat(storage): expose copy-on-write mmap flag and fix MMAP_POPULATE flag placement

This PR exposes a copy-on-write mmap option through the public StorageOptions API and fixes a bug where the MAP_POPULATE flag was applied to the wrong mmap() argument.
This commit is contained in:
Jalin Wang
2026-06-17 14:18:12 +08:00
committed by GitHub
parent e12bad5e37
commit 85cd08d383
4 changed files with 290 additions and 11 deletions
+10 -9
View File
@@ -312,18 +312,20 @@ ssize_t File::offset(void) const {
void *File::MemoryMap(NativeHandle handle, ssize_t off, size_t len, int opts) {
int prot =
((opts & File::MMAP_READONLY) ? PROT_READ : PROT_READ | PROT_WRITE);
int flags = (opts & File::MMAP_SHARED) ? MAP_SHARED : MAP_PRIVATE;
#if defined(MAP_POPULATE)
if (opts & File::MMAP_POPULATE) {
prot |= MAP_POPULATE;
flags |= MAP_POPULATE;
}
#endif
int flags = (opts & File::MMAP_SHARED) ? MAP_SHARED : MAP_PRIVATE;
#if defined(MAP_HUGETLB)
if (opts & File::MMAP_HUGE_PAGE) {
flags |= MAP_HUGETLB;
}
#endif
void *addr = mmap(nullptr, len, prot, flags, handle, off);
ailego_null_if_false(addr != MAP_FAILED);
@@ -344,14 +346,13 @@ void *File::MemoryMap(size_t len, int opts) {
#if defined(MAP_ANONYMOUS)
int prot =
((opts & File::MMAP_READONLY) ? PROT_READ : PROT_READ | PROT_WRITE);
#if defined(MAP_POPULATE)
if (opts & File::MMAP_POPULATE) {
prot |= MAP_POPULATE;
}
#endif
int flags = (opts & File::MMAP_SHARED) ? MAP_SHARED | MAP_ANONYMOUS
: MAP_PRIVATE | MAP_ANONYMOUS;
#if defined(MAP_POPULATE)
if (opts & File::MMAP_POPULATE) {
flags |= MAP_POPULATE;
}
#endif
#if defined(MAP_HUGETLB)
if (opts & File::MMAP_HUGE_PAGE) {
flags |= MAP_HUGETLB;
@@ -776,4 +777,4 @@ void File::MemoryWarmup(void *addr, size_t len) {
}
} // namespace ailego
} // namespace zvec
} // namespace zvec
+11
View File
@@ -17,6 +17,7 @@
#include <zvec/core/framework/index_storage.h>
#include <zvec/core/interface/index.h>
#include "mixed_reducer/mixed_reducer_params.h"
#include "utility/utility_params.h"
namespace zvec::core_interface {
@@ -255,6 +256,16 @@ int Index::Open(const std::string &file_path, StorageOptions storage_options) {
// storage_params.set("proxima.mmap_file.storage.memory_warmup", true);
// storage_params.set("proxima.mmap_file.storage.segment_meta_capacity",
// 1024);
storage_params.set(core::MMAPFILE_STORAGE_COPY_ON_WRITE,
storage_options.copy_on_write);
// force_flush must be enabled with copy_on_write so that
// IndexMapping::flush() actually persists data written via file_.write() to
// disk; without it, data would be lost. See IndexMapping::flush() in
// index_mapping.cc.
storage_params.set(core::MMAPFILE_STORAGE_FORCE_FLUSH,
storage_options.copy_on_write);
switch (storage_options.type) {
case StorageOptions::StorageType::kMMAP: {
storage_ = core::IndexFactory::CreateStorage("MMapFileStorage");
@@ -41,6 +41,12 @@ struct StorageOptions {
StorageType type = StorageType::kNone;
bool create_new = false;
bool read_only = false;
// Only meaningful when type == kMMAP.
// false: MAP_SHARED. Writes through mmap auto-persist to the file.
// true : MAP_PRIVATE on a writable file. Flush/close forces dirty pages
// back to disk via explicit pwrite.
bool copy_on_write = false;
};
struct MergeOptions {
@@ -444,4 +450,4 @@ struct DiskAnnIndexParam : public BaseIndexParam {
bool omit_empty_value = false) const override;
};
} // namespace zvec::core_interface
} // namespace zvec::core_interface