Skip to content

Commit 2dc6b72

Browse files
authored
chore: move memfd detect to detect crate (#213)
Signed-off-by: usamoi <[email protected]>
1 parent 4d34b45 commit 2dc6b72

File tree

11 files changed

+157
-123
lines changed

11 files changed

+157
-123
lines changed

Cargo.lock

Lines changed: 93 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ bincode.workspace = true
2828
half.workspace = true
2929
num-traits.workspace = true
3030
service = { path = "crates/service" }
31+
detect = { path = "crates/detect" }
3132
pgrx = { git = "https://github.com/tensorchord/pgrx.git", rev = "7c30e2023876c1efce613756f5ec81f3ab05696b", default-features = false, features = [
3233
] }
3334
openai_api_rust = { git = "https://github.com/tensorchord/openai-api.git", rev = "228d54b6002e98257b3c81501a054942342f585f" }
@@ -70,7 +71,7 @@ half = { version = "~2.3", features = [
7071
] }
7172
num-traits = "~0.2"
7273
validator = { version = "~0.16", features = ["derive"] }
73-
rustix = { version = "~0.38", features = ["net", "mm"] }
74+
rustix = { version = "~0.38", features = ["fs", "net", "mm"] }
7475

7576
[profile.dev]
7677
panic = "unwind"

crates/detect/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition.workspace = true
66
[dependencies]
77
std_detect = { git = "https://github.com/tensorchord/stdarch.git", branch = "avx512fp16" }
88
ctor = "0.2.6"
9+
rustix.workspace = true

crates/detect/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pub mod linux;
12
pub mod x86_64;

crates/detect/src/linux.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![cfg(target_os = "linux")]
2+
3+
use std::sync::atomic::{AtomicBool, Ordering};
4+
5+
static ATOMIC_MEMFD: AtomicBool = AtomicBool::new(false);
6+
7+
pub fn test_memfd() -> bool {
8+
use rustix::fs::MemfdFlags;
9+
use std::io::ErrorKind;
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+
}
16+
17+
#[ctor::ctor]
18+
fn ctor_memfd() {
19+
ATOMIC_MEMFD.store(test_memfd(), Ordering::Relaxed);
20+
}
21+
22+
pub fn detect_memfd() -> bool {
23+
ATOMIC_MEMFD.load(Ordering::Relaxed)
24+
}

crates/detect/src/x86_64.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ fn ctor_v2() {
8080
ATOMIC_V2.store(test_v2(), Ordering::Relaxed);
8181
}
8282

83-
pub fn _detect_v2() -> bool {
83+
pub fn detect_v2() -> bool {
8484
ATOMIC_V2.load(Ordering::Relaxed)
8585
}

crates/detect/tests/linux.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![cfg(target_os = "linux")]
2+
3+
#[test]
4+
fn print() {
5+
assert_eq!(detect::linux::test_memfd(), detect::linux::detect_memfd());
6+
}

crates/detect/tests/x86_64.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![cfg(target_arch = "x86_64")]
2+
3+
#[test]
4+
fn print() {
5+
assert_eq!(
6+
detect::x86_64::test_avx512fp16(),
7+
detect::x86_64::detect_avx512fp16()
8+
);
9+
assert_eq!(detect::x86_64::test_v4(), detect::x86_64::detect_v4());
10+
assert_eq!(detect::x86_64::test_v3(), detect::x86_64::detect_v3());
11+
assert_eq!(detect::x86_64::test_v2(), detect::x86_64::detect_v2());
12+
}

src/ipc/client/mod.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ impl Rpc {
5050
pub fn new(socket: ClientSocket) -> Self {
5151
Self { socket }
5252
}
53-
pub fn create(self: &mut ClientGuard<Self>, id: Id, options: IndexOptions) {
53+
}
54+
55+
impl ClientGuard<Rpc> {
56+
pub fn create(&mut self, id: Id, options: IndexOptions) {
5457
let packet = RpcPacket::Create { id, options };
5558
self.socket.send(packet).friendly();
5659
let create::CreatePacket::Leave {} = self.socket.recv().friendly();
5760
}
5861
pub fn search(
59-
self: &mut ClientGuard<Self>,
62+
&mut self,
6063
id: Id,
6164
search: (DynamicVector, usize),
6265
prefilter: bool,
@@ -81,7 +84,7 @@ impl Rpc {
8184
}
8285
}
8386
}
84-
pub fn delete(self: &mut ClientGuard<Self>, id: Id, mut t: impl Delete) {
87+
pub fn delete(&mut self, id: Id, mut t: impl Delete) {
8588
let packet = RpcPacket::Delete { id };
8689
self.socket.send(packet).friendly();
8790
loop {
@@ -97,32 +100,28 @@ impl Rpc {
97100
}
98101
}
99102
}
100-
pub fn insert(self: &mut ClientGuard<Self>, id: Id, insert: (DynamicVector, Pointer)) {
103+
pub fn insert(&mut self, id: Id, insert: (DynamicVector, Pointer)) {
101104
let packet = RpcPacket::Insert { id, insert };
102105
self.socket.send(packet).friendly();
103106
let insert::InsertPacket::Leave {} = self.socket.recv().friendly();
104107
}
105-
pub fn flush(self: &mut ClientGuard<Self>, id: Id) {
108+
pub fn flush(&mut self, id: Id) {
106109
let packet = RpcPacket::Flush { id };
107110
self.socket.send(packet).friendly();
108111
let flush::FlushPacket::Leave {} = self.socket.recv().friendly();
109112
}
110-
pub fn destory(self: &mut ClientGuard<Self>, ids: Vec<Id>) {
113+
pub fn destory(&mut self, ids: Vec<Id>) {
111114
let packet = RpcPacket::Destory { ids };
112115
self.socket.send(packet).friendly();
113116
let destory::DestoryPacket::Leave {} = self.socket.recv().friendly();
114117
}
115-
pub fn stat(self: &mut ClientGuard<Self>, id: Id) -> IndexStat {
118+
pub fn stat(&mut self, id: Id) -> IndexStat {
116119
let packet = RpcPacket::Stat { id };
117120
self.socket.send(packet).friendly();
118121
let stat::StatPacket::Leave { result } = self.socket.recv().friendly();
119122
result
120123
}
121-
pub fn vbase(
122-
mut self: ClientGuard<Self>,
123-
id: Id,
124-
vbase: (DynamicVector, usize),
125-
) -> ClientGuard<Vbase> {
124+
pub fn vbase(mut self, id: Id, vbase: (DynamicVector, usize)) -> ClientGuard<Vbase> {
126125
let packet = RpcPacket::Vbase { id, vbase };
127126
self.socket.send(packet).friendly();
128127
let vbase::VbaseErrorPacket {} = self.socket.recv().friendly();
@@ -155,13 +154,16 @@ pub struct Vbase {
155154
}
156155

157156
impl Vbase {
158-
pub fn next(self: &mut ClientGuard<Self>) -> Option<Pointer> {
157+
pub fn next(&mut self) -> Option<Pointer> {
159158
let packet = vbase::VbasePacket::Next {};
160159
self.socket.send(packet).friendly();
161160
let vbase::VbaseNextPacket { p } = self.socket.recv().friendly();
162161
p
163162
}
164-
pub fn leave(mut self: ClientGuard<Self>) -> ClientGuard<Rpc> {
163+
}
164+
165+
impl ClientGuard<Vbase> {
166+
pub fn leave(mut self) -> ClientGuard<Rpc> {
165167
let packet = vbase::VbasePacket::Leave {};
166168
self.socket.send(packet).friendly();
167169
let vbase::VbaseLeavePacket {} = self.socket.recv().friendly();

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//!
33
//! Provides an easy-to-use extension for vector similarity search.
44
#![feature(offset_of)]
5-
#![feature(arbitrary_self_types)]
6-
#![feature(lazy_cell)]
75
#![feature(never_type)]
86

97
mod bgworker;

src/utils/os.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ use rustix::fd::{AsFd, OwnedFd};
22
use rustix::mm::{MapFlags, ProtFlags};
33
use std::sync::atomic::AtomicU32;
44

5-
#[cfg(target_os = "linux")]
6-
static SUPPORT_MEMFD: std::sync::LazyLock<bool> = std::sync::LazyLock::new(|| {
7-
use rustix::fs::MemfdFlags;
8-
use std::io::ErrorKind;
9-
match rustix::fs::memfd_create(".memfd.VECTORS.SUPPORT", MemfdFlags::empty()) {
10-
Ok(_) => true,
11-
Err(e) if e.kind() == ErrorKind::Unsupported => false,
12-
Err(_) => false,
13-
}
14-
});
15-
165
#[cfg(target_os = "linux")]
176
pub unsafe fn futex_wait(futex: &AtomicU32, value: u32) {
187
const FUTEX_TIMEOUT: libc::timespec = libc::timespec {
@@ -39,7 +28,7 @@ pub unsafe fn futex_wake(futex: &AtomicU32) {
3928

4029
#[cfg(target_os = "linux")]
4130
pub fn memfd_create() -> std::io::Result<OwnedFd> {
42-
if *SUPPORT_MEMFD {
31+
if detect::linux::detect_memfd() {
4332
use rustix::fs::MemfdFlags;
4433
Ok(rustix::fs::memfd_create(
4534
format!(".memfd.VECTORS.{:x}", std::process::id()),

0 commit comments

Comments
 (0)