Add more debugging/error messages when mlockall fails
This commit is contained in:
parent
d99b5dfdb4
commit
1020b321ce
|
@ -20,27 +20,32 @@
|
||||||
package org.elasticsearch.bootstrap;
|
package org.elasticsearch.bootstrap;
|
||||||
|
|
||||||
import com.sun.jna.Native;
|
import com.sun.jna.Native;
|
||||||
|
import com.sun.jna.Structure;
|
||||||
|
|
||||||
|
import org.apache.lucene.util.Constants;
|
||||||
import org.elasticsearch.common.logging.ESLogger;
|
import org.elasticsearch.common.logging.ESLogger;
|
||||||
import org.elasticsearch.common.logging.Loggers;
|
import org.elasticsearch.common.logging.Loggers;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class JNACLibrary {
|
final class JNACLibrary {
|
||||||
|
|
||||||
private static final ESLogger logger = Loggers.getLogger(JNACLibrary.class);
|
private static final ESLogger logger = Loggers.getLogger(JNACLibrary.class);
|
||||||
|
|
||||||
public static final int MCL_CURRENT = 1;
|
public static final int MCL_CURRENT = 1;
|
||||||
public static final int MCL_FUTURE = 2;
|
|
||||||
|
|
||||||
public static final int ENOMEM = 12;
|
public static final int ENOMEM = 12;
|
||||||
|
public static final int RLIMIT_MEMLOCK = Constants.MAC_OS_X ? 6 : 8;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
Native.register("c");
|
Native.register("c");
|
||||||
} catch (UnsatisfiedLinkError e) {
|
} catch (UnsatisfiedLinkError e) {
|
||||||
logger.warn("unable to link C library. native methods (mlockall) will be disabled.");
|
logger.warn("unable to link C library. native methods (mlockall) will be disabled.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,6 +53,25 @@ class JNACLibrary {
|
||||||
|
|
||||||
static native int geteuid();
|
static native int geteuid();
|
||||||
|
|
||||||
|
/** corresponds to struct rlimit */
|
||||||
|
public static final class Rlimit extends Structure implements Structure.ByReference {
|
||||||
|
public long rlim_cur = 0;
|
||||||
|
public long rlim_max = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List getFieldOrder() {
|
||||||
|
return Arrays.asList(new String[] { "rlim_cur", "rlim_max" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static native int getrlimit(int resource, Rlimit rlimit);
|
||||||
|
|
||||||
|
static native String strerror(int errno);
|
||||||
|
|
||||||
private JNACLibrary() {
|
private JNACLibrary() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String args[]) throws Exception {
|
||||||
|
JNANatives.tryMlockall();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,26 +43,40 @@ class JNANatives {
|
||||||
|
|
||||||
static void tryMlockall() {
|
static void tryMlockall() {
|
||||||
int errno = Integer.MIN_VALUE;
|
int errno = Integer.MIN_VALUE;
|
||||||
|
String errMsg = null;
|
||||||
|
boolean rlimitSuccess = false;
|
||||||
|
long softLimit = 0;
|
||||||
|
long hardLimit = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int result = JNACLibrary.mlockall(JNACLibrary.MCL_CURRENT);
|
int result = JNACLibrary.mlockall(JNACLibrary.MCL_CURRENT);
|
||||||
if (result != 0) {
|
if (result == 0) {
|
||||||
errno = Native.getLastError();
|
|
||||||
} else {
|
|
||||||
LOCAL_MLOCKALL = true;
|
LOCAL_MLOCKALL = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = Native.getLastError();
|
||||||
|
errMsg = JNACLibrary.strerror(errno);
|
||||||
|
JNACLibrary.Rlimit rlimit = new JNACLibrary.Rlimit();
|
||||||
|
if (JNACLibrary.getrlimit(JNACLibrary.RLIMIT_MEMLOCK, rlimit) == 0) {
|
||||||
|
rlimitSuccess = true;
|
||||||
|
softLimit = rlimit.rlim_cur;
|
||||||
|
hardLimit = rlimit.rlim_max;
|
||||||
|
} else {
|
||||||
|
logger.warn("Unable to retrieve resource limits: " + JNACLibrary.strerror(Native.getLastError()));
|
||||||
}
|
}
|
||||||
} catch (UnsatisfiedLinkError e) {
|
} catch (UnsatisfiedLinkError e) {
|
||||||
// this will have already been logged by CLibrary, no need to repeat it
|
// this will have already been logged by CLibrary, no need to repeat it
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errno != Integer.MIN_VALUE) {
|
// mlockall failed for some reason
|
||||||
if (errno == JNACLibrary.ENOMEM && System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) {
|
logger.warn("Unable to lock JVM Memory: error=" + errno + ",reason=" + errMsg + ". This can result in part of the JVM being swapped out");
|
||||||
logger.warn("Unable to lock JVM memory (ENOMEM)."
|
if (errno == JNACLibrary.ENOMEM) {
|
||||||
+ " This can result in part of the JVM being swapped out."
|
if (rlimitSuccess) {
|
||||||
+ " Increase RLIMIT_MEMLOCK (ulimit).");
|
logger.warn("Increase RLIMIT_MEMLOCK (ulimit). soft limit:" + softLimit + ", hard limit:" + hardLimit);
|
||||||
} else if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("mac")) {
|
} else {
|
||||||
// OS X allows mlockall to be called, but always returns an error
|
logger.warn("Increase RLIMIT_MEMLOCK (ulimit).");
|
||||||
logger.warn("Unknown mlockall error " + errno);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue