diff --git a/CHANGES.md b/CHANGES.md index 5661442aea..a0dbd74f0f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -47,6 +47,7 @@ Features * [#616](https://github.com/java-native-access/jna/pull/616): Allow access to base interfaces (most important IDispatch) via ProxyObject and improve binding by allowing to use dispId for the call - [@matthiasblaesing](https://github.com/matthiasblaesing). * [#621](https://github.com/java-native-access/jna/pull/621): Added TYPEFLAGS-constants for `wTypeFlags` in `com.sun.jna.platform.win32.OaIdl.TYPEATTR` - [@SevenOf9Sleeper](https://github.com/SevenOf9Sleeper). * [#625](https://github.com/java-native-access/jna/pull/625): Make conversion to/from java to/from VARIANT in `com.sun.jna.platform.win32.COM.util.Convert` more flexible and dependable - [@matthiasblaesing](https://github.com/matthiasblaesing). +* [#639](https://github.com/java-native-access/jna/pull/639): Add getloadavg() to OS X and Unix - [@dbwiddis](https://github.com/dbwiddis). Bug Fixes --------- diff --git a/contrib/platform/src/com/sun/jna/platform/mac/SystemB.java b/contrib/platform/src/com/sun/jna/platform/mac/SystemB.java index a1d6b682bb..c4dfd7cbd7 100644 --- a/contrib/platform/src/com/sun/jna/platform/mac/SystemB.java +++ b/contrib/platform/src/com/sun/jna/platform/mac/SystemB.java @@ -168,6 +168,15 @@ protected List getFieldOrder() { * @return the host's name port */ int mach_host_self(); + + /** + * The mach_task_self system call returns the calling thread's task_self + * port. It has an effect equivalent to receiving a send right for the task's + * kernel port. + * + * @return the task's kernel port + */ + int mach_task_self(); /** * The host_page_size function returns the page size for the given host. @@ -333,4 +342,20 @@ int sysctlbyname(String name, Pointer oldp, IntByReference oldlenp, */ int host_processor_info(int machPort, int flavor, IntByReference procCount, PointerByReference procInfo, IntByReference procInfoCount); + + /** + * The getloadavg() function returns the number of processes in the system + * run queue averaged over various periods of time. Up to nelem samples are + * retrieved and assigned to successive elements of loadavg[]. The system + * imposes a maximum of 3 samples, representing averages over the last 1, 5, + * and 15 minutes, respectively. + * @param loadavg + * An array of doubles which will be filled with the results + * @param nelem + * Number of samples to return + * @return If the load average was unobtainable, -1 is returned; otherwise, + * the number of samples actually retrieved is returned. + * @see getloadavg(3) + */ + int getloadavg(double[] loadavg, int nelem); } diff --git a/contrib/platform/src/com/sun/jna/platform/unix/LibCAPI.java b/contrib/platform/src/com/sun/jna/platform/unix/LibCAPI.java index 9418abacb4..888701240f 100644 --- a/contrib/platform/src/com/sun/jna/platform/unix/LibCAPI.java +++ b/contrib/platform/src/com/sun/jna/platform/unix/LibCAPI.java @@ -73,4 +73,18 @@ public interface LibCAPI extends Reboot, Resource { * @see getenv(3) */ int unsetenv(String name); + + /** + * The getloadavg() function returns the number of processes in the system + * run queue averaged over various periods of time. Up to nelem samples are + * retrieved and assigned to successive elements of loadavg[]. The system + * imposes a maximum of 3 samples, representing averages over the last 1, 5, + * and 15 minutes, respectively. + * @param loadavg An array of doubles which will be filled with the results + * @param nelem Number of samples to return + * @return If the load average was unobtainable, -1 is returned; otherwise, + * the number of samples actually retrieved is returned. + * @see getloadavg(3) + */ + int getloadavg(double[] loadavg, int nelem); } diff --git a/contrib/platform/test/com/sun/jna/platform/mac/SystemBTest.java b/contrib/platform/test/com/sun/jna/platform/mac/SystemBTest.java index 1b11b5cd5c..0ac161e0d4 100644 --- a/contrib/platform/test/com/sun/jna/platform/mac/SystemBTest.java +++ b/contrib/platform/test/com/sun/jna/platform/mac/SystemBTest.java @@ -152,7 +152,23 @@ public void testHostProcessorInfo() { assertEquals(procCpuLoadInfo.getValue().getIntArray(0, procInfoCount.getValue()).length, procInfoCount.getValue()); } - + + public void testMachPorts() { + int machPort = SystemB.INSTANCE.mach_host_self(); + assertTrue(machPort > 0); + machPort = SystemB.INSTANCE.mach_task_self(); + assertTrue(machPort > 0); + } + + public void testGetLoadAvg() { + double[] loadavg = new double[3]; + int retval = SystemB.INSTANCE.getloadavg(loadavg, 3); + assertEquals(retval, 3); + assertTrue(loadavg[0] >= 0); + assertTrue(loadavg[1] >= 0); + assertTrue(loadavg[2] >= 0); + } + public static void main(java.lang.String[] argList) { junit.textui.TestRunner.run(SystemBTest.class); } diff --git a/contrib/platform/test/com/sun/jna/platform/unix/LibCTest.java b/contrib/platform/test/com/sun/jna/platform/unix/LibCTest.java index 6a7539a6aa..5c5efa93b9 100644 --- a/contrib/platform/test/com/sun/jna/platform/unix/LibCTest.java +++ b/contrib/platform/test/com/sun/jna/platform/unix/LibCTest.java @@ -48,4 +48,14 @@ public void testSetenv() { LibC.INSTANCE.unsetenv(name); } } + + @Test + public void testGetLoadAvg() { + double[] loadavg = new double[3]; + int retval = LibC.INSTANCE.getloadavg(loadavg, 3); + assertEquals(retval, 3); + assertTrue(loadavg[0] >= 0); + assertTrue(loadavg[1] >= 0); + assertTrue(loadavg[2] >= 0); + } }