summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-04-26 13:47:16 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2022-04-26 13:47:16 +0200
commitbe717cbb0d681e1e9d0afaf9d6321dc51e556932 (patch)
tree2ac74346b29b31021af44eb34bf446f421b06466
parentcad36df5132815879cc078c3faed8ec51f13a23d (diff)
Add mallinfo2 implementation.
-rw-r--r--security-utils/pom.xml2
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/NativeIO.java9
-rw-r--r--vespajlib/src/main/java/com/yahoo/nativec/MallInfo2.java29
-rw-r--r--vespajlib/src/main/java/com/yahoo/nativec/NativeC.java (renamed from vespajlib/src/main/java/com/yahoo/io/NativeC.java)11
-rw-r--r--vespajlib/src/main/java/com/yahoo/nativec/PosixFAdvise.java12
-rw-r--r--vespajlib/src/main/java/com/yahoo/nativec/package-info.java5
-rw-r--r--vespajlib/src/test/java/com/yahoo/nativec/MallInfo2TestCase.java18
-rw-r--r--vespajlib/src/test/java/com/yahoo/nativec/PosixFAdviseTestCase.java18
8 files changed, 91 insertions, 13 deletions
diff --git a/security-utils/pom.xml b/security-utils/pom.xml
index ac856dff6c3..5c4deecc437 100644
--- a/security-utils/pom.xml
+++ b/security-utils/pom.xml
@@ -96,7 +96,7 @@
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
- <Bundle-Version>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}</Bundle-Version>
+ <Bundle-Version>7.0.0</Bundle-Version>
<Export-Package>com.yahoo.security.*;version=1.0.0;-noimport:=true</Export-Package>
<_nouses>true</_nouses> <!-- Don't include 'uses' directives for package exports -->
<_fixupmessages>"Classes found in the wrong directory"</_fixupmessages> <!-- Hide warnings for bouncycastle multi-release jars -->
diff --git a/vespajlib/src/main/java/com/yahoo/io/NativeIO.java b/vespajlib/src/main/java/com/yahoo/io/NativeIO.java
index 8d6fe65b8e2..af101211b4a 100644
--- a/vespajlib/src/main/java/com/yahoo/io/NativeIO.java
+++ b/vespajlib/src/main/java/com/yahoo/io/NativeIO.java
@@ -1,6 +1,8 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;
+import com.yahoo.nativec.PosixFAdvise;
+
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
@@ -16,7 +18,6 @@ import java.util.logging.Logger;
public class NativeIO {
private static final Logger logger = Logger.getLogger(NativeIO.class.getName());
private static final String DISABLE_NATIVE_IO = "DISABLE_NATIVE_IO";
- private static final int POSIX_FADV_DONTNEED = 4; // See /usr/include/linux/fadvise.h
private static final InitResult fdField = new InitResult();
private static class InitResult {
private final boolean initialized;
@@ -27,7 +28,7 @@ public class NativeIO {
boolean initComplete = false;
boolean disabled = true;
Field field = null;
- Throwable exception = NativeC.init();
+ Throwable exception = PosixFAdvise.init();
try {
if (exception == null) {
disabled = System.getenv().containsKey(DISABLE_NATIVE_IO);
@@ -66,7 +67,7 @@ public class NativeIO {
if (fdField.isEnabled()) {
logger.warning("Native IO not possible due to " + getError().getMessage());
} else {
- logger.info("Native IO has been disable explicit via system property " + DISABLE_NATIVE_IO);
+ logger.info("Native IO has been disabled explicit via system property " + DISABLE_NATIVE_IO);
}
}
}
@@ -87,7 +88,7 @@ public class NativeIO {
}
}
if (valid()) {
- NativeC.posix_fadvise(fdField.getNativeFD(fd), offset, len, POSIX_FADV_DONTNEED);
+ PosixFAdvise.posix_fadvise(fdField.getNativeFD(fd), offset, len, PosixFAdvise.POSIX_FADV_DONTNEED);
}
}
/**
diff --git a/vespajlib/src/main/java/com/yahoo/nativec/MallInfo2.java b/vespajlib/src/main/java/com/yahoo/nativec/MallInfo2.java
new file mode 100644
index 00000000000..1db73710d76
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/nativec/MallInfo2.java
@@ -0,0 +1,29 @@
+package com.yahoo.nativec;
+
+import com.sun.jna.Structure;
+
+public class MallInfo2 extends NativeC {
+ private final static Throwable initException = loadLibrary();
+ public static Throwable init() {
+ return initException;
+ }
+ // Equivalent JNA mapping
+ private static class MallInfo2Struct extends Structure {
+ public static class ByValue extends MallInfo2Struct implements Structure.ByValue { }
+ public long arena; /* Non-mmapped space allocated (bytes) */
+ public long ordblks; /* Number of free chunks */
+ public long smblks; /* Number of free fastbin blocks */
+ public long hblks; /* Number of mmapped regions */
+ public long hblkhd; /* Space allocated in mmapped regions (bytes) */
+ public long usmblks; /* See below */
+ public long fsmblks; /* Space in freed fastbin blocks (bytes) */
+ public long uordblks; /* Total allocated space (bytes) */
+ public long fordblks; /* Total free space (bytes) */
+ public long keepcost; /* Top-most, releasable space (bytes) */
+ }
+ private static native MallInfo2Struct.ByValue mallinfo2();
+ public MallInfo2() {
+ mallinfo = mallinfo2();
+ }
+ private final MallInfo2Struct mallinfo;
+}
diff --git a/vespajlib/src/main/java/com/yahoo/io/NativeC.java b/vespajlib/src/main/java/com/yahoo/nativec/NativeC.java
index 7dbf4015bcf..e86fe16d3f7 100644
--- a/vespajlib/src/main/java/com/yahoo/io/NativeC.java
+++ b/vespajlib/src/main/java/com/yahoo/nativec/NativeC.java
@@ -1,12 +1,10 @@
-package com.yahoo.io;
+package com.yahoo.nativec;
-import com.sun.jna.LastErrorException;
import com.sun.jna.Native;
import com.sun.jna.Platform;
class NativeC {
- private final static Throwable initException = loadLibrary();
- private static Throwable loadLibrary() {
+ protected static Throwable loadLibrary() {
if (Platform.isLinux()) {
try {
Native.register(Platform.C_LIBRARY_NAME);
@@ -18,8 +16,5 @@ class NativeC {
}
return null;
}
- static Throwable init() {
- return initException;
- }
- static native int posix_fadvise(int fd, long offset, long len, int flag) throws LastErrorException;
+
}
diff --git a/vespajlib/src/main/java/com/yahoo/nativec/PosixFAdvise.java b/vespajlib/src/main/java/com/yahoo/nativec/PosixFAdvise.java
new file mode 100644
index 00000000000..b29ff889a25
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/nativec/PosixFAdvise.java
@@ -0,0 +1,12 @@
+package com.yahoo.nativec;
+
+import com.sun.jna.LastErrorException;
+
+public class PosixFAdvise extends NativeC {
+ public static final int POSIX_FADV_DONTNEED = 4; // See /usr/include/linux/fadvise.h
+ private final static Throwable initException = loadLibrary();
+ public static Throwable init() {
+ return initException;
+ }
+ public static native int posix_fadvise(int fd, long offset, long len, int flag) throws LastErrorException;
+}
diff --git a/vespajlib/src/main/java/com/yahoo/nativec/package-info.java b/vespajlib/src/main/java/com/yahoo/nativec/package-info.java
new file mode 100644
index 00000000000..cdf497ea39b
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/nativec/package-info.java
@@ -0,0 +1,5 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+@ExportPackage
+package com.yahoo.nativec;
+
+import com.yahoo.osgi.annotation.ExportPackage;
diff --git a/vespajlib/src/test/java/com/yahoo/nativec/MallInfo2TestCase.java b/vespajlib/src/test/java/com/yahoo/nativec/MallInfo2TestCase.java
new file mode 100644
index 00000000000..87a92a2d640
--- /dev/null
+++ b/vespajlib/src/test/java/com/yahoo/nativec/MallInfo2TestCase.java
@@ -0,0 +1,18 @@
+package com.yahoo.nativec;
+
+import com.sun.jna.Platform;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class MallInfo2TestCase {
+ @Test
+ public void requireThatMallInfo2IsDetected() {
+ if (Platform.isLinux()) {
+ assertNull(MallInfo2.init());
+ } else {
+ assertEquals("Platform is unsúpported. Only supported on linux.", MallInfo2.init().getMessage());
+ }
+ }
+}
diff --git a/vespajlib/src/test/java/com/yahoo/nativec/PosixFAdviseTestCase.java b/vespajlib/src/test/java/com/yahoo/nativec/PosixFAdviseTestCase.java
new file mode 100644
index 00000000000..8750dcb0a86
--- /dev/null
+++ b/vespajlib/src/test/java/com/yahoo/nativec/PosixFAdviseTestCase.java
@@ -0,0 +1,18 @@
+package com.yahoo.nativec;
+
+import com.sun.jna.Platform;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class PosixFAdviseTestCase {
+ @Test
+ public void requireThatPosixFAdviseIsDetected() {
+ if (Platform.isLinux()) {
+ assertNull(PosixFAdvise.init());
+ } else {
+ assertEquals("Platform is unsúpported. Only supported on linux.", PosixFAdvise.init().getMessage());
+ }
+ }
+}