Skip to content

Commit 5ab05fa

Browse files
authored
Fix CI on Windows due to missing posix_madvise support (#383)
1 parent 0a3584a commit 5ab05fa

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

jvector-native/src/main/java/io/github/jbellis/jvector/disk/MemorySegmentReader.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
import java.nio.file.Path;
3333
import java.nio.file.StandardOpenOption;
3434

35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
37+
3538
/**
3639
* {@link MemorySegment} based implementation of RandomAccessReader. This is the recommended
3740
* RandomAccessReader implementation included with JVector.
@@ -40,6 +43,7 @@
4043
* of {@link SimpleMappedReader}.
4144
*/
4245
public class MemorySegmentReader implements RandomAccessReader {
46+
private static final Logger logger = LoggerFactory.getLogger(MemorySegmentReader.class);
4347

4448
private static final int MADV_RANDOM = 1; // Value for Linux
4549
private static final OfInt intLayout = ValueLayout.JAVA_INT_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN);
@@ -57,15 +61,24 @@ public MemorySegmentReader(Path path) throws IOException {
5761

5862
// Apply MADV_RANDOM advice
5963
var linker = Linker.nativeLinker();
60-
var madvise = linker.downcallHandle(linker.defaultLookup().find("posix_madvise").get(),
61-
FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS, ValueLayout.JAVA_LONG, ValueLayout.JAVA_INT));
62-
try {
63-
int result = (int) madvise.invokeExact(memory, memory.byteSize(), MADV_RANDOM);
64-
if (result != 0) {
65-
throw new IOException("posix_madvise failed with error code: " + result);
64+
var maybeMadvise = linker.defaultLookup().find("posix_madvise");
65+
if (maybeMadvise.isPresent()) {
66+
var madvise = linker.downcallHandle(maybeMadvise.get(),
67+
FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS, ValueLayout.JAVA_LONG, ValueLayout.JAVA_INT));
68+
try
69+
{
70+
int result = (int) madvise.invokeExact(memory, memory.byteSize(), MADV_RANDOM);
71+
if (result != 0)
72+
{
73+
throw new IOException("posix_madvise failed with error code: " + result);
74+
}
75+
}
76+
catch (Throwable t)
77+
{
78+
throw new RuntimeException(t);
6679
}
67-
} catch (Throwable t) {
68-
throw new RuntimeException(t);
80+
} else {
81+
logger.warn("posix_madvise not found, MADV_RANDOM advice not applied");
6982
}
7083
} catch (Exception e) {
7184
arena.close();

0 commit comments

Comments
 (0)