ChrisHegarty commented on code in PR #13196: URL: https://github.com/apache/lucene/pull/13196#discussion_r1534254114
########## lucene/core/src/java21/org/apache/lucene/store/NativeAccess.java: ########## @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.store; + +import java.io.IOException; +import java.lang.foreign.MemorySegment; +import java.util.logging.Logger; +import org.apache.lucene.util.Constants; + +@SuppressWarnings("preview") +abstract class NativeAccess { + private static final Logger LOG = Logger.getLogger(NativeAccess.class.getName()); + + // these constants were extracted form glibc and macos header files - luckily they are the same: + + /** No further special treatment. */ + public static final int POSIX_MADV_NORMAL = 0; + + /** Expect random page references. */ + public static final int POSIX_MADV_RANDOM = 1; + + /** Expect sequential page references. */ + public static final int POSIX_MADV_SEQUENTIAL = 2; + + /** Will need these pages. */ + public static final int POSIX_MADV_WILLNEED = 3; + + /** Don't need these pages. */ + public static final int POSIX_MADV_DONTNEED = 4; + + /** Invoke the {@code madvise} call for the given {@link MemorySegment}. */ + public abstract void madvise(MemorySegment segment, int advise) throws IOException; + + /** + * Return the NativeAccess instance for this platform. At moment we only support Linux and MacOS + */ + public static NativeAccess getImplementation() { + if (Constants.LINUX || Constants.MAC_OS_X) { Review Comment: For me, this is the right level of abstraction. Support can be added for other platforms if and when desired - no need to block anything on Windows! ;-) ########## lucene/core/src/java21/org/apache/lucene/store/PosixNativeAccess.java: ########## @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.store; + +import java.io.IOException; +import java.lang.foreign.FunctionDescriptor; +import java.lang.foreign.Linker; +import java.lang.foreign.MemorySegment; +import java.lang.foreign.SymbolLookup; +import java.lang.foreign.ValueLayout; +import java.lang.invoke.MethodHandle; +import java.util.Locale; + +@SuppressWarnings("preview") +final class PosixNativeAccess extends NativeAccess { + + private final MethodHandle mh$posix_madvise; + + PosixNativeAccess() { + final Linker linker = Linker.nativeLinker(); + final SymbolLookup stdlib = linker.defaultLookup(); + this.mh$posix_madvise = + findFunction( + linker, + stdlib, + "posix_madvise", + FunctionDescriptor.of( + ValueLayout.JAVA_INT, + ValueLayout.ADDRESS, + ValueLayout.JAVA_LONG, + ValueLayout.JAVA_INT)); + } + + private static MethodHandle findFunction( + Linker linker, SymbolLookup lookup, String name, FunctionDescriptor desc) { + final MemorySegment symbol = + lookup + .find(name) + .orElseThrow( + () -> + new UnsupportedOperationException( + "Platform has no symbol for '" + name + "' in libc.")); + return linker.downcallHandle(symbol, desc); Review Comment: we can use captureCallState to capture the errno if this fails. https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/foreign/Linker.Option.html#captureCallState(java.lang.String...) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org