Skip to content

Commit 7a2eb0a

Browse files
authored
fix: fallback for memfd (kernel 3.17) (#204)
Signed-off-by: usamoi <[email protected]>
1 parent 5bd1792 commit 7a2eb0a

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

Diff for: Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pgrx = { git = "https://github.com/tensorchord/pgrx.git", rev = "7c30e2023876c1e
3333
openai_api_rust = { git = "https://github.com/tensorchord/openai-api.git", rev = "228d54b6002e98257b3c81501a054942342f585f" }
3434
env_logger = "0.10.0"
3535
toml = "0.8.8"
36+
rand = "0.8.5"
3637

3738
[dev-dependencies]
3839
pgrx-tests = { git = "https://github.com/tensorchord/pgrx.git", rev = "7c30e2023876c1efce613756f5ec81f3ab05696b" }

Diff for: src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Provides an easy-to-use extension for vector similarity search.
44
#![feature(offset_of)]
55
#![feature(arbitrary_self_types)]
6-
#![feature(try_blocks)]
6+
#![feature(lazy_cell)]
77

88
mod bgworker;
99
mod datatype;

Diff for: src/utils/os.rs

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
use rustix::fd::{AsFd, OwnedFd};
22
use rustix::mm::{MapFlags, ProtFlags};
3+
use std::io::ErrorKind;
34
use std::sync::atomic::AtomicU32;
5+
use std::sync::LazyLock;
6+
7+
#[cfg(target_os = "linux")]
8+
static SUPPORT_MEMFD: LazyLock<bool> = LazyLock::new(|| {
9+
use rustix::fs::MemfdFlags;
10+
match rustix::fs::memfd_create(".memfd.VECTORS.SUPPORT", MemfdFlags::empty()) {
11+
Ok(_) => true,
12+
Err(e) if e.kind() == ErrorKind::Unsupported => false,
13+
Err(_) => false,
14+
}
15+
});
416

517
#[cfg(target_os = "linux")]
618
pub unsafe fn futex_wait(futex: &AtomicU32, value: u32) {
@@ -28,11 +40,30 @@ pub unsafe fn futex_wake(futex: &AtomicU32) {
2840

2941
#[cfg(target_os = "linux")]
3042
pub fn memfd_create() -> std::io::Result<OwnedFd> {
31-
use rustix::fs::MemfdFlags;
32-
Ok(rustix::fs::memfd_create(
33-
format!(".memfd.VECTORS.{:x}", std::process::id()),
34-
MemfdFlags::empty(),
35-
)?)
43+
if *SUPPORT_MEMFD {
44+
use rustix::fs::MemfdFlags;
45+
Ok(rustix::fs::memfd_create(
46+
format!(".memfd.VECTORS.{:x}", std::process::id()),
47+
MemfdFlags::empty(),
48+
)?)
49+
} else {
50+
use rustix::fs::Mode;
51+
use rustix::fs::OFlags;
52+
// POSIX fcntl locking do not support shmem, so we use a regular file here.
53+
// reference: https://man7.org/linux/man-pages/man3/fcntl.3p.html
54+
let name = format!(
55+
".shm.VECTORS.{:x}.{:x}",
56+
std::process::id(),
57+
rand::random::<u32>()
58+
);
59+
let fd = rustix::fs::open(
60+
&name,
61+
OFlags::RDWR | OFlags::CREATE | OFlags::EXCL,
62+
Mode::RUSR | Mode::WUSR,
63+
)?;
64+
rustix::fs::unlink(&name)?;
65+
Ok(fd)
66+
}
3667
}
3768

3869
#[cfg(target_os = "linux")]

0 commit comments

Comments
 (0)